@mikeargento/bitgraph-verify 1.30.0 → 1.32.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 (49) hide show
  1. package/README.md +23 -0
  2. package/dist/__tests__/base-schedule.test.d.ts +2 -0
  3. package/dist/__tests__/base-schedule.test.d.ts.map +1 -0
  4. package/dist/__tests__/base-schedule.test.js +53 -0
  5. package/dist/__tests__/base-schedule.test.js.map +1 -0
  6. package/dist/__tests__/carrier-tot.test.d.ts +2 -0
  7. package/dist/__tests__/carrier-tot.test.d.ts.map +1 -0
  8. package/dist/__tests__/carrier-tot.test.js +80 -0
  9. package/dist/__tests__/carrier-tot.test.js.map +1 -0
  10. package/dist/__tests__/carrier6.test.js +13 -1
  11. package/dist/__tests__/carrier6.test.js.map +1 -1
  12. package/dist/__tests__/tot-floor.test.js +54 -0
  13. package/dist/__tests__/tot-floor.test.js.map +1 -1
  14. package/dist/carrier-verify.d.ts +6 -0
  15. package/dist/carrier-verify.d.ts.map +1 -1
  16. package/dist/carrier-verify.js.map +1 -1
  17. package/dist/carrier5.d.ts.map +1 -1
  18. package/dist/carrier5.js +25 -4
  19. package/dist/carrier5.js.map +1 -1
  20. package/dist/ceiling.d.ts +16 -7
  21. package/dist/ceiling.d.ts.map +1 -1
  22. package/dist/ceiling.js +25 -9
  23. package/dist/ceiling.js.map +1 -1
  24. package/dist/fuse.d.ts +11 -0
  25. package/dist/fuse.d.ts.map +1 -1
  26. package/dist/fuse.js +20 -0
  27. package/dist/fuse.js.map +1 -1
  28. package/dist/index.d.ts +2 -2
  29. package/dist/index.d.ts.map +1 -1
  30. package/dist/index.js +2 -2
  31. package/dist/index.js.map +1 -1
  32. package/dist/tot-floor.d.ts +24 -9
  33. package/dist/tot-floor.d.ts.map +1 -1
  34. package/dist/tot-floor.js +22 -13
  35. package/dist/tot-floor.js.map +1 -1
  36. package/dist/verifier.js +5 -5
  37. package/dist/verifier.js.map +1 -1
  38. package/package.json +1 -1
  39. package/src/__tests__/base-schedule.test.ts +62 -0
  40. package/src/__tests__/carrier-tot.test.ts +86 -0
  41. package/src/__tests__/carrier6.test.ts +14 -1
  42. package/src/__tests__/tot-floor.test.ts +59 -0
  43. package/src/carrier-verify.ts +6 -1
  44. package/src/carrier5.ts +25 -3
  45. package/src/ceiling.ts +25 -8
  46. package/src/fuse.ts +20 -0
  47. package/src/index.ts +2 -1
  48. package/src/tot-floor.ts +42 -20
  49. package/src/verifier.ts +4 -4
package/src/tot-floor.ts CHANGED
@@ -18,12 +18,17 @@
18
18
  * The receipt is exactly as valid as one with no floor.
19
19
  * not-checked the lookup could not be made (no network, a chain this checker does not know).
20
20
  *
21
- * `checkFloorAgainstBlock` is the whole rule and fetches nothing: the caller supplies what the chain says.
22
- * `checkFloorOnline` asks a node and then applies it.
21
+ * `checkFloorOnline` asks a node of the floor's network and applies the rule to its answer.
22
+ * `checkFloorAgainstBlock` applies the same rule to a block the caller hands in, and fetches nothing.
23
+ *
24
+ * Every answer says where its block came from (`source`). Only "lookup" means this checker asked the chain. A
25
+ * "supplied" block is the caller's word for what the chain says: a header saved in a file, sent with a receipt or
26
+ * built to fit will match its own hash and answer `confirmed`, and that shows nothing about the chain. Say a floor
27
+ * is confirmed on its chain only for a lookup, or for a supplied block you yourself read from the chain.
23
28
  */
24
29
 
25
30
  import type { BitGraphProof, SlotFloor } from "./types.js";
26
- import { base64ToBytes, slotFloorProblem, SLOT_VERSION_2, FLOOR_NETWORK_BASE } from "./fuse.js";
31
+ import { base64ToBytes, slotFormProblem, SLOT_VERSION_2, FLOOR_NETWORK_BASE } from "./fuse.js";
27
32
  import { headerRlpFromRpc, type RpcBlockHeader } from "./ceiling.js";
28
33
  import { bytesToHex } from "./ceiling-evm.js";
29
34
 
@@ -42,6 +47,12 @@ export interface FloorCheck {
42
47
  networkName?: string;
43
48
  /** A page where a person can look the block up, when this checker knows the network. */
44
49
  blockUrl?: string;
50
+ /**
51
+ * Where the block this answer rests on came from. "lookup": this checker asked a node of the floor's network.
52
+ * "supplied": the caller handed the block in, and the answer is only as good as where the caller got it.
53
+ * Absent when no block was considered (status "none", or a network this checker does not know).
54
+ */
55
+ source?: "lookup" | "supplied";
45
56
  /** Why it is not confirmed, or why it was not checked. */
46
57
  detail?: string;
47
58
  /** confirmed: the block's own time, milliseconds since 1970. */
@@ -63,7 +74,7 @@ export interface FloorCheck {
63
74
  /** The floor a receipt's TOT carries under its signature, or null: no floor, not a slot/2 record, or not in form. */
64
75
  export function totFloorOf(proof: Pick<BitGraphProof, "slotAllocation"> | null | undefined): SlotFloor | null {
65
76
  const slot = proof?.slotAllocation;
66
- if (!slot || slot.version !== SLOT_VERSION_2 || slotFloorProblem(slot) !== null) return null;
77
+ if (!slot || slot.version !== SLOT_VERSION_2 || slotFormProblem(slot) !== null) return null;
67
78
  const f = slot.floor!;
68
79
  return { network: f.network, number: f.number, hashB64: f.hashB64 };
69
80
  }
@@ -84,35 +95,46 @@ function describe(floor: SlotFloor): Pick<FloorCheck, "floor" | "hashHex" | "net
84
95
  };
85
96
  }
86
97
 
87
- /**
88
- * The rule, with what the chain says supplied by the caller. `block` is the chain's block at the floor's number as
89
- * a node returns it (eth_getBlockByNumber), or null when the chain has no block of that number.
90
- */
91
- export function checkFloorAgainstBlock(args: {
98
+ interface RuleArgs {
92
99
  proof: Pick<BitGraphProof, "slotAllocation">;
93
100
  block: (Partial<RpcBlockHeader> & { timestampMs?: string }) | null;
94
101
  attestedAtMs?: number | null;
95
102
  maxGapMs?: number;
96
- }): FloorCheck {
103
+ }
104
+
105
+ /**
106
+ * The rule applied to a block the caller supplies: the chain's block at the floor's number as a node returns it
107
+ * (eth_getBlockByNumber), or null for "the chain has no block of that number". Nothing is fetched, and the answer
108
+ * carries `source: "supplied"`: `confirmed` here means the block given carries the floor's hash, which says the
109
+ * chain holds it only if the block really came from the chain. Use `checkFloorOnline` to ask the chain.
110
+ */
111
+ export function checkFloorAgainstBlock(args: RuleArgs): FloorCheck {
112
+ return applyRule(args, "supplied");
113
+ }
114
+
115
+ function applyRule(args: RuleArgs, source: "lookup" | "supplied"): FloorCheck {
97
116
  const floor = totFloorOf(args.proof);
98
117
  if (floor === null) return { status: "none" };
99
118
  const d = describe(floor);
100
119
  const attested = typeof args.attestedAtMs === "number" ? { attestedAtMs: args.attestedAtMs } : {};
101
- if (args.block === null) return { status: "not-confirmed", ...d, ...attested, detail: `${d.networkName ?? floor.network} has no block ${floor.number}` };
120
+ // A network this checker does not know is never judged, whatever block is handed in (spec section 4).
121
+ if (!FLOOR_NETWORKS[floor.network]) return { status: "not-checked", ...d, ...attested, detail: `this checker does not know the network ${floor.network}` };
122
+ const from = { source };
123
+ if (args.block === null) return { status: "not-confirmed", ...d, ...attested, ...from, detail: `${d.networkName ?? floor.network} has no block ${floor.number}` };
102
124
  const b = args.block;
103
125
  const got = typeof b.hash === "string" ? b.hash.toLowerCase() : "";
104
126
  const numberMatches = typeof b.number === "string" && /^0x[0-9a-f]+$/i.test(b.number) && BigInt(b.number).toString(10) === floor.number;
105
- if (!numberMatches) return { status: "not-confirmed", ...d, ...attested, detail: "the block returned is not the block asked for" };
106
- if (got !== d.hashHex) return { status: "not-confirmed", ...d, ...attested, detail: `block ${floor.number} on ${d.networkName ?? floor.network} has another hash` };
127
+ if (!numberMatches) return { status: "not-confirmed", ...d, ...attested, ...from, detail: "the block returned is not the block asked for" };
128
+ if (got !== d.hashHex) return { status: "not-confirmed", ...d, ...attested, ...from, detail: `block ${floor.number} on ${d.networkName ?? floor.network} has another hash` };
107
129
 
108
130
  // The block is there. Its time: the whole seconds are part of the header the hash covers, when the header rebuilds.
109
131
  const seconds = typeof b.timestamp === "string" && /^0x[0-9a-f]+$/i.test(b.timestamp) ? Number(BigInt(b.timestamp)) : null;
110
- if (seconds === null || !Number.isSafeInteger(seconds)) return { status: "confirmed", ...d, ...attested };
132
+ if (seconds === null || !Number.isSafeInteger(seconds)) return { status: "confirmed", ...d, ...attested, ...from };
111
133
  const timeBoundByHash = headerRlpFromRpc(b as RpcBlockHeader, d.hashHex!) !== null;
112
134
  const finer = typeof b.timestampMs === "string" && /^0x[0-9a-f]+$/i.test(b.timestampMs) ? Number(BigInt(b.timestampMs)) : null;
113
135
  // A finer time is used only when it falls inside the header's own second.
114
136
  const blockTimeMs = finer !== null && Number.isSafeInteger(finer) && Math.floor(finer / 1000) === seconds ? finer : seconds * 1000;
115
- const out: FloorCheck = { status: "confirmed", ...d, ...attested, blockTimeMs, timeBoundByHash };
137
+ const out: FloorCheck = { status: "confirmed", ...d, ...attested, ...from, blockTimeMs, timeBoundByHash };
116
138
  if (typeof args.attestedAtMs === "number") {
117
139
  out.gapMs = args.attestedAtMs - blockTimeMs;
118
140
  if (typeof args.maxGapMs === "number") out.stale = out.gapMs > args.maxGapMs;
@@ -128,7 +150,7 @@ export async function checkFloorOnline(args: {
128
150
  proof: Pick<BitGraphProof, "slotAllocation">;
129
151
  attestedAtMs?: number | null;
130
152
  maxGapMs?: number;
131
- /** A node to ask. Default: the network's public node. */
153
+ /** A node of the floor's network to ask. Default: the network's public node. */
132
154
  rpcUrl?: string;
133
155
  fetcher?: typeof fetch;
134
156
  timeoutMs?: number;
@@ -138,8 +160,8 @@ export async function checkFloorOnline(args: {
138
160
  const d = describe(floor);
139
161
  const attested = typeof args.attestedAtMs === "number" ? { attestedAtMs: args.attestedAtMs } : {};
140
162
  const known = FLOOR_NETWORKS[floor.network];
141
- const url = args.rpcUrl ?? known?.rpcUrl;
142
- if (!url) return { status: "not-checked", ...d, ...attested, detail: `this checker does not know the network ${floor.network}` };
163
+ if (!known) return { status: "not-checked", ...d, ...attested, detail: `this checker does not know the network ${floor.network}` };
164
+ const url = args.rpcUrl ?? known.rpcUrl;
143
165
  let block: unknown;
144
166
  try {
145
167
  const r = await (args.fetcher ?? fetch)(url, {
@@ -156,10 +178,10 @@ export async function checkFloorOnline(args: {
156
178
  return { status: "not-checked", ...d, ...attested, detail: `the lookup failed: ${e instanceof Error ? e.message : String(e)}` };
157
179
  }
158
180
  if (block !== null && (typeof block !== "object" || Array.isArray(block))) return { status: "not-checked", ...d, ...attested, detail: "the node's answer is not a block" };
159
- return checkFloorAgainstBlock({
181
+ return applyRule({
160
182
  proof: args.proof,
161
183
  block: block as (Partial<RpcBlockHeader> & { timestampMs?: string }) | null,
162
184
  ...(typeof args.attestedAtMs === "number" ? { attestedAtMs: args.attestedAtMs } : {}),
163
185
  ...(typeof args.maxGapMs === "number" ? { maxGapMs: args.maxGapMs } : {}),
164
- });
186
+ }, "lookup");
165
187
  }
package/src/verifier.ts CHANGED
@@ -83,7 +83,7 @@ import { createVerify, createHash } from "node:crypto";
83
83
  import { verifyAsync as ed25519VerifyAsync } from "@noble/ed25519";
84
84
  import { sha256 } from "@noble/hashes/sha256";
85
85
  import { canonicalize, constantTimeEqual } from "./canonical.js";
86
- import { SLOT_VERSION_2, slotFloorProblem } from "./fuse.js";
86
+ import { SLOT_VERSION_2, slotFormProblem } from "./fuse.js";
87
87
  import type { EnforcementTier, BitGraphProof, SignedBody, SlotAllocation, VerificationPolicy, AgencyEnvelope, AuthorizationPayload, WebAuthnAuthorization } from "./types.js";
88
88
 
89
89
  // ---------------------------------------------------------------------------
@@ -901,9 +901,9 @@ async function verifySlotAllocation(proof: BitGraphProof): Promise<string | null
901
901
  if (slot.version !== "bitgraph/slot/1" && slot.version !== SLOT_VERSION_2) {
902
902
  return `slotAllocation.version must be "bitgraph/slot/1" or "bitgraph/slot/2", got "${String(slot.version)}"`;
903
903
  }
904
- // A floor is signed in a slot/2 record and absent from a slot/1 record.
905
- const floorProblem = slotFloorProblem(slot);
906
- if (floorProblem) return floorProblem;
904
+ // A floor is signed in a slot/2 record and absent from a slot/1 record; a slot/2 record holds only its fields.
905
+ const formProblem = slotFormProblem(slot);
906
+ if (formProblem) return formProblem;
907
907
  if (typeof slot.nonceB64 !== "string" || slot.nonceB64.length === 0) {
908
908
  return "slotAllocation.nonceB64 must be a non-empty string";
909
909
  }