@mikeargento/bitgraph-verify 1.15.2 → 1.17.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 (66) hide show
  1. package/README.md +24 -3
  2. package/dist/__tests__/carrier2.test.js +62 -3
  3. package/dist/__tests__/carrier2.test.js.map +1 -1
  4. package/dist/__tests__/ceiling.test.js +11 -4
  5. package/dist/__tests__/ceiling.test.js.map +1 -1
  6. package/dist/carrier-assemble.d.ts +12 -2
  7. package/dist/carrier-assemble.d.ts.map +1 -1
  8. package/dist/carrier-assemble.js +14 -3
  9. package/dist/carrier-assemble.js.map +1 -1
  10. package/dist/carrier-verify.d.ts +15 -4
  11. package/dist/carrier-verify.d.ts.map +1 -1
  12. package/dist/carrier-verify.js +201 -59
  13. package/dist/carrier-verify.js.map +1 -1
  14. package/dist/carrier.d.ts +48 -13
  15. package/dist/carrier.d.ts.map +1 -1
  16. package/dist/carrier.js +74 -16
  17. package/dist/carrier.js.map +1 -1
  18. package/dist/ceiling.d.ts +115 -6
  19. package/dist/ceiling.d.ts.map +1 -1
  20. package/dist/ceiling.js +189 -23
  21. package/dist/ceiling.js.map +1 -1
  22. package/dist/export.d.ts +122 -0
  23. package/dist/export.d.ts.map +1 -0
  24. package/dist/export.js +510 -0
  25. package/dist/export.js.map +1 -0
  26. package/dist/fuse.d.ts +45 -11
  27. package/dist/fuse.d.ts.map +1 -1
  28. package/dist/fuse.js +100 -18
  29. package/dist/fuse.js.map +1 -1
  30. package/dist/index.d.ts +20 -2
  31. package/dist/index.d.ts.map +1 -1
  32. package/dist/index.js +18 -1
  33. package/dist/index.js.map +1 -1
  34. package/dist/measurements.d.ts +30 -0
  35. package/dist/measurements.d.ts.map +1 -0
  36. package/dist/measurements.js +22 -0
  37. package/dist/measurements.js.map +1 -0
  38. package/dist/output-root.d.ts +88 -0
  39. package/dist/output-root.d.ts.map +1 -0
  40. package/dist/output-root.js +260 -0
  41. package/dist/output-root.js.map +1 -0
  42. package/dist/stream.d.ts +46 -0
  43. package/dist/stream.d.ts.map +1 -0
  44. package/dist/stream.js +290 -0
  45. package/dist/stream.js.map +1 -0
  46. package/dist/tree.d.ts +167 -0
  47. package/dist/tree.d.ts.map +1 -0
  48. package/dist/tree.js +485 -0
  49. package/dist/tree.js.map +1 -0
  50. package/dist/types.d.ts +21 -0
  51. package/dist/types.d.ts.map +1 -1
  52. package/package.json +1 -1
  53. package/src/__tests__/carrier2.test.ts +65 -3
  54. package/src/__tests__/ceiling.test.ts +12 -4
  55. package/src/carrier-assemble.ts +21 -3
  56. package/src/carrier-verify.ts +204 -61
  57. package/src/carrier.ts +95 -27
  58. package/src/ceiling.ts +217 -28
  59. package/src/export.ts +592 -0
  60. package/src/fuse.ts +126 -23
  61. package/src/index.ts +47 -1
  62. package/src/measurements.ts +49 -0
  63. package/src/output-root.ts +301 -0
  64. package/src/stream.ts +291 -0
  65. package/src/tree.ts +580 -0
  66. package/src/types.ts +22 -0
package/src/carrier.ts CHANGED
@@ -41,6 +41,17 @@
41
41
  * never reads as authority. A reader that knows only v1 reports an unknown
42
42
  * version (UNDETERMINED), never a verdict.
43
43
  *
44
+ * bitgraph-carrier/3 (2026-10-06, enclave v10) is /2 for a proof whose floor
45
+ * is a Base block (`commit.slotFloor`). The floor is that block's header
46
+ * itself, `{ status: "present", basis: "base-header", header }`, checked
47
+ * against the signed floor (hash, number and time, on Base mainnet's
48
+ * schedule); there is no anchor proof under it. There is no ceiling in
49
+ * POSITION either: anchors ended with the cutover, and the order among
50
+ * BitGraphs is the chain of proof hashes, so the block states
51
+ * `{ status: "none", basis: "hash-chain" }`, a fact, not a pending part.
52
+ * Everything else (ceiling in time, settlement, pins, attestation witness) is
53
+ * /2's. Ethereum-floor proofs keep travelling as /1 and /2.
54
+ *
44
55
  * ⚠️ TWO BYTE-IDENTICAL COPIES OF THIS FILE EXIST, on purpose:
45
56
  * packages/verify/src/carrier.ts (canonical, published with the package)
46
57
  * website/src/lib/carrier.ts (the site cannot import unpublished code)
@@ -63,7 +74,8 @@ export const MAX_CARRIER_PAYLOAD = 8 * 1024 * 1024;
63
74
 
64
75
  export const CARRIER_VERSION = "bitgraph-carrier/1";
65
76
  export const CARRIER_VERSION_2 = "bitgraph-carrier/2";
66
- export type CarrierVersion = typeof CARRIER_VERSION | typeof CARRIER_VERSION_2;
77
+ export const CARRIER_VERSION_3 = "bitgraph-carrier/3";
78
+ export type CarrierVersion = typeof CARRIER_VERSION | typeof CARRIER_VERSION_2 | typeof CARRIER_VERSION_3;
67
79
  /** The sidecar and pointer formats a v2 block carries; their shapes are checked by their own modules. */
68
80
  export const CEILING_SIDECAR_VERSION = "bitgraph-ceiling/1";
69
81
  export const SETTLEMENT_POINTER_VERSION = "bitgraph-settlement/1";
@@ -83,12 +95,24 @@ export type CarrierProof = Record<string, unknown>;
83
95
 
84
96
  export interface CarrierFloor {
85
97
  status: "present";
98
+ basis?: never;
86
99
  /** The anchor proof named by the carried proof's signed `commit.slotAnchor`. */
87
100
  anchor: CarrierProof;
88
101
  witness: CarrierWitness;
89
102
  }
90
103
 
104
+ /** v3: the Base block the carried proof signs as `commit.slotFloor`, as its raw header (0x hex RLP). No anchor proof stands under it. */
105
+ export interface CarrierBaseFloor {
106
+ status: "present";
107
+ basis: "base-header";
108
+ header: string;
109
+ anchor?: never;
110
+ witness?: never;
111
+ }
112
+
91
113
  export type CarrierCeiling =
114
+ /** v3: no ceiling in position exists; order among BitGraphs is the chain of proof hashes. */
115
+ | { status: "none"; basis: "hash-chain" }
92
116
  | {
93
117
  status: "unfetched";
94
118
  /** Set when a completion looked and nothing had landed yet, so "never looked" and "not yet" read apart. */
@@ -124,16 +148,17 @@ export interface CarrierPayload {
124
148
  carrier: CarrierVersion;
125
149
  /** The bitgraph/1 proof exactly as the commit returned it. */
126
150
  proof: CarrierProof;
127
- floor: CarrierFloor;
128
- /** The ceiling in position: the next anchor in the chain. Every proof ever made has one. */
151
+ /** v1 and v2: the Ethereum anchor proof and its header. v3: the Base header. */
152
+ floor: CarrierFloor | CarrierBaseFloor;
153
+ /** The ceiling in position: the next anchor in the chain (v1, v2), or on v3 the statement that there is none. */
129
154
  ceiling: CarrierCeiling;
130
- /** v2 only. Required on a v2 block, so absence reads as a corrupt block, never as "no ceiling". */
155
+ /** v2 and v3. Required there, so absence reads as a corrupt block, never as "no ceiling". */
131
156
  ceilingInTime?: CarrierCeilingInTime;
132
- /** v2 only, optional. */
157
+ /** v2 and v3, optional. */
133
158
  settlement?: CarrierSettlement;
134
- /** v2 only, optional. */
159
+ /** v2 and v3, optional. */
135
160
  pins?: CarrierPins;
136
- /** v2 only, optional: the proof's attestation as aws-nitro-witness/1 evidence. */
161
+ /** v2 and v3, optional: the proof's attestation as aws-nitro-witness/1 evidence. */
137
162
  attestation?: Record<string, unknown>;
138
163
  }
139
164
 
@@ -231,19 +256,29 @@ export function buildCarrier(inner: Uint8Array, payload: CarrierPayload): Uint8A
231
256
  function payloadShapeError(p: unknown): string | null {
232
257
  const o = record(p);
233
258
  if (o === null) return "payload is not an object";
234
- const v2 = o["carrier"] === CARRIER_VERSION_2;
259
+ const v3 = o["carrier"] === CARRIER_VERSION_3;
260
+ const v2 = o["carrier"] === CARRIER_VERSION_2 || v3;
235
261
  if (o["carrier"] !== CARRIER_VERSION && !v2) return `unknown carrier version ${JSON.stringify(o["carrier"])}`;
236
262
  if (record(o["proof"]) === null) return "payload carries no proof object";
237
263
  const floor = record(o["floor"]);
238
264
  if (floor === null || floor["status"] !== "present") return "floor must be present: a floorless carrier is not a not-before";
239
- if (record(floor["anchor"]) === null) return "floor carries no anchor proof";
240
- const fw = record(floor["witness"]);
241
- if (fw === null || typeof fw["headerRlpHex"] !== "string" || typeof fw["blockNumber"] !== "number" || typeof fw["blockHash"] !== "string") {
242
- return "floor carries no readable block-header witness";
243
- }
244
265
  const ceil = record(o["ceiling"]);
245
266
  if (ceil === null) return "ceiling is missing: absence must be a stated fact, not a missing key";
246
- if (ceil["status"] !== "unfetched") {
267
+ if (v3) {
268
+ // v3: the floor is a Base header, and no ceiling in position exists.
269
+ if (floor["basis"] !== "base-header") return `a ${CARRIER_VERSION_3} floor must be a Base header (basis "base-header"), got ${JSON.stringify(floor["basis"])}`;
270
+ if (typeof floor["header"] !== "string" || baseHeaderFields(floor["header"]) === null) return "floor carries no readable Base block header";
271
+ if (floor["anchor"] !== undefined || floor["witness"] !== undefined) return `a ${CARRIER_VERSION_3} floor carries no anchor proof`;
272
+ if (ceil["status"] !== "none" || ceil["basis"] !== "hash-chain") return `a ${CARRIER_VERSION_3} block states no ceiling in position ({ status: "none", basis: "hash-chain" })`;
273
+ } else {
274
+ if (floor["basis"] !== undefined) return `unknown floor basis ${JSON.stringify(floor["basis"])} on ${String(o["carrier"])}`;
275
+ if (record(floor["anchor"]) === null) return "floor carries no anchor proof";
276
+ const fw = record(floor["witness"]);
277
+ if (fw === null || typeof fw["headerRlpHex"] !== "string" || typeof fw["blockNumber"] !== "number" || typeof fw["blockHash"] !== "string") {
278
+ return "floor carries no readable block-header witness";
279
+ }
280
+ }
281
+ if (!v3 && ceil["status"] !== "unfetched") {
247
282
  if (ceil["status"] !== "present") return `ceiling status must be "unfetched" or "present", got ${JSON.stringify(ceil["status"])}`;
248
283
  if (ceil["basis"] !== "counter-order") return `unknown ceiling basis ${JSON.stringify(ceil["basis"])}`;
249
284
  if (record(ceil["anchor"]) === null) return "ceiling carries no anchor proof";
@@ -253,7 +288,7 @@ function payloadShapeError(p: unknown): string | null {
253
288
  }
254
289
  }
255
290
  if (!v2) return null;
256
- // v2: the ceiling in time is stated either way; settlement, pins and the attestation witness are optional.
291
+ // v2 and v3: the ceiling in time is stated either way; settlement, pins and the attestation witness are optional.
257
292
  const ct = record(o["ceilingInTime"]);
258
293
  if (ct === null) return "ceilingInTime is missing: a v2 block states the ceiling in time or that it was not fetched";
259
294
  if (ct["status"] === "present") {
@@ -276,9 +311,9 @@ function payloadShapeError(p: unknown): string | null {
276
311
  return null;
277
312
  }
278
313
 
279
- /** 1 or 2, from a parsed payload. */
280
- export function carrierVersionOf(payload: CarrierPayload): 1 | 2 {
281
- return payload.carrier === CARRIER_VERSION_2 ? 2 : 1;
314
+ /** 1, 2 or 3, from a parsed payload. */
315
+ export function carrierVersionOf(payload: CarrierPayload): 1 | 2 | 3 {
316
+ return payload.carrier === CARRIER_VERSION_3 ? 3 : payload.carrier === CARRIER_VERSION_2 ? 2 : 1;
282
317
  }
283
318
 
284
319
  export function parseCarrier(bytes: Uint8Array): CarrierParse {
@@ -315,6 +350,7 @@ export function completeCarrier(
315
350
  const p = parseCarrier(bytes);
316
351
  if (p.kind !== "carrier") return { changed: false, bytes, error: p.kind === "none" ? "not a carrier" : `corrupt block: ${p.reason}` };
317
352
  const cur = p.payload.ceiling;
353
+ if (cur.status === "none") return { changed: false, bytes, error: `a ${CARRIER_VERSION_3} block has no ceiling in position: its order is the chain of proof hashes` };
318
354
  if (cur.status === "present") {
319
355
  if (ceiling === null) return { changed: false, bytes };
320
356
  const a = record(record(cur.anchor)?.["commit"]);
@@ -346,7 +382,7 @@ export function completeCarrierInTime(
346
382
  ): { changed: boolean; bytes: Uint8Array; error?: string } {
347
383
  const p = parseCarrier(bytes);
348
384
  if (p.kind !== "carrier") return { changed: false, bytes, error: p.kind === "none" ? "not a carrier" : `corrupt block: ${p.reason}` };
349
- if (carrierVersionOf(p.payload) !== 2) return { changed: false, bytes, error: "a bitgraph-carrier/1 block has no ceiling in time; rebuild the file as carrier/2" };
385
+ if (carrierVersionOf(p.payload) === 1) return { changed: false, bytes, error: "a bitgraph-carrier/1 block has no ceiling in time; rebuild the file as carrier/2" };
350
386
  const cur = p.payload.ceilingInTime;
351
387
  if (cur && cur.status === "present") {
352
388
  if (sidecar === null) return { changed: false, bytes };
@@ -380,7 +416,7 @@ export function completeCarrierSettlement(
380
416
  ): { changed: boolean; bytes: Uint8Array; error?: string } {
381
417
  const p = parseCarrier(bytes);
382
418
  if (p.kind !== "carrier") return { changed: false, bytes, error: p.kind === "none" ? "not a carrier" : `corrupt block: ${p.reason}` };
383
- if (carrierVersionOf(p.payload) !== 2) return { changed: false, bytes, error: "a bitgraph-carrier/1 block has no settlement; rebuild the file as carrier/2" };
419
+ if (carrierVersionOf(p.payload) === 1) return { changed: false, bytes, error: "a bitgraph-carrier/1 block has no settlement; rebuild the file as carrier/2" };
384
420
  const cur = p.payload.settlement;
385
421
  if (cur && cur.status === "present") {
386
422
  const a = record(cur.pointer["l1"]), b = record(pointer["l1"]);
@@ -583,15 +619,39 @@ export function verifyWitnessHeader(w: CarrierWitness): WitnessCheck {
583
619
  return { ok: true, error: null, timestamp: rlpNumber(items[11]!) };
584
620
  }
585
621
 
622
+ /**
623
+ * v3: the block a Base header names, read from its bytes: keccak-256 is its
624
+ * hash, items 8 and 11 its number and time. Null when it is not a readable
625
+ * header. Says nothing about whether it is the signed floor: verifyCarrier
626
+ * checks that against the proof.
627
+ */
628
+ export function baseHeaderFields(headerHex: string): { blockNumber: number; blockHash: string; timestamp: number } | null {
629
+ const raw = typeof headerHex === "string" ? hexToBytes(headerHex) : null;
630
+ if (raw === null || raw.length === 0) return null;
631
+ const items = decodeRlpTop(raw);
632
+ if (items === null || items.length < 12) return null;
633
+ const blockNumber = rlpNumber(items[8]!), timestamp = rlpNumber(items[11]!);
634
+ if (blockNumber === null || timestamp === null) return null;
635
+ const blockHash = "0x" + Array.from(keccak_256(raw), (b) => b.toString(16).padStart(2, "0")).join("");
636
+ return { blockNumber, blockHash, timestamp };
637
+ }
638
+
586
639
  /* ── The window a reader states ─────────────────────────────────────────── */
587
640
 
588
641
  export interface CarrierBounds {
589
- notBefore: { blockNumber: number; blockHash: string; timestamp: number | null };
590
- /** The ceiling in POSITION: the next anchor. null means NOT FETCHED — say so in those words — never "none exists". */
642
+ /** The floor, on its chain: Ethereum (v1, v2) or Base (v3). */
643
+ notBefore: { chain: "ethereum" | "base"; blockNumber: number; blockHash: string; timestamp: number | null };
644
+ /**
645
+ * The ceiling in POSITION: the next anchor. On v1 and v2, null means NOT
646
+ * FETCHED (say so in those words), never "none exists". On v3 it is always
647
+ * null because none exists: order is the chain of proof hashes.
648
+ */
591
649
  notAfter: { blockNumber: number; blockHash: string; timestamp: number | null } | null;
592
650
  /**
593
- * v2: the ceiling in TIME, as the sidecar states it (verifyCarrier recomputes
594
- * it from the header). null on a v1 block or when not fetched.
651
+ * v2: the ceiling in TIME. From carrierBounds, as the sidecar states it;
652
+ * from verifyCarrier, only as verified from the header, and null when the
653
+ * ceiling did not verify or its stamp was withheld as a bound
654
+ * (CarrierVerifyResult.baseTimeWithheld). null on a v1 block or when not fetched.
595
655
  */
596
656
  existedBy: { chain: "base"; blockNumber: number; blockHash: string; timestamp: number } | null;
597
657
  /** v2: the Ethereum block that committed the Base batch data, as the pointer states it. */
@@ -599,9 +659,17 @@ export interface CarrierBounds {
599
659
  }
600
660
 
601
661
  export function carrierBounds(payload: CarrierPayload): CarrierBounds {
602
- const f = payload.floor.witness;
603
- const floorTs = verifyWitnessHeader(f);
604
- const notBefore = { blockNumber: f.blockNumber, blockHash: f.blockHash.toLowerCase(), timestamp: floorTs.ok ? floorTs.timestamp : null };
662
+ let notBefore: CarrierBounds["notBefore"];
663
+ if (payload.floor.basis === "base-header") {
664
+ const h = baseHeaderFields(payload.floor.header);
665
+ notBefore = h === null
666
+ ? { chain: "base", blockNumber: 0, blockHash: "", timestamp: null }
667
+ : { chain: "base", blockNumber: h.blockNumber, blockHash: h.blockHash, timestamp: h.timestamp };
668
+ } else {
669
+ const f = payload.floor.witness;
670
+ const floorTs = verifyWitnessHeader(f);
671
+ notBefore = { chain: "ethereum", blockNumber: f.blockNumber, blockHash: f.blockHash.toLowerCase(), timestamp: floorTs.ok ? floorTs.timestamp : null };
672
+ }
605
673
  let notAfter: CarrierBounds["notAfter"] = null;
606
674
  if (payload.ceiling.status === "present") {
607
675
  const c = payload.ceiling.witness;
package/src/ceiling.ts CHANGED
@@ -3,9 +3,13 @@
3
3
  /**
4
4
  * A ceiling in TIME for a BitGraph, carried beside the proof, never inside it.
5
5
  *
6
- * Every BitGraph already has a floor in time (the Ethereum block the enclave
7
- * fixes at slot allocation and signs at commit, `commit.slotAnchor`) and a ceiling in POSITION (the
8
- * anchors that follow it in the chain). This adds the one thing a record
6
+ * Every BitGraph already has a floor in time: the block the enclave fixes at
7
+ * slot allocation and signs at commit, an Ethereum anchor (`commit.slotAnchor`,
8
+ * enclave v7 to v9) or a Base block (`commit.slotFloor`, enclave v10; read both
9
+ * through signedFloorOf). An Ethereum-floor BitGraph also has a ceiling in
10
+ * POSITION (the anchors that follow it in the chain); after the cutover to Base
11
+ * floors there are no anchors, and order among BitGraphs is the chain of proof
12
+ * hashes. This adds the one thing a record
9
13
  * cannot supply for itself: something outside the operator's control that
10
14
  * depends on the record. After a commit, a writer puts a Merkle root over
11
15
  * record hashes into a Base transaction. The block that includes it proves
@@ -30,9 +34,10 @@ import { sha256 } from "@noble/hashes/sha256";
30
34
  import { verifyProofIntegrity, createVerificationContext } from "./verifier.js";
31
35
  import { computeProofHash } from "./proof-hash.js";
32
36
  import { merkleLeafHash, merkleRootFromPath } from "./fuse-merkle.js";
33
- import { base64ToBytes } from "./fuse.js";
37
+ import { base64ToBytes, signedFloorOf, type SignedFloor } from "./fuse.js";
34
38
  import {
35
- bytesEqual, bytesToHex, decodeEip1559, decodeHeader, hexToBytes, keccak256, mptVerify, txTrieKey,
39
+ bytesEqual, bytesToHex, decodeEip1559, decodeHeader, hexToBytes, keccak256, mptVerify, rlpEncode, txTrieKey,
40
+ type DecodedHeader,
36
41
  } from "./ceiling-evm.js";
37
42
  import type { BitGraphProof, VerificationPolicy } from "./types.js";
38
43
 
@@ -70,9 +75,12 @@ export interface CeilingSidecar {
70
75
  statusObserved: { included: string | null; safe: string | null; finalized: string | null };
71
76
  /**
72
77
  * The floor block's header, so the window reads offline. Optional and
73
- * unsigned: checked by keccak256 against the proof's SIGNED slotAnchor hash.
78
+ * unsigned: checked by keccak256 against the floor the proof SIGNS
79
+ * (signedFloorOf). `chain` names the floor's chain: absent is Ethereum (every
80
+ * sidecar before enclave v10), "base" a Base floor, whose header's time must
81
+ * also equal the signed blockTimestamp.
74
82
  */
75
- floor?: { blockNumber: number; blockHash: string; blockTimestamp: number; blockHeader: string } | null;
83
+ floor?: { blockNumber: number; blockHash: string; blockTimestamp: number; blockHeader: string; chain?: "ethereum" | "base" } | null;
76
84
  /** Reserved for v2: the Ethereum batch that settles this Base block. */
77
85
  settlement: null;
78
86
  }
@@ -132,14 +140,14 @@ export interface CeilingCheck {
132
140
  }
133
141
 
134
142
  export interface CeilingVerifyResult {
135
- /** True when every offline check passed. Says nothing about chain canonicality. */
143
+ /** True when every offline check that ran passed, the carried floor header included. Says nothing about chain canonicality. */
136
144
  ok: boolean;
137
145
  reason?: string;
138
146
  checks: CeilingCheck[];
139
147
  status?: CeilingStatus;
140
148
  window?: {
141
- /** Floor: the proof's signed slotAnchor block. Time only if a checked header was carried. */
142
- floor: { blockNumber: number; blockHash: string; blockTimestamp: number | null };
149
+ /** Floor: the block the proof signs (signedFloorOf), on its chain. Time only if a checked header was carried. */
150
+ floor: { chain: "ethereum" | "base" | null; blockNumber: number; blockHash: string; blockTimestamp: number | null };
143
151
  /** Ceiling: the Base block, time read from its header. */
144
152
  ceiling: { chainId: number; blockNumber: number; blockHash: string; blockTimestamp: number };
145
153
  widthSeconds: number | null;
@@ -181,8 +189,9 @@ export function ceilingLabel(status: CeilingStatus, blockNumber: number, blockTi
181
189
  }
182
190
 
183
191
  /**
184
- * Check a ceiling sidecar against its proof, offline. Every check runs and
185
- * is reported; the first failure is the `reason`.
192
+ * Check a ceiling sidecar against its proof, offline. The checks run in
193
+ * order and stop at the first failure, which is the `reason`; `ok` is true
194
+ * only when every check that ran passed, a carried floor header included.
186
195
  */
187
196
  export async function verifyCeiling(
188
197
  proof: BitGraphProof,
@@ -194,6 +203,21 @@ export async function verifyCeiling(
194
203
  checks.push({ name, ok: false, detail });
195
204
  return { ok: false, reason: `${name}: ${detail}`, checks, headerCheckedAgainstChain: false };
196
205
  };
206
+ // A malformed field anywhere is a failed check, never an exception.
207
+ try {
208
+ return await verifyCeilingChecks(proof, sidecar, opts, checks, fail);
209
+ } catch (e) {
210
+ return fail("malformed", `a field of the sidecar is malformed: ${e instanceof Error ? e.message : String(e)}`);
211
+ }
212
+ }
213
+
214
+ async function verifyCeilingChecks(
215
+ proof: BitGraphProof,
216
+ sidecar: CeilingSidecar,
217
+ opts: CeilingVerifyOptions,
218
+ checks: CeilingCheck[],
219
+ fail: (name: string, detail: string) => CeilingVerifyResult,
220
+ ): Promise<CeilingVerifyResult> {
197
221
  const pass = (name: string, detail?: string) => checks.push(detail === undefined ? { name, ok: true } : { name, ok: true, detail });
198
222
 
199
223
  if (sidecar?.version !== CEILING_VERSION) return fail("format", `not a ${CEILING_VERSION} sidecar`);
@@ -233,7 +257,9 @@ export async function verifyCeiling(
233
257
  if (bytesToHex(computedRoot) !== sidecar.root.toLowerCase()) return fail("merkle", "the leaf and path do not reach the root");
234
258
  pass("merkle", `leaf ${sidecar.leafIndex} of ${sidecar.leafCount} reaches the root`);
235
259
 
236
- if (sidecar.status === "pending" || !sidecar.anchor) {
260
+ // Pending is the absence of a transaction to check. The status field is
261
+ // the writer's report, unproven by the file, and never decides a result.
262
+ if (!sidecar.anchor) {
237
263
  return { ok: false, reason: "ceiling pending: no transaction included yet", checks, status: "pending", headerCheckedAgainstChain: false };
238
264
  }
239
265
  const a = sidecar.anchor;
@@ -280,24 +306,38 @@ export async function verifyCeiling(
280
306
  if (!inBlock || !bytesEqual(inBlock, rawTx)) return fail("inclusion", "the transaction is not in this block");
281
307
  pass("inclusion", `transaction ${a.txIndex} of block ${header.number}`);
282
308
 
283
- // 5. The window. The floor is the proof's own signed slotAnchor.
284
- const slotAnchor = proof.commit.slotAnchor;
285
- let floor: { blockNumber: number; blockHash: string; blockTimestamp: number | null } | null = null;
286
- if (slotAnchor) {
287
- floor = { blockNumber: slotAnchor.blockNumber, blockHash: slotAnchor.blockHash, blockTimestamp: null };
309
+ // 5. The window. The floor is the block the proof itself signs, on its own chain.
310
+ let signed: SignedFloor | null;
311
+ try {
312
+ signed = signedFloorOf(proof);
313
+ } catch (e) {
314
+ return fail("floor", (e as Error).message);
315
+ }
316
+ let floor: { chain: "ethereum" | "base"; blockNumber: number; blockHash: string; blockTimestamp: number | null } | null = null;
317
+ if (signed) {
318
+ floor = { chain: signed.chain, blockNumber: signed.blockNumber, blockHash: signed.blockHash, blockTimestamp: null };
288
319
  const w = sidecar.floor;
289
- if (w && w.blockHeader) {
320
+ if (w !== null && w !== undefined) {
321
+ if (typeof w !== "object" || typeof w.blockHeader !== "string" || w.blockHeader.length === 0) {
322
+ return fail("floor", "the sidecar carries a floor without a header (absence is null; a present floor carries its header)");
323
+ }
324
+ let raw: Uint8Array;
290
325
  try {
291
- const fh = decodeHeader(hexToBytes(w.blockHeader));
292
- if (fh.hash === slotAnchor.blockHash.toLowerCase() && fh.number === slotAnchor.blockNumber) {
293
- floor.blockTimestamp = fh.timestamp;
294
- pass("floor", `Ethereum block ${fh.number}, header checked against the signed hash`);
295
- } else {
296
- checks.push({ name: "floor", ok: false, detail: "the carried floor header does not match the proof's signed slotAnchor; ignored" });
297
- }
326
+ raw = hexToBytes(w.blockHeader);
298
327
  } catch {
299
- checks.push({ name: "floor", ok: false, detail: "the carried floor header does not decode; ignored" });
328
+ return fail("floor", "the carried floor header does not decode");
329
+ }
330
+ // Every part a sidecar carries must hold: a copy of the floor that is
331
+ // not the signed floor block fails the sidecar, it is not skipped.
332
+ const fh = checkFloorHeader(signed, raw, w.chain);
333
+ if (!fh.ok) {
334
+ if (fh.reason.startsWith("the header does not decode")) return fail("floor", "the carried floor header does not decode");
335
+ const field = signed.chain === "base" ? "slotFloor" : "slotAnchor";
336
+ const said = signed.chain === "ethereum" && w.chain === undefined ? "" : `: ${fh.reason}`;
337
+ return fail("floor", `the carried floor header does not match the proof's signed ${field}${said}`);
300
338
  }
339
+ floor.blockTimestamp = fh.header.timestamp;
340
+ pass("floor", `${chainName(signed.chain)} block ${fh.header.number}, header checked against the signed hash`);
301
341
  }
302
342
  }
303
343
  const width = floor?.blockTimestamp != null ? header.timestamp - floor.blockTimestamp : null;
@@ -307,7 +347,7 @@ export async function verifyCeiling(
307
347
  checks,
308
348
  status,
309
349
  window: {
310
- floor: floor ?? { blockNumber: 0, blockHash: "", blockTimestamp: null },
350
+ floor: floor ?? { chain: null, blockNumber: 0, blockHash: "", blockTimestamp: null },
311
351
  ceiling: { chainId: a.chainId, blockNumber: header.number, blockHash: header.hash, blockTimestamp: header.timestamp },
312
352
  widthSeconds: width,
313
353
  },
@@ -340,3 +380,152 @@ export async function checkCeilingOnline(
340
380
  }
341
381
 
342
382
  export { keccak256 as ceilingKeccak256 };
383
+
384
+ // ── The floor block's header ───────────────────────────────────────────────
385
+
386
+ /**
387
+ * Base mainnet stamps every block by its height: block n is
388
+ * BASE_MAINNET_GENESIS_TIME + BASE_BLOCK_TIME_SECONDS * n (OP Stack, checked
389
+ * against the live chain 2026-10-06). The enclave (v10) refuses a floor header
390
+ * off this schedule; a verifier refuses one too, since such a header is not a
391
+ * Base mainnet block (another chain, a testnet, or made up).
392
+ */
393
+ export const BASE_MAINNET_GENESIS_TIME = 1686789347;
394
+ export const BASE_BLOCK_TIME_SECONDS = 2;
395
+
396
+ /** True when a block's stamp is Base mainnet's schedule for its number. */
397
+ export function onBaseSchedule(blockNumber: number, timestampSec: number): boolean {
398
+ return timestampSec === BASE_MAINNET_GENESIS_TIME + BASE_BLOCK_TIME_SECONDS * blockNumber;
399
+ }
400
+
401
+ const chainName = (c: "ethereum" | "base") => (c === "base" ? "Base" : "Ethereum");
402
+
403
+ /**
404
+ * Check a floor header against the floor the proof signs (signedFloorOf). The
405
+ * header must decode, hash (keccak-256) to the signed block hash and carry the
406
+ * signed number. For a Base floor its time must also equal the signed
407
+ * blockTimestamp and be Base mainnet's schedule for its number. When the
408
+ * carrier of the header names a chain (`declaredChain`, absent = Ethereum), it
409
+ * must be the signed floor's chain: a Base floor read as an Ethereum header,
410
+ * or the reverse, fails here even before any hash is compared.
411
+ */
412
+ export function checkFloorHeader(
413
+ signed: SignedFloor,
414
+ headerRlp: Uint8Array,
415
+ declaredChain?: unknown,
416
+ ): { ok: true; header: DecodedHeader } | { ok: false; reason: string } {
417
+ const declared = declaredChain === undefined || declaredChain === null ? "ethereum" : declaredChain;
418
+ if (declared !== "ethereum" && declared !== "base") return { ok: false, reason: `the header names an unknown chain (${JSON.stringify(declaredChain)})` };
419
+ const aBlock = (c: "ethereum" | "base") => (c === "base" ? "a Base block" : "an Ethereum block");
420
+ if (declared !== signed.chain) return { ok: false, reason: `the header is given as ${aBlock(declared)}, and the proof signs ${aBlock(signed.chain)}` };
421
+ let h: DecodedHeader;
422
+ try {
423
+ h = decodeHeader(headerRlp);
424
+ } catch (e) {
425
+ return { ok: false, reason: `the header does not decode (${(e as Error).message})` };
426
+ }
427
+ if (h.hash !== signed.blockHash.toLowerCase()) return { ok: false, reason: `the header does not hash to the signed ${chainName(signed.chain)} block hash` };
428
+ if (h.number !== signed.blockNumber) return { ok: false, reason: `the header is block ${h.number}, the proof signs block ${signed.blockNumber}` };
429
+ if (signed.chain === "base") {
430
+ if (h.timestamp !== signed.blockTimestamp) return { ok: false, reason: `the header's time is ${h.timestamp}, the proof signs ${signed.blockTimestamp}` };
431
+ if (!onBaseSchedule(h.number, h.timestamp)) return { ok: false, reason: `Base block ${h.number} is stamped ${h.timestamp}, off Base mainnet's schedule (${BASE_MAINNET_GENESIS_TIME + BASE_BLOCK_TIME_SECONDS * h.number})` };
432
+ }
433
+ return { ok: true, header: h };
434
+ }
435
+
436
+ /**
437
+ * Whether the floor block's time may be stated as a bound (review, 2026-10-06):
438
+ * the floor block must be no later than the commit's attestation document,
439
+ * the time AWS's hardware signed. A floor stamped after it cannot be a "not
440
+ * before" for a record that already existed; the block hash still floors the
441
+ * record (it did not exist before its block), but its time is withheld.
442
+ * Holds for either chain.
443
+ */
444
+ export function floorTimeIsBound(floorTimestampSec: number, attestedAtMs: number | null | undefined): { ok: true } | { ok: false; reason: string } {
445
+ if (attestedAtMs != null && floorTimestampSec * 1000 > attestedAtMs) {
446
+ return { ok: false, reason: `the floor block is stamped ${isoSec(floorTimestampSec)}, after the attestation document was made (${isoMs(attestedAtMs)})` };
447
+ }
448
+ return { ok: true };
449
+ }
450
+
451
+ /** A block as an EVM node's JSON-RPC returns it (eth_getBlockByNumber), the header fields only. */
452
+ export interface RpcBlockHeader {
453
+ hash: string; parentHash: string; sha3Uncles: string; miner: string; stateRoot: string;
454
+ transactionsRoot: string; receiptsRoot: string; logsBloom: string;
455
+ difficulty: string; number: string; gasLimit: string; gasUsed: string;
456
+ timestamp: string; extraData: string; mixHash: string; nonce: string;
457
+ baseFeePerGas?: string; withdrawalsRoot?: string; blobGasUsed?: string;
458
+ excessBlobGas?: string; parentBeaconBlockRoot?: string; requestsHash?: string;
459
+ }
460
+
461
+ /**
462
+ * The header's RLP, rebuilt from a node's JSON in canonical field order
463
+ * (through Prague; Base headers have the same shape), or null when it does not
464
+ * reproduce `expectedHash`. Null is the honest answer for a field this encoder
465
+ * does not know: nothing unchecked is ever returned.
466
+ */
467
+ export function headerRlpFromRpc(b: RpcBlockHeader | null | undefined, expectedHash: string): Uint8Array | null {
468
+ if (!b || typeof b !== "object") return null;
469
+ try {
470
+ const q = (x: string | undefined) => {
471
+ const h = String(x ?? "0x").replace(/^0x/i, "").replace(/^0+/, "");
472
+ return h === "" ? new Uint8Array(0) : hexToBytes(h.length % 2 ? `0${h}` : h);
473
+ };
474
+ const d = (x: string | undefined) => hexToBytes(String(x ?? "0x"));
475
+ const fields: Uint8Array[] = [
476
+ d(b.parentHash), d(b.sha3Uncles), d(b.miner), d(b.stateRoot), d(b.transactionsRoot),
477
+ d(b.receiptsRoot), d(b.logsBloom), q(b.difficulty), q(b.number), q(b.gasLimit),
478
+ q(b.gasUsed), q(b.timestamp), d(b.extraData), d(b.mixHash), d(b.nonce),
479
+ ];
480
+ if (b.baseFeePerGas != null) fields.push(q(b.baseFeePerGas));
481
+ if (b.withdrawalsRoot != null) fields.push(d(b.withdrawalsRoot));
482
+ if (b.blobGasUsed != null) fields.push(q(b.blobGasUsed));
483
+ if (b.excessBlobGas != null) fields.push(q(b.excessBlobGas));
484
+ if (b.parentBeaconBlockRoot != null) fields.push(d(b.parentBeaconBlockRoot));
485
+ if (b.requestsHash != null) fields.push(d(b.requestsHash));
486
+ const rlp = rlpEncode(fields);
487
+ return bytesToHex(keccak256(rlp)) === expectedHash.toLowerCase() ? rlp : null;
488
+ } catch {
489
+ return null;
490
+ }
491
+ }
492
+
493
+ // ── The Base stamp as a time bound ─────────────────────────────────────────
494
+
495
+ const isoSec = (unix: number) => new Date(unix * 1000).toISOString().replace(".000Z", "Z");
496
+ const isoMs = (ms: number) => new Date(ms).toISOString();
497
+
498
+ /**
499
+ * The slack allowed between a Base block's stamp and the attestation
500
+ * document's time: one Base block. A Base stamp is whole seconds on Base's
501
+ * fixed two-second schedule; the document's time is milliseconds on the AWS
502
+ * Nitro hypervisor's clock. The writer puts the ceiling on Base only after
503
+ * the proof exists, so an honest stamp is seconds to minutes after the
504
+ * document; one block covers sealing latency and clock skew.
505
+ */
506
+ export const BASE_STAMP_TOLERANCE_SECONDS = 2;
507
+
508
+ /**
509
+ * Whether a Base block's stamp may be stated as an "existed by" time for a
510
+ * record. Base stamps every block by its number, so after a halt the missed
511
+ * time is refilled with blocks stamped in the past; in the 2026-06-25 and
512
+ * 2026-06-26 halts those blocks were empty, but nothing in the protocol makes
513
+ * them so. A stamp earlier than the floor block's time, or more than
514
+ * BASE_STAMP_TOLERANCE_SECONDS earlier than the attestation document (made
515
+ * after the signed body it binds), cannot be a bound for this record.
516
+ * Passing says only that the stamp is not known to be wrong: the Base time
517
+ * stays provisional until the block is checked against Base.
518
+ */
519
+ export function baseTimeIsBound(
520
+ baseTimestampSec: number,
521
+ ref: { floorTimestampSec?: number | null; attestedAtMs?: number | null },
522
+ ): { ok: true } | { ok: false; reason: string } {
523
+ if (ref.floorTimestampSec != null && baseTimestampSec < ref.floorTimestampSec) {
524
+ return { ok: false, reason: `Base stamped this block ${isoSec(baseTimestampSec)}, before the floor block's own time (${isoSec(ref.floorTimestampSec)})` };
525
+ }
526
+ if (ref.attestedAtMs != null && baseTimestampSec + BASE_STAMP_TOLERANCE_SECONDS < ref.attestedAtMs / 1000) {
527
+ return { ok: false, reason: `Base stamped this block ${isoSec(baseTimestampSec)}, more than ${BASE_STAMP_TOLERANCE_SECONDS} s before the attestation document was made (${isoMs(ref.attestedAtMs)}), as Base does when it refills time after a halt` };
528
+ }
529
+ return { ok: true };
530
+ }
531
+