// Verify AgentGuard gate receipts against the published signing key. Node 18 or newer, no dependencies. // // node verify-receipt.js agentguard-receipts.jsonl every receipt the handler kept, one per line // node verify-receipt.js receipt.json one receipt // node verify-receipt.js receipt.json --jwks jwks.json offline, with a copy of the published key list // // The published key list is https://agentguard.run/.well-known/jwks.json (set AGENTGUARD_JWKS_URL to read another // copy). A receipt verifies when the key it names is on that list, the key fingerprint it carries (if any) is that // key's, and its Ed25519 signature matches every field except the signature, the key and the fingerprint. Change any // signed field, name a key that is not on the list or carry a wrong fingerprint and the answer turns false. Exit code 0 when every receipt verifies, 1 otherwise. // // A valid signature says the gate signed this record. It does not say the action was correct. 'use strict'; const crypto = require('node:crypto'); const fs = require('node:fs'); const JWKS_URL = process.env.AGENTGUARD_JWKS_URL || 'https://agentguard.run/.well-known/jwks.json'; const ED25519_SPKI_PREFIX = Buffer.from('302a300506032b6570032100', 'hex'); // The exact text the gate signs: JSON with object keys sorted at every level and no spaces. function canonicalJson(value) { if (value === null || typeof value !== 'object') return JSON.stringify(value); if (Array.isArray(value)) return '[' + value.map(canonicalJson).join(',') + ']'; return '{' + Object.keys(value).sort().map((key) => JSON.stringify(key) + ':' + canonicalJson(value[key])).join(',') + '}'; } // The Ed25519 public keys on a published key list, as lower case hex. function publishedKeysHex(jwks) { const keys = jwks && Array.isArray(jwks.keys) ? jwks.keys : []; return keys.filter((k) => k && k.kty === 'OKP' && k.crv === 'Ed25519' && typeof k.x === 'string') .map((k) => Buffer.from(k.x, 'base64url').toString('hex')); } // receipt: the signed receipt exactly as the gate returned it. jwks: the published key list. // Returns { ok, reason }: reason is null, not_a_signed_receipt, key_not_published or signature_invalid. function verifyReceipt(receipt, jwks) { const { signature, public_key_hex: keyHex, public_key_fingerprint: keyFingerprint, ...signed } = receipt && typeof receipt === 'object' ? receipt : {}; if (typeof signature !== 'string' || !/^[0-9a-f]{128}$/i.test(signature) || typeof keyHex !== 'string' || !/^[0-9a-f]{64}$/i.test(keyHex)) return { ok: false, reason: 'not_a_signed_receipt' }; // The receipt names its key. Trust it only when the published list has it; anyone can sign with a key of their own. if (!publishedKeysHex(jwks).includes(keyHex.toLowerCase())) return { ok: false, reason: 'key_not_published' }; // The fingerprint sits beside the signature, not inside it: when the receipt carries one, it must be its key's // (the first 8 bytes of the SHA-256 of the raw key, as hex). if (keyFingerprint !== undefined && String(keyFingerprint).toLowerCase() !== crypto.createHash('sha256').update(Buffer.from(keyHex, 'hex')).digest().subarray(0, 8).toString('hex')) return { ok: false, reason: 'fingerprint_mismatch' }; const publicKey = crypto.createPublicKey({ key: Buffer.concat([ED25519_SPKI_PREFIX, Buffer.from(keyHex, 'hex')]), format: 'der', type: 'spki' }); const ok = crypto.verify(null, Buffer.from(canonicalJson(signed), 'utf8'), publicKey, Buffer.from(signature, 'hex')); return ok ? { ok: true, reason: null } : { ok: false, reason: 'signature_invalid' }; } // One receipt as JSON, or one per line as the handlers write them. function readReceipts(file) { const text = fs.readFileSync(file, 'utf8').trim(); try { const one = JSON.parse(text); return Array.isArray(one) ? one : [one]; } catch { /* one per line */ } return text.split('\n').filter((line) => line.trim()).map((line) => JSON.parse(line)); } async function main(argv) { const at = argv.indexOf('--jwks'); const jwksFile = at >= 0 ? argv[at + 1] : null; const file = argv.find((arg, i) => !arg.startsWith('--') && (at < 0 || i !== at + 1)); if (!file) { process.stderr.write('usage: node verify-receipt.js [--jwks jwks.json]\n'); return 2; } const jwks = jwksFile ? JSON.parse(fs.readFileSync(jwksFile, 'utf8')) : await (await fetch(JWKS_URL)).json(); const receipts = readReceipts(file); let good = 0; for (const receipt of receipts) { const { ok, reason } = verifyReceipt(receipt, jwks); if (ok) good += 1; process.stdout.write(`${ok ? 'verified' : 'NOT VERIFIED'} ${receipt && receipt.receipt_id ? receipt.receipt_id : '(no receipt id)'} ${receipt && receipt.decision ? receipt.decision : ''}${ok ? '' : ' ' + reason}\n`); } process.stdout.write(`${good} of ${receipts.length} receipts verified against ${jwksFile || JWKS_URL}\n`); return good === receipts.length && receipts.length > 0 ? 0 : 1; } if (require.main === module) { main(process.argv.slice(2)).then((code) => { process.exitCode = code; }, (error) => { process.stderr.write(`could not verify: ${error.message}\n`); process.exitCode = 2; }); } module.exports = { verifyReceipt, canonicalJson, publishedKeysHex };