Transfers
This walkthrough covers how to sign ARC-0001 transactions, broadcast transactions, handle grouped transactions, and be compatible with traditional transaction APIs through the UKey Wallet Algorand Provider.
Signed transaction (ARC-0001)
The following example constructs a payment transaction, encodes it and gives it to the wallet for signature:
import algosdk from "algosdk";
// Build transaction
const suggestedParams = await algodClient.getTransactionParams().do();
const txn = algosdk.makePaymentTxnWithSuggestedParamsFromObject({
from: senderAddress,
to: recipientAddress,
amount: 1000000, // one ALGO (microAlgos)
suggestedParams,
});
// Prepare for signing
const walletTxns = [
{
txn: Buffer.from(algosdk.encodeUnsignedTransaction(txn)).toString("base64"),
},
];
// Sign with the wallet
const signedTxns = await provider.signTxns(walletTxns);
console.log("Signed payload:", signedTxns);
Sign multiple transactions
// Note: trading group
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 every transaction
const signedTxns = await provider.signTxns(walletTxns);
Partial signature (multiple signatures)
// Note: Only sign specific transactions in the group
const walletTxns = [
{ txn: encodedTxn1 },
{ txn: encodedTxn2, signers: [] }, // Skip this one
{ txn: encodedTxn3 },
];
const signedTxns = await provider.signTxns(walletTxns);
// signedTxns[1] 在这种情况下会是 null
Submit transaction
Broadcast a signed transaction:
const callResult = await provider.postTxns(signedTxns);
console.log("Tx ID:", callResult.txIDs);
Sign and submit
Complete signing and broadcasting in one step:
const callResult = await provider.signAndPostTxns(walletTxns);
console.log("Tx ID:", callResult.txIDs);
Traditional trading API
Signature transaction (traditional)
// Build the transaction as Uint8Array
const txnBytes = algosdk.encodeUnsignedTransaction(txn);
// Sign with the wallet
const signedTxnBytes = await provider.signTransaction([txnBytes]);
// submit
const { txId } = await algodClient.sendRawTransaction(signedTxnBytes[0]).do();
Sign and send (traditional)
const callResult = await provider.signAndSendTransaction([txnBytes]);
console.log("Tx ID:", callResult.txId);
Complete example
import algosdk from "algosdk";
async function submitAlgoTransfer() {
// establish connection
const provider = window.$ukey?.algo;
const { address } = await provider.connect();
// Set up the client
const algodClient = new algosdk.Algodv2(
"",
"https://mainnet-api.algonode.cloud",
"",
);
// Retrieve inputs
const suggestedParams = await algodClient.getTransactionParams().do();
// Build transaction
const txn = algosdk.makePaymentTxnWithSuggestedParamsFromObject({
from: address,
to: "TARGET_ACCOUNT_ADDRESS",
amount: 1000000, // one ALGO
suggestedParams,
});
// Sign with the wallet
const walletTxns = [
{
txn: Buffer.from(algosdk.encodeUnsignedTransaction(txn)).toString(
"base64",
),
},
];
const signedTxns = await provider.signTxns(walletTxns);
// submit
const { txId } = await algodClient
.sendRawTransaction(Buffer.from(signedTxns[0], "base64"))
.do();
console.log("Tx ID:", txId);
// Wait for confirmation
await algosdk.waitForConfirmation(algodClient, txId, 4);
console.log("Confirmation received!");
}