Created
March 31, 2023 11:39
-
-
Save nullun/e1f0f08c08c1ba2aa8b17f6e3f4e819d to your computer and use it in GitHub Desktop.
Calculating TxID for all transactions in a block, then verify those TxIDs exist.
This file contains 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
import algosdk from 'algosdk'; | |
const token = 'a'.repeat(64); | |
const address = 'http://192.168.1.100'; | |
const port = 8080; | |
const client = new algosdk.Algodv2(token, address, port); | |
// Recursively remove all null values | |
function removeNulls(obj) { | |
for (let key in obj) { | |
if (obj[key] === null) { | |
delete obj[key]; | |
} else if (typeof obj[key] === 'object') { | |
removeNulls(obj[key]); | |
} | |
} | |
} | |
(async() => { | |
let status = await client.status().do(); | |
while (true) { | |
let last_round = status['last-round']; | |
console.log(last_round); | |
const round = await client.block(last_round).do(); | |
//console.log(round); | |
const block = round['block'] | |
//console.log(block); | |
const txns = block['txns']; | |
//console.log(txns); | |
for (const t in txns) { | |
const tx = txns[t]; | |
const txn = txns[t]['txn']; | |
// Skip StateProofs | |
if (txn['type'] == 'stpf') | |
continue | |
// Remove nulls (mainly for appl args) | |
removeNulls(txn); | |
let gh = block.gh; | |
let gen = block.gen; | |
// Unset gen if `hgi` isn't set | |
if (!tx['hgi']) | |
gen = null; | |
// Construct Transaction | |
const transaction = algosdk.Transaction.from_obj_for_encoding({...txn, gh, gen}); | |
const txid = transaction.txID(); | |
// Validate the TxID exists against the node | |
// !! Don't run on a public endpoint, you'll probably get rate limited !! | |
const valid = await client.pendingTransactionInformation(txid).do(); | |
if (valid) | |
console.log(txid + "\t Valid"); | |
} | |
// Wait for next round | |
status = await client.statusAfterBlock(last_round).do(); | |
last_round = status['last-round']; | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment