@mikeargento/bitgraph-verify 1.11.0 → 1.13.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (62) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +19 -3
  3. package/dist/__tests__/carrier.test.d.ts +2 -0
  4. package/dist/__tests__/carrier.test.d.ts.map +1 -0
  5. package/dist/__tests__/carrier.test.js +162 -0
  6. package/dist/__tests__/carrier.test.js.map +1 -0
  7. package/dist/anchor.js +1 -1
  8. package/dist/anchor.js.map +1 -1
  9. package/dist/canonical.js +1 -1
  10. package/dist/canonical.js.map +1 -1
  11. package/dist/carrier-verify.d.ts +16 -0
  12. package/dist/carrier-verify.d.ts.map +1 -0
  13. package/dist/carrier-verify.js +82 -0
  14. package/dist/carrier-verify.js.map +1 -0
  15. package/dist/carrier.d.ts +140 -0
  16. package/dist/carrier.d.ts.map +1 -0
  17. package/dist/carrier.js +457 -0
  18. package/dist/carrier.js.map +1 -0
  19. package/dist/fuse-member.js +1 -1
  20. package/dist/fuse-member.js.map +1 -1
  21. package/dist/fuse-merkle.js +1 -1
  22. package/dist/fuse-merkle.js.map +1 -1
  23. package/dist/fuse-verify.js +1 -1
  24. package/dist/fuse-verify.js.map +1 -1
  25. package/dist/fuse.js +1 -1
  26. package/dist/fuse.js.map +1 -1
  27. package/dist/index.d.ts +6 -2
  28. package/dist/index.d.ts.map +1 -1
  29. package/dist/index.js +5 -2
  30. package/dist/index.js.map +1 -1
  31. package/dist/proof-hash.js +1 -1
  32. package/dist/proof-hash.js.map +1 -1
  33. package/dist/types.js +1 -1
  34. package/dist/types.js.map +1 -1
  35. package/dist/verifier.d.ts +29 -0
  36. package/dist/verifier.d.ts.map +1 -1
  37. package/dist/verifier.js +51 -39
  38. package/dist/verifier.js.map +1 -1
  39. package/package.json +13 -2
  40. package/src/__tests__/carrier.test.ts +184 -0
  41. package/src/__tests__/fixtures/carrier/ceiling2.anchor.json +67 -0
  42. package/src/__tests__/fixtures/carrier/ceiling2.witness.json +5 -0
  43. package/src/__tests__/fixtures/carrier/demo1.proof.json +47 -0
  44. package/src/__tests__/fixtures/carrier/demo1.txt +12 -0
  45. package/src/__tests__/fixtures/carrier/demo2.proof.json +47 -0
  46. package/src/__tests__/fixtures/carrier/demo2.txt +14 -0
  47. package/src/__tests__/fixtures/carrier/floor1.anchor.json +67 -0
  48. package/src/__tests__/fixtures/carrier/floor1.witness.json +5 -0
  49. package/src/__tests__/fixtures/carrier/floor2.anchor.json +67 -0
  50. package/src/__tests__/fixtures/carrier/floor2.witness.json +5 -0
  51. package/src/anchor.ts +1 -1
  52. package/src/canonical.ts +1 -1
  53. package/src/carrier-verify.ts +101 -0
  54. package/src/carrier.ts +478 -0
  55. package/src/fuse-member.ts +1 -1
  56. package/src/fuse-merkle.ts +1 -1
  57. package/src/fuse-verify.ts +1 -1
  58. package/src/fuse.ts +1 -1
  59. package/src/index.ts +18 -3
  60. package/src/proof-hash.ts +1 -1
  61. package/src/types.ts +1 -1
  62. package/src/verifier.ts +78 -27
package/src/carrier.ts ADDED
@@ -0,0 +1,478 @@
1
+ // Copyright (c) 2024-2026 Argento Computing Inc. Licensed under the MIT License. See LICENSE.
2
+
3
+ /**
4
+ * bitgraph-carrier/1 — the file that carries its own proof.
5
+ *
6
+ * A carrier is the committed bytes followed by one structural block holding
7
+ * the proof and the evidence needed to read its time window offline:
8
+ *
9
+ * [ committed bytes ............................ ] ← exactly what was hashed
10
+ * [ BGPROOF\x01 ][ u32 len ][ payload ][ u32 len ][ BGPROOF\x01 ]
11
+ *
12
+ * The committed digest covers the bytes BEFORE the block, never the block.
13
+ * The block is found from the END of the file only — last 8 bytes must be the
14
+ * magic, then the trailing length, then the leading magic and length must
15
+ * agree — never by scanning, so a carrier fused inside another carrier
16
+ * resolves deterministically to the outermost block, and no ordinary file can
17
+ * be misread as one by accident.
18
+ *
19
+ * THE ENVELOPE RULE. The carrier's own hash is committed nowhere and proves
20
+ * nothing. Anyone can repackage the same committed bytes with the same block
21
+ * and get a different outer hash; that is fine and expected. Never describe
22
+ * the carrier as recorded — the bytes inside it are.
23
+ *
24
+ * THE TIME WINDOW. The payload always carries the floor: the anchor the
25
+ * enclave signed into the slot (`commit.slotAnchor`, present since enclave
26
+ * v7) plus that block's header, so "placed no earlier than this block" checks
27
+ * offline by identity. The ceiling cannot exist when the carrier is written —
28
+ * the anchor that follows the commit has not landed yet — so it is either
29
+ * `present` (stamped in later from public data) or `unfetched`, stated in so
30
+ * many words. There is no third state, and absence of the field is a corrupt
31
+ * block, not a missing bound.
32
+ *
33
+ * ⚠️ TWO BYTE-IDENTICAL COPIES OF THIS FILE EXIST, on purpose:
34
+ * packages/verify/src/carrier.ts (canonical, published with the package)
35
+ * website/src/lib/carrier.ts (the site cannot import unpublished code)
36
+ * A parity test in the website suite asserts they match. Edit both or the
37
+ * test fails the build. The file is self-contained (only @noble imports) so
38
+ * the copies can be identical.
39
+ */
40
+
41
+ import { sha256 } from "@noble/hashes/sha256";
42
+ import { keccak_256 } from "@noble/hashes/sha3";
43
+
44
+ /* ── The block ──────────────────────────────────────────────────────────── */
45
+
46
+ /** "BGPROOF" + 0x01: seven ASCII bytes and a block version. */
47
+ export const CARRIER_MAGIC = Uint8Array.from([0x42, 0x47, 0x50, 0x52, 0x4f, 0x4f, 0x46, 0x01]);
48
+ /** magic + u32 + u32 + magic around the payload. */
49
+ export const CARRIER_OVERHEAD = 24;
50
+ /** A payload larger than this is refused as corrupt: a manifest of 100k members fits in a tenth of it. */
51
+ export const MAX_CARRIER_PAYLOAD = 8 * 1024 * 1024;
52
+
53
+ export const CARRIER_VERSION = "bitgraph-carrier/1";
54
+
55
+ /* ── The payload ────────────────────────────────────────────────────────── */
56
+
57
+ /** The raw Ethereum block header, so the block's identity and time check offline. */
58
+ export interface CarrierWitness {
59
+ headerRlpHex: string;
60
+ blockNumber: number;
61
+ blockHash: string;
62
+ }
63
+
64
+ /** A minimal view of a bitgraph/1 proof; the real shape is the verify package's. */
65
+ export type CarrierProof = Record<string, unknown>;
66
+
67
+ export interface CarrierFloor {
68
+ status: "present";
69
+ /** The anchor proof named by the carried proof's signed `commit.slotAnchor`. */
70
+ anchor: CarrierProof;
71
+ witness: CarrierWitness;
72
+ }
73
+
74
+ export type CarrierCeiling =
75
+ | {
76
+ status: "unfetched";
77
+ /** Set when a completion looked and nothing had landed yet, so "never looked" and "not yet" read apart. */
78
+ searched?: { asOfCounter: string; epochId: string };
79
+ }
80
+ | {
81
+ status: "present";
82
+ /** How the ceiling anchor is ordered after the proof. v1 stamps counter-order only. */
83
+ basis: "counter-order";
84
+ anchor: CarrierProof;
85
+ witness: CarrierWitness;
86
+ };
87
+
88
+ export interface CarrierPayload {
89
+ carrier: typeof CARRIER_VERSION;
90
+ /** The bitgraph/1 proof exactly as the commit returned it. */
91
+ proof: CarrierProof;
92
+ floor: CarrierFloor;
93
+ ceiling: CarrierCeiling;
94
+ }
95
+
96
+ export type CarrierParse =
97
+ /** No block: the bytes are not a carrier. Not a verdict about anything. */
98
+ | { kind: "none" }
99
+ /** A block is there but cannot be read. A corrupted block, not a forgery. */
100
+ | { kind: "corrupt"; reason: string }
101
+ | {
102
+ kind: "carrier";
103
+ /** The committed bytes: everything before the block. A view, not a copy. */
104
+ inner: Uint8Array;
105
+ payload: CarrierPayload;
106
+ /** Where the block starts, = inner.length. */
107
+ blockOffset: number;
108
+ };
109
+
110
+ /* ── Small helpers ──────────────────────────────────────────────────────── */
111
+
112
+ const te = new TextEncoder();
113
+
114
+ function u32be(n: number): Uint8Array {
115
+ return Uint8Array.from([(n >>> 24) & 0xff, (n >>> 16) & 0xff, (n >>> 8) & 0xff, n & 0xff]);
116
+ }
117
+ function readU32be(b: Uint8Array, at: number): number {
118
+ return ((b[at]! << 24) | (b[at + 1]! << 16) | (b[at + 2]! << 8) | b[at + 3]!) >>> 0;
119
+ }
120
+ function magicAt(b: Uint8Array, at: number): boolean {
121
+ if (at < 0 || at + 8 > b.length) return false;
122
+ for (let i = 0; i < 8; i++) if (b[at + i] !== CARRIER_MAGIC[i]!) return false;
123
+ return true;
124
+ }
125
+ function record(v: unknown): Record<string, unknown> | null {
126
+ return typeof v === "object" && v !== null && !Array.isArray(v) ? (v as Record<string, unknown>) : null;
127
+ }
128
+ /** base64 (either alphabet, padded or not) → bytes, or null. */
129
+ export function b64ToBytes(s: string): Uint8Array | null {
130
+ if (typeof s !== "string" || !/^[A-Za-z0-9+/_-]*={0,2}$/.test(s)) return null;
131
+ const std = s.replace(/-/g, "+").replace(/_/g, "/");
132
+ const pad = std.length % 4 === 0 ? std : std + "=".repeat(4 - (std.length % 4));
133
+ try {
134
+ if (typeof atob === "function") {
135
+ const bin = atob(pad);
136
+ const out = new Uint8Array(bin.length);
137
+ for (let i = 0; i < bin.length; i++) out[i] = bin.charCodeAt(i);
138
+ return out;
139
+ }
140
+ return new Uint8Array(Buffer.from(pad, "base64"));
141
+ } catch {
142
+ return null;
143
+ }
144
+ }
145
+ export function bytesToB64(b: Uint8Array): string {
146
+ if (typeof btoa === "function") {
147
+ let s = "";
148
+ for (let i = 0; i < b.length; i++) s += String.fromCharCode(b[i]!);
149
+ return btoa(s);
150
+ }
151
+ return Buffer.from(b).toString("base64");
152
+ }
153
+ /** Digest equality across the two base64 alphabets. */
154
+ export function sameDigest(a: unknown, b: unknown): boolean {
155
+ if (typeof a !== "string" || typeof b !== "string") return false;
156
+ const na = b64ToBytes(a), nb = b64ToBytes(b);
157
+ if (na === null || nb === null || na.length !== nb.length) return false;
158
+ let diff = 0;
159
+ for (let i = 0; i < na.length; i++) diff |= na[i]! ^ nb[i]!;
160
+ return diff === 0;
161
+ }
162
+
163
+ /* ── Build, parse, complete ─────────────────────────────────────────────── */
164
+
165
+ export function encodeCarrierBlock(payload: CarrierPayload): Uint8Array {
166
+ const body = te.encode(JSON.stringify(payload));
167
+ if (body.length > MAX_CARRIER_PAYLOAD) throw new Error(`carrier payload too large: ${body.length} bytes`);
168
+ const out = new Uint8Array(body.length + CARRIER_OVERHEAD);
169
+ out.set(CARRIER_MAGIC, 0);
170
+ out.set(u32be(body.length), 8);
171
+ out.set(body, 12);
172
+ out.set(u32be(body.length), 12 + body.length);
173
+ out.set(CARRIER_MAGIC, 16 + body.length);
174
+ return out;
175
+ }
176
+
177
+ /** The committed bytes followed by their block. The inner bytes are copied, never altered. */
178
+ export function buildCarrier(inner: Uint8Array, payload: CarrierPayload): Uint8Array {
179
+ const block = encodeCarrierBlock(payload);
180
+ const out = new Uint8Array(inner.length + block.length);
181
+ out.set(inner, 0);
182
+ out.set(block, inner.length);
183
+ return out;
184
+ }
185
+
186
+ /** Structural payload check: is this object a readable bitgraph-carrier/1 payload? */
187
+ function payloadShapeError(p: unknown): string | null {
188
+ const o = record(p);
189
+ if (o === null) return "payload is not an object";
190
+ if (o["carrier"] !== CARRIER_VERSION) return `unknown carrier version ${JSON.stringify(o["carrier"])}`;
191
+ if (record(o["proof"]) === null) return "payload carries no proof object";
192
+ const floor = record(o["floor"]);
193
+ if (floor === null || floor["status"] !== "present") return "floor must be present: a floorless carrier is not a not-before";
194
+ if (record(floor["anchor"]) === null) return "floor carries no anchor proof";
195
+ const fw = record(floor["witness"]);
196
+ if (fw === null || typeof fw["headerRlpHex"] !== "string" || typeof fw["blockNumber"] !== "number" || typeof fw["blockHash"] !== "string") {
197
+ return "floor carries no readable block-header witness";
198
+ }
199
+ const ceil = record(o["ceiling"]);
200
+ if (ceil === null) return "ceiling is missing: absence must be a stated fact, not a missing key";
201
+ if (ceil["status"] === "unfetched") return null;
202
+ if (ceil["status"] !== "present") return `ceiling status must be "unfetched" or "present", got ${JSON.stringify(ceil["status"])}`;
203
+ if (ceil["basis"] !== "counter-order") return `unknown ceiling basis ${JSON.stringify(ceil["basis"])}`;
204
+ if (record(ceil["anchor"]) === null) return "ceiling carries no anchor proof";
205
+ const cw = record(ceil["witness"]);
206
+ if (cw === null || typeof cw["headerRlpHex"] !== "string" || typeof cw["blockNumber"] !== "number" || typeof cw["blockHash"] !== "string") {
207
+ return "ceiling carries no readable block-header witness";
208
+ }
209
+ return null;
210
+ }
211
+
212
+ export function parseCarrier(bytes: Uint8Array): CarrierParse {
213
+ if (bytes.length < CARRIER_OVERHEAD + 2) return { kind: "none" };
214
+ if (!magicAt(bytes, bytes.length - 8)) return { kind: "none" };
215
+ const len = readU32be(bytes, bytes.length - 12);
216
+ if (len > MAX_CARRIER_PAYLOAD) return { kind: "corrupt", reason: `declared payload length ${len} exceeds the cap` };
217
+ const start = bytes.length - CARRIER_OVERHEAD - len;
218
+ if (start < 0) return { kind: "corrupt", reason: "declared payload length runs past the start of the file" };
219
+ if (!magicAt(bytes, start)) return { kind: "corrupt", reason: "leading magic missing where the trailing length points" };
220
+ if (readU32be(bytes, start + 8) !== len) return { kind: "corrupt", reason: "leading and trailing payload lengths disagree" };
221
+ let payload: unknown;
222
+ try {
223
+ payload = JSON.parse(new TextDecoder("utf-8", { fatal: true }).decode(bytes.subarray(start + 12, start + 12 + len)));
224
+ } catch {
225
+ return { kind: "corrupt", reason: "payload is not valid UTF-8 JSON" };
226
+ }
227
+ const shape = payloadShapeError(payload);
228
+ if (shape !== null) return { kind: "corrupt", reason: shape };
229
+ return { kind: "carrier", inner: bytes.subarray(0, start), payload: payload as CarrierPayload, blockOffset: start };
230
+ }
231
+
232
+ /**
233
+ * Stamp a verified ceiling into a carrier, or record that a completion looked
234
+ * and found nothing yet. Pure: callers verify the material BEFORE stamping.
235
+ * Never overwrites: a present ceiling that differs from the offered one is
236
+ * reported, since a conflicting embedded ceiling is evidence worth keeping.
237
+ */
238
+ export function completeCarrier(
239
+ bytes: Uint8Array,
240
+ ceiling: { anchor: CarrierProof; witness: CarrierWitness } | null,
241
+ searched?: { asOfCounter: string; epochId: string },
242
+ ): { changed: boolean; bytes: Uint8Array; error?: string } {
243
+ const p = parseCarrier(bytes);
244
+ if (p.kind !== "carrier") return { changed: false, bytes, error: p.kind === "none" ? "not a carrier" : `corrupt block: ${p.reason}` };
245
+ const cur = p.payload.ceiling;
246
+ if (cur.status === "present") {
247
+ if (ceiling === null) return { changed: false, bytes };
248
+ const a = record(record(cur.anchor)?.["commit"]);
249
+ const b = record(record(ceiling.anchor)?.["commit"]);
250
+ const same = a !== null && b !== null && a["counter"] === b["counter"] && cur.witness.blockHash.toLowerCase() === ceiling.witness.blockHash.toLowerCase();
251
+ return same ? { changed: false, bytes } : { changed: false, bytes, error: "carrier already holds a different ceiling; refusing to overwrite it" };
252
+ }
253
+ const payload: CarrierPayload = {
254
+ ...p.payload,
255
+ ceiling: ceiling === null
256
+ ? { status: "unfetched", ...(searched ? { searched } : {}) }
257
+ : { status: "present", basis: "counter-order", anchor: ceiling.anchor, witness: ceiling.witness },
258
+ };
259
+ if (ceiling === null && !searched) return { changed: false, bytes };
260
+ return { changed: true, bytes: buildCarrier(p.inner, payload) };
261
+ }
262
+
263
+ /* ── Consistency checks (no network, no signatures — bindings only) ─────── */
264
+
265
+ /** The identity an authenticated anchor proof claims: enclave-signed since v7. */
266
+ export function anchorIdentity(anchor: unknown): { counter: string; epochId: string; chainId: string; publicKeyB64: string; blockNumber: number; blockHash: string } | null {
267
+ const p = record(anchor);
268
+ const commit = record(p?.["commit"]);
269
+ const signer = record(p?.["signer"]);
270
+ const a = record(commit?.["anchor"]);
271
+ if (!p || !commit || !signer || !a) return null;
272
+ const counter = commit["counter"], epochId = commit["epochId"], chainId = commit["chainId"];
273
+ const publicKeyB64 = signer["publicKeyB64"], blockNumber = a["blockNumber"], blockHash = a["blockHash"];
274
+ if (typeof counter !== "string" || typeof epochId !== "string" || typeof chainId !== "string") return null;
275
+ if (typeof publicKeyB64 !== "string" || typeof blockNumber !== "number" || typeof blockHash !== "string") return null;
276
+ return { counter, epochId, chainId, publicKeyB64, blockNumber, blockHash };
277
+ }
278
+
279
+ function partitionOf(proof: unknown): { epochId: string; chainId: string; publicKeyB64: string; counter: string; slotAnchor: { counter: string; blockNumber: number; blockHash: string } | null } | null {
280
+ const p = record(proof);
281
+ const commit = record(p?.["commit"]);
282
+ const signer = record(p?.["signer"]);
283
+ if (!p || !commit || !signer) return null;
284
+ const epochId = commit["epochId"], chainId = commit["chainId"], counter = commit["counter"], publicKeyB64 = signer["publicKeyB64"];
285
+ if (typeof epochId !== "string" || typeof chainId !== "string" || typeof counter !== "string" || typeof publicKeyB64 !== "string") return null;
286
+ const sa = record(commit["slotAnchor"]);
287
+ const slotAnchor = sa && typeof sa["counter"] === "string" && typeof sa["blockNumber"] === "number" && typeof sa["blockHash"] === "string"
288
+ ? { counter: sa["counter"], blockNumber: sa["blockNumber"], blockHash: sa["blockHash"] }
289
+ : null;
290
+ return { epochId, chainId, publicKeyB64, counter, slotAnchor };
291
+ }
292
+
293
+ /**
294
+ * The floor must be the anchor the enclave signed into the slot — matched by
295
+ * identity against `commit.slotAnchor`, never merely "some anchor below the
296
+ * slot". An older anchor would read as an earlier not-before than was signed.
297
+ */
298
+ export function checkFloorBinding(proof: unknown, floor: CarrierFloor): string[] {
299
+ const errs: string[] = [];
300
+ const part = partitionOf(proof);
301
+ if (part === null) return ["proof is missing commit or signer fields"];
302
+ if (part.slotAnchor === null) return ["proof carries no signed slotAnchor (pre-v7): it cannot travel as a carrier floor"];
303
+ const id = anchorIdentity(floor.anchor);
304
+ if (id === null) return ["floor anchor is not an authenticated anchor (no signed commit.anchor)"];
305
+ if (id.chainId !== part.chainId || id.epochId !== part.epochId || id.publicKeyB64 !== part.publicKeyB64) {
306
+ errs.push("floor anchor is not from the proof's own chain, epoch and key");
307
+ }
308
+ if (id.counter !== part.slotAnchor.counter) errs.push(`floor anchor counter ${id.counter} is not the signed slotAnchor counter ${part.slotAnchor.counter}`);
309
+ if (id.blockNumber !== part.slotAnchor.blockNumber) errs.push(`floor anchor block ${id.blockNumber} is not the signed slotAnchor block ${part.slotAnchor.blockNumber}`);
310
+ if (id.blockHash.toLowerCase() !== part.slotAnchor.blockHash.toLowerCase()) errs.push("floor anchor block hash differs from the signed slotAnchor block hash");
311
+ if (floor.witness.blockNumber !== id.blockNumber) errs.push("floor witness is for a different block number than the floor anchor");
312
+ if (floor.witness.blockHash.toLowerCase() !== id.blockHash.toLowerCase()) errs.push("floor witness is for a different block hash than the floor anchor");
313
+ return errs;
314
+ }
315
+
316
+ /** A present ceiling: same partition, strictly after the commit, counter-order. */
317
+ export function checkCeilingBinding(proof: unknown, ceiling: Extract<CarrierCeiling, { status: "present" }>): string[] {
318
+ const errs: string[] = [];
319
+ const part = partitionOf(proof);
320
+ if (part === null) return ["proof is missing commit or signer fields"];
321
+ const id = anchorIdentity(ceiling.anchor);
322
+ if (id === null) return ["ceiling anchor is not an authenticated anchor (no signed commit.anchor)"];
323
+ if (id.chainId !== part.chainId || id.epochId !== part.epochId || id.publicKeyB64 !== part.publicKeyB64) {
324
+ errs.push("ceiling anchor is not from the proof's own chain, epoch and key: counter-order says nothing across epochs");
325
+ }
326
+ let after = false;
327
+ try {
328
+ after = BigInt(id.counter) > BigInt(part.counter);
329
+ } catch {
330
+ errs.push("ceiling or commit counter is not a whole number");
331
+ }
332
+ if (!after) errs.push(`ceiling anchor counter ${id.counter} does not follow the commit counter ${part.counter}`);
333
+ if (ceiling.witness.blockNumber !== id.blockNumber) errs.push("ceiling witness is for a different block number than the ceiling anchor");
334
+ if (ceiling.witness.blockHash.toLowerCase() !== id.blockHash.toLowerCase()) errs.push("ceiling witness is for a different block hash than the ceiling anchor");
335
+ return errs;
336
+ }
337
+
338
+ /**
339
+ * An anchor commits SHA-256 of its signed message, the block hash string.
340
+ * This ties the anchor's artifact digest to the block it claims, offline.
341
+ * Also returns the exact bytes the anchor's own digest covers, for verify().
342
+ */
343
+ export function anchorMessageBytes(anchor: unknown): { bytes: Uint8Array; error: string | null } {
344
+ const p = record(anchor);
345
+ const attr = record(p?.["attribution"]);
346
+ const id = anchorIdentity(anchor);
347
+ const msg = typeof attr?.["message"] === "string" ? (attr["message"] as string) : id ? id.blockHash.toLowerCase() : null;
348
+ if (msg === null) return { bytes: new Uint8Array(), error: "anchor carries no signed message and no block hash" };
349
+ const bytes = te.encode(msg);
350
+ const artifact = record(p?.["artifact"]);
351
+ const digest = artifact?.["digestB64"];
352
+ if (typeof digest !== "string" || !sameDigest(bytesToB64(sha256(bytes)), digest)) {
353
+ return { bytes, error: "anchor's artifact digest is not the SHA-256 of its signed block-hash message" };
354
+ }
355
+ if (id && msg.toLowerCase() !== id.blockHash.toLowerCase()) return { bytes, error: "anchor's signed message is not its claimed block hash" };
356
+ return { bytes, error: null };
357
+ }
358
+
359
+ /* ── The block-header witness (keccak + minimal RLP) ────────────────────── */
360
+ /* The same procedure bitgraph-audit's witness.ts runs (docs/BUNDLE-FORMAT.md
361
+ §10.3), reduced to what a carrier needs: recompute the hash, cross-check
362
+ number, read the timestamp. Ethereum headers RLP-encode as one list whose
363
+ items 8 and 11 are the block number and timestamp. */
364
+
365
+ function hexToBytes(hex: string): Uint8Array | null {
366
+ const clean = hex.startsWith("0x") || hex.startsWith("0X") ? hex.slice(2) : hex;
367
+ if (clean.length % 2 !== 0 || !/^[0-9a-fA-F]*$/.test(clean)) return null;
368
+ const out = new Uint8Array(clean.length / 2);
369
+ for (let i = 0; i < out.length; i++) out[i] = parseInt(clean.slice(i * 2, i * 2 + 2), 16);
370
+ return out;
371
+ }
372
+
373
+ /** Top-level items of one RLP list, as raw byte payloads. Null when malformed. */
374
+ export function decodeRlpTop(bytes: Uint8Array): Uint8Array[] | null {
375
+ if (bytes.length === 0) return null;
376
+ const b0 = bytes[0]!;
377
+ let at: number, end: number;
378
+ if (b0 >= 0xf8) {
379
+ const ll = b0 - 0xf7;
380
+ if (1 + ll > bytes.length) return null;
381
+ let len = 0;
382
+ for (let i = 0; i < ll; i++) len = len * 256 + bytes[1 + i]!;
383
+ at = 1 + ll;
384
+ end = at + len;
385
+ } else if (b0 >= 0xc0) {
386
+ at = 1;
387
+ end = at + (b0 - 0xc0);
388
+ } else return null;
389
+ if (end !== bytes.length) return null;
390
+ const items: Uint8Array[] = [];
391
+ while (at < end) {
392
+ const h = bytes[at]!;
393
+ if (h < 0x80) {
394
+ items.push(bytes.subarray(at, at + 1));
395
+ at += 1;
396
+ } else if (h <= 0xb7) {
397
+ const len = h - 0x80;
398
+ if (at + 1 + len > end) return null;
399
+ items.push(bytes.subarray(at + 1, at + 1 + len));
400
+ at += 1 + len;
401
+ } else if (h <= 0xbf) {
402
+ const ll = h - 0xb7;
403
+ if (at + 1 + ll > end) return null;
404
+ let len = 0;
405
+ for (let i = 0; i < ll; i++) len = len * 256 + bytes[at + 1 + i]!;
406
+ if (at + 1 + ll + len > end) return null;
407
+ items.push(bytes.subarray(at + 1 + ll, at + 1 + ll + len));
408
+ at += 1 + ll + len;
409
+ } else {
410
+ // A nested list inside a header's top level (withdrawals etc. stay hashed): skip it whole.
411
+ let len: number, hl: number;
412
+ if (h <= 0xf7) {
413
+ len = h - 0xc0;
414
+ hl = 1;
415
+ } else {
416
+ const ll = h - 0xf7;
417
+ len = 0;
418
+ for (let i = 0; i < ll; i++) len = len * 256 + bytes[at + 1 + i]!;
419
+ hl = 1 + ll;
420
+ }
421
+ if (at + hl + len > end) return null;
422
+ items.push(bytes.subarray(at + hl, at + hl + len));
423
+ at += hl + len;
424
+ }
425
+ }
426
+ return items;
427
+ }
428
+
429
+ function rlpNumber(item: Uint8Array): number | null {
430
+ if (item.length > 6) return null;
431
+ let n = 0;
432
+ for (const b of item) n = n * 256 + b;
433
+ return n;
434
+ }
435
+
436
+ export interface WitnessCheck {
437
+ ok: boolean;
438
+ error: string | null;
439
+ /** The block's own timestamp, seconds since the epoch, read from the verified header. */
440
+ timestamp: number | null;
441
+ }
442
+
443
+ /** keccak256(header) must reproduce the block hash; the header's number must match. Offline. */
444
+ export function verifyWitnessHeader(w: CarrierWitness): WitnessCheck {
445
+ const raw = hexToBytes(w.headerRlpHex);
446
+ if (raw === null) return { ok: false, error: "witness header is not hex", timestamp: null };
447
+ const hash = "0x" + Array.from(keccak_256(raw), (b) => b.toString(16).padStart(2, "0")).join("");
448
+ if (hash !== w.blockHash.toLowerCase()) return { ok: false, error: "keccak-256 of the witness header does not reproduce the block hash", timestamp: null };
449
+ const items = decodeRlpTop(raw);
450
+ if (items === null || items.length < 12) return { ok: false, error: "witness header is not a well-formed block-header list", timestamp: null };
451
+ const num = rlpNumber(items[8]!);
452
+ if (num !== w.blockNumber) return { ok: false, error: `witness header is block ${num ?? "?"}, not ${w.blockNumber}`, timestamp: null };
453
+ return { ok: true, error: null, timestamp: rlpNumber(items[11]!) };
454
+ }
455
+
456
+ /* ── The window a reader states ─────────────────────────────────────────── */
457
+
458
+ export interface CarrierBounds {
459
+ notBefore: { blockNumber: number; blockHash: string; timestamp: number | null };
460
+ /** null means NOT FETCHED — say so in those words — never "none exists". */
461
+ notAfter: { blockNumber: number; blockHash: string; timestamp: number | null } | null;
462
+ }
463
+
464
+ export function carrierBounds(payload: CarrierPayload): CarrierBounds {
465
+ const f = payload.floor.witness;
466
+ const floorTs = verifyWitnessHeader(f);
467
+ const notBefore = { blockNumber: f.blockNumber, blockHash: f.blockHash.toLowerCase(), timestamp: floorTs.ok ? floorTs.timestamp : null };
468
+ if (payload.ceiling.status !== "present") return { notBefore, notAfter: null };
469
+ const c = payload.ceiling.witness;
470
+ const ceilTs = verifyWitnessHeader(c);
471
+ return { notBefore, notAfter: { blockNumber: c.blockNumber, blockHash: c.blockHash.toLowerCase(), timestamp: ceilTs.ok ? ceilTs.timestamp : null } };
472
+ }
473
+
474
+ /** SHA-256 of the committed bytes, compared to the carried proof's artifact digest. */
475
+ export function innerDigestMatches(inner: Uint8Array, payload: CarrierPayload): boolean {
476
+ const artifact = record(payload.proof["artifact"]);
477
+ return sameDigest(bytesToB64(sha256(inner)), artifact?.["digestB64"] as string);
478
+ }
@@ -1,4 +1,4 @@
1
- // Copyright (c) 2024-2026 Mike Argento. Licensed under the MIT License. See LICENSE.
1
+ // Copyright (c) 2024-2026 Argento Computing Inc. Licensed under the MIT License. See LICENSE.
2
2
 
3
3
  /**
4
4
  * BitGraph Sets, phase 1: verifying ONE MEMBER of a set.
@@ -1,4 +1,4 @@
1
- // Copyright (c) 2024-2026 Mike Argento. Licensed under the MIT License. See LICENSE.
1
+ // Copyright (c) 2024-2026 Argento Computing Inc. Licensed under the MIT License. See LICENSE.
2
2
 
3
3
  /**
4
4
  * The Merkle tree a set/2 commits to: RFC 6962 (Certificate Transparency)
@@ -1,4 +1,4 @@
1
- // Copyright (c) 2024-2026 Mike Argento. Licensed under the MIT License. See LICENSE.
1
+ // Copyright (c) 2024-2026 Argento Computing Inc. Licensed under the MIT License. See LICENSE.
2
2
 
3
3
  /**
4
4
  * BitGraph Fuse verification (spec 10): two paths, one comparison.
package/src/fuse.ts CHANGED
@@ -1,4 +1,4 @@
1
- // Copyright (c) 2024-2026 Mike Argento. Licensed under the MIT License. See LICENSE.
1
+ // Copyright (c) 2024-2026 Argento Computing Inc. Licensed under the MIT License. See LICENSE.
2
2
 
3
3
  /**
4
4
  * BitGraph Fuse, profile `bitgraph-fuse/1`: construction and parsing.
package/src/index.ts CHANGED
@@ -1,4 +1,4 @@
1
- // Copyright (c) 2024-2026 Mike Argento. Licensed under the MIT License. See LICENSE.
1
+ // Copyright (c) 2024-2026 Argento Computing Inc. Licensed under the MIT License. See LICENSE.
2
2
 
3
3
  /**
4
4
  * @mikeargento/bitgraph-verify
@@ -32,8 +32,8 @@ export type {
32
32
  AgencyEnvelope,
33
33
  } from "./types.js";
34
34
 
35
- export { verify, verifyProofIntegrity, resetEpochLinkState } from "./verifier.js";
36
- export type { VerifyResult, ProofIntegrityResult } from "./verifier.js";
35
+ export { verify, verifyProofIntegrity, resetEpochLinkState, createVerificationContext } from "./verifier.js";
36
+ export type { VerifyResult, ProofIntegrityResult, VerificationContext } from "./verifier.js";
37
37
 
38
38
  export { computeProofHash, computeChainHash, buildSignedBody, computeSignedBodyHash } from "./proof-hash.js";
39
39
 
@@ -87,3 +87,18 @@ export { merkleLeafHash, merkleNodeHash, merkleRoot, merklePath, merkleRootFromP
87
87
  // MADE with the commitment inside it, so look for it). verifyFuse handles both;
88
88
  // these were briefly two profiles, which was one idea wearing two names.
89
89
  export { ENCODING_BASE64URL, isCarryEncoding, findCommitment, inlineAttribution } from "./fuse.js";
90
+
91
+ /* bitgraph-carrier/1: the file that carries its own proof (2026-09-24). */
92
+ export {
93
+ CARRIER_MAGIC, CARRIER_OVERHEAD, MAX_CARRIER_PAYLOAD, CARRIER_VERSION,
94
+ encodeCarrierBlock, buildCarrier, parseCarrier, completeCarrier,
95
+ anchorIdentity, checkFloorBinding, checkCeilingBinding, anchorMessageBytes,
96
+ decodeRlpTop, verifyWitnessHeader, carrierBounds, innerDigestMatches,
97
+ sameDigest, b64ToBytes, bytesToB64,
98
+ } from "./carrier.js";
99
+ export type {
100
+ CarrierWitness, CarrierProof, CarrierFloor, CarrierCeiling, CarrierPayload,
101
+ CarrierParse, WitnessCheck, CarrierBounds,
102
+ } from "./carrier.js";
103
+ export { verifyCarrier } from "./carrier-verify.js";
104
+ export type { CarrierVerifyResult } from "./carrier-verify.js";
package/src/proof-hash.ts CHANGED
@@ -1,4 +1,4 @@
1
- // Copyright (c) 2024-2026 Mike Argento. Licensed under the MIT License. See LICENSE.
1
+ // Copyright (c) 2024-2026 Argento Computing Inc. Licensed under the MIT License. See LICENSE.
2
2
 
3
3
  /**
4
4
  * bitgraph-core proof hash
package/src/types.ts CHANGED
@@ -1,4 +1,4 @@
1
- // Copyright (c) 2024-2026 Mike Argento. Licensed under the MIT License. See LICENSE.
1
+ // Copyright (c) 2024-2026 Argento Computing Inc. Licensed under the MIT License. See LICENSE.
2
2
 
3
3
  /**
4
4
  * bitgraph-core types