@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.
- package/README.md +24 -3
- package/dist/__tests__/carrier2.test.js +62 -3
- package/dist/__tests__/carrier2.test.js.map +1 -1
- package/dist/__tests__/ceiling.test.js +11 -4
- package/dist/__tests__/ceiling.test.js.map +1 -1
- package/dist/carrier-assemble.d.ts +12 -2
- package/dist/carrier-assemble.d.ts.map +1 -1
- package/dist/carrier-assemble.js +14 -3
- package/dist/carrier-assemble.js.map +1 -1
- package/dist/carrier-verify.d.ts +15 -4
- package/dist/carrier-verify.d.ts.map +1 -1
- package/dist/carrier-verify.js +201 -59
- package/dist/carrier-verify.js.map +1 -1
- package/dist/carrier.d.ts +48 -13
- package/dist/carrier.d.ts.map +1 -1
- package/dist/carrier.js +74 -16
- package/dist/carrier.js.map +1 -1
- package/dist/ceiling.d.ts +115 -6
- package/dist/ceiling.d.ts.map +1 -1
- package/dist/ceiling.js +189 -23
- package/dist/ceiling.js.map +1 -1
- package/dist/export.d.ts +122 -0
- package/dist/export.d.ts.map +1 -0
- package/dist/export.js +510 -0
- package/dist/export.js.map +1 -0
- package/dist/fuse.d.ts +45 -11
- package/dist/fuse.d.ts.map +1 -1
- package/dist/fuse.js +100 -18
- package/dist/fuse.js.map +1 -1
- package/dist/index.d.ts +20 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +18 -1
- package/dist/index.js.map +1 -1
- package/dist/measurements.d.ts +30 -0
- package/dist/measurements.d.ts.map +1 -0
- package/dist/measurements.js +22 -0
- package/dist/measurements.js.map +1 -0
- package/dist/output-root.d.ts +88 -0
- package/dist/output-root.d.ts.map +1 -0
- package/dist/output-root.js +260 -0
- package/dist/output-root.js.map +1 -0
- package/dist/stream.d.ts +46 -0
- package/dist/stream.d.ts.map +1 -0
- package/dist/stream.js +290 -0
- package/dist/stream.js.map +1 -0
- package/dist/tree.d.ts +167 -0
- package/dist/tree.d.ts.map +1 -0
- package/dist/tree.js +485 -0
- package/dist/tree.js.map +1 -0
- package/dist/types.d.ts +21 -0
- package/dist/types.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/__tests__/carrier2.test.ts +65 -3
- package/src/__tests__/ceiling.test.ts +12 -4
- package/src/carrier-assemble.ts +21 -3
- package/src/carrier-verify.ts +204 -61
- package/src/carrier.ts +95 -27
- package/src/ceiling.ts +217 -28
- package/src/export.ts +592 -0
- package/src/fuse.ts +126 -23
- package/src/index.ts +47 -1
- package/src/measurements.ts +49 -0
- package/src/output-root.ts +301 -0
- package/src/stream.ts +291 -0
- package/src/tree.ts +580 -0
- package/src/types.ts +22 -0
package/src/fuse.ts
CHANGED
|
@@ -204,9 +204,76 @@ export const FUSE2_DOMAIN: Uint8Array = (() => {
|
|
|
204
204
|
return out;
|
|
205
205
|
})();
|
|
206
206
|
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
207
|
+
// ---------------------------------------------------------------------------
|
|
208
|
+
// bitgraph-fuse/3 (2026-10-06, enclave v10): the floor is a Base block.
|
|
209
|
+
//
|
|
210
|
+
// commitment/3 = SHA256(UTF8("bitgraph-fuse/3") || 0x00 || slotRecordHash || nonce || floorBlockHash)
|
|
211
|
+
//
|
|
212
|
+
// floorBlockHash is the 32 raw bytes of commit.slotFloor.blockHash: the Base
|
|
213
|
+
// block the enclave fixed at allocation from a header it hashed itself, and
|
|
214
|
+
// signed into the proof with its chain named. fuse/2 is unchanged and still
|
|
215
|
+
// binds commit.slotAnchor (an Ethereum anchor). A proof carries one floor or
|
|
216
|
+
// the other, never both: a proof with both is ambiguous and binds nothing.
|
|
217
|
+
// ---------------------------------------------------------------------------
|
|
218
|
+
|
|
219
|
+
export const FUSE3_PROFILE = "bitgraph-fuse/3" as const;
|
|
220
|
+
export const FUSE3_ATTRIBUTION_NAME = FUSE3_PROFILE;
|
|
221
|
+
|
|
222
|
+
/** "bitgraph-fuse/3" followed by one zero byte. */
|
|
223
|
+
export const FUSE3_DOMAIN: Uint8Array = (() => {
|
|
224
|
+
const label = new TextEncoder().encode(FUSE3_PROFILE);
|
|
225
|
+
const out = new Uint8Array(label.length + 1);
|
|
226
|
+
out.set(label, 0);
|
|
227
|
+
out[label.length] = 0x00;
|
|
228
|
+
return out;
|
|
229
|
+
})();
|
|
230
|
+
|
|
231
|
+
/** The fused commitment versions: 1 binds no floor, 2 an Ethereum anchor, 3 a Base block. */
|
|
232
|
+
export type FuseVersion = 1 | 2 | 3;
|
|
233
|
+
|
|
234
|
+
/** Which commitment a fused proof's signed marker declares, or null when the proof is not fused. */
|
|
235
|
+
export function fuseVersionOfName(name: unknown): FuseVersion | null {
|
|
236
|
+
return name === FUSE_ATTRIBUTION_NAME ? 1 : name === FUSE2_ATTRIBUTION_NAME ? 2 : name === FUSE3_ATTRIBUTION_NAME ? 3 : null;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/** The signed marker name for a version. */
|
|
240
|
+
export function fuseNameOfVersion(version: FuseVersion): string {
|
|
241
|
+
return version === 3 ? FUSE3_ATTRIBUTION_NAME : version === 2 ? FUSE2_ATTRIBUTION_NAME : FUSE_ATTRIBUTION_NAME;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/**
|
|
245
|
+
* The floor a proof signs, whichever kind it is: an Ethereum anchor fixed at
|
|
246
|
+
* allocation (commit.slotAnchor, enclave v7 to v9) or a Base block
|
|
247
|
+
* (commit.slotFloor, enclave v10). Null when the proof signs no floor. Throws
|
|
248
|
+
* when it signs both, or a slotFloor that does not name Base: such a proof is
|
|
249
|
+
* ambiguous and nothing should be read from it as a floor.
|
|
250
|
+
*/
|
|
251
|
+
export interface SignedFloor {
|
|
252
|
+
chain: "ethereum" | "base";
|
|
253
|
+
blockNumber: number;
|
|
254
|
+
/** 0x-prefixed lowercase hex. */
|
|
255
|
+
blockHash: string;
|
|
256
|
+
/** Unix seconds; signed only for a Base floor (an Ethereum floor's time is read from its header). */
|
|
257
|
+
blockTimestamp?: number;
|
|
258
|
+
/** The anchor proof's counter, for an Ethereum floor. */
|
|
259
|
+
anchorCounter?: string;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
export function signedFloorOf(proof: BitGraphProof): SignedFloor | null {
|
|
263
|
+
const a = proof.commit?.slotAnchor;
|
|
264
|
+
const f = proof.commit?.slotFloor;
|
|
265
|
+
if (a && f) throw new TypeError("the proof signs two floors (commit.slotAnchor and commit.slotFloor); it is ambiguous and binds neither");
|
|
266
|
+
if (f) {
|
|
267
|
+
if (f.chain !== "base" || f.evmChainId !== 8453) throw new TypeError("commit.slotFloor does not name Base mainnet");
|
|
268
|
+
if (typeof f.blockHash !== "string" || !/^0x[0-9a-f]{64}$/.test(f.blockHash)) throw new TypeError("commit.slotFloor.blockHash is not a 32-byte hash");
|
|
269
|
+
if (!Number.isSafeInteger(f.blockNumber) || !Number.isSafeInteger(f.blockTimestamp)) throw new TypeError("commit.slotFloor carries no block number or time");
|
|
270
|
+
return { chain: "base", blockNumber: f.blockNumber, blockHash: f.blockHash, blockTimestamp: f.blockTimestamp };
|
|
271
|
+
}
|
|
272
|
+
if (a) {
|
|
273
|
+
if (typeof a.blockHash !== "string") throw new TypeError("commit.slotAnchor.blockHash is missing");
|
|
274
|
+
return { chain: "ethereum", blockNumber: a.blockNumber, blockHash: a.blockHash.toLowerCase(), anchorCounter: a.counter };
|
|
275
|
+
}
|
|
276
|
+
return null;
|
|
210
277
|
}
|
|
211
278
|
|
|
212
279
|
/** True for either fused marker name. Use this, never a string compare against one version. */
|
|
@@ -234,30 +301,54 @@ export function computeSlotCommitment2(slot: SlotAllocation, floorBlockHash: str
|
|
|
234
301
|
return sha256(slotCommitment2Preimage(slot, floorBlockHash));
|
|
235
302
|
}
|
|
236
303
|
|
|
304
|
+
/** The exact preimage of commitment/3: domain/3 || slotRecordHash || nonce || floorBlockHash (a Base block). */
|
|
305
|
+
export function slotCommitment3Preimage(slot: SlotAllocation, floorBlockHash: string | Uint8Array): Uint8Array {
|
|
306
|
+
const nonce = base64ToBytes(slot.nonceB64);
|
|
307
|
+
if (nonce === null || nonce.length !== 32) {
|
|
308
|
+
throw new TypeError("slot.nonceB64 must decode to exactly 32 bytes");
|
|
309
|
+
}
|
|
310
|
+
return concat(FUSE3_DOMAIN, computeSlotRecordHash(slot), nonce, floorHashBytes(floorBlockHash));
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
/** commitment/3 = SHA256(domain/3 || slotRecordHash || nonce || floorBlockHash). */
|
|
314
|
+
export function computeSlotCommitment3(slot: SlotAllocation, floorBlockHash: string | Uint8Array): Uint8Array {
|
|
315
|
+
return sha256(slotCommitment3Preimage(slot, floorBlockHash));
|
|
316
|
+
}
|
|
317
|
+
|
|
237
318
|
/**
|
|
238
319
|
* The commitment a fused proof's bytes must carry, chosen by its signed marker:
|
|
239
|
-
* fuse/
|
|
240
|
-
*
|
|
320
|
+
* fuse/3 binds the proof's signed Base floor (commit.slotFloor), fuse/2 its
|
|
321
|
+
* signed Ethereum anchor (commit.slotAnchor), fuse/1 no floor. Throws when the
|
|
322
|
+
* marker's floor is missing, or the proof signs both floors.
|
|
241
323
|
*/
|
|
242
324
|
export function commitmentForProof(proof: BitGraphProof, slot: SlotAllocation): Uint8Array {
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
325
|
+
const version = fuseVersionOfName(proof.attribution?.name);
|
|
326
|
+
if (version === 1 || version === null) return computeSlotCommitment(slot);
|
|
327
|
+
const floor = signedFloorOf(proof);
|
|
328
|
+
if (version === 2) {
|
|
329
|
+
if (floor?.chain !== "ethereum") throw new TypeError("a bitgraph-fuse/2 proof must carry a signed commit.slotAnchor to bind");
|
|
330
|
+
return computeSlotCommitment2(slot, floor.blockHash);
|
|
247
331
|
}
|
|
248
|
-
|
|
332
|
+
if (floor?.chain !== "base") throw new TypeError("a bitgraph-fuse/3 proof must carry a signed commit.slotFloor (a Base block) to bind");
|
|
333
|
+
return computeSlotCommitment3(slot, floor.blockHash);
|
|
249
334
|
}
|
|
250
335
|
|
|
251
336
|
/**
|
|
252
337
|
* The commitment a PRODUCER writes into new bytes, from what the allocation
|
|
253
|
-
* returned: fuse/
|
|
254
|
-
*
|
|
255
|
-
* fuseAttribution's version argument.
|
|
338
|
+
* returned: fuse/3 when the enclave handed back a Base floor (v10), fuse/2 for
|
|
339
|
+
* an Ethereum floor anchor (v9), else fuse/1. The attribution name must match;
|
|
340
|
+
* see fuseAttribution's version argument.
|
|
256
341
|
*/
|
|
257
|
-
export function producerCommitment(
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
342
|
+
export function producerCommitment(
|
|
343
|
+
slot: SlotAllocation,
|
|
344
|
+
floor?: { blockHash: string; chain?: string } | null,
|
|
345
|
+
): { commitment: Uint8Array; version: FuseVersion } {
|
|
346
|
+
if (floor && typeof floor.blockHash === "string") {
|
|
347
|
+
return floor.chain === "base"
|
|
348
|
+
? { commitment: computeSlotCommitment3(slot, floor.blockHash), version: 3 }
|
|
349
|
+
: { commitment: computeSlotCommitment2(slot, floor.blockHash), version: 2 };
|
|
350
|
+
}
|
|
351
|
+
return { commitment: computeSlotCommitment(slot), version: 1 };
|
|
261
352
|
}
|
|
262
353
|
|
|
263
354
|
// ---------------------------------------------------------------------------
|
|
@@ -336,7 +427,9 @@ export function parseFusePayload(bytes: Uint8Array): { commitment: Uint8Array; o
|
|
|
336
427
|
// ---------------------------------------------------------------------------
|
|
337
428
|
|
|
338
429
|
const BLOCK = 512;
|
|
339
|
-
|
|
430
|
+
/** The largest file a container placement can hold: 8 GiB - 1, the ustar header's 11-digit octal size field (SPEC 7.3). */
|
|
431
|
+
export const MAX_CONTAINER_ENTRY_BYTES = 0o77777777777;
|
|
432
|
+
const MAX_ENTRY = MAX_CONTAINER_ENTRY_BYTES;
|
|
340
433
|
|
|
341
434
|
function octal(n: number, width: number): Uint8Array {
|
|
342
435
|
const s = n.toString(8).padStart(width - 1, "0") + "\0";
|
|
@@ -496,6 +589,9 @@ const container1: Placement = {
|
|
|
496
589
|
// The archive must be the one this module would build: headers included.
|
|
497
590
|
const rebuilt = buildContainer(o.data, m.data);
|
|
498
591
|
if (!bytesEqual(rebuilt, fused)) return null;
|
|
592
|
+
// And the original inside must be the one the manifest names: the declared
|
|
593
|
+
// digest is never taken on the archive's word.
|
|
594
|
+
if (!bytesEqual(sha256(o.data), payload.originDigest)) return null;
|
|
499
595
|
return { commitment: payload.commitment, originDigest: payload.originDigest, originalBytes: o.data };
|
|
500
596
|
},
|
|
501
597
|
frame({ originalSize, originDigest, commitment }) {
|
|
@@ -546,6 +642,8 @@ const container2: Placement = {
|
|
|
546
642
|
// The archive must be the one this module would build: headers included.
|
|
547
643
|
const rebuilt = buildContainer2(o.data, m.data);
|
|
548
644
|
if (!bytesEqual(rebuilt, fused)) return null;
|
|
645
|
+
// And the original inside must be the one the manifest names.
|
|
646
|
+
if (!bytesEqual(sha256(o.data), payload.originDigest)) return null;
|
|
549
647
|
return { commitment: payload.commitment, originDigest: payload.originDigest, originalBytes: o.data };
|
|
550
648
|
},
|
|
551
649
|
frame({ originalSize, originDigest, commitment }) {
|
|
@@ -991,11 +1089,11 @@ export function getPlacement(id: string): Placement | undefined {
|
|
|
991
1089
|
* set/1 refuses an origin digest: a set/1 marker carrying one is out of
|
|
992
1090
|
* profile and verifyFuseMember refuses it.
|
|
993
1091
|
*/
|
|
994
|
-
export function fuseAttribution(placement: PlacementId, originDigest?: Uint8Array, version:
|
|
1092
|
+
export function fuseAttribution(placement: PlacementId, originDigest?: Uint8Array, version: FuseVersion = 1): Attribution {
|
|
995
1093
|
if (originDigest !== undefined && originDigest.length !== 32) throw new TypeError("originDigest must be 32 bytes");
|
|
996
1094
|
if ((placement === SET_PLACEMENT_ID || placement === SET2_PLACEMENT_ID) && originDigest !== undefined) throw new TypeError(`${placement} has no single origin; a set marker carries no origin digest`);
|
|
997
1095
|
return {
|
|
998
|
-
name: version
|
|
1096
|
+
name: fuseNameOfVersion(version),
|
|
999
1097
|
title: placement,
|
|
1000
1098
|
...(originDigest !== undefined ? { message: bytesToBase64(originDigest) } : {}),
|
|
1001
1099
|
};
|
|
@@ -1018,7 +1116,7 @@ export interface FuseMarker {
|
|
|
1018
1116
|
/** "attribution" when the proof's signed attribution marks it fused, else "manifest". */
|
|
1019
1117
|
source: MarkerSource;
|
|
1020
1118
|
/** Which commitment the signed marker declares; absent on a manifest-only marker, which means 1. */
|
|
1021
|
-
version?:
|
|
1119
|
+
version?: FuseVersion;
|
|
1022
1120
|
}
|
|
1023
1121
|
|
|
1024
1122
|
/** Read the fused marker from a proof's signed attribution, or null when the proof is not marked fused. */
|
|
@@ -1069,14 +1167,19 @@ export function findCommitment(bytes: Uint8Array, commitment: Uint8Array, encodi
|
|
|
1069
1167
|
}
|
|
1070
1168
|
|
|
1071
1169
|
/** The attribution a producer sends for an artifact made with the commitment inside it. */
|
|
1072
|
-
export function inlineAttribution(version:
|
|
1073
|
-
return { name: version
|
|
1170
|
+
export function inlineAttribution(version: FuseVersion = 1): { name: string; title: string } {
|
|
1171
|
+
return { name: fuseNameOfVersion(version), title: ENCODING_BASE64URL };
|
|
1074
1172
|
}
|
|
1075
1173
|
|
|
1076
1174
|
export function readFuseAttribution(proof: BitGraphProof): FuseMarker | null {
|
|
1077
1175
|
const a = proof.attribution;
|
|
1078
1176
|
const version = fuseVersionOfName(a?.name);
|
|
1079
1177
|
if (a === undefined || version === null) return null;
|
|
1178
|
+
// A tree/1 proof carries the fuse/2 or fuse/3 name, but it is not a single fused file:
|
|
1179
|
+
// its committed artifact is the tree's root document and its message is the
|
|
1180
|
+
// SPEC hash, not an origin. Read it with verifyTreeMember (tree.ts), never
|
|
1181
|
+
// here, or the spec hash would be taken for an origin digest.
|
|
1182
|
+
if (a.title === "tree/1") return null;
|
|
1080
1183
|
const declared = typeof a.title === "string" && a.title.length > 0;
|
|
1081
1184
|
const marker: FuseMarker = { placement: declared ? (a.title as string) : null, placementSource: declared ? "attribution" : null, source: "attribution", version };
|
|
1082
1185
|
if (typeof a.message === "string" && a.message.length > 0) {
|
package/src/index.ts
CHANGED
|
@@ -62,6 +62,13 @@ export {
|
|
|
62
62
|
FUSE2_PROFILE,
|
|
63
63
|
FUSE2_DOMAIN,
|
|
64
64
|
FUSE2_ATTRIBUTION_NAME,
|
|
65
|
+
FUSE3_PROFILE,
|
|
66
|
+
FUSE3_DOMAIN,
|
|
67
|
+
FUSE3_ATTRIBUTION_NAME,
|
|
68
|
+
fuseNameOfVersion,
|
|
69
|
+
signedFloorOf,
|
|
70
|
+
slotCommitment3Preimage,
|
|
71
|
+
computeSlotCommitment3,
|
|
65
72
|
fuseVersionOfName,
|
|
66
73
|
isFuseMarkerName,
|
|
67
74
|
slotCommitment2Preimage,
|
|
@@ -82,7 +89,11 @@ export {
|
|
|
82
89
|
hexToBytes,
|
|
83
90
|
bytesEqual,
|
|
84
91
|
} from "./fuse.js";
|
|
85
|
-
export type { PlacementId, Placement, Located, FusePayload, FuseFrame, FuseMarker, MarkerSource, SetMember, SetManifest, FusedFrame } from "./fuse.js";
|
|
92
|
+
export type { PlacementId, Placement, Located, FusePayload, FuseFrame, FuseMarker, MarkerSource, SetMember, SetManifest, FusedFrame, FuseVersion, SignedFloor } from "./fuse.js";
|
|
93
|
+
export { MAX_CONTAINER_ENTRY_BYTES } from "./fuse.js";
|
|
94
|
+
/* Streaming checks: a file of any size against its leaf, in constant memory. */
|
|
95
|
+
export { bytesSource, blobSource, streamDigest, streamLeafCheck } from "./stream.js";
|
|
96
|
+
export type { ByteSource, StreamPlacement, StreamLeafCheck } from "./stream.js";
|
|
86
97
|
export { verifyFuse, assembledAfterCommit } from "./fuse-verify.js";
|
|
87
98
|
export type { FuseCategory, FuseSpan, FuseVerifyResult, FuseVerifyOptions } from "./fuse-verify.js";
|
|
88
99
|
export { verifyFuseMember } from "./fuse-member.js";
|
|
@@ -121,6 +132,12 @@ export type { CarrierVersion, CarrierCeilingInTime, CarrierSettlement, CarrierPi
|
|
|
121
132
|
export { assembleCarrierV2Payload, carrierBlockSize, CARRIER_BLOCK_ZIP_LIMIT } from "./carrier-assemble.js";
|
|
122
133
|
export type { CarrierV2Parts } from "./carrier-assemble.js";
|
|
123
134
|
|
|
135
|
+
/* bitgraph-carrier/3 (2026-10-06, enclave v10): the floor is the Base header the proof signs; no ceiling in position exists. */
|
|
136
|
+
export { CARRIER_VERSION_3, baseHeaderFields } from "./carrier.js";
|
|
137
|
+
export type { CarrierBaseFloor } from "./carrier.js";
|
|
138
|
+
export { assembleCarrierV3Payload } from "./carrier-assemble.js";
|
|
139
|
+
export type { CarrierV3Parts } from "./carrier-assemble.js";
|
|
140
|
+
|
|
124
141
|
/* AWS Nitro attestation: decode, verify (chain to the AWS root, validity at the document's instant), and the openssl-checkable witness. */
|
|
125
142
|
export {
|
|
126
143
|
AWS_NITRO_ROOT_CA_PEM, awsNitroRootDer, awsNitroRootSha256, decodeNitroAttestation, verifyNitroAttestation,
|
|
@@ -142,3 +159,32 @@ export { rlpEncode, rlpDecode, decodeHeader, decodeEip1559, mptVerify, txTrieKey
|
|
|
142
159
|
hexToBytes as evmHexToBytes, bytesToHex as evmBytesToHex } from "./ceiling-evm.js";
|
|
143
160
|
export { txTrieProof } from "./ceiling-trie.js";
|
|
144
161
|
export type { RlpItem, DecodedHeader, DecodedTx } from "./ceiling-evm.js";
|
|
162
|
+
|
|
163
|
+
/* tree/1 (2026-10-03): every BitGraph is a Merkle tree under one position; a single file is a tree of one. */
|
|
164
|
+
export {
|
|
165
|
+
TREE_PLACEMENT_ID, TREE_PROFILE, TREE_DOMAIN, TREE_LEAF_BYTES, TREE_ROOT_DOCUMENT_BYTES, MAX_TREE_LEAVES, TREE_METADATA_KEY,
|
|
166
|
+
KNOWN_TREE_SPEC_HASHES, TREE_SPEC_V1_HASH, TREE_SPEC_V2_HASH, LEAF_AS_IS, LEAF_PLACEMENTS, TREE_MEMBER_CATEGORIES,
|
|
167
|
+
leafCodeOf, encodeTreeLeaf, decodeTreeLeaf, treeLeafHash, sortTreeLeaves, buildTree,
|
|
168
|
+
buildTreeRootDocument, parseTreeRootDocument, readTreeMetadata, treeAttribution, currentTreeSpecHash, isTreeProof,
|
|
169
|
+
buildTreeMemberEvidence, parseTreeMemberEvidence, treeRootFromMember, encodeTreeLeaves, decodeTreeLeaves, verifyTreeLeaves,
|
|
170
|
+
committedBytesFor, leafFor, verifyTreeMember,
|
|
171
|
+
} from "./tree.js";
|
|
172
|
+
export type { TreeLeaf, BuiltTree, TreeMemberEvidence, TreeLeavesCheck, TreeCategory, TreeVerifyOptions, TreeVerifyResult } from "./tree.js";
|
|
173
|
+
|
|
174
|
+
/* bitgraph-output-root/1: a Base ceiling settled on Ethereum through Base's output root, without blobs. */
|
|
175
|
+
export {
|
|
176
|
+
OUTPUT_ROOT_VERSION, HISTORY_STORAGE_ADDRESS, HISTORY_SERVE_WINDOW, BASE_DISPUTE_GAME_FACTORY,
|
|
177
|
+
computeOutputRoot, historySlot, verifyOutputRootSettlement,
|
|
178
|
+
} from "./output-root.js";
|
|
179
|
+
export type { OutputRootSettlement, OutputRootCheck, OutputRootVerifyResult, OutputRootVerifyOptions } from "./output-root.js";
|
|
180
|
+
|
|
181
|
+
/* bitgraph-export/1: one JSON file that, with the file, checks a tree/1 BitGraph with nothing of BitGraph's required. */
|
|
182
|
+
export { EXPORT_FORMAT, parseExport, verifyExport, buildExport } from "./export.js";
|
|
183
|
+
export { baseTimeIsBound, BASE_STAMP_TOLERANCE_SECONDS } from "./ceiling.js";
|
|
184
|
+
/* The floor block's header, on either chain (enclave v10 floors are Base blocks). */
|
|
185
|
+
export { checkFloorHeader, floorTimeIsBound, onBaseSchedule, headerRlpFromRpc, BASE_MAINNET_GENESIS_TIME, BASE_BLOCK_TIME_SECONDS } from "./ceiling.js";
|
|
186
|
+
export type { RpcBlockHeader } from "./ceiling.js";
|
|
187
|
+
/* The enclave images BitGraph has published: the default measurement policy (SPEC section 16). */
|
|
188
|
+
export { PUBLISHED_ENCLAVE_MEASUREMENTS, PUBLISHED_PCR0S, publishedMeasurement } from "./measurements.js";
|
|
189
|
+
export type { PublishedMeasurement } from "./measurements.js";
|
|
190
|
+
export type { BitGraphExport, ExportClaim, ExportClaimResult, ExportLookups, ExportVerifyOptions, ExportVerifyResult } from "./export.js";
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
// Copyright (c) 2024-2026 Argento Computing Inc. Licensed under the MIT License. See LICENSE.
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The enclave images BitGraph has published (SPEC.md section 16), oldest
|
|
5
|
+
* first: every PCR0 a real BitGraph proof can carry.
|
|
6
|
+
*
|
|
7
|
+
* An attestation that chains to the AWS Nitro root shows that SOME Nitro
|
|
8
|
+
* enclave signed; anyone can run one. Which image is BitGraph's is a
|
|
9
|
+
* measurement policy, and this list is that policy by default:
|
|
10
|
+
* verifyExport and verifyCarrier judge `attestation.pins` against it when
|
|
11
|
+
* the caller names no list of their own, and a proof from any other image is
|
|
12
|
+
* not a BitGraph. A caller who rebuilt an image from its tagged source and
|
|
13
|
+
* wants to accept exactly that passes their own list instead.
|
|
14
|
+
*
|
|
15
|
+
* Keep in step with SPEC.md section 16, the site's
|
|
16
|
+
* website/src/lib/enclave-measurements.ts and the player's
|
|
17
|
+
* KNOWN_ENCLAVE_MEASUREMENTS; a root test checks all of them against this.
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
export interface PublishedMeasurement {
|
|
21
|
+
/** The image's name in BitGraph's history: "genesis", "v2", ... */
|
|
22
|
+
version: string;
|
|
23
|
+
/** The UTC day it went live (YYYY-MM-DD). */
|
|
24
|
+
since: string;
|
|
25
|
+
/** PCR0, 96 lowercase hex characters. */
|
|
26
|
+
pcr0: string;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export const PUBLISHED_ENCLAVE_MEASUREMENTS: readonly PublishedMeasurement[] = Object.freeze([
|
|
30
|
+
{ version: "genesis", since: "2026-05-15", pcr0: "8530a6399399c4f23d89f5a1faa2e8bf2e09a5959f117070fca08148377f92c902c695fc926c17f67f35f110327dca92" },
|
|
31
|
+
{ version: "v2", since: "2026-06-27", pcr0: "bb9dd158703603ec222fe565495ceaa7edc08f665da5c1cddad91442ac2211731390267036d79deb720d13fb704f648a" },
|
|
32
|
+
{ version: "v4", since: "2026-07-05", pcr0: "e2fccbae77ee40aac4830e84f195e05d69eb4547bbd961f4d3459feba10807140424aca42ad03810354982598c86b9cb" },
|
|
33
|
+
{ version: "v5", since: "2026-07-29", pcr0: "6483cedffed74680ffb287507744a398b288c3fb943eb3f2e4fe889f8b60b3d575ad8942350360b69a1bd7bf713df27f" },
|
|
34
|
+
{ version: "v6", since: "2026-09-05", pcr0: "cd8ba52d340fb1be78610b59953ded2ceca23be1cfcc7ab504a26b8fdcd7ba92090f49e28a32d008df046ec4212f77bf" },
|
|
35
|
+
{ version: "v7", since: "2026-09-06", pcr0: "394c3cf515651dc27187d85e4716c12dfeb99c1227f1fe0eacfaa427d80018e1a28ebba9469e99c7936601f901d74e1d" },
|
|
36
|
+
{ version: "v8", since: "2026-09-07", pcr0: "eccfc1c78006f4b74f929c992785575c908a0f60eca08ff638cd6c0842f993f182ebb002457b8ef3e732a6a10805c72b" },
|
|
37
|
+
{ version: "v9", since: "2026-09-30", pcr0: "934feb8bb6f4f7e2d2f85d902a7d5edd0981f706d9d2385638988ac096a05ea0583c3d00eef2a7947865ec66efc1fcf8" },
|
|
38
|
+
{ version: "v10", since: "2026-10-08", pcr0: "5a947cc66095adcceefa9e5ece5d1416dfe08c3470df1bcaa5b2bc5267b0480e6cdc172fe077cd06b0afb07614307973" },
|
|
39
|
+
].map((m) => Object.freeze(m)));
|
|
40
|
+
|
|
41
|
+
/** PUBLISHED_ENCLAVE_MEASUREMENTS as the bare PCR0 list a verifier's pins take. */
|
|
42
|
+
export const PUBLISHED_PCR0S: readonly string[] = Object.freeze(PUBLISHED_ENCLAVE_MEASUREMENTS.map((m) => m.pcr0));
|
|
43
|
+
|
|
44
|
+
/** The published image a PCR0 names, or null when it is not one of BitGraph's. Case-insensitive. */
|
|
45
|
+
export function publishedMeasurement(pcr0: unknown): PublishedMeasurement | null {
|
|
46
|
+
if (typeof pcr0 !== "string") return null;
|
|
47
|
+
const want = pcr0.toLowerCase();
|
|
48
|
+
return PUBLISHED_ENCLAVE_MEASUREMENTS.find((m) => m.pcr0 === want) ?? null;
|
|
49
|
+
}
|
|
@@ -0,0 +1,301 @@
|
|
|
1
|
+
// Copyright (c) 2024-2026 Argento Computing Inc. Licensed under the MIT License. See LICENSE.
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* bitgraph-output-root/1: settling a Base ceiling on Ethereum without blobs
|
|
5
|
+
* (2026-10-03).
|
|
6
|
+
*
|
|
7
|
+
* A Base ceiling is a transaction in Base block B. Base posts an OUTPUT ROOT
|
|
8
|
+
* for one of its blocks P to Ethereum about every 600 Base blocks, as the
|
|
9
|
+
* rootClaim of a dispute game:
|
|
10
|
+
*
|
|
11
|
+
* outputRoot = keccak256(version (32 zero bytes) || stateRoot_P || messagePasserStorageRoot_P || blockHash_P)
|
|
12
|
+
*
|
|
13
|
+
* Base runs the EIP-2935 history contract, which keeps the hashes of the
|
|
14
|
+
* previous 8,191 blocks in its storage, so B's hash is provable from P's state
|
|
15
|
+
* when 1 <= P - B <= 8,191 (the contract holds earlier blocks only). When
|
|
16
|
+
* B = P the output root names P's hash directly.
|
|
17
|
+
*
|
|
18
|
+
* The chain this module checks, every link by hash:
|
|
19
|
+
* Ethereum tx in block H carries outputRoot
|
|
20
|
+
* -> outputRoot preimage (stateRoot_P, blockHash_P)
|
|
21
|
+
* -> B's hash (from P's state by storage proof, or blockHash_P when B = P)
|
|
22
|
+
* -> B's header (checked by the ceiling sidecar)
|
|
23
|
+
*
|
|
24
|
+
* What it proves: the ceiling transaction, and so the root it carries, existed
|
|
25
|
+
* by Ethereum block H. Any Ethereum transaction whose bytes commit to the
|
|
26
|
+
* output root by hash proves that, whoever sent it and whether or not Base's
|
|
27
|
+
* claim is ever upheld; so the sender and the game's outcome are not checked.
|
|
28
|
+
* What it does NOT prove: that B is canonical on Base, or B's timestamp. Those
|
|
29
|
+
* need Base itself (checkCeilingOnline). Whether H is canonical is the one
|
|
30
|
+
* outside lookup this leaves.
|
|
31
|
+
*/
|
|
32
|
+
|
|
33
|
+
import { bytesEqual, decodeHeader, hexToBytes, bytesToHex, keccak256, mptVerify, rlpDecode, txTrieKey, type RlpItem } from "./ceiling-evm.js";
|
|
34
|
+
|
|
35
|
+
export const OUTPUT_ROOT_VERSION = "bitgraph-output-root/1" as const;
|
|
36
|
+
/** EIP-2935 history storage contract (the same address on Ethereum and on OP Stack chains). */
|
|
37
|
+
export const HISTORY_STORAGE_ADDRESS = "0x0000f90827f1c53a10cb7a02335b175320002935" as const;
|
|
38
|
+
/** EIP-2935 ring buffer size: the contract keeps the previous 8,191 block hashes. */
|
|
39
|
+
export const HISTORY_SERVE_WINDOW = 8191;
|
|
40
|
+
/** Base mainnet's dispute game factory on Ethereum (OptimismPortal 0x49048044D57e1C92A77f79988d21Fa8fAF74E97e). Informational. */
|
|
41
|
+
export const BASE_DISPUTE_GAME_FACTORY = "0x43edb88c4b80fdd2adff2412a7bebf9df42cb40e" as const;
|
|
42
|
+
|
|
43
|
+
export interface OutputRootSettlement {
|
|
44
|
+
version: typeof OUTPUT_ROOT_VERSION;
|
|
45
|
+
/** The ceiling's Base block: number and hash (the hash is also the ceiling sidecar's). */
|
|
46
|
+
base: { chainId: number; blockNumber: number; blockHash: string };
|
|
47
|
+
/** The output-root preimage for Base block P. */
|
|
48
|
+
outputRoot: {
|
|
49
|
+
blockNumber: number;
|
|
50
|
+
/** 32 bytes, all zero for output-root version 0. */
|
|
51
|
+
version: string;
|
|
52
|
+
stateRoot: string;
|
|
53
|
+
messagePasserStorageRoot: string;
|
|
54
|
+
blockHash: string;
|
|
55
|
+
};
|
|
56
|
+
/** B's hash in P's state. Null exactly when B = P. */
|
|
57
|
+
history: {
|
|
58
|
+
address: string;
|
|
59
|
+
/** uint256(B mod 8191), 32 bytes. */
|
|
60
|
+
slot: string;
|
|
61
|
+
accountProof: string[];
|
|
62
|
+
storageProof: string[];
|
|
63
|
+
} | null;
|
|
64
|
+
/** The Ethereum transaction that carries the output root, and its block. */
|
|
65
|
+
ethereum: {
|
|
66
|
+
chainId: number;
|
|
67
|
+
blockNumber: number;
|
|
68
|
+
blockHash: string;
|
|
69
|
+
blockTimestamp: number;
|
|
70
|
+
header: string;
|
|
71
|
+
txHash: string;
|
|
72
|
+
txIndex: number;
|
|
73
|
+
rawTx: string;
|
|
74
|
+
txInclusionProof: string[];
|
|
75
|
+
};
|
|
76
|
+
/** Informational, never verified: which dispute game this was. */
|
|
77
|
+
game?: { factory: string; gameType: number; index: number; proxy: string };
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export interface OutputRootCheck { name: string; ok: boolean; detail?: string }
|
|
81
|
+
|
|
82
|
+
export interface OutputRootVerifyResult {
|
|
83
|
+
ok: boolean;
|
|
84
|
+
reason?: string;
|
|
85
|
+
checks: OutputRootCheck[];
|
|
86
|
+
/** On success: the ceiling transaction existed by this Ethereum block. Canonicality of the block is not checked here. */
|
|
87
|
+
existedBy?: { chainId: number; blockNumber: number; blockHash: string; blockTimestamp: number };
|
|
88
|
+
/** The output root that was recomputed (0x hex). */
|
|
89
|
+
outputRootHex?: string;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const ZERO32 = new Uint8Array(32);
|
|
93
|
+
|
|
94
|
+
function b32(hex: string, name: string): Uint8Array {
|
|
95
|
+
const b = hexToBytes(hex);
|
|
96
|
+
if (b.length !== 32) throw new TypeError(`${name} must be 32 bytes`);
|
|
97
|
+
return b;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** keccak256(version || stateRoot || messagePasserStorageRoot || blockHash). */
|
|
101
|
+
export function computeOutputRoot(o: OutputRootSettlement["outputRoot"]): Uint8Array {
|
|
102
|
+
const parts = [b32(o.version, "outputRoot.version"), b32(o.stateRoot, "outputRoot.stateRoot"), b32(o.messagePasserStorageRoot, "outputRoot.messagePasserStorageRoot"), b32(o.blockHash, "outputRoot.blockHash")];
|
|
103
|
+
const pre = new Uint8Array(128);
|
|
104
|
+
parts.forEach((p, i) => pre.set(p, i * 32));
|
|
105
|
+
return keccak256(pre);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** The EIP-2935 storage slot holding block n's hash: uint256(n mod 8191), big-endian. */
|
|
109
|
+
export function historySlot(blockNumber: number): Uint8Array {
|
|
110
|
+
const slot = new Uint8Array(32);
|
|
111
|
+
let v = blockNumber % HISTORY_SERVE_WINDOW;
|
|
112
|
+
for (let i = 31; i >= 0 && v > 0; i--) {
|
|
113
|
+
slot[i] = v & 0xff;
|
|
114
|
+
v = Math.floor(v / 256);
|
|
115
|
+
}
|
|
116
|
+
return slot;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
function indexOfBytes(hay: Uint8Array, needle: Uint8Array): number {
|
|
120
|
+
outer: for (let i = 0; i + needle.length <= hay.length; i++) {
|
|
121
|
+
for (let j = 0; j < needle.length; j++) if (hay[i + j] !== needle[j]) continue outer;
|
|
122
|
+
return i;
|
|
123
|
+
}
|
|
124
|
+
return -1;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/** RLP bytes as an unsigned integer value padded to 32 bytes (storage values are RLP of the minimal big-endian bytes). */
|
|
128
|
+
function storageWord(item: RlpItem): Uint8Array | null {
|
|
129
|
+
if (!(item instanceof Uint8Array) || item.length > 32) return null;
|
|
130
|
+
const out = new Uint8Array(32);
|
|
131
|
+
out.set(item, 32 - item.length);
|
|
132
|
+
return out;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Verify an output-root settlement, offline. Every link is recomputed from
|
|
137
|
+
* bytes; the result names the Ethereum block whose canonicality is the one
|
|
138
|
+
* remaining lookup.
|
|
139
|
+
*/
|
|
140
|
+
export interface OutputRootVerifyOptions {
|
|
141
|
+
/** The chain the ceiling is on. Default Base mainnet, 8453. */
|
|
142
|
+
baseChainId?: number;
|
|
143
|
+
/** The chain the settlement is on. Default Ethereum mainnet, 1. */
|
|
144
|
+
ethereumChainId?: number;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/** The chain a raw Ethereum transaction is signed for: field 0 of a typed transaction, or EIP-155's v. Null when it names none. */
|
|
148
|
+
function transactionChainId(raw: Uint8Array): bigint | null {
|
|
149
|
+
if (raw.length === 0) return null;
|
|
150
|
+
const big = (b: Uint8Array) => b.reduce((n, x) => (n << 8n) | BigInt(x), 0n);
|
|
151
|
+
try {
|
|
152
|
+
if (raw[0]! <= 0x7f) {
|
|
153
|
+
const item = rlpDecode(raw.subarray(1));
|
|
154
|
+
return Array.isArray(item) && item[0] instanceof Uint8Array ? big(item[0]) : null;
|
|
155
|
+
}
|
|
156
|
+
const item = rlpDecode(raw);
|
|
157
|
+
if (!Array.isArray(item) || item.length !== 9 || !(item[6] instanceof Uint8Array)) return null;
|
|
158
|
+
const v = big(item[6]);
|
|
159
|
+
return v >= 35n ? (v - 35n) / 2n : null;
|
|
160
|
+
} catch {
|
|
161
|
+
return null;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
export function verifyOutputRootSettlement(s: OutputRootSettlement, opts: OutputRootVerifyOptions = {}): OutputRootVerifyResult {
|
|
166
|
+
const checks: OutputRootCheck[] = [];
|
|
167
|
+
const fail = (name: string, detail: string): OutputRootVerifyResult => {
|
|
168
|
+
checks.push({ name, ok: false, detail });
|
|
169
|
+
return { ok: false, reason: `${name}: ${detail}`, checks };
|
|
170
|
+
};
|
|
171
|
+
// A malformed field anywhere is a failed check, never an exception.
|
|
172
|
+
try {
|
|
173
|
+
return verifyOutputRootChecks(s, opts, checks, fail);
|
|
174
|
+
} catch (e) {
|
|
175
|
+
return fail("malformed", `a field of the settlement is malformed: ${e instanceof Error ? e.message : String(e)}`);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
function verifyOutputRootChecks(
|
|
180
|
+
s: OutputRootSettlement,
|
|
181
|
+
opts: OutputRootVerifyOptions,
|
|
182
|
+
checks: OutputRootCheck[],
|
|
183
|
+
fail: (name: string, detail: string) => OutputRootVerifyResult,
|
|
184
|
+
): OutputRootVerifyResult {
|
|
185
|
+
const pass = (name: string, detail?: string) => checks.push(detail === undefined ? { name, ok: true } : { name, ok: true, detail });
|
|
186
|
+
|
|
187
|
+
if (s?.version !== OUTPUT_ROOT_VERSION) return fail("format", `not a ${OUTPUT_ROOT_VERSION} settlement`);
|
|
188
|
+
|
|
189
|
+
// 0. The chains: the labels are part of what the result reports, so they are checked, never echoed.
|
|
190
|
+
const baseChain = opts.baseChainId ?? 8453;
|
|
191
|
+
const l1Chain = opts.ethereumChainId ?? 1;
|
|
192
|
+
if (s.base?.chainId !== baseChain) return fail("chain", `the settlement is for chain ${String(s.base?.chainId)}; this verifier settles Base ${baseChain}`);
|
|
193
|
+
if (s.ethereum?.chainId !== l1Chain) return fail("chain", `the settlement names chain ${String(s.ethereum?.chainId)}; this verifier settles on Ethereum ${l1Chain}`);
|
|
194
|
+
|
|
195
|
+
// 1. The output root from its preimage.
|
|
196
|
+
let outputRoot: Uint8Array;
|
|
197
|
+
try {
|
|
198
|
+
if (!bytesEqual(b32(s.outputRoot.version, "outputRoot.version"), ZERO32)) return fail("output-root", "only output-root version 0 (32 zero bytes) is defined");
|
|
199
|
+
outputRoot = computeOutputRoot(s.outputRoot);
|
|
200
|
+
} catch (e) {
|
|
201
|
+
return fail("output-root", (e as Error).message);
|
|
202
|
+
}
|
|
203
|
+
pass("output-root", `${bytesToHex(outputRoot)} for Base block ${s.outputRoot.blockNumber}`);
|
|
204
|
+
|
|
205
|
+
// 2. B's hash from P: directly when B = P, else by EIP-2935 storage proof.
|
|
206
|
+
const B = s.base.blockNumber;
|
|
207
|
+
const P = s.outputRoot.blockNumber;
|
|
208
|
+
let bHash: Uint8Array;
|
|
209
|
+
try {
|
|
210
|
+
bHash = b32(s.base.blockHash, "base.blockHash");
|
|
211
|
+
} catch (e) {
|
|
212
|
+
return fail("base", (e as Error).message);
|
|
213
|
+
}
|
|
214
|
+
if (!Number.isInteger(B) || !Number.isInteger(P) || B < 0) return fail("base", "block numbers must be non-negative integers");
|
|
215
|
+
if (B === P) {
|
|
216
|
+
if (s.history !== null) return fail("history", "B = P needs no history proof, and one was given");
|
|
217
|
+
if (!bytesEqual(b32(s.outputRoot.blockHash, "outputRoot.blockHash"), bHash)) return fail("history", "B = P, but the output root names a different block hash");
|
|
218
|
+
pass("history", `B = P: the output root names block ${B}'s hash directly`);
|
|
219
|
+
} else {
|
|
220
|
+
const d = P - B;
|
|
221
|
+
if (d < 1 || d > HISTORY_SERVE_WINDOW) return fail("history", `P - B is ${d}; the history contract holds blocks 1 to ${HISTORY_SERVE_WINDOW} before P`);
|
|
222
|
+
const h = s.history;
|
|
223
|
+
if (!h) return fail("history", "B < P needs a history storage proof");
|
|
224
|
+
if (h.address.toLowerCase() !== HISTORY_STORAGE_ADDRESS) return fail("history", `the proof is for ${h.address}, not the EIP-2935 contract`);
|
|
225
|
+
let slot: Uint8Array;
|
|
226
|
+
try {
|
|
227
|
+
slot = b32(h.slot, "history.slot");
|
|
228
|
+
} catch (e) {
|
|
229
|
+
return fail("history", (e as Error).message);
|
|
230
|
+
}
|
|
231
|
+
if (!bytesEqual(slot, historySlot(B))) return fail("history", `the slot is not B mod ${HISTORY_SERVE_WINDOW}`);
|
|
232
|
+
let account: Uint8Array | null;
|
|
233
|
+
try {
|
|
234
|
+
account = mptVerify(b32(s.outputRoot.stateRoot, "outputRoot.stateRoot"), keccak256(hexToBytes(HISTORY_STORAGE_ADDRESS)), h.accountProof.map(hexToBytes));
|
|
235
|
+
} catch {
|
|
236
|
+
account = null;
|
|
237
|
+
}
|
|
238
|
+
if (!account) return fail("history", "the account proof does not reach P's state root");
|
|
239
|
+
let storageRoot: Uint8Array;
|
|
240
|
+
try {
|
|
241
|
+
const fields = rlpDecode(account);
|
|
242
|
+
if (!Array.isArray(fields) || fields.length !== 4 || !(fields[2] instanceof Uint8Array) || fields[2].length !== 32) throw new TypeError("account");
|
|
243
|
+
storageRoot = fields[2];
|
|
244
|
+
} catch {
|
|
245
|
+
return fail("history", "the account record is not [nonce, balance, storageRoot, codeHash]");
|
|
246
|
+
}
|
|
247
|
+
let raw: Uint8Array | null;
|
|
248
|
+
try {
|
|
249
|
+
raw = mptVerify(storageRoot, keccak256(slot), h.storageProof.map(hexToBytes));
|
|
250
|
+
} catch {
|
|
251
|
+
raw = null;
|
|
252
|
+
}
|
|
253
|
+
if (!raw) return fail("history", "the storage proof does not reach the contract's storage root");
|
|
254
|
+
let word: Uint8Array | null;
|
|
255
|
+
try {
|
|
256
|
+
word = storageWord(rlpDecode(raw));
|
|
257
|
+
} catch {
|
|
258
|
+
word = null;
|
|
259
|
+
}
|
|
260
|
+
if (!word || !bytesEqual(word, bHash)) return fail("history", `P's state does not hold block ${B}'s hash at that slot`);
|
|
261
|
+
pass("history", `block ${B}'s hash is in block ${P}'s state (${d} blocks back)`);
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
// 3. The Ethereum transaction carries the output root, and is in block H.
|
|
265
|
+
const e = s.ethereum;
|
|
266
|
+
let header;
|
|
267
|
+
try {
|
|
268
|
+
header = decodeHeader(hexToBytes(e.header));
|
|
269
|
+
} catch (err) {
|
|
270
|
+
return fail("ethereum", `header: ${(err as Error).message}`);
|
|
271
|
+
}
|
|
272
|
+
if (header.hash !== e.blockHash.toLowerCase()) return fail("ethereum", "the header does not hash to blockHash");
|
|
273
|
+
if (header.number !== e.blockNumber) return fail("ethereum", `the header is block ${header.number}, the settlement says ${e.blockNumber}`);
|
|
274
|
+
if (header.timestamp !== e.blockTimestamp) return fail("ethereum", `the header's timestamp is ${header.timestamp}, the settlement says ${e.blockTimestamp}`);
|
|
275
|
+
let rawTx: Uint8Array;
|
|
276
|
+
try {
|
|
277
|
+
rawTx = hexToBytes(e.rawTx);
|
|
278
|
+
} catch {
|
|
279
|
+
return fail("ethereum", "rawTx is not hex");
|
|
280
|
+
}
|
|
281
|
+
if (bytesToHex(keccak256(rawTx)) !== e.txHash.toLowerCase()) return fail("ethereum", "the raw transaction does not hash to txHash");
|
|
282
|
+
const txChain = transactionChainId(rawTx);
|
|
283
|
+
if (txChain === null) return fail("ethereum", "the transaction names no chain (a legacy transaction without EIP-155)");
|
|
284
|
+
if (txChain !== BigInt(l1Chain)) return fail("ethereum", `the transaction is signed for chain ${txChain}, not Ethereum ${l1Chain}`);
|
|
285
|
+
let inBlock: Uint8Array | null;
|
|
286
|
+
try {
|
|
287
|
+
inBlock = mptVerify(hexToBytes(header.transactionsRoot), txTrieKey(e.txIndex), e.txInclusionProof.map(hexToBytes));
|
|
288
|
+
} catch {
|
|
289
|
+
inBlock = null;
|
|
290
|
+
}
|
|
291
|
+
if (!inBlock || !bytesEqual(inBlock, rawTx)) return fail("ethereum", "the transaction is not in this block");
|
|
292
|
+
if (indexOfBytes(rawTx, outputRoot) < 0) return fail("ethereum", "the transaction's bytes do not contain the output root");
|
|
293
|
+
pass("ethereum", `transaction ${e.txIndex} of Ethereum block ${header.number} carries the output root`);
|
|
294
|
+
|
|
295
|
+
return {
|
|
296
|
+
ok: true,
|
|
297
|
+
checks,
|
|
298
|
+
existedBy: { chainId: e.chainId, blockNumber: header.number, blockHash: header.hash, blockTimestamp: header.timestamp },
|
|
299
|
+
outputRootHex: bytesToHex(outputRoot),
|
|
300
|
+
};
|
|
301
|
+
}
|