-
-
Save cannikin/2034f6cc91129863c417793908220d75 to your computer and use it in GitHub Desktop.
Verify signed algosdk transaction (javascript)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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