AlgoSigner V1 to V2 Transition Guide


Connect

Connecting to AlgoSigner is not effected by the transition

AlgoSigner.connect()
.catch(e => console.error(e));
AlgoSigner.connect()
.catch(e => console.error(e));
response

SDK Setup

The algosdk client setup is not effected by the transition

const algodServer = 'https://testnet-algorand.api.purestake.io/ps2';
const token = { 'X-API-Key': 'YOUR API KEY HERE' };

algodClient = new algosdk.Algodv2(token, algodServer, '');

algodClient.status().do()
.then(d => console.log(d['last-round']))
.catch(e =>  console.error(e));
const algodServer = 'https://testnet-algorand.api.purestake.io/ps2';
const token = { 'X-API-Key': 'YOUR API KEY HERE' };

algodClient = new algosdk.Algodv2(token, algodServer, '');

algodClient.status().do()
.then(d => console.log(d['last-round']))
.catch(e =>  console.error(e));
response

Get TestNet Accounts

Retrieval of user accounts is not effected by the transition

AlgoSigner.accounts({ ledger: 'TestNet' })
.then(d => accounts = d)
.catch(e => console.error(e));
AlgoSigner.accounts({ ledger: 'TestNet' })
.then(d => accounts = d)
.catch(e => console.error(e));
response

Get Params

There is a change in the way paramaters are expected to be obtained

Use getTransactionParams(): Use the algoClient.getTransactionParams() function instead of direct calls to the api

AlgoSigner.algod({
    ledger: 'TestNet',
    path: '/v2/transactions/params'
})
.then(d => txParams = d)
.catch(e => console.error(e));
algodClient.getTransactionParams().do()
.then(d => txParamsJS = d)
.catch(e => console.error(e));
response

Sign Pay Transaction

There are several notable differences in the signing functionality

Make Transaction: The make transaction functions in algosdk can now be used directly
Params: Suggested params in make functions use JavaScript naming from the result of algodClient.getTransactionParams() instead of the API - 'genesis-id' -> 'genesisID'
Binary Values: Previously converted fields ['note','group','appArgs','appApprovalProgram','appClearProgram'] should be binary
Transaction Encoding: The transaction must be converted to base64 after undergoing a toByte() - AlgoSigner.encoding.msgpackToBase64(txn.toByte())
Use signTxn(): The signing method is now AlgoSigner.signTxn() instead of sign()
Array Transactions: The function signTxn() accepts an array of transactions following the signing standards structure here


AlgoSigner.sign({
    from: document.getElementById('from').value,
    to: document.getElementById('to').value,
    amount: +document.getElementById('amount').value,
    note: document.getElementById('note').value,
    type: 'pay',
    fee: txParams['min-fee'],
    firstRound: txParams['last-round'],
    lastRound: txParams['last-round'] + 1000,
    genesisID: txParams['genesis-id'],
    genesisHash: txParams['genesis-hash'],
    flatFee: true
})
.then(d => signedTx = d)
.catch(e => console.error(e));
const txn = algosdk.makePaymentTxnWithSuggestedParamsFromObject({
    from: document.getElementById('from').value,
    to: document.getElementById('to').value,
    amount: +document.getElementById('amount').value,
    note: AlgoSigner.encoding.stringToByteArray(document.getElementById('note').value),
    suggestedParams: {...txParamsJS}
});

// Use the AlgoSigner encoding library to make the transactions base64
let txn_b64 = AlgoSigner.encoding.msgpackToBase64(txn.toByte());

AlgoSigner.signTxn([{txn: txn_b64}])
.then(d => signedTxs = d)
.catch(e => console.error(e));
response

Send Signed Transaction

Send transaction is mostly the same, with one exception.

Array from signTxn(): The result of signTxn() is an array so use the index to obtain a specific transaction

AlgoSigner.send({
  ledger: 'TestNet',
  tx: signedTx.blob
})
.then(d => tx = d)
.catch(e => console.error(e));
AlgoSigner.send({
  ledger: 'TestNet',
  tx: signedTxs[0].blob
})
.then(d => tx = d)
.catch(e => console.error(e));
response