Send tx
After connecting to UKey Wallet, applications can construct Solana transactions and request user confirmation. The transaction is signed and sent only after the user approves it in the wallet.
The typical process is as follows:
- Create an unsigned transaction.
- Set recent blockhash, fee payer and necessary instructions.
- Request UKey Wallet to sign and send the transaction.
- Use Solana JSON RPC to query transaction status or wait for confirmation.
For more Solana transaction structures, please refer to solana-web3.js Documentation and Solana Cookbook.
Signing and sending transaction
After the transaction construction is completed, the application can call signAndSendTransaction to let the wallet complete signing and broadcasting.
This path is usually chosen because the transaction is submitted immediately upon user confirmation and the application only needs to track the returned signature. If the project uses request package, you can also call the method with the same name.
Use signAndSendTransaction method
const provider = getProvider(); // Use the helper from "Detecting Providers"
const network = "<DEMO_NETWORK_URL>";
const connection = new Connection(network);
const transaction = new Transaction();
const { signature } = await provider.signAndSendTransaction(transaction);
await connection.getSignatureStatus(signature);
Use request
const provider = getProvider(); // Use the helper from "Detecting Providers"
const network = "<DEMO_NETWORK_URL>";
const connection = new Connection(network);
const transaction = new Transaction();
const { signature } = await provider.request({
method: "signAndSendTransaction",
requestParams: {
message: bs58.encode(transaction.serializeMessage()),
},
});
await connection.getSignatureStatus(signature);
To set sending options, pass SendOptions object as the second inputs to signAndSendTransaction, or put options in request.
Other signing methods
The following methods can still be used, but are generally not advised as a priority. Having the wallet broadcast the transaction immediately after signing can reduce the risk of misuse or delayed submission of the signed transaction after the application obtains it. If you only sign without sending, the wallet will show additional reminders to the user.
Signing transaction (without sending)
signTransaction Only allows the wallet to sign and is not responsible for broadcasting. After the application gets the signed transaction, it needs to submit it to the network by itself.
Once signed, it can be broadcast via sendRawTransaction for web3.js.
Use signTransaction method
const provider = getProvider();
const network = "<DEMO_NETWORK_URL>";
const connection = new Connection(network);
const transaction = new Transaction();
const signedTransaction = await provider.signTransaction(transaction);
const signature = await connection.sendRawTransaction(
signedTransaction.serialize(),
);
Use request
const provider = getProvider();
const network = "<DEMO_NETWORK_URL>";
const connection = new Connection(network);
const transaction = new Transaction();
const signedTransaction = await provider.request({
method: "signTransaction",
requestParams: {
message: bs58.encode(transaction.serializeMessage()),
},
});
const signature = await connection.sendRawTransaction(
signedTransaction.serialize(),
);
Signing multiple transactions
If the business requires batch processing, you can use signAllTransactions to request the user to sign multiple transactions. Make sure every transaction is understandable to users in the wallet and clearly demonstrate the purpose of batch signing on the front-end.
Use signAllTransactions method
const signedTransactions = await provider.signAllTransactions(transactions);
Use request
const message = transactions.map((transaction) => {
return bs58.encode(transaction.serializeMessage());
});
const signedTransactions = await provider.request({
method: "signAllTransactions",
requestParams: { message },
});