#!/usr/bin/env node 'use strict'; /* * Gov5(TM) Verify v1.0.0 * Copyright (c) 2026 DeBacco Nexus LLC. Gov5 and Governedware are trademarks of DeBacco Nexus LLC. * * Checks a Gov5 signed receipt on your own computer. Nothing is sent anywhere unless you ask * this tool to fetch the official public key. * * Needs Node.js 18 or newer. No other installs. * * node gov5-verify.cjs receipt.json --key gov5-public-key.pem * node gov5-verify.cjs receipt.json --fetch-key (gets the official keys from gateway.debacconexus.ai, * including retired keys for older receipts) * node gov5-verify.cjs receipt.json --fetch-key https://your-gateway.example/api/receipt/public-key * * A receipt file is the JSON a Gov5 Gateway returns: { receipt, receiptHash, signature, payload, publicKey }. * The tool checks three things: * 1. The receipt text has not changed: its SHA-256 matches receiptHash. * 2. The signature is valid for that text under the official Gov5 public key (Ed25519). * 3. The key inside the receipt, if any, is the official key. A receipt can carry its own key, * so the key you trust must come from you (--key) or from the official site (--fetch-key). * Exit code 0 means VERIFIED. Any other code means do not rely on the receipt. */ const fs = require('fs'), crypto = require('crypto'); const OFFICIAL_KEY_URL = 'https://gateway.debacconexus.ai/api/receipt/public-keys'; // current key and retired keys function verify(file, keyPem) { if (Array.isArray(keyPem)) { let best = null; for (const k of keyPem) { const o = verify(file, k.pem); o.keyUsed = k.label; if (o.verified) return o; best = best || o; } return best || done({ checks: [] }); } const out = { checks: [] }, add = (name, pass, detail) => out.checks.push({ name, pass, detail }); let r; try { r = typeof file === 'string' ? JSON.parse(file) : file; } catch (e) { add('receipt is readable JSON', false, e.message); return done(out); } if (!r || typeof r !== 'object' || !r.receipt || !r.signature) { add('receipt has receipt and signature', false, 'missing receipt or signature'); return done(out); } const payload = JSON.stringify(r.receipt); if (r.payload != null) add('payload matches the receipt', r.payload === payload, r.payload === payload ? 'same text' : 'the payload and the receipt differ'); const hash = crypto.createHash('sha256').update(payload).digest('hex'); add('receipt text unchanged (SHA-256)', !!r.receiptHash && hash === r.receiptHash, hash === r.receiptHash ? hash.slice(0, 16) + '...' : 'hash does not match'); let key = null; try { key = crypto.createPublicKey(String(keyPem)); } catch (e) { add('official public key loads', false, e.message); return done(out); } let sigOk = false; try { sigOk = crypto.verify(null, Buffer.from(payload), key, Buffer.from(String(r.signature), 'base64')); } catch (e) {} add('signature valid under the official Gov5 key', sigOk, sigOk ? 'Ed25519 signature checks out' : 'signature does not match'); if (r.publicKey) { const same = norm(r.publicKey) === norm(key.export({ type: 'spki', format: 'pem' })); add('key inside the receipt is the official key', same, same ? 'same key' : 'the receipt carries a different key'); } return done(out); } const norm = p => String(p).replace(/\\n/g, '\n').replace(/\s+/g, ''); const done = out => (out.verified = out.checks.length > 0 && out.checks.every(c => c.pass), out); async function main(argv) { const args = argv.slice(2); if (!args.length || args.includes('--help') || args.includes('-h')) { console.log(fs.readFileSync(__filename, 'utf8').split('*/')[0].replace(/^[\s\S]*?\/\*\n?/, '').replace(/^ \* ?/gm, '')); return 2; } const file = args[0]; let keyPem = null; const ki = args.indexOf('--key'), fi = args.indexOf('--fetch-key'); if (ki > -1) keyPem = fs.readFileSync(args[ki + 1], 'utf8'); else if (fi > -1) { const u = args[fi + 1] && !args[fi + 1].startsWith('--') ? args[fi + 1] : OFFICIAL_KEY_URL; const res = await fetch(u); if (!res.ok) { console.error('Could not fetch the public key: HTTP ' + res.status); return 3; } const body = await res.text(); try { const j = JSON.parse(body); keyPem = [{ pem: j.current, label: 'current key' }].concat((j.retired || []).map(k => ({ pem: k.publicKey, label: 'retired key ' + k.id + ' (retired ' + k.retired + ')' }))).filter(k => k.pem); } catch (e) { keyPem = body; } } else { console.error('Give the official key: --key or --fetch-key'); return 2; } const out = verify(fs.readFileSync(file, 'utf8'), keyPem); console.log('Gov5 Verify v1.0.0' + (out.keyUsed ? ' (checked with the ' + out.keyUsed + ')' : '')); for (const c of out.checks) console.log((c.pass ? ' PASS ' : ' FAIL ') + c.name + ': ' + c.detail); const old = out.verified && /retired/.test(out.keyUsed || ''); console.log(!out.verified ? '\nNOT VERIFIED: do not rely on this receipt.' : old ? '\nVERIFIED WITH A RETIRED KEY: trust this only if you received the receipt before that key was retired. The retired key could have been copied, so new receipts signed with it are not trusted.' : '\nVERIFIED: this receipt was signed by the Gov5 key and has not changed.'); if (old) return 4; return out.verified ? 0 : 1; } if (require.main === module) main(process.argv).then(c => process.exit(c), e => { console.error(e.message); process.exit(3); }); module.exports = { verify, VERSION: '1.0.0' };