@mikeargento/bitgraph-verify 1.29.2 → 1.31.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.
- package/dist/__tests__/base-schedule.test.d.ts +2 -0
- package/dist/__tests__/base-schedule.test.d.ts.map +1 -0
- package/dist/__tests__/base-schedule.test.js +53 -0
- package/dist/__tests__/base-schedule.test.js.map +1 -0
- package/dist/__tests__/measurements-windows.test.js +5 -1
- package/dist/__tests__/measurements-windows.test.js.map +1 -1
- package/dist/__tests__/tot-floor.test.d.ts +2 -0
- package/dist/__tests__/tot-floor.test.d.ts.map +1 -0
- package/dist/__tests__/tot-floor.test.js +233 -0
- package/dist/__tests__/tot-floor.test.js.map +1 -0
- package/dist/ceiling.d.ts +16 -7
- package/dist/ceiling.d.ts.map +1 -1
- package/dist/ceiling.js +25 -9
- package/dist/ceiling.js.map +1 -1
- package/dist/fuse.d.ts +14 -0
- package/dist/fuse.d.ts.map +1 -1
- package/dist/fuse.js +37 -0
- package/dist/fuse.js.map +1 -1
- package/dist/index.d.ts +5 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -2
- package/dist/index.js.map +1 -1
- package/dist/measurements.d.ts +2 -0
- package/dist/measurements.d.ts.map +1 -1
- package/dist/measurements.js +3 -2
- package/dist/measurements.js.map +1 -1
- package/dist/tot-floor.d.ts +101 -0
- package/dist/tot-floor.d.ts.map +1 -0
- package/dist/tot-floor.js +116 -0
- package/dist/tot-floor.js.map +1 -0
- package/dist/types.d.ts +18 -2
- package/dist/types.d.ts.map +1 -1
- package/dist/verifier.d.ts.map +1 -1
- package/dist/verifier.js +10 -2
- package/dist/verifier.js.map +1 -1
- package/package.json +1 -1
- package/src/__tests__/base-schedule.test.ts +62 -0
- package/src/__tests__/measurements-windows.test.ts +5 -1
- package/src/__tests__/tot-floor.test.ts +256 -0
- package/src/ceiling.ts +25 -8
- package/src/fuse.ts +35 -0
- package/src/index.ts +8 -1
- package/src/measurements.ts +3 -3
- package/src/tot-floor.ts +187 -0
- package/src/types.ts +19 -2
- package/src/verifier.ts +9 -2
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
// Copyright (c) 2024-2026 Argento Computing Inc. Licensed under the MIT License. See LICENSE.
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* A TOT's public floor (bitgraph/slot/2): the record's form and signature, and the rule that looks the block up.
|
|
5
|
+
*
|
|
6
|
+
* The receipts are signed here with a throwaway key. The block is a real one, Base 66088566, as a node returned its
|
|
7
|
+
* header: its hash can be rebuilt from its fields, so the test needs no network.
|
|
8
|
+
*
|
|
9
|
+
* cd packages/verify && npx tsc -p . && node --test dist/__tests__/tot-floor.test.js
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import { test } from "node:test";
|
|
13
|
+
import { strict as assert } from "node:assert";
|
|
14
|
+
import { getPublicKeyAsync, signAsync } from "@noble/ed25519";
|
|
15
|
+
import { sha256 } from "@noble/hashes/sha256";
|
|
16
|
+
import { canonicalize } from "../canonical.js";
|
|
17
|
+
import { verifyProofIntegrity, createVerificationContext } from "../verifier.js";
|
|
18
|
+
import { canonicalSlotBody, computeSlotRecordHash, slotFloorProblem, bytesToBase64 } from "../fuse.js";
|
|
19
|
+
import { buildSignedBody } from "../proof-hash.js";
|
|
20
|
+
import { totFloorOf, floorHashHex, checkFloorAgainstBlock, checkFloorOnline, FLOOR_NETWORKS } from "../tot-floor.js";
|
|
21
|
+
import { headerRlpFromRpc, type RpcBlockHeader } from "../ceiling.js";
|
|
22
|
+
import { bytesToHex, keccak256, rlpDecode, rlpEncode } from "../ceiling-evm.js";
|
|
23
|
+
import type { BitGraphProof, SlotAllocation, SlotFloor } from "../types.js";
|
|
24
|
+
|
|
25
|
+
/** Base block 66088566, header fields as https://mainnet.base.org returned them on 2026-12-04. */
|
|
26
|
+
const BLOCK: Record<string, string> = {"hash":"0xbd6130b95b52264e50e2fb1e74f0168c89191c202ae0ce15a62414c15e4d01d6","parentHash":"0xac9581aa1851c9f1368cea45ec5451a15fd3348287cdf39f383bfaf9a756167d","sha3Uncles":"0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347","miner":"0x4200000000000000000000000000000000000011","stateRoot":"0x55846798a21048527efd37d499ec491057e5a7d047e45c9ff6f5deff31c8abc3","transactionsRoot":"0x420b8c61001dd68559adec79ae5df4f76be3d1100bbd25225782d19f550249b2","receiptsRoot":"0x380c536b34ed35c580ebcc4c3266bb2755ee1ac5e4c6f5cfcec5dfe36901d7fb","logsBloom":"0x800204a80a02110800841000020800200568000c63041100420780090900340130400c000f09c048c106821020029087a040050c10003100021884020034242040000300040e810a1000041a010d808601101805308c108030500113a0041010049200002282000006254002820028000c0044284806a0008000021a000801401001000108040100808002035a000500212281c100208806544da000010002800202000090008080061000cc0011c000701c240200032408021210a0410041a30c2460060080107101100a20212940080284902010185910440200010a007024801098404704000a66040041c2400201000220180041804080220802620a0109","difficulty":"0x0","number":"0x3f06e76","gasLimit":"0x2625a00","gasUsed":"0x47aef9","timestamp":"0x6b130318","timestampMs":"0x1a2424418e0","extraData":"0x01000003e80000000500000000004c4b40","mixHash":"0xb782c05be76a203d41b7e3a6e6769f6e14d8395a823d75380b07a83f4bdcf059","nonce":"0x0000000000000000","baseFeePerGas":"0x93e206","withdrawalsRoot":"0x0628322cfb77711bb4d904ceaf62715a09971a80b87cc560436c0e729abb1b3c","blobGasUsed":"0x954d0","excessBlobGas":"0x0","parentBeaconBlockRoot":"0x2d530eab54966f8bcbd0a2f37d5f30fee307bc2942ecd8c09c8fe55e0f797aee","requestsHash":"0xe3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"};
|
|
27
|
+
const BLOCK_NUMBER = "66088566";
|
|
28
|
+
const BLOCK_HASH_B64 = Buffer.from(BLOCK["hash"]!.slice(2), "hex").toString("base64");
|
|
29
|
+
const BLOCK_TIME_MS = 1796408088800;
|
|
30
|
+
const FLOOR: SlotFloor = { network: "eip155:8453", number: BLOCK_NUMBER, hashB64: BLOCK_HASH_B64 };
|
|
31
|
+
|
|
32
|
+
const KEY = sha256(new TextEncoder().encode("tot-floor test key"));
|
|
33
|
+
const b64 = (u: Uint8Array) => bytesToBase64(u);
|
|
34
|
+
const fill = (n: number) => b64(new Uint8Array(32).fill(n));
|
|
35
|
+
|
|
36
|
+
/** A receipt as the enclave makes one, signed with the test key: slot/2 with the floor, or slot/1 without. */
|
|
37
|
+
async function receipt(floor?: SlotFloor): Promise<BitGraphProof> {
|
|
38
|
+
const publicKeyB64 = b64(await getPublicKeyAsync(KEY));
|
|
39
|
+
const body = {
|
|
40
|
+
version: floor ? "bitgraph/slot/2" : "bitgraph/slot/1",
|
|
41
|
+
nonceB64: fill(7), counter: "41", epochId: fill(9), publicKeyB64, chainId: "bitgraph:main",
|
|
42
|
+
...(floor ? { floor } : {}),
|
|
43
|
+
};
|
|
44
|
+
const slotCanonical = canonicalize(body as unknown as Record<string, unknown>);
|
|
45
|
+
const slot = { ...body, signatureB64: b64(await signAsync(slotCanonical, KEY)) } as unknown as SlotAllocation;
|
|
46
|
+
const proof = {
|
|
47
|
+
version: "bitgraph/1",
|
|
48
|
+
artifact: { hashAlg: "sha256", digestB64: fill(3) },
|
|
49
|
+
commit: { nonceB64: fill(7), counter: "42", slotCounter: "41", slotHashB64: b64(sha256(slotCanonical)), epochId: fill(9), atomic: true, chainId: "bitgraph:main" },
|
|
50
|
+
signer: { publicKeyB64, signatureB64: "" },
|
|
51
|
+
environment: { enforcement: "measured-tee", measurement: "ab".repeat(48), attestation: { format: "aws-nitro", reportB64: "AA==" } },
|
|
52
|
+
slotAllocation: slot,
|
|
53
|
+
} as unknown as BitGraphProof;
|
|
54
|
+
proof.signer.signatureB64 = b64(await signAsync(canonicalize(buildSignedBody(proof)), KEY));
|
|
55
|
+
return proof;
|
|
56
|
+
}
|
|
57
|
+
const integrity = (proof: BitGraphProof) => verifyProofIntegrity({ proof, trustAnchors: { requireAtomic: true }, context: createVerificationContext() });
|
|
58
|
+
const withSlot = (p: BitGraphProof, change: Record<string, unknown>): BitGraphProof => ({ ...p, slotAllocation: { ...p.slotAllocation!, ...change } as SlotAllocation });
|
|
59
|
+
|
|
60
|
+
test("a slot/2 TOT with its floor verifies, and the spend holds the hash of the body with the floor", async () => {
|
|
61
|
+
const p = await receipt(FLOOR);
|
|
62
|
+
const r = await integrity(p);
|
|
63
|
+
assert.equal(r.valid, true, r.reason);
|
|
64
|
+
assert.deepEqual(Object.keys(canonicalSlotBody(p.slotAllocation!)).sort(), ["chainId", "counter", "epochId", "floor", "nonceB64", "publicKeyB64", "version"]);
|
|
65
|
+
assert.equal(b64(computeSlotRecordHash(p.slotAllocation!)), p.commit.slotHashB64);
|
|
66
|
+
assert.deepEqual(totFloorOf(p), FLOOR);
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
test("a slot/1 TOT still verifies and carries no floor", async () => {
|
|
70
|
+
const p = await receipt();
|
|
71
|
+
assert.equal((await integrity(p)).valid, true);
|
|
72
|
+
assert.deepEqual(Object.keys(canonicalSlotBody(p.slotAllocation!)).sort(), ["chainId", "counter", "epochId", "nonceB64", "publicKeyB64", "version"]);
|
|
73
|
+
assert.equal(totFloorOf(p), null);
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
test("the floor is under the TOT's signature: any change to it breaks the receipt", async () => {
|
|
77
|
+
const p = await receipt(FLOOR);
|
|
78
|
+
for (const floor of [{ ...FLOOR, number: "66088567" }, { ...FLOOR, hashB64: fill(1) }, { ...FLOOR, network: "eip155:1" }]) {
|
|
79
|
+
const r = await integrity(withSlot(p, { floor }));
|
|
80
|
+
assert.equal(r.valid, false);
|
|
81
|
+
assert.match(String(r.reason), /signature verification failed/);
|
|
82
|
+
}
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
test("a floor on a slot/1 TOT is not signed, so the receipt is refused and no floor is read from it", async () => {
|
|
86
|
+
const p = withSlot(await receipt(), { floor: FLOOR });
|
|
87
|
+
const r = await integrity(p);
|
|
88
|
+
assert.equal(r.valid, false);
|
|
89
|
+
assert.match(String(r.reason), /only signed in a bitgraph\/slot\/2 record/);
|
|
90
|
+
assert.equal(totFloorOf(p), null);
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
test("a slot/2 TOT must carry one floor, in form", async () => {
|
|
94
|
+
const p = await receipt(FLOOR);
|
|
95
|
+
const { floor: _f, ...noFloor } = p.slotAllocation as unknown as Record<string, unknown>;
|
|
96
|
+
assert.match(String((await integrity({ ...p, slotAllocation: noFloor as unknown as SlotAllocation })).reason), /must carry a floor/);
|
|
97
|
+
const bad: Array<[unknown, RegExp]> = [
|
|
98
|
+
[null, /must carry a floor/],
|
|
99
|
+
["eip155:8453", /must carry a floor/],
|
|
100
|
+
[{ ...FLOOR, extra: "x" }, /exactly network, number and hashB64/],
|
|
101
|
+
[{ network: FLOOR.network, number: FLOOR.number }, /exactly network, number and hashB64/],
|
|
102
|
+
[{ ...FLOOR, network: "Base" }, /CAIP-2/],
|
|
103
|
+
[{ ...FLOOR, number: "066088566" }, /decimal string/],
|
|
104
|
+
[{ ...FLOOR, number: 66088566 }, /decimal string/],
|
|
105
|
+
[{ ...FLOOR, hashB64: b64(new Uint8Array(31)) }, /32 bytes/],
|
|
106
|
+
[{ ...FLOOR, hashB64: BLOCK_HASH_B64.replace(/=$/, "") }, /32 bytes/],
|
|
107
|
+
];
|
|
108
|
+
for (const [floor, why] of bad) {
|
|
109
|
+
assert.match(String(slotFloorProblem({ version: "bitgraph/slot/2", floor })), why);
|
|
110
|
+
const r = await integrity(withSlot(p, { floor }));
|
|
111
|
+
assert.equal(r.valid, false, JSON.stringify(floor));
|
|
112
|
+
assert.equal(totFloorOf(withSlot(p, { floor })), null);
|
|
113
|
+
}
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
test("the chain holds the block: confirmed, with the block's time and the gap to the attestation", async () => {
|
|
117
|
+
const p = await receipt(FLOOR);
|
|
118
|
+
const c = checkFloorAgainstBlock({ proof: p, block: BLOCK, attestedAtMs: BLOCK_TIME_MS + 412 });
|
|
119
|
+
assert.equal(c.status, "confirmed");
|
|
120
|
+
assert.equal(c.hashHex, BLOCK["hash"]);
|
|
121
|
+
assert.equal(c.networkName, "Base");
|
|
122
|
+
assert.equal(c.blockUrl, "https://basescan.org/block/66088566");
|
|
123
|
+
assert.equal(c.blockTimeMs, BLOCK_TIME_MS);
|
|
124
|
+
assert.equal(c.timeBoundByHash, true, "the header rebuilds to the floor's hash, so its second is covered by it");
|
|
125
|
+
assert.equal(c.gapMs, 412);
|
|
126
|
+
assert.equal(c.stale, undefined, "no limit was given, so nothing is flagged");
|
|
127
|
+
assert.equal(floorHashHex(FLOOR), BLOCK["hash"]);
|
|
128
|
+
assert.equal(FLOOR_NETWORKS["eip155:8453"]!.name, "Base");
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
test("two clocks: a block stamped after the attestation gives a negative gap, and that is not a failure", async () => {
|
|
132
|
+
const c = checkFloorAgainstBlock({ proof: await receipt(FLOOR), block: BLOCK, attestedAtMs: BLOCK_TIME_MS - 300, maxGapMs: 2000 });
|
|
133
|
+
assert.equal(c.status, "confirmed");
|
|
134
|
+
assert.equal(c.gapMs, -300);
|
|
135
|
+
assert.equal(c.stale, false);
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
test("a stale floor is flagged when a limit is given, and stays confirmed", async () => {
|
|
139
|
+
const p = await receipt(FLOOR);
|
|
140
|
+
const stale = checkFloorAgainstBlock({ proof: p, block: BLOCK, attestedAtMs: BLOCK_TIME_MS + 90_000, maxGapMs: 2000 });
|
|
141
|
+
assert.equal(stale.status, "confirmed");
|
|
142
|
+
assert.equal(stale.gapMs, 90_000);
|
|
143
|
+
assert.equal(stale.stale, true);
|
|
144
|
+
assert.equal(checkFloorAgainstBlock({ proof: p, block: BLOCK, attestedAtMs: BLOCK_TIME_MS + 2000, maxGapMs: 2000 }).stale, false);
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
test("a finer time is used only inside the header's own second; without one the second stands", async () => {
|
|
148
|
+
const p = await receipt(FLOOR);
|
|
149
|
+
assert.equal(checkFloorAgainstBlock({ proof: p, block: { ...BLOCK, timestampMs: "0x1" } }).blockTimeMs, 1796408088000);
|
|
150
|
+
const { timestampMs: _ms, ...seconds } = BLOCK;
|
|
151
|
+
assert.equal(checkFloorAgainstBlock({ proof: p, block: seconds }).blockTimeMs, 1796408088000);
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
test("the chain does not hold the block: not confirmed, and it says why", async () => {
|
|
155
|
+
const p = await receipt(FLOOR);
|
|
156
|
+
const unknown = checkFloorAgainstBlock({ proof: p, block: null, attestedAtMs: 5 });
|
|
157
|
+
assert.equal(unknown.status, "not-confirmed");
|
|
158
|
+
assert.match(String(unknown.detail), /has no block 66088566/);
|
|
159
|
+
const other = checkFloorAgainstBlock({ proof: p, block: { ...BLOCK, hash: "0x" + "11".repeat(32) } });
|
|
160
|
+
assert.equal(other.status, "not-confirmed");
|
|
161
|
+
assert.match(String(other.detail), /another hash/);
|
|
162
|
+
const wrong = checkFloorAgainstBlock({ proof: p, block: { ...BLOCK, number: "0x1" } });
|
|
163
|
+
assert.equal(wrong.status, "not-confirmed");
|
|
164
|
+
// A floor naming a block that is not this one.
|
|
165
|
+
const fake = await receipt({ ...FLOOR, hashB64: fill(5) });
|
|
166
|
+
assert.equal(checkFloorAgainstBlock({ proof: fake, block: BLOCK }).status, "not-confirmed");
|
|
167
|
+
assert.equal(unknown.blockTimeMs, undefined, "an unconfirmed floor reports no time");
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
test("a header whose fields do not rebuild to the hash is confirmed by the hash alone; its time is the node's word", async () => {
|
|
171
|
+
const c = checkFloorAgainstBlock({ proof: await receipt(FLOOR), block: { ...BLOCK, gasUsed: "0x1" } });
|
|
172
|
+
assert.equal(c.status, "confirmed");
|
|
173
|
+
assert.equal(c.timeBoundByHash, false);
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
test("no floor, nothing to check", async () => {
|
|
177
|
+
assert.deepEqual(checkFloorAgainstBlock({ proof: await receipt(), block: BLOCK }), { status: "none" });
|
|
178
|
+
assert.deepEqual(await checkFloorOnline({ proof: await receipt(), fetcher: (async () => { throw new Error("must not be called"); }) as unknown as typeof fetch }), { status: "none" });
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
test("the online step asks for the block by number and applies the same rule", async () => {
|
|
182
|
+
const p = await receipt(FLOOR);
|
|
183
|
+
const asked: unknown[] = [];
|
|
184
|
+
const node = (result: unknown, status = 200) => (async (_url: string, init: { body: string }) => {
|
|
185
|
+
asked.push(JSON.parse(init.body));
|
|
186
|
+
return new Response(JSON.stringify({ jsonrpc: "2.0", id: 1, result }), { status });
|
|
187
|
+
}) as unknown as typeof fetch;
|
|
188
|
+
const ok = await checkFloorOnline({ proof: p, attestedAtMs: BLOCK_TIME_MS + 100, maxGapMs: 2000, fetcher: node(BLOCK) });
|
|
189
|
+
assert.equal(ok.status, "confirmed");
|
|
190
|
+
assert.equal(ok.gapMs, 100);
|
|
191
|
+
assert.deepEqual((asked[0] as { method: string; params: unknown[] }).params, ["0x3f06e76", false]);
|
|
192
|
+
assert.equal((await checkFloorOnline({ proof: p, fetcher: node(null) })).status, "not-confirmed");
|
|
193
|
+
// A lookup that cannot be made is not a verdict on the floor.
|
|
194
|
+
assert.equal((await checkFloorOnline({ proof: p, fetcher: node(null, 502) })).status, "not-checked");
|
|
195
|
+
assert.equal((await checkFloorOnline({ proof: p, fetcher: (async () => { throw new Error("offline"); }) as unknown as typeof fetch })).status, "not-checked");
|
|
196
|
+
const elsewhere = await checkFloorOnline({ proof: await receipt({ ...FLOOR, network: "eip155:1" }), fetcher: node(BLOCK) });
|
|
197
|
+
assert.equal(elsewhere.status, "not-checked");
|
|
198
|
+
assert.match(String(elsewhere.detail), /does not know the network/);
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
test("every answer says where its block came from: a lookup, or a block the caller supplied", async () => {
|
|
202
|
+
const p = await receipt(FLOOR);
|
|
203
|
+
const node = (result: unknown) => (async () => new Response(JSON.stringify({ jsonrpc: "2.0", id: 1, result }))) as unknown as typeof fetch;
|
|
204
|
+
assert.equal((await checkFloorOnline({ proof: p, fetcher: node(BLOCK) })).source, "lookup");
|
|
205
|
+
assert.equal((await checkFloorOnline({ proof: p, fetcher: node(null) })).source, "lookup");
|
|
206
|
+
assert.equal(checkFloorAgainstBlock({ proof: p, block: BLOCK }).source, "supplied");
|
|
207
|
+
assert.equal(checkFloorAgainstBlock({ proof: p, block: null }).source, "supplied");
|
|
208
|
+
assert.equal(checkFloorAgainstBlock({ proof: p, block: { ...BLOCK, hash: "0x" + "11".repeat(32) } }).source, "supplied");
|
|
209
|
+
// No block was considered: nothing to name.
|
|
210
|
+
assert.equal((await checkFloorOnline({ proof: p, fetcher: (async () => { throw new Error("offline"); }) as unknown as typeof fetch })).source, undefined);
|
|
211
|
+
assert.equal(checkFloorAgainstBlock({ proof: await receipt(), block: BLOCK }).source, undefined);
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
test("a made-up header matches its own hash, so a supplied block is never a lookup; the chain refutes it", async () => {
|
|
215
|
+
// A header nobody mined: the real one with a number Base has not reached and a time to suit. It is
|
|
216
|
+
// self-consistent, so its fields rebuild to its hash exactly as a real header's do.
|
|
217
|
+
const real = rlpDecode(headerRlpFromRpc(BLOCK as unknown as RpcBlockHeader, BLOCK["hash"]!)!) as Uint8Array[];
|
|
218
|
+
const number = "0x3b9aca00"; // 1,000,000,000
|
|
219
|
+
const timestamp = "0x77359400"; // 2033
|
|
220
|
+
const be = (hex: string) => new Uint8Array(Buffer.from(hex.slice(2), "hex"));
|
|
221
|
+
const forged = [...real];
|
|
222
|
+
forged[8] = be(number);
|
|
223
|
+
forged[11] = be(timestamp);
|
|
224
|
+
const hash = bytesToHex(keccak256(rlpEncode(forged)));
|
|
225
|
+
const { timestampMs: _ms, ...rest } = BLOCK;
|
|
226
|
+
const madeUp = { ...rest, number, timestamp, hash };
|
|
227
|
+
const p = await receipt({ network: "eip155:8453", number: "1000000000", hashB64: Buffer.from(hash.slice(2), "hex").toString("base64") });
|
|
228
|
+
assert.equal((await integrity(p)).valid, true, "a floor is signed for its form, not for its block: the receipt itself is in order");
|
|
229
|
+
|
|
230
|
+
// Handed in by the caller, it matches itself. The answer says so and claims no lookup.
|
|
231
|
+
const supplied = checkFloorAgainstBlock({ proof: p, block: madeUp });
|
|
232
|
+
assert.equal(supplied.status, "confirmed");
|
|
233
|
+
assert.equal(supplied.timeBoundByHash, true);
|
|
234
|
+
assert.equal(supplied.source, "supplied");
|
|
235
|
+
|
|
236
|
+
// Asked of the chain, there is no such block.
|
|
237
|
+
const node = (result: unknown) => (async () => new Response(JSON.stringify({ jsonrpc: "2.0", id: 1, result }))) as unknown as typeof fetch;
|
|
238
|
+
const looked = await checkFloorOnline({ proof: p, fetcher: node(null) });
|
|
239
|
+
assert.equal(looked.status, "not-confirmed");
|
|
240
|
+
assert.equal(looked.source, "lookup");
|
|
241
|
+
});
|
|
242
|
+
|
|
243
|
+
test("a network this checker does not know is never judged, whatever block is handed in", async () => {
|
|
244
|
+
const p = await receipt({ ...FLOOR, network: "eip155:1" });
|
|
245
|
+
for (const block of [null, BLOCK]) {
|
|
246
|
+
const c = checkFloorAgainstBlock({ proof: p, block });
|
|
247
|
+
assert.equal(c.status, "not-checked");
|
|
248
|
+
assert.match(String(c.detail), /does not know the network eip155:1/);
|
|
249
|
+
assert.equal(c.source, undefined);
|
|
250
|
+
}
|
|
251
|
+
// Naming a node does not teach the checker a network: it cannot tell which chain the node serves.
|
|
252
|
+
let asked = 0;
|
|
253
|
+
const node = (async () => { asked++; return new Response(JSON.stringify({ jsonrpc: "2.0", id: 1, result: BLOCK })); }) as unknown as typeof fetch;
|
|
254
|
+
assert.equal((await checkFloorOnline({ proof: p, rpcUrl: "https://node.example", fetcher: node })).status, "not-checked");
|
|
255
|
+
assert.equal(asked, 0);
|
|
256
|
+
});
|
package/src/ceiling.ts
CHANGED
|
@@ -387,16 +387,25 @@ export { keccak256 as ceilingKeccak256 };
|
|
|
387
387
|
// ── The floor block's header ───────────────────────────────────────────────
|
|
388
388
|
|
|
389
389
|
/**
|
|
390
|
-
* Base
|
|
391
|
-
* BASE_MAINNET_GENESIS_TIME + BASE_BLOCK_TIME_SECONDS * n
|
|
392
|
-
*
|
|
393
|
-
*
|
|
394
|
-
*
|
|
390
|
+
* The Base floor of SPEC-v2 (enclave v10 to v12, proofs that sign `commit.slotFloor`). That format fixes a
|
|
391
|
+
* floor block's time by its height: block n is BASE_MAINNET_GENESIS_TIME + BASE_BLOCK_TIME_SECONDS * n. The
|
|
392
|
+
* enclave refused a floor header off this schedule and a verifier of that format refuses one too (SPEC-v2,
|
|
393
|
+
* "What a verifier checks"), so the rule stays exactly as that format states it.
|
|
394
|
+
*
|
|
395
|
+
* It describes Base as it ran while that format was in use, not Base today. Base kept the two-second schedule
|
|
396
|
+
* through block BASE_TWO_SECOND_SCHEDULE_LAST_BLOCK (stamped 2026-11-05T18:00:01Z) and has made a block every
|
|
397
|
+
* 200 ms since (read from Base mainnet, 2026-12-04). A later block is therefore never on this schedule and can
|
|
398
|
+
* not be a floor of this format; the spec says of that day that the check "fails closed". No proof of this
|
|
399
|
+
* format names a later block: the enclaves that signed these floors refused them.
|
|
400
|
+
*
|
|
401
|
+
* A TOT's floor (bitgraph/slot/2, tot-floor.ts) has no schedule rule and does not use this.
|
|
395
402
|
*/
|
|
396
403
|
export const BASE_MAINNET_GENESIS_TIME = 1686789347;
|
|
397
404
|
export const BASE_BLOCK_TIME_SECONDS = 2;
|
|
405
|
+
/** The last Base mainnet block stamped on the two-second schedule. */
|
|
406
|
+
export const BASE_TWO_SECOND_SCHEDULE_LAST_BLOCK = 53556127;
|
|
398
407
|
|
|
399
|
-
/** True when a block's stamp is
|
|
408
|
+
/** True when a block's stamp is the two-second schedule's for its number: the SPEC-v2 floor rule. */
|
|
400
409
|
export function onBaseSchedule(blockNumber: number, timestampSec: number): boolean {
|
|
401
410
|
return timestampSec === BASE_MAINNET_GENESIS_TIME + BASE_BLOCK_TIME_SECONDS * blockNumber;
|
|
402
411
|
}
|
|
@@ -407,7 +416,7 @@ const chainName = (c: "ethereum" | "base") => (c === "base" ? "Base" : "Ethereum
|
|
|
407
416
|
* Check a floor header against the floor the proof signs (signedFloorOf). The
|
|
408
417
|
* header must decode, hash (keccak-256) to the signed block hash and carry the
|
|
409
418
|
* signed number. For a Base floor its time must also equal the signed
|
|
410
|
-
* blockTimestamp and be
|
|
419
|
+
* blockTimestamp and be on the two-second schedule for its number (above). When the
|
|
411
420
|
* carrier of the header names a chain (`declaredChain`, absent = Ethereum), it
|
|
412
421
|
* must be the signed floor's chain: a Base floor read as an Ethereum header,
|
|
413
422
|
* or the reverse, fails here even before any hash is compared.
|
|
@@ -431,7 +440,15 @@ export function checkFloorHeader(
|
|
|
431
440
|
if (h.number !== signed.blockNumber) return { ok: false, reason: `the header is block ${h.number}, the proof signs block ${signed.blockNumber}` };
|
|
432
441
|
if (signed.chain === "base") {
|
|
433
442
|
if (h.timestamp !== signed.blockTimestamp) return { ok: false, reason: `the header's time is ${h.timestamp}, the proof signs ${signed.blockTimestamp}` };
|
|
434
|
-
if (!onBaseSchedule(h.number, h.timestamp))
|
|
443
|
+
if (!onBaseSchedule(h.number, h.timestamp)) {
|
|
444
|
+
const want = BASE_MAINNET_GENESIS_TIME + BASE_BLOCK_TIME_SECONDS * h.number;
|
|
445
|
+
return {
|
|
446
|
+
ok: false,
|
|
447
|
+
reason: h.number > BASE_TWO_SECOND_SCHEDULE_LAST_BLOCK
|
|
448
|
+
? `Base block ${h.number} is stamped ${h.timestamp}, off the two-second schedule this proof format requires (${want}); Base left that schedule after block ${BASE_TWO_SECOND_SCHEDULE_LAST_BLOCK}, and no proof of this format names a later block`
|
|
449
|
+
: `Base block ${h.number} is stamped ${h.timestamp}, off Base mainnet's schedule (${want})`,
|
|
450
|
+
};
|
|
451
|
+
}
|
|
435
452
|
}
|
|
436
453
|
return { ok: true, header: h };
|
|
437
454
|
}
|
package/src/fuse.ts
CHANGED
|
@@ -154,9 +154,44 @@ export function canonicalSlotBody(slot: SlotAllocation): Record<string, unknown>
|
|
|
154
154
|
epochId: slot.epochId,
|
|
155
155
|
publicKeyB64: slot.publicKeyB64,
|
|
156
156
|
...(slot.chainId ? { chainId: slot.chainId } : {}),
|
|
157
|
+
// bitgraph/slot/2: the floor is part of what the enclave signed. Rebuilt field by field, so nothing else that
|
|
158
|
+
// sits in the object is carried into the signed bytes.
|
|
159
|
+
...(slot.version === SLOT_VERSION_2 && slot.floor
|
|
160
|
+
? { floor: { network: slot.floor.network, number: slot.floor.number, hashB64: slot.floor.hashB64 } }
|
|
161
|
+
: {}),
|
|
157
162
|
};
|
|
158
163
|
}
|
|
159
164
|
|
|
165
|
+
/** The slot record version that carries a floor. */
|
|
166
|
+
export const SLOT_VERSION_2 = "bitgraph/slot/2";
|
|
167
|
+
/** Base mainnet, as a CAIP-2 id: the floor network the enclave accepts. */
|
|
168
|
+
export const FLOOR_NETWORK_BASE = "eip155:8453";
|
|
169
|
+
|
|
170
|
+
const FLOOR_NETWORK = /^[-a-z0-9]{3,8}:[-_a-zA-Z0-9]{1,32}$/;
|
|
171
|
+
const FLOOR_NUMBER = /^(0|[1-9][0-9]{0,19})$/;
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* Why a slot record's floor is not in form, or null when it is. A bitgraph/slot/2 record carries exactly one floor:
|
|
175
|
+
* { network, number, hashB64 } and nothing else, the network a CAIP-2 id, the number a decimal string of at most 20
|
|
176
|
+
* digits, the hash 32 bytes in canonical base64. A bitgraph/slot/1 record carries none: a floor on one is not signed,
|
|
177
|
+
* so it is refused, never shown.
|
|
178
|
+
*/
|
|
179
|
+
export function slotFloorProblem(slot: { version?: unknown; floor?: unknown }): string | null {
|
|
180
|
+
const f = slot.floor;
|
|
181
|
+
if (slot.version !== SLOT_VERSION_2) {
|
|
182
|
+
return f === undefined ? null : "slotAllocation.floor is only signed in a bitgraph/slot/2 record; this record is not one";
|
|
183
|
+
}
|
|
184
|
+
if (f === null || typeof f !== "object" || Array.isArray(f)) return "a bitgraph/slot/2 record must carry a floor object";
|
|
185
|
+
const o = f as Record<string, unknown>;
|
|
186
|
+
const keys = Object.keys(o).sort().join(",");
|
|
187
|
+
if (keys !== "hashB64,network,number") return "slotAllocation.floor must hold exactly network, number and hashB64";
|
|
188
|
+
if (typeof o["network"] !== "string" || !FLOOR_NETWORK.test(o["network"])) return "slotAllocation.floor.network must be a CAIP-2 chain id";
|
|
189
|
+
if (typeof o["number"] !== "string" || !FLOOR_NUMBER.test(o["number"])) return "slotAllocation.floor.number must be a decimal string";
|
|
190
|
+
const h = typeof o["hashB64"] === "string" ? base64ToBytes(o["hashB64"]) : null;
|
|
191
|
+
if (h === null || h.length !== 32 || bytesToBase64(h) !== o["hashB64"]) return "slotAllocation.floor.hashB64 must be 32 bytes in canonical base64";
|
|
192
|
+
return null;
|
|
193
|
+
}
|
|
194
|
+
|
|
160
195
|
/** SHA-256 of the canonical slot body; equals the proof's commit.slotHashB64. */
|
|
161
196
|
export function computeSlotRecordHash(slot: SlotAllocation): Uint8Array {
|
|
162
197
|
return sha256(canonicalize(canonicalSlotBody(slot) as unknown as BitGraphProof));
|
package/src/index.ts
CHANGED
|
@@ -26,6 +26,7 @@ export type {
|
|
|
26
26
|
Attribution,
|
|
27
27
|
PolicyBinding,
|
|
28
28
|
SlotAllocation,
|
|
29
|
+
SlotFloor,
|
|
29
30
|
ActorIdentity,
|
|
30
31
|
AuthorizationPayload,
|
|
31
32
|
WebAuthnAuthorization,
|
|
@@ -46,6 +47,9 @@ export { verifyAttested } from "./attested.js";
|
|
|
46
47
|
// A file of a tree, checked by its own hash (bitgraph-tree-member/1, 2026-10-20). verifyAttested applies it.
|
|
47
48
|
export { TREE_MEMBER_METADATA_KEY, treeMemberOf, checkTreeMember, treeRootFromPath, digestTreeRootText, parseDigestTreeRootText, treeRootTextBytes, type TreeMemberBlock } from "./tree-member.js";
|
|
48
49
|
export type { AttestedOptions, AttestedResult } from "./attested.js";
|
|
50
|
+
/* A TOT's public floor (bitgraph/slot/2): the signed floor, and the optional step that looks its block up. */
|
|
51
|
+
export { FLOOR_NETWORKS, totFloorOf, floorHashHex, checkFloorAgainstBlock, checkFloorOnline } from "./tot-floor.js";
|
|
52
|
+
export type { FloorCheck } from "./tot-floor.js";
|
|
49
53
|
export type { StretchResult } from "./stretch.js";
|
|
50
54
|
|
|
51
55
|
// BitGraph Fuse (profile bitgraph-fuse/1): construction, parsing, verification.
|
|
@@ -65,6 +69,9 @@ export {
|
|
|
65
69
|
parseSetManifest,
|
|
66
70
|
readSetMetadata,
|
|
67
71
|
canonicalSlotBody,
|
|
72
|
+
SLOT_VERSION_2,
|
|
73
|
+
FLOOR_NETWORK_BASE,
|
|
74
|
+
slotFloorProblem,
|
|
68
75
|
computeSlotRecordHash,
|
|
69
76
|
slotCommitmentPreimage,
|
|
70
77
|
computeSlotCommitment,
|
|
@@ -201,7 +208,7 @@ export type { OutputRootSettlement, OutputRootCheck, OutputRootVerifyResult, Out
|
|
|
201
208
|
export { EXPORT_FORMAT, parseExport, verifyExport, buildExport } from "./export.js";
|
|
202
209
|
export { baseTimeIsBound, BASE_STAMP_TOLERANCE_SECONDS } from "./ceiling.js";
|
|
203
210
|
/* The floor block's header, on either chain (enclave v10 floors are Base blocks). */
|
|
204
|
-
export { checkFloorHeader, floorTimeIsBound, onBaseSchedule, headerRlpFromRpc, BASE_MAINNET_GENESIS_TIME, BASE_BLOCK_TIME_SECONDS } from "./ceiling.js";
|
|
211
|
+
export { checkFloorHeader, floorTimeIsBound, onBaseSchedule, headerRlpFromRpc, BASE_MAINNET_GENESIS_TIME, BASE_BLOCK_TIME_SECONDS, BASE_TWO_SECOND_SCHEDULE_LAST_BLOCK } from "./ceiling.js";
|
|
205
212
|
export type { RpcBlockHeader } from "./ceiling.js";
|
|
206
213
|
/* The enclave images BitGraph has published: the default measurement policy (SPEC section 16). */
|
|
207
214
|
export { PUBLISHED_ENCLAVE_MEASUREMENTS, PUBLISHED_PCR0S, publishedMeasurement, imageIsAtomic, imageRetiredAfterMs, IMAGE_RETIREMENT_GRACE_DAYS, PUBLISHED_WRITERS, isPublishedWriter } from "./measurements.js";
|
package/src/measurements.ts
CHANGED
|
@@ -39,6 +39,7 @@ export const PUBLISHED_ENCLAVE_MEASUREMENTS: readonly PublishedMeasurement[] = O
|
|
|
39
39
|
{ version: "v11", since: "2026-10-17", pcr0: "d9128d67c406a30f988f74ece8b5cced5c9a96491289037f50bddb5f10aac2bc9d1d315dd7bf65428899ccde72b95b86" },
|
|
40
40
|
{ version: "v12", since: "2026-10-18", pcr0: "cf1ca497e1c3f678cce99ec2215f3991c72fd1b54f083a1f58563e88f5fe742e5190c7ebaf27f3aa98622605687e27c9" },
|
|
41
41
|
{ version: "v13", since: "2026-10-19", pcr0: "adaad0bef62292d35197ae8db19e40685229f1a1e6070a7c77a148c4ada88ee66a1ca50819eebc2017c6716a49f2692f" },
|
|
42
|
+
{ version: "v14", since: "2026-12-04", pcr0: "10273c68951dd81156364b4b86d240208f37a1999dac27d54ebf3fc7a2fc723a1e2e852b91528d0a24aaf0544af6c52f" },
|
|
42
43
|
].map((m) => Object.freeze(m)));
|
|
43
44
|
|
|
44
45
|
/** PUBLISHED_ENCLAVE_MEASUREMENTS as the bare PCR0 list a verifier's pins take. */
|
|
@@ -53,10 +54,9 @@ export function publishedMeasurement(pcr0: unknown): PublishedMeasurement | null
|
|
|
53
54
|
|
|
54
55
|
/** Images from v11 on bind atomically: the nonce at N and the digest at N+1 in one action. Earlier images could hand
|
|
55
56
|
* out a position before its content arrived. */
|
|
56
|
-
const ATOMIC_IMAGES: ReadonlySet<string> = new Set(["v11", "v12", "v13"]);
|
|
57
|
-
|
|
58
57
|
export function imageIsAtomic(version: string): boolean {
|
|
59
|
-
|
|
58
|
+
const m = /^v([0-9]+)$/.exec(version);
|
|
59
|
+
return m !== null && Number(m[1]) >= 11;
|
|
60
60
|
}
|
|
61
61
|
|
|
62
62
|
/**
|
package/src/tot-floor.ts
ADDED
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
// Copyright (c) 2024-2026 Argento Computing Inc. Licensed under the MIT License. See LICENSE.
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* A TOT's public floor (record version bitgraph/slot/2), and the optional online step that looks it up.
|
|
5
|
+
*
|
|
6
|
+
* The floor is a block of a public chain: its network, number and hash, signed into the TOT body with the random
|
|
7
|
+
* value. The host that ran the enclave named it; the enclave has no network and did not check it. So the floor is
|
|
8
|
+
* a claim until someone looks: when that chain holds a block with that number and that hash, the TOT was made after
|
|
9
|
+
* the block existed, because nobody can know a block's hash before the block. That is all a floor shows, and it is
|
|
10
|
+
* a statement about order, not a clock reading of the receipt.
|
|
11
|
+
*
|
|
12
|
+
* Looking is optional and never changes whether a receipt is valid. Three answers:
|
|
13
|
+
*
|
|
14
|
+
* confirmed the chain holds that block. The block's time is reported, with the gap from it to the
|
|
15
|
+
* attestation document's time. The two times come from two clocks that know nothing of each
|
|
16
|
+
* other (the chain's sequencer and AWS's hardware), so the gap can come out negative.
|
|
17
|
+
* not-confirmed the chain holds no block of that number, or one with another hash. The floor shows nothing.
|
|
18
|
+
* The receipt is exactly as valid as one with no floor.
|
|
19
|
+
* not-checked the lookup could not be made (no network, a chain this checker does not know).
|
|
20
|
+
*
|
|
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.
|
|
28
|
+
*/
|
|
29
|
+
|
|
30
|
+
import type { BitGraphProof, SlotFloor } from "./types.js";
|
|
31
|
+
import { base64ToBytes, slotFloorProblem, SLOT_VERSION_2, FLOOR_NETWORK_BASE } from "./fuse.js";
|
|
32
|
+
import { headerRlpFromRpc, type RpcBlockHeader } from "./ceiling.js";
|
|
33
|
+
import { bytesToHex } from "./ceiling-evm.js";
|
|
34
|
+
|
|
35
|
+
/** What each known floor network is called, where its blocks can be read, and where a person can look one up. */
|
|
36
|
+
export const FLOOR_NETWORKS: Readonly<Record<string, { name: string; rpcUrl: string; blockUrl: (number: string) => string }>> = Object.freeze({
|
|
37
|
+
[FLOOR_NETWORK_BASE]: { name: "Base", rpcUrl: "https://mainnet.base.org", blockUrl: (n: string) => `https://basescan.org/block/${n}` },
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
export interface FloorCheck {
|
|
41
|
+
/** "none": the TOT carries no signed floor. */
|
|
42
|
+
status: "none" | "confirmed" | "not-confirmed" | "not-checked";
|
|
43
|
+
floor?: SlotFloor;
|
|
44
|
+
/** The floor's block hash as the chain writes it: 0x and 64 lowercase hex. */
|
|
45
|
+
hashHex?: string;
|
|
46
|
+
/** The network's plain name, when this checker knows it. */
|
|
47
|
+
networkName?: string;
|
|
48
|
+
/** A page where a person can look the block up, when this checker knows the network. */
|
|
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";
|
|
56
|
+
/** Why it is not confirmed, or why it was not checked. */
|
|
57
|
+
detail?: string;
|
|
58
|
+
/** confirmed: the block's own time, milliseconds since 1970. */
|
|
59
|
+
blockTimeMs?: number;
|
|
60
|
+
/**
|
|
61
|
+
* confirmed: whether the block's time is bound by its hash. True when the header the node returned rebuilds to
|
|
62
|
+
* the floor's hash: its whole-second time is then part of what the hash covers, and any finer part is as the
|
|
63
|
+
* node reports. False when the header could not be rebuilt and the time is the node's word alone.
|
|
64
|
+
*/
|
|
65
|
+
timeBoundByHash?: boolean;
|
|
66
|
+
/** The attestation document's time, when the caller gave it. */
|
|
67
|
+
attestedAtMs?: number;
|
|
68
|
+
/** confirmed, with attestedAtMs: the attestation's time minus the block's. Negative when the two clocks disagree. */
|
|
69
|
+
gapMs?: number;
|
|
70
|
+
/** With maxGapMs: whether the gap is larger than it. A flag for the reader; it never makes a receipt invalid. */
|
|
71
|
+
stale?: boolean;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** The floor a receipt's TOT carries under its signature, or null: no floor, not a slot/2 record, or not in form. */
|
|
75
|
+
export function totFloorOf(proof: Pick<BitGraphProof, "slotAllocation"> | null | undefined): SlotFloor | null {
|
|
76
|
+
const slot = proof?.slotAllocation;
|
|
77
|
+
if (!slot || slot.version !== SLOT_VERSION_2 || slotFloorProblem(slot) !== null) return null;
|
|
78
|
+
const f = slot.floor!;
|
|
79
|
+
return { network: f.network, number: f.number, hashB64: f.hashB64 };
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** The floor's hash as 0x hex, or null when it is not 32 bytes. */
|
|
83
|
+
export function floorHashHex(floor: SlotFloor): string | null {
|
|
84
|
+
const b = base64ToBytes(floor.hashB64);
|
|
85
|
+
return b !== null && b.length === 32 ? bytesToHex(b) : null; // bytesToHex writes the 0x itself
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function describe(floor: SlotFloor): Pick<FloorCheck, "floor" | "hashHex" | "networkName" | "blockUrl"> {
|
|
89
|
+
const known = FLOOR_NETWORKS[floor.network];
|
|
90
|
+
const hashHex = floorHashHex(floor);
|
|
91
|
+
return {
|
|
92
|
+
floor,
|
|
93
|
+
...(hashHex ? { hashHex } : {}),
|
|
94
|
+
...(known ? { networkName: known.name, blockUrl: known.blockUrl(floor.number) } : {}),
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
interface RuleArgs {
|
|
99
|
+
proof: Pick<BitGraphProof, "slotAllocation">;
|
|
100
|
+
block: (Partial<RpcBlockHeader> & { timestampMs?: string }) | null;
|
|
101
|
+
attestedAtMs?: number | null;
|
|
102
|
+
maxGapMs?: number;
|
|
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 {
|
|
116
|
+
const floor = totFloorOf(args.proof);
|
|
117
|
+
if (floor === null) return { status: "none" };
|
|
118
|
+
const d = describe(floor);
|
|
119
|
+
const attested = typeof args.attestedAtMs === "number" ? { attestedAtMs: args.attestedAtMs } : {};
|
|
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}` };
|
|
124
|
+
const b = args.block;
|
|
125
|
+
const got = typeof b.hash === "string" ? b.hash.toLowerCase() : "";
|
|
126
|
+
const numberMatches = typeof b.number === "string" && /^0x[0-9a-f]+$/i.test(b.number) && BigInt(b.number).toString(10) === floor.number;
|
|
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` };
|
|
129
|
+
|
|
130
|
+
// The block is there. Its time: the whole seconds are part of the header the hash covers, when the header rebuilds.
|
|
131
|
+
const seconds = typeof b.timestamp === "string" && /^0x[0-9a-f]+$/i.test(b.timestamp) ? Number(BigInt(b.timestamp)) : null;
|
|
132
|
+
if (seconds === null || !Number.isSafeInteger(seconds)) return { status: "confirmed", ...d, ...attested, ...from };
|
|
133
|
+
const timeBoundByHash = headerRlpFromRpc(b as RpcBlockHeader, d.hashHex!) !== null;
|
|
134
|
+
const finer = typeof b.timestampMs === "string" && /^0x[0-9a-f]+$/i.test(b.timestampMs) ? Number(BigInt(b.timestampMs)) : null;
|
|
135
|
+
// A finer time is used only when it falls inside the header's own second.
|
|
136
|
+
const blockTimeMs = finer !== null && Number.isSafeInteger(finer) && Math.floor(finer / 1000) === seconds ? finer : seconds * 1000;
|
|
137
|
+
const out: FloorCheck = { status: "confirmed", ...d, ...attested, ...from, blockTimeMs, timeBoundByHash };
|
|
138
|
+
if (typeof args.attestedAtMs === "number") {
|
|
139
|
+
out.gapMs = args.attestedAtMs - blockTimeMs;
|
|
140
|
+
if (typeof args.maxGapMs === "number") out.stale = out.gapMs > args.maxGapMs;
|
|
141
|
+
}
|
|
142
|
+
return out;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* The online step: ask a node of the floor's network for the block at the floor's number, then apply the rule.
|
|
147
|
+
* A lookup that cannot be made is "not-checked", never a verdict on the floor.
|
|
148
|
+
*/
|
|
149
|
+
export async function checkFloorOnline(args: {
|
|
150
|
+
proof: Pick<BitGraphProof, "slotAllocation">;
|
|
151
|
+
attestedAtMs?: number | null;
|
|
152
|
+
maxGapMs?: number;
|
|
153
|
+
/** A node of the floor's network to ask. Default: the network's public node. */
|
|
154
|
+
rpcUrl?: string;
|
|
155
|
+
fetcher?: typeof fetch;
|
|
156
|
+
timeoutMs?: number;
|
|
157
|
+
}): Promise<FloorCheck> {
|
|
158
|
+
const floor = totFloorOf(args.proof);
|
|
159
|
+
if (floor === null) return { status: "none" };
|
|
160
|
+
const d = describe(floor);
|
|
161
|
+
const attested = typeof args.attestedAtMs === "number" ? { attestedAtMs: args.attestedAtMs } : {};
|
|
162
|
+
const known = FLOOR_NETWORKS[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;
|
|
165
|
+
let block: unknown;
|
|
166
|
+
try {
|
|
167
|
+
const r = await (args.fetcher ?? fetch)(url, {
|
|
168
|
+
method: "POST",
|
|
169
|
+
headers: { "content-type": "application/json" },
|
|
170
|
+
body: JSON.stringify({ jsonrpc: "2.0", id: 1, method: "eth_getBlockByNumber", params: [`0x${BigInt(floor.number).toString(16)}`, false] }),
|
|
171
|
+
signal: AbortSignal.timeout(args.timeoutMs ?? 8000),
|
|
172
|
+
});
|
|
173
|
+
if (!r.ok) return { status: "not-checked", ...d, ...attested, detail: `the node answered HTTP ${r.status}` };
|
|
174
|
+
const j = (await r.json()) as { result?: unknown; error?: unknown };
|
|
175
|
+
if (j.error !== undefined || !("result" in j)) return { status: "not-checked", ...d, ...attested, detail: "the node did not answer the lookup" };
|
|
176
|
+
block = j.result;
|
|
177
|
+
} catch (e) {
|
|
178
|
+
return { status: "not-checked", ...d, ...attested, detail: `the lookup failed: ${e instanceof Error ? e.message : String(e)}` };
|
|
179
|
+
}
|
|
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" };
|
|
181
|
+
return applyRule({
|
|
182
|
+
proof: args.proof,
|
|
183
|
+
block: block as (Partial<RpcBlockHeader> & { timestampMs?: string }) | null,
|
|
184
|
+
...(typeof args.attestedAtMs === "number" ? { attestedAtMs: args.attestedAtMs } : {}),
|
|
185
|
+
...(typeof args.maxGapMs === "number" ? { maxGapMs: args.maxGapMs } : {}),
|
|
186
|
+
}, "lookup");
|
|
187
|
+
}
|
package/src/types.ts
CHANGED
|
@@ -778,9 +778,24 @@ export interface AgencyEnvelope {
|
|
|
778
778
|
* Together these checks prove the nonce existed before the artifact was
|
|
779
779
|
* bound to it — the BitGraph atomic causality invariant.
|
|
780
780
|
*/
|
|
781
|
+
/**
|
|
782
|
+
* A public floor inside a TOT (bitgraph/slot/2): a block of a public chain, named by the host that ran the enclave
|
|
783
|
+
* and signed into the TOT body with the random value. The enclave has no network and does not check it. When the
|
|
784
|
+
* block is found on that chain with this hash, the TOT was made after that block existed, because nobody can know a
|
|
785
|
+
* block's hash earlier. When it is not found, the floor says nothing and the receipt is as valid as one without it.
|
|
786
|
+
*/
|
|
787
|
+
export interface SlotFloor {
|
|
788
|
+
/** The chain, as a CAIP-2 id. Base is "eip155:8453". */
|
|
789
|
+
network: string;
|
|
790
|
+
/** The block's number, a decimal string. */
|
|
791
|
+
number: string;
|
|
792
|
+
/** The block's 32-byte hash, standard base64 with padding. */
|
|
793
|
+
hashB64: string;
|
|
794
|
+
}
|
|
795
|
+
|
|
781
796
|
export interface SlotAllocation {
|
|
782
|
-
/** Schema version for slot records. Domain separation from proof bodies. */
|
|
783
|
-
version: "bitgraph/slot/1";
|
|
797
|
+
/** Schema version for slot records. Domain separation from proof bodies. slot/2 is slot/1 plus a signed floor. */
|
|
798
|
+
version: "bitgraph/slot/1" | "bitgraph/slot/2";
|
|
784
799
|
/** NSM-generated nonce. Becomes commit.nonceB64 when consumed. */
|
|
785
800
|
nonceB64: string;
|
|
786
801
|
/** Monotonic counter at allocation time. Must be < commit counter. */
|
|
@@ -793,6 +808,8 @@ export interface SlotAllocation {
|
|
|
793
808
|
publicKeyB64: string;
|
|
794
809
|
/** Chain isolation identifier. Omitted for the default "global" chain. */
|
|
795
810
|
chainId?: string;
|
|
811
|
+
/** The public floor: present on every bitgraph/slot/2 record and never on a bitgraph/slot/1 record. */
|
|
812
|
+
floor?: SlotFloor;
|
|
796
813
|
/** Ed25519 signature over canonical slot body (all fields above). */
|
|
797
814
|
signatureB64: string;
|
|
798
815
|
}
|