On this page
Sending a Transaction
Once a web application is connected to OneKey, it can prompt the user for permission to send transactions on their behalf. In order to send a transaction, a web application must:
- Create an unsigned transaction.
- Have the transaction be signed and submitted to the network by the user’s OneKey wallet.
- Optionally await network confirmation using a Solana JSON RPC connection.
For more information about the nature of Solana transactions, please review the solana-web3.js docs as well as the Solana Cookbook .
Prerequisites
The examples use Connection and Transaction from @solana/web3.js; the raw request examples also use bs58. Connect the wallet first, and build a transaction with instructions, a fee payer, and a recent blockhash from the intended network. Replace <NETWORK_URL> with that network’s RPC endpoint. An empty new Transaction() cannot be signed or sent.
Signing and Sending a Transaction
Once a transaction is created, the web application may ask the user’s OneKey wallet to sign and send the transaction.
If accepted, OneKey will sign the transaction with the user’s private key and submit it via a Solana JSON RPC connection. By far the easiest and most recommended way of doing this is by using the signAndSendTransaction method on the provider, but it is also possible to do with request. In both cases, the call will return a Promise for an object containing the signature.
signAndSendTransaction
const provider = getProvider(); // see "Detecting the Provider"
const network = "<NETWORK_URL>";
const connection = new Connection(network);
// `transaction` is a fully constructed Transaction with instructions,
// feePayer, and a recent blockhash for this connection.
const { signature } = await provider.signAndSendTransaction(transaction);
const status = await connection.getSignatureStatus(signature);
// This is a status query, not a wait for confirmation. Inspect status.value.Request
const provider = getProvider(); // see "Detecting the Provider"
const network = "<NETWORK_URL>";
const connection = new Connection(network);
// `transaction` is a fully constructed Transaction with instructions,
// feePayer, and a recent blockhash for this connection.
const { signature } = await provider.request({
method: "signAndSendTransaction",
params: {
message: bs58.encode(transaction.serialize({ requireAllSignatures: false })),
},
});
const status = await connection.getSignatureStatus(signature);
// This is a status query, not a wait for confirmation. Inspect status.value.You can also specify a SendOptions object as a second argument into signAndSendTransaction or as an options parameter when using request.
Other Signing Methods
Use signing-only methods when your application needs to control submission. They return signed transactions without broadcasting them; your application must submit and confirm the result. The provider marks these convenience methods as deprecated, so check the integration path supported by your SDK version.
Signing a Transaction (Without Sending)
Once a transaction is created, a web application may ask the user’s OneKey wallet to sign the transaction without also submitting it to the network. The easiest and most recommended way of doing this is via the signTransaction method on the provider, but it is also possible to do via request. In both cases, the call will return a Promise for the signed transaction.
After the transaction has been signed, an application may submit the transaction itself via web3js’s sendRawTransaction.
signTransaction
const provider = getProvider();
const network = "<NETWORK_URL>";
const connection = new Connection(network);
// `transaction` is a fully constructed Transaction with instructions,
// feePayer, and a recent blockhash for this connection.
const signedTransaction = await provider.signTransaction(transaction);
const signature = await connection.sendRawTransaction(signedTransaction.serialize());Request
const provider = getProvider();
const network = "<NETWORK_URL>";
const connection = new Connection(network);
// `transaction` is a fully constructed Transaction with instructions,
// feePayer, and a recent blockhash for this connection.
const signedTransaction = await provider.request({
method: "signTransaction",
params: {
message: bs58.encode(transaction.serialize({ requireAllSignatures: false })),
},
});
const signature = await connection.sendRawTransaction(signedTransaction.serialize());Signing Multiple Transactions
Use signAllTransactions to sign multiple transactions. It does not send them; submit each signed transaction separately when appropriate.
signAllTransactions
const signedTransactions = await provider.signAllTransactions(transactions);request
const message = transactions.map(transaction => {
return bs58.encode(transaction.serialize({ requireAllSignatures: false }));
});
const signedTransactions = await provider.request({
method: "signAllTransactions",
params: { message },
});