Skip to Content
dApps

Transactions

Connect the provider before signing. The transaction examples use the algosdk 2-style from/to fields; match the SDK major version you install. algodClient, addresses, and grouped transactions must be prepared before running the snippets. Browser examples using Buffer need an explicit buffer import or an equivalent base64 encoder.

Learn how to sign and send transactions on Algorand.


Sign Transactions (ARC-0001)

Sign one or more transactions:

import algosdk from 'algosdk' // Create transaction const suggestedParams = await algodClient.getTransactionParams().do() const txn = algosdk.makePaymentTxnWithSuggestedParamsFromObject({ from: senderAddress, to: recipientAddress, amount: 1000000, // 1 ALGO (microAlgos) suggestedParams, }) // Prepare for signing const walletTxns = [{ txn: Buffer.from(algosdk.encodeUnsignedTransaction(txn)).toString('base64'), }] // Sign const signedTxns = await provider.signTxns(walletTxns) console.log('Signed transactions:', signedTxns)

Sign Multiple Transactions

// Group transactions const txns = [txn1, txn2, txn3] algosdk.assignGroupID(txns) // Prepare for signing const walletTxns = txns.map(txn => ({ txn: Buffer.from(algosdk.encodeUnsignedTransaction(txn)).toString('base64'), })) // Sign all const signedTxns = await provider.signTxns(walletTxns)

Partial Signing (Multi-sig)

// Only sign specific transactions in a group const walletTxns = [ { txn: encodedTxn1 }, { txn: encodedTxn2, signers: [] }, // Skip this one { txn: encodedTxn3 }, ] const signedTxns = await provider.signTxns(walletTxns) // signedTxns[1] will be null

Post Transactions

Broadcast signed transactions:

const completeTxns = signedTxns.filter((txn) => txn !== null) if (completeTxns.length !== signedTxns.length) { throw new Error('Complete all required signatures before broadcasting the group') } const result = await provider.postTxns(completeTxns) console.log('Transaction IDs:', result.txnIDs)

Sign and Post

Sign and broadcast in one step:

const result = await provider.signAndPostTxns(walletTxns) console.log('Transaction IDs:', result.txnIDs)

Legacy Transaction API

Sign Transaction (Legacy)

// Create transaction as Uint8Array const txnBytes = algosdk.encodeUnsignedTransaction(txn) // Sign const signedTxnBytes = await provider.signTransaction([txnBytes]) // Send const { txId } = await algodClient.sendRawTransaction(signedTxnBytes[0]).do()

Sign and Send (Legacy)

const result = await provider.signAndSendTransaction([txnBytes]) console.log('Transaction ID:', result.txId)

Complete Example

import algosdk from 'algosdk' async function sendAlgo() { // Connect const provider = window.$onekey?.algo if (!provider) throw new Error("OneKey Algorand provider is unavailable") const { address } = await provider.connect() // Setup client const algodClient = new algosdk.Algodv2( '', 'https://mainnet-api.algonode.cloud', '' ) // Get params const suggestedParams = await algodClient.getTransactionParams().do() // Create transaction const txn = algosdk.makePaymentTxnWithSuggestedParamsFromObject({ from: address, to: 'RECIPIENT_ADDRESS', amount: 1000000, // 1 ALGO suggestedParams, }) // Sign const walletTxns = [{ txn: Buffer.from(algosdk.encodeUnsignedTransaction(txn)).toString('base64'), }] const signedTxns = await provider.signTxns(walletTxns) if (!signedTxns[0]) throw new Error("The transaction was not signed") // Send const { txId } = await algodClient.sendRawTransaction( Buffer.from(signedTxns[0], 'base64') ).do() console.log('Transaction ID:', txId) // Wait for confirmation await algosdk.waitForConfirmation(algodClient, txId, 4) console.log('Confirmed!') }
Last updated on