To sponsor gas fees for transactions on Solana, see our guide here.

Use the signTransaction method exported from the useSignTransaction hook to sign a transaction with a Solana wallet.

signTransaction: (input: {
  transaction: SupportedSolanaTransaction;
  connection: Connection;
  uiOptions?: SendTransactionModalUIOptions;
  transactionOptions?: SendTransactionOptions;
  address?: string;
}) => Promise<SupportedSolanaTransaction>;

Usage

import {useSignTransaction} from '@privy-io/react-auth/solana';
import {Connection, Transaction, VersionedTransaction} from '@solana/web3.js';

// Inside your component
const {signTransaction} = useSignTransaction();

// Configure your connection to point to the correct Solana network
const connection = new Connection('https://api.mainnet-beta.solana.com');

// Create your transaction (either legacy Transaction or VersionedTransaction)
const transaction = new Transaction(); // or new VersionedTransaction()
// Add your instructions to the transaction...

// Sign the transaction
const signedTransaction = await signTransaction({
  transaction: transaction,
  connection: connection
});

console.log('Transaction signed successfully');

Parameters

transaction
SupportedSolanaTransaction
required

The transaction to be signed. Can be either a legacy Transaction or VersionedTransaction from @solana/web3.js.

connection
Connection
required

Connection to an SVM (Solana) network.

uiOptions
SendTransactionModalUIOptions

UI options to customize the transaction request modal.

transactionOptions
SendTransactionOptions

Transaction options to customize the transaction request.

address
string

Address for the embedded wallet signing the transaction. Defaults to the user’s embedded wallet at HD index 0.

Response

signedTransaction
SupportedSolanaTransaction

The signed transaction that can be sent to the network.

Sign all transactions

Use the signAllTransactions method on a Solana wallet to sign all transactions in a batch.

signAllTransactions: (transactions: (Transaction | VersionedTransaction)[]) =>
  Promise<(Transaction | VersionedTransaction)[]>;
This signAllTransactions method is only available with whitelabel UIs

Usage

import {
  clusterApiUrl,
  Connection,
  PublicKey,
  getVersionedTransaction,
  getLegacyTransaction
} from '@solana/web3.js';
import {useSolanaWallets} from '@privy-io/react-auth/solana';

const {wallets} = useSolanaWallets();
const wallet = wallets[0];

const connection = new Connection(clusterApiUrl('devnet'));
const walletPublicKey = new PublicKey(wallet.address);

const tx1 = await getVersionedTransaction({
  connection,
  from: walletPublicKey
});
const tx2 = await getLegacyTransaction({
  connection,
  from: walletPublicKey
});

const signedTxs = await wallet.signAllTransactions([tx1, tx2]);

Parameters

transactions
(Transaction | VersionedTransaction)[]
required

The transactions to sign.

Returns

signedTransactions
(Transaction | VersionedTransaction)[]

The array of signed transactions.