Clients Setup & Usage See code on GitHub

Setup Algod client and fetch transactions parameters


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

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

algodClient.getTransactionParams().do()
.then(d => {
  console.log(d);
  txnParams = d;
})
.catch(e => { 
  console.error(e); 
});
          
code snippet output

Enable account discovery See code on GitHub

Request from the user which accounts will be shared with the dApp


algorand.enable({ genesisID: txnParams.genesisID })
.then((d) => {
  console.log(d);
  sharedAccounts = d.accounts;
})
.catch((e) => {
  console.error(e);
});
          
code snippet output

Sign Payment Transaction See code on GitHub

Ask the user to sign a payment transaction using the signTxns method


let unsignedTxn = algosdk.makePaymentTxnWithSuggestedParamsFromObject({
  from: document.getElementById('from').value,
  to: document.getElementById('to').value,
  amount: +document.getElementById('amount').value,
  note: algorand.encoding.stringToByteArray(document.getElementById('note').value),
  suggestedParams: {...txnParams}
});
console.log(unsignedTxn._getDictForDisplay());

// Use the provided library to make the transaction base64-encoded
let b64EncodedTxn = algorand.encoding.msgpackToBase64(unsignedTxn.toByte());

algorand.signTxns([{txn: b64EncodedTxn}])
.then((d) => {
  console.log(d);
  signedTxns = d;
})
.catch((e) => {
    console.error(e);
});
          

code snippet output

Post Transaction to Network See code on GitHub

Post the previously signed transaction to the network


console.log('Waiting for network confirmation...');

algorand.postTxns(signedTxns)
.then((d) => {
  console.log(d);
})
.catch((e) => {
  console.error(e);
});
          
code snippet output

Sign and Post transactions See code on GitHub

Request AlgoSigner to post the transaction to the network as soon as it's signed


let unsignedTxn = algosdk.makePaymentTxnWithSuggestedParamsFromObject({
  from: document.getElementById('from').value,
  to: document.getElementById('to').value,
  amount: +document.getElementById('amount').value,
  note: algorand.encoding.stringToByteArray('sent without returning'),
  suggestedParams: {...txnParams}
});
console.log(unsignedTxn._getDictForDisplay());

// Use the provided library to make the transaction base64-encoded
let b64EncodedTxn = algorand.encoding.msgpackToBase64(unsignedTxn.toByte());

algorand.signAndPostTxns([{txn: b64EncodedTxn}])
.then((d) => {
  console.log(d);
})
.catch((e) => {
  console.error(e);
});
          
code snippet output