Using the Transaction Module in Services Kit Core Client
The Transaction Module allows you to interact with user transactions. It enables you to let the user sign a transaction and also allows you to sign and broadcast a transaction through Ledger Live.
Transaction Module Overview
Access the Transaction module via walletApiClient.transaction
.
Methods:
1. Sign Transaction
Let the user sign a transaction without broadcasting it. This is useful if you want to sign a transaction for later use.
walletApiClient.transaction.sign(
accountId: string,
transaction: Transaction,
options?: TransactionSign["params"]["options"],
meta?: Record<string, unknown>
): Promise<Buffer>
Parameters:
accountId
(required): The ID of the account where the transaction will be made.transaction
(required): The transaction object in the currency family-specific format.options
(optional): Extra parameters that might be needed for the transaction. See references for more information.meta
(optional): Metadata for the transaction.
Returns: A promise that resolves with the raw signed transaction as a Buffer.
Required permission:
transaction.sign
Example:
async function signTransaction(walletApiClient, accountId, transaction, options, meta) {
try {
const signedTransaction = await walletApiClient.transaction.sign(accountId, transaction, options, meta);
console.log('Signed transaction:', signedTransaction);
} catch (error) {
console.error('Error signing transaction:', error);
}
}
2. Sign and Broadcast Transaction
Let the user sign a transaction and broadcast it to the network.
walletApiClient.transaction.signAndBroadcast(
accountId: string,
transaction: Transaction,
options?: TransactionSignAndBroadcast["params"]["options"],
meta?: Record<string, unknown>
): Promise<string>
Parameters:
accountId
(required): The ID of the account where the transaction will be made.transaction
(required): The transaction object in the currency family-specific format.options
(optional): Extra parameters that might be needed for the transaction. See references for more information.meta
(optional): Metadata for the transaction.
Returns: A promise that resolves with the transaction hash as a string.
Required permission:
transaction.signAndBroadcast
Example:
async function signAndBroadcastTransaction(walletApiClient, accountId, transaction, options, meta) {
try {
const transactionHash = await walletApiClient.transaction.signAndBroadcast(accountId, transaction, options, meta);
console.log('Transaction Hash:', transactionHash);
} catch (error) {
console.error('Error signing and broadcasting transaction:', error);
}
}
Handling Errors
Make sure to handle errors gracefully and provide appropriate feedback to the user. Additionally, always remember to disconnect the WindowMessageTransport
when you’re done interacting with the Ledger Services Kit to free up resources.