Skip to content

Instantly share code, notes, and snippets.

@cannikin
Forked from joe-p/verify_txn.js
Created February 10, 2022 16:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cannikin/2034f6cc91129863c417793908220d75 to your computer and use it in GitHub Desktop.
Save cannikin/2034f6cc91129863c417793908220d75 to your computer and use it in GitHub Desktop.
Verify signed algosdk transaction (javascript)
const algosdk = require('algosdk');
const nobleEd25519 = require('@noble/ed25519');
// setup connection to algoexplorer API
const algodClient = new algosdk.Algodv2("", 'https://api.testnet.algoexplorer.io', '');
// generate a standalone account
const account = algosdk.generateAccount();
(async () => {
// create a transaction that will be signed
const params = await algodClient.getTransactionParams().do();
const txn = algosdk.makePaymentTxnWithSuggestedParamsFromObject({
suggestedParams: {
...params,
},
from: account.addr,
to: account.addr,
amount: 0
});
// sign and decode transaction
const sTxn = algosdk.signTransaction(txn, account.sk)
const dTxn = algosdk.decodeSignedTransaction(sTxn.blob)
// decode address to get the public key
const dAddr = algosdk.decodeAddress(account.addr);
const publicKey = dAddr.publicKey
// get the bytes to sign from the transaction
const hTxn = Buffer.from(txn.bytesToSign()).toString('hex');
// get the signature from the signed transaction
const sig = Buffer.from(dTxn.sig)
// verify signature
const verified = await nobleEd25519.verify(sig, hTxn, publicKey);
// log verification status (should be true)
console.log(verified)
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment