Signing And Posting Transactions
Overview
Providers may have the functionality to both sign & post transactions. This method allows clients to avoid using the previous methods of signing transactions and posting transactions separately and simply performs the functionality of these previous methods within one method.
Sending transactions to be signed & posted to the provider
note
See signing transactions for more examples.
- Javascript
- TypeScript
const algosdk = require('algosdk');
// create a transaction
try {
const client = new algosdk.Algodv2(
'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
'http://localhost',
'4001',
);
const encoder = new TextEncoder();
const suggestedParams = await client.getTransactionParams().do();
const transaction = algosdk.makePaymentTxnWithSuggestedParamsFromObject({
amount: algosdk.algosToMicroalgos(1),
from: 'P3AIQVDJ2CTH54KSJE63YWB7IZGS4W4JGC53I6GK72BGZ5BXO2B2PS4M4U',
note: encoder.encode('Hello Human!'),
suggestedParams,
to: '6GT6EXFDAHZDZYUOPT725ZRWYBZDCEGYT7SYYXGJKRFUAG5B7JMI7DQRNQ',
});
const transactionBytes = transaction.toByte();
} catch (error) {
// handle error
}
// initialized client
client.onSignAndPostTransactions(({ error, result }) => {
if (error) {
console.error('error:', error);
return;
}
console.log(result);
/*
{
providerId: '02657eaf-be17-4efc-b0a4-19d654b2448e',
txnIDs: [
'OKU6A2Q...',
],
}
*/
});
// send a sign and post transactions request
client.signAndPostTransactions({
providerId: '02657eaf-be17-4efc-b0a4-19d654b2448e',
txns: [
{
txn: btoa(String.fromCodePoint(...transactionBytes)), // convert to base64 string
},
],
});
import {
Algodv2,
algosToMicroalgos,
makePaymentTxnWithSuggestedParamsFromObject,
SuggestedParams,
Transaction,
} from 'algosdk';
import { IAVMWebClientCallbackOptions } from '@agoralabs-sh/avm-web-provider';
// create a transaction
try {
const client: Algodv2 = new Algodv2(
'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
'http://localhost',
'4001',
);
const encoder: TextEncoder = new TextEncoder();
const suggestedParams: SuggestedParams = await client.getTransactionParams().do();
const transaction: Transaction = makePaymentTxnWithSuggestedParamsFromObject({
amount: algosToMicroalgos(1),
from: 'P3AIQVDJ2CTH54KSJE63YWB7IZGS4W4JGC53I6GK72BGZ5BXO2B2PS4M4U',
note: encoder.encode('Hello Human!'),
suggestedParams,
to: '6GT6EXFDAHZDZYUOPT725ZRWYBZDCEGYT7SYYXGJKRFUAG5B7JMI7DQRNQ',
});
const transactionBytes: Uint8Array = transaction.toByte();
} catch (error) {
// handle error
}
// initialized client
client.onSignAndPostTransactions(({ error, result }: IAVMWebClientCallbackOptions) => {
if (error) {
console.error('error:', error);
return;
}
console.log(result);
/*
{
providerId: '02657eaf-be17-4efc-b0a4-19d654b2448e',
txnIDs: [
'OKU6A2Q...',
],
}
*/
});
// send a sign and post transactions request
client.signAndPostTransactions({
providerId: '02657eaf-be17-4efc-b0a4-19d654b2448e',
txns: [
{
txn: btoa(String.fromCodePoint(...transactionBytes)), // convert to base64 string
},
],
});
caution
If this method is not supported, then a MethodNotSupportedError
should be thrown.
caution
If there is a group of atomic transactions, but the computed group ID is invalid, then a InvalidGroupIdError
should be thrown.
caution
If any of the transactions are malformed, then a InvalidInputError
should be thrown.
caution
If the transactions fail to be accepted by the network, then a FailedToPostSomeTransactionsError
should be thrown.