#!/usr/bin/env node // 🤖 Verify a ledger stream end-to-end (issue #1224): fetch HEAD, walk the // prev-hash chain newest→oldest re-hashing every entry's exact bytes, and // fail loudly on the first broken link. Needs no credentials — the whole // walk rides the bucket's public https door, so anyone can run it. // // node tools/ledger-verify.mjs import { fetchPublic, getHead } from "./ledgerstream.mjs"; import { verifyLink } from "./ledgerchain.mjs"; const ledger = process.argv[2]; if (!ledger) { console.error("usage: node tools/ledger-verify.mjs "); process.exit(1); } const head = await getHead(ledger); if (!head) { console.error(ledger + ": no HEAD — stream doesn't exist"); process.exit(1); } let key = head.head_key, claimedSha = head.head_sha256, walked = 0; while (key) { const bytes = await fetchPublic(key); if (!bytes) { console.error("BROKEN: " + key + " is missing"); process.exit(1); } const v = verifyLink({ bytes, key, claimedSha }); if (!v.ok) { console.error("BROKEN at " + key + ": " + v.reason); process.exit(1); } walked++; key = v.entry.prev_key; claimedSha = v.entry.prev_sha256; } if (walked !== head.count) { console.error("BROKEN: HEAD counts " + head.count + " but the chain walks " + walked); process.exit(1); } console.log(ledger + ": chain OK — " + walked + " entr" + (walked === 1 ? "y" : "ies") + ", head " + head.head_sha256.slice(0, 12) + "…");