When using Privy’s server-side SDKs to sign transactions, you can use the authorization context to
automatically sign requests. Learn more about signing on the
server.
- React
- React Native
- Swift
- Android
- Flutter
- NodeJS
- NodeJS (server-auth)
- Java
- Rust
- REST API
Use the
signTransaction method exported from the useSignTransaction hook to sign a transaction with a Solana wallet.Report incorrect code
Copy
Ask AI
signTransaction: (input: {
transaction: SupportedSolanaTransaction;
wallet: ConnectedStandardSolanaWallet;
options?: SolanaSignTransactionOptions & {uiOptions?: SendTransactionModalUIOptions};
}) =>
Promise<{
signedTransaction: Uint8Array;
}>;
Usage
Report incorrect code
Copy
Ask AI
import {useSignTransaction, useWallets} from '@privy-io/react-auth/solana';
import {pipe, createSolanaRpc, getTransactionEncoder, createTransactionMessage} from '@solana/kit';
// Inside your component
const {signTransaction} = useSignTransaction();
const {wallets} = useWallets();
const selectedWallet = wallets[0];
// Configure your connection to point to the correct Solana network
const {getLatestBlockhash} = createSolanaRpc('https://api.mainnet-beta.solana.com');
// Get the latest blockhash
const {value} = await getLatestBlockhash().send();
// Create your transaction (either legacy Transaction or VersionedTransaction)
const transaction = pipe(
createTransactionMessage({version: 0}),
// Set the message fee payer...
// Set recent blockhash...
// Add your instructions to the transaction...
// Finally encode the transaction
(tx) => new Uint8Array(getTransactionEncoder().encode(tx))
);
// Sign the transaction
const signedTransaction = await signTransaction({
transaction: transaction,
wallet: selectedWallet
});
console.log('Transaction signed successfully');
Parameters
The encoded transaction to be signed.
The Solana wallet to use for signing the transaction.
Type of all Solana chains supported by Privy.
Response
The signed transaction that can be sent to the network.
To sign a transaction from a wallet using the React Native SDK, use the
request method from the wallet’s provider:Report incorrect code
Copy
Ask AI
request: (request: {
method: 'signTransaction',
params: {
transaction: Transaction | VersionedTransaction,
}
}) => Promise<{ signedTransaction: Transaction }>
The Expo SDK does not support built-in UIs for signing transactions.
The
signTransaction method gives you complete control over the experience and UI.Usage
Report incorrect code
Copy
Ask AI
import {useEmbeddedSolanaWallet} from '@privy-io/expo';
const { wallets } = useEmbeddedSolanaWallet();
const wallet = wallets[0];
// Get the provider
const provider = await wallet.getProvider();
// Create your transaction (either legacy Transaction or VersionedTransaction)
// transaction = ...
// Sign the transaction
const { signedTransaction } = await provider.request({
method: 'signTransaction',
params: {
transaction: transaction,
},
});
console.log("Transaction signed successfully", signedTransaction);
Parameters
The RPC method executed with the wallet.
Parameters for the transaction.
Hide child attributes
Hide child attributes
The transaction to sign. This can be either a legacy Transaction or a VersionedTransaction object from @solana/web3.js.
Returns
The signature of the transaction.
Sign a Solana transaction without submitting it to the network.
Report incorrect code
Copy
Ask AI
public protocol EmbeddedSolanaWalletProvider {
func signTransaction(transaction: Data) async throws -> String
}
Usage
Report incorrect code
Copy
Ask AI
import SolanaSwift
// Get the provider for wallet (assumes wallet is already obtained)
let provider = wallet.provider
// Create a Solana RPC client
let solana = JSONRPCAPIClient(endpoint: URL(string: SolanaCluster.mainnet.rpcUrl)!)
// Build the transaction using your preferred method
// In this example, we're using the SolanaSwift SDK
let latestBlockhash = try await solana.getLatestBlockhash()
let walletPK = try PublicKey(string: wallet.address)
var tx = Transaction()
tx.instructions.append(
SystemProgram.transferInstruction(
from: walletPK,
to: try PublicKey(string: "9NvE68JVWHHHGLp5NNELtM5fiBw6SXHrzqQJjUqaykC1"),
lamports: 100000000000000
)
)
tx.recentBlockhash = latestBlockhash
tx.feePayer = walletPK
// Sign using the Privy Embedded Wallet
// Get back the signed transaction serialized as a base64-encoded string
let signedTxBase64 = try await provider.signTransaction(transaction: tx.serialize())
let signedTx = try Transaction.from(data: Data(base64Encoded: signedTxBase64)!)
Parameters
The serialized transaction to sign.
Returns
The base64-encoded signed transaction.
Use the
signTransaction method on the Solana wallet provider to sign a transaction and then submit it to the network.Report incorrect code
Copy
Ask AI
public suspend fun signTransaction(
transaction: ByteArray
): Result<String>
Usage
Report incorrect code
Copy
Ask AI
// Retrieve user's Solana wallet (assumes wallet is already obtained)
val solanaWallet = user.embeddedSolanaWallets.first()
// Create a Solana RPC client with cluster RPC URL
val connection = Connection(SolanaCluster.MainNet.rpcUrl())
// Build the transaction
val walletPublicKey = PublicKey(solanaWallet.address)
val instruction = SystemProgram.transfer(
fromPubkey = walletPublicKey,
toPubkey = PublicKey(recipientAddress),
lamports = amount
)
// Get recent blockhash
val recentBlockhash = connection.getLatestBlockhash()
// Create transaction
val transaction = Transaction()
transaction.add(instruction)
transaction.recentBlockhash = recentBlockhash
transaction.feePayer = walletPublicKey
// Serialize transaction to byte array
val serializedTransaction = transaction.serialize()
// Sign the transaction (returns base64-encoded signed transaction)
val signResult = solanaWallet.provider.signTransaction(serializedTransaction)
signResult.onSuccess { signedTransactionBase64 ->
// Decode the signed transaction and submit to network
println("Transaction signed with signature: $signature")
}.onFailure { error ->
println("Failed to sign transaction: ${error.message}")
}
Parameters
The serialized transaction to sign.
Returns
A Result that, when successful, contains the base64-encoded signed transaction.
Use the
signTransaction method on the Solana wallet provider to sign a transaction and then submit it to the network.Report incorrect code
Copy
Ask AI
Future<Result<String>> signTransaction(Uint8List transaction);
Usage
Report incorrect code
Copy
Ask AI
// Retrieve the user's Solana wallet (assumes wallet is already obtained)
final solanaWallet = user.embeddedSolanaWallets.first;
// Create a Solana RPC client
final connection = Connection('https://api.mainnet-beta.solana.com');
// Build the transaction
final walletPublicKey = PublicKey(solanaWallet.address);
final instruction = SystemProgram.transfer(
fromPubkey: walletPublicKey,
toPubkey: PublicKey(recipientAddress),
lamports: amount
);
// Get recent blockhash
final recentBlockhash = await connection.getLatestBlockhash();
// Create transaction
final transaction = Transaction();
transaction.add(instruction);
transaction.recentBlockhash = recentBlockhash;
transaction.feePayer = walletPublicKey;
// Serialize transaction to bytes
final serializedTransaction = transaction.serializeMessage();
// Sign the transaction
final result = await solanaWallet.provider.signTransaction(serializedTransaction);
Parameters
The serialized transaction message bytes to sign.
Returns
A Result that, when successful, contains the base64-encoded signature.
Use the
signTransaction method on the Solana interface to sign a transaction with an Solana wallet.Usage
Report incorrect code
Copy
Ask AI
import {
clusterApiUrl,
Connection,
LAMPORTS_PER_SOL,
PublicKey,
SystemProgram,
Transaction,
VersionedTransaction,
TransactionMessage
} from '@solana/web3.js';
const walletPublicKey = new PublicKey(wallet.address);
const connection = new Connection(clusterApiUrl('devnet'));
const instruction = SystemProgram.transfer({
fromPubkey: walletPublicKey,
toPubkey: new PublicKey(address),
lamports: value * LAMPORTS_PER_SOL
});
const {blockhash: recentBlockhash} = await connection.getLatestBlockhash();
const message = new TransactionMessage({
payerKey: walletPublicKey,
// Replace with your desired instructions
instructions: [instruction],
recentBlockhash
});
const yourSolanaTransaction = new VersionedTransaction(message.compileToV0Message());
// Get the signed transaction object from the response
const {signedTransaction} = await privy.wallets().solana().signTransaction(wallet.id, {
transaction: Buffer.from(yourSolanaTransaction.serialize()).toString('base64')
});
Parameters and Returns
Check out the API reference for more details.The
@privy-io/server-auth library is deprecated. We recommend integrating @privy-io/node for
the latest features and support.signTransaction method on the Solana client to sign a transaction with an Solana wallet.Report incorrect code
Copy
Ask AI
signTransaction: (input: SolanaSignTransactionInputType) => Promise<SolanaSignTransactionResponseType>
Usage
Report incorrect code
Copy
Ask AI
import {
clusterApiUrl,
Connection,
LAMPORTS_PER_SOL,
PublicKey,
SystemProgram,
Transaction,
VersionedTransaction,
TransactionMessage
} from '@solana/web3.js';
const walletPublicKey = new PublicKey(wallet.address);
const connection = new Connection(clusterApiUrl('devnet'));
const instruction = SystemProgram.transfer({
fromPubkey: walletPublicKey,
toPubkey: new PublicKey(address),
lamports: value * LAMPORTS_PER_SOL
});
const {blockhash: recentBlockhash} = await connection.getLatestBlockhash();
const message = new TransactionMessage({
payerKey: walletPublicKey,
// Replace with your desired instructions
instructions: [instruction],
recentBlockhash
});
const yourSolanaTransaction = new VersionedTransaction(message.compileToV0Message());
// Get the signed transaction object from the response
const {signedTransaction} = await privy.walletApi.solana.signTransaction({
walletId: wallet.id,
transaction: yourSolanaTransaction
});
Parameters
The ID of the wallet to send the transaction from.
The transaction to sign. This can be either a legacy Transaction or a VersionedTransaction object from @solana/web3.js.
Returns
The signed transaction.
The encoding format for the returned
signedTransaction. Currently, only 'base64' is supported for Solana.To sign a transaction from your wallet, use the
signTransaction method.
It will sign your transaction, and return the signed transaction to you.Usage
Report incorrect code
Copy
Ask AI
try {
// A base64 encoded serialized transaction to sign
String transaction = "insert-base-64-encoded-serialized-transaction";
// Example: If wallet's owner is an authorization private key
AuthorizationContext authorizationContext = AuthorizationContext.builder()
.addAuthorizationPrivateKey("authorization-key")
.build();
SolanaSignTransactionRpcResponseData response = privyClient.wallets().solana()
.signTransaction(
transaction,
authorizationContext
);
String signedTransaction = response.signedTransaction();
} catch (APIException e) {
String errorBody = e.bodyAsString();
System.err.println(errorBody);
} catch (Exception e) {
System.err.println(e.getMessage());
}
Parameters
A base64 encoded serialized transaction to sign.
Returns
TheSolanaSignTransactionRpcResponseData object contains the following fields:The signed transaction.
Use the
sign_transaction method on the Solana service to sign a transaction.Usage
Report incorrect code
Copy
Ask AI
use privy_rs::{AuthorizationContext, PrivyClient};
let client = PrivyClient::new("app_id".to_string(), "app_secret".to_string())?;
let solana_service = client.wallets().solana();
let auth_ctx = AuthorizationContext::new();
// Base64-encoded Solana transaction (example)
let transaction = "AQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABAAEDArczbMia1tLmq7zz4DinMNN0pJ1JtLdqIJPUw3YrGCzYAMHBsgN27lcgB6H2WQvFgyZuJYHa46puOQo9yQ8CVQbd9uHXZaGT2cvhRs7reawctIXtX1s3kTqM9YV+/wCp20C7Wj2aiuk5TReAXo+VTVg8QTHjs0UjNMMKCvpzZ+ABAgEBARU=";
let signed_tx = solana_service.sign_transaction(
&wallet_id,
transaction,
&auth_ctx,
None
).await?;
println!("Transaction signed successfully");
For transaction construction, you’ll typically use the
@solana/web3.js library or
solana_sdk to create your transaction, serialize it to base64, and
then sign it with Privy.
Parameters and Returns
See the Rust SDK documentation for detailed parameter and return types, including embedded examples:For REST API details, see the API reference.To sign a transaction make a A successful response will look like the following:
POST request toReport incorrect code
Copy
Ask AI
https://api.privy.io/v1/wallets/<wallet_id>/rpc
Usage
Report incorrect code
Copy
Ask AI
$ curl --request POST https://api.privy.io/v1/wallets/<wallet_id>/rpc \
-u "<your-privy-app-id>:<your-privy-app-secret>" \
-H "privy-app-id: <your-privy-app-id>" \
-H "privy-authorization-signature: <authorization-signature-for-request>" \
-H 'Content-Type: application/json' \
-d '{
"method": "signTransaction",
"params": {
"transaction": "insert-base-64-encoded-serialized-transaction",
"encoding": "base64"
}
}'
Report incorrect code
Copy
Ask AI
{
"method": "signTransaction",
"data": {
"signed_transaction": "base64-encoded-serialized-signed-transaction",
"encoding": "base64"
}
}
Parameters
The RPC method executed with the wallet.
Returns
The RPC method executed with the wallet.
The signed transaction.
The encoding format for the signed transaction. Currently, only
'base64' is supported for Ethereum.
