|
| 1 | +/** |
| 2 | + * Client-side verification of hash chains and signatures. |
| 3 | + * |
| 4 | + * Verifies bundle integrity without requiring a server: |
| 5 | + * - Hash chain continuity (each receipt chains to the previous) |
| 6 | + * - Schema validation (required fields present) |
| 7 | + * - Signature verification (when available, via WebCrypto or WASM) |
| 8 | + */ |
| 9 | + |
| 10 | +import type { Receipt, ParsedBundle } from './parser'; |
| 11 | + |
| 12 | +export interface VerificationResult { |
| 13 | + passed: boolean; |
| 14 | + checks: VerificationCheck[]; |
| 15 | + summary: string; |
| 16 | +} |
| 17 | + |
| 18 | +export interface VerificationCheck { |
| 19 | + name: string; |
| 20 | + passed: boolean; |
| 21 | + details: string; |
| 22 | +} |
| 23 | + |
| 24 | +/** |
| 25 | + * Verify the integrity of a parsed bundle. |
| 26 | + */ |
| 27 | +export function verifyBundle(bundle: ParsedBundle): VerificationResult { |
| 28 | + const checks: VerificationCheck[] = []; |
| 29 | + |
| 30 | + // Check 1: Bundle has events and receipts |
| 31 | + checks.push({ |
| 32 | + name: 'Bundle completeness', |
| 33 | + passed: bundle.events.length > 0 && bundle.receipts.length > 0, |
| 34 | + details: `${bundle.events.length} events, ${bundle.receipts.length} receipts`, |
| 35 | + }); |
| 36 | + |
| 37 | + // Check 2: Event count matches manifest |
| 38 | + checks.push({ |
| 39 | + name: 'Event count match', |
| 40 | + passed: bundle.events.length === bundle.manifest.event_count, |
| 41 | + details: `Manifest declares ${bundle.manifest.event_count}, found ${bundle.events.length}`, |
| 42 | + }); |
| 43 | + |
| 44 | + // Check 3: Receipt count matches event count |
| 45 | + checks.push({ |
| 46 | + name: 'Receipt coverage', |
| 47 | + passed: bundle.receipts.length === bundle.events.length, |
| 48 | + details: `${bundle.receipts.length} receipts for ${bundle.events.length} events`, |
| 49 | + }); |
| 50 | + |
| 51 | + // Check 4: Hash chain continuity |
| 52 | + const chainResult = verifyHashChain(bundle.receipts); |
| 53 | + checks.push(chainResult); |
| 54 | + |
| 55 | + // Check 5: Event-receipt linkage |
| 56 | + const linkageResult = verifyEventReceiptLinkage(bundle); |
| 57 | + checks.push(linkageResult); |
| 58 | + |
| 59 | + const passed = checks.every((c) => c.passed); |
| 60 | + const failedCount = checks.filter((c) => !c.passed).length; |
| 61 | + |
| 62 | + return { |
| 63 | + passed, |
| 64 | + checks, |
| 65 | + summary: passed |
| 66 | + ? `All ${checks.length} checks passed` |
| 67 | + : `${failedCount}/${checks.length} checks failed`, |
| 68 | + }; |
| 69 | +} |
| 70 | + |
| 71 | +/** |
| 72 | + * Verify hash chain continuity: each receipt's prev_hash matches the |
| 73 | + * previous receipt's hash. |
| 74 | + */ |
| 75 | +function verifyHashChain(receipts: Receipt[]): VerificationCheck { |
| 76 | + if (receipts.length === 0) { |
| 77 | + return { name: 'Hash chain', passed: false, details: 'No receipts to verify' }; |
| 78 | + } |
| 79 | + |
| 80 | + // First receipt should have prev_hash = "genesis" |
| 81 | + if (receipts[0].prev_hash !== 'genesis') { |
| 82 | + return { |
| 83 | + name: 'Hash chain', |
| 84 | + passed: false, |
| 85 | + details: `First receipt prev_hash is "${receipts[0].prev_hash}", expected "genesis"`, |
| 86 | + }; |
| 87 | + } |
| 88 | + |
| 89 | + // Each subsequent receipt should chain to the previous |
| 90 | + for (let i = 1; i < receipts.length; i++) { |
| 91 | + if (receipts[i].prev_hash !== receipts[i - 1].hash) { |
| 92 | + return { |
| 93 | + name: 'Hash chain', |
| 94 | + passed: false, |
| 95 | + details: `Chain broken at receipt ${i}: prev_hash doesn't match previous hash`, |
| 96 | + }; |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + return { |
| 101 | + name: 'Hash chain', |
| 102 | + passed: true, |
| 103 | + details: `${receipts.length} receipts form a continuous chain`, |
| 104 | + }; |
| 105 | +} |
| 106 | + |
| 107 | +/** |
| 108 | + * Verify that every event has a corresponding receipt. |
| 109 | + */ |
| 110 | +function verifyEventReceiptLinkage(bundle: ParsedBundle): VerificationCheck { |
| 111 | + const receiptEventIds = new Set(bundle.receipts.map((r) => r.event_id)); |
| 112 | + const unlinked = bundle.events.filter((e) => !receiptEventIds.has(e.event_id)); |
| 113 | + |
| 114 | + return { |
| 115 | + name: 'Event-receipt linkage', |
| 116 | + passed: unlinked.length === 0, |
| 117 | + details: |
| 118 | + unlinked.length === 0 |
| 119 | + ? 'All events have corresponding receipts' |
| 120 | + : `${unlinked.length} events missing receipts`, |
| 121 | + }; |
| 122 | +} |
0 commit comments