本页目录
交易
签名前先连接 provider。交易示例使用 algosdk 2 的 from / to 字段,请安装匹配的 SDK 主版本。运行示例前,需要准备好 algodClient、地址和分组交易。在浏览器中使用 Buffer 时,应显式导入 buffer,或使用等效的 base64 编码工具。
了解如何在 Algorand 上签署和发送交易。
签署交易 (ARC-0001)
签署一个或多个交易:
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)签署多个交易
// 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)部分签名(多签)
// 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提交交易
广播已签名的交易:
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)签名并提交
一步完成签名和广播:
const result = await provider.signAndPostTxns(walletTxns)
console.log('Transaction IDs:', result.txnIDs)旧版交易 API
签署交易(旧版)
// 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()签署并发送(旧版)
const result = await provider.signAndSendTransaction([txnBytes])
console.log('Transaction ID:', result.txId)完整示例
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!')
}最后更新于