useSignTransaction

Sign a Solana transaction with your customers' Comet wallet.

Since Comet accounts come with a built-in cloud wallet, you can use this wallet to provide a signature for any Solana transaction using the useSignTransaction hook. The user will be prompted to log in if necessary, and will be asked to confirm the transaction to be signed (similar to the flow for Metamask or Phantom wallets).

API

const { signTransaction } = useSignTransaction({ transaction: TRANSACTION });

In the example above, TRANSACTION is a Transaction object generated by the Solana Web3 library (see @solana/web3.js).

signTransaction is a function that can be called later on, which brings up a confirmation dialog. When the user clicks "Sign", the transaction is partially signed by the private key in the user's Comet wallet. Keep in mind that if the transaction requires additional signatures, those will need to be provided separately.

signTransaction returns a successful Promise containing the Transaction object with the added signature, or a failed Promise if the user clicks "Cancel".

Example

import { useAccount, useSignTransaction } from '@comet-labs/react';
import { PublicKey, Transaction, SystemProgram } from '@solana/web3.js';

function App() {
  const account = useAccount();

  return (
    <div className="App">
      {
        account && <SignTransactionButton account={account} />
      }
    </div>
  );
}

function SignTransactionButton(props) {
  const tx = new Transaction().add(
    SystemProgram.transfer({
      fromPubkey: new PublicKey(props.account.address),
      toPubkey: new PublicKey('Dgq5B8i5NJJfPoUgpkFZDzRr84zd1BJrUBntJt1EBvgd'),
      lamports: 2000000,
    })
  );

  const { signTransaction } = useSignTransaction({ transaction: tx });

  return (
    <button onClick={signTransaction} className='bg-gray-800 text-white p-4 rounded-full'>
      sign transaction! &#x1F389;
    </button>
  );
}

Last updated