@mikeargento/bitgraph-verify 1.13.1 → 1.15.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 +1 -1
- package/dist/__tests__/carrier.test.js +19 -0
- package/dist/__tests__/carrier.test.js.map +1 -1
- package/dist/__tests__/carrier2.test.d.ts +2 -0
- package/dist/__tests__/carrier2.test.d.ts.map +1 -0
- package/dist/__tests__/carrier2.test.js +172 -0
- package/dist/__tests__/carrier2.test.js.map +1 -0
- package/dist/__tests__/ceiling.test.d.ts +8 -0
- package/dist/__tests__/ceiling.test.d.ts.map +1 -0
- package/dist/__tests__/ceiling.test.js +247 -0
- package/dist/__tests__/ceiling.test.js.map +1 -0
- package/dist/__tests__/settlement.test.d.ts +12 -0
- package/dist/__tests__/settlement.test.d.ts.map +1 -0
- package/dist/__tests__/settlement.test.js +185 -0
- package/dist/__tests__/settlement.test.js.map +1 -0
- package/dist/carrier-assemble.d.ts +33 -0
- package/dist/carrier-assemble.d.ts.map +1 -0
- package/dist/carrier-assemble.js +57 -0
- package/dist/carrier-assemble.js.map +1 -0
- package/dist/carrier-verify.d.ts +47 -1
- package/dist/carrier-verify.d.ts.map +1 -1
- package/dist/carrier-verify.js +292 -53
- package/dist/carrier-verify.js.map +1 -1
- package/dist/carrier.d.ts +93 -3
- package/dist/carrier.d.ts.map +1 -1
- package/dist/carrier.js +144 -19
- package/dist/carrier.js.map +1 -1
- package/dist/ceiling-evm.d.ts +49 -0
- package/dist/ceiling-evm.d.ts.map +1 -0
- package/dist/ceiling-evm.js +331 -0
- package/dist/ceiling-evm.js.map +1 -0
- package/dist/ceiling-trie.d.ts +9 -0
- package/dist/ceiling-trie.d.ts.map +1 -0
- package/dist/ceiling-trie.js +113 -0
- package/dist/ceiling-trie.js.map +1 -0
- package/dist/ceiling.d.ts +123 -0
- package/dist/ceiling.d.ts.map +1 -0
- package/dist/ceiling.js +247 -0
- package/dist/ceiling.js.map +1 -0
- package/dist/fuse-member.js +2 -2
- package/dist/fuse-member.js.map +1 -1
- package/dist/fuse-verify.js +3 -3
- package/dist/fuse-verify.js.map +1 -1
- package/dist/fuse.d.ts +34 -2
- package/dist/fuse.d.ts.map +1 -1
- package/dist/fuse.js +83 -6
- package/dist/fuse.js.map +1 -1
- package/dist/index.d.ts +15 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +13 -2
- package/dist/index.js.map +1 -1
- package/dist/nitro.d.ts +128 -0
- package/dist/nitro.d.ts.map +1 -0
- package/dist/nitro.js +551 -0
- package/dist/nitro.js.map +1 -0
- package/dist/settlement.d.ts +114 -0
- package/dist/settlement.d.ts.map +1 -0
- package/dist/settlement.js +194 -0
- package/dist/settlement.js.map +1 -0
- package/package.json +2 -1
- package/src/__tests__/carrier.test.ts +18 -0
- package/src/__tests__/carrier2.test.ts +193 -0
- package/src/__tests__/ceiling.test.ts +281 -0
- package/src/__tests__/fixtures/carrier2/ceiling-26088459.witness.json +1 -0
- package/src/__tests__/fixtures/carrier2/ceiling-4548.anchor.json +1 -0
- package/src/__tests__/fixtures/carrier2/demo4546.ceiling.json +106 -0
- package/src/__tests__/fixtures/carrier2/demo4546.proof.json +47 -0
- package/src/__tests__/fixtures/carrier2/demo4546.txt +29 -0
- package/src/__tests__/fixtures/carrier2/floor-26088457.witness.json +1 -0
- package/src/__tests__/fixtures/carrier2/floor-4542.anchor.json +1 -0
- package/src/__tests__/fixtures/ceiling/floor-26037892.header.hex +1 -0
- package/src/__tests__/fixtures/settlement/pointer-51979918-tx1.json +39 -0
- package/src/__tests__/fixtures/settlement/pointer-51979918.json +69 -0
- package/src/__tests__/settlement.test.ts +215 -0
- package/src/carrier-assemble.ts +74 -0
- package/src/carrier-verify.ts +328 -47
- package/src/carrier.ts +174 -18
- package/src/ceiling-evm.ts +326 -0
- package/src/ceiling-trie.ts +112 -0
- package/src/ceiling.ts +333 -0
- package/src/fuse-member.ts +2 -2
- package/src/fuse-verify.ts +3 -3
- package/src/fuse.ts +93 -6
- package/src/index.ts +42 -2
- package/src/nitro.ts +602 -0
- package/src/settlement.ts +263 -0
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
// Copyright (c) 2024-2026 Argento Computing Inc. Licensed under the MIT License. See LICENSE.
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Settlement of a Base ceiling on Ethereum: L1 DATA INCLUSION.
|
|
5
|
+
*
|
|
6
|
+
* A Base block's transactions reach Ethereum as batch data: Base's batcher
|
|
7
|
+
* posts blobs (EIP-4844) to the batch inbox address. The blob that carries the
|
|
8
|
+
* batch containing the ceiling transaction was committed by an Ethereum block,
|
|
9
|
+
* so the ceiling transaction's bytes existed by that Ethereum block's time,
|
|
10
|
+
* whatever Base's own state says. This is a ceiling in time with Ethereum as
|
|
11
|
+
* the root, and it arrives minutes after the Base block ("safe"), not after
|
|
12
|
+
* the week-long fault-proof window.
|
|
13
|
+
*
|
|
14
|
+
* Two layers, because Ethereum nodes prune blob bytes after about 18 days and
|
|
15
|
+
* keep only the commitment:
|
|
16
|
+
*
|
|
17
|
+
* POINTER (this module, offline, no blobs): the Ethereum block header hashes
|
|
18
|
+
* to its hash; the batcher transaction is in that block (Merkle-Patricia
|
|
19
|
+
* proof against transactionsRoot); it is a type-3 transaction to the batch
|
|
20
|
+
* inbox, signed by the batcher; and it lists the blob versioned hashes the
|
|
21
|
+
* record names. Which addresses are Base's batcher and inbox is a PIN the
|
|
22
|
+
* verifier supplies; the record never vouches for its own pins.
|
|
23
|
+
*
|
|
24
|
+
* BLOBS (bitgraph-audit, Node only): with the blob bytes kept beside the
|
|
25
|
+
* package, each blob is verified against its KZG commitment (versioned hash
|
|
26
|
+
* = 0x01 || SHA-256(commitment)[1:]), decoded to frames, assembled into the
|
|
27
|
+
* channel, decompressed, and the Base block and the ceiling transaction are
|
|
28
|
+
* located inside the batch. That closes the loop from Ethereum's block to the
|
|
29
|
+
* ceiling transaction's bytes.
|
|
30
|
+
*
|
|
31
|
+
* What it does not prove: that Base's derivation accepted the batch (a posted
|
|
32
|
+
* batch can still be rejected by the rollup rules), and that the Ethereum
|
|
33
|
+
* header is canonical (offline it is taken as given; one lookup confirms it).
|
|
34
|
+
*/
|
|
35
|
+
|
|
36
|
+
import { decodeHeader, hexToBytes, keccak256, mptVerify, txTrieKey, bytesEqual, bytesToHex } from "./ceiling-evm.js";
|
|
37
|
+
import { sha256 } from "@noble/hashes/sha256";
|
|
38
|
+
|
|
39
|
+
export const SETTLEMENT_VERSION = "bitgraph-settlement/1";
|
|
40
|
+
|
|
41
|
+
/** Base mainnet's batch inbox and batcher on Ethereum mainnet, as pins a verifier names (never read from the record). */
|
|
42
|
+
export const BASE_MAINNET_SETTLEMENT_PINS = Object.freeze({
|
|
43
|
+
l1ChainId: 1,
|
|
44
|
+
batchInbox: "0xff00000000000000000000000000000000008453",
|
|
45
|
+
batcher: "0x5050f69a9786f081509234f1a7f4684b5e5b76c9",
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
export interface SettlementPins {
|
|
49
|
+
l1ChainId: number;
|
|
50
|
+
batchInbox: string;
|
|
51
|
+
batcher: string;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export interface SettlementBlobRef {
|
|
55
|
+
/** 0x01 || SHA-256(kzgCommitment)[1:], as the transaction lists it. */
|
|
56
|
+
versionedHash: string;
|
|
57
|
+
/** 48-byte KZG commitment, 0x hex. */
|
|
58
|
+
kzgCommitment: string;
|
|
59
|
+
/** Index among the transaction's blobs. */
|
|
60
|
+
index: number;
|
|
61
|
+
/** Where the blob bytes are kept beside the package (a file name), when they are. */
|
|
62
|
+
file?: string;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** The record a ceiling carries once its Base block's batch is on Ethereum. */
|
|
66
|
+
export interface SettlementPointer {
|
|
67
|
+
version: typeof SETTLEMENT_VERSION;
|
|
68
|
+
/** The Base block the ceiling transaction is in. */
|
|
69
|
+
baseBlockNumber: number;
|
|
70
|
+
baseBlockHash: string;
|
|
71
|
+
l1: {
|
|
72
|
+
chainId: number;
|
|
73
|
+
txHash: string;
|
|
74
|
+
/** The signed type-3 transaction without its blobs (the network form), 0x hex. */
|
|
75
|
+
rawTx: string;
|
|
76
|
+
blockNumber: number;
|
|
77
|
+
blockHash: string;
|
|
78
|
+
blockTimestamp: number;
|
|
79
|
+
/** RLP, 0x hex; hashes to blockHash. */
|
|
80
|
+
blockHeader: string;
|
|
81
|
+
txIndex: number;
|
|
82
|
+
/** Merkle-Patricia proof of rawTx in the header's transactionsRoot. */
|
|
83
|
+
txInclusionProof: string[];
|
|
84
|
+
};
|
|
85
|
+
/**
|
|
86
|
+
* The blobs of that transaction a reader needs, in order: from the first carrying a frame of the
|
|
87
|
+
* channel holding this Base block, up to the one whose frames complete the ceiling transaction's
|
|
88
|
+
* bytes (a prefix of the compressed stream decodes to a prefix of the channel). A writer may list
|
|
89
|
+
* every channel blob instead; each listed one must be among the transaction's.
|
|
90
|
+
*/
|
|
91
|
+
blobs: SettlementBlobRef[];
|
|
92
|
+
/** The channel the Base block was found in, for readers that decode the blobs. */
|
|
93
|
+
channel?: { id: string; frames: number; compression: "brotli" | "zlib"; firstBaseBlock: number; lastBaseBlock: number };
|
|
94
|
+
/** Where the ceiling transaction's bytes sit, once located by the blob layer. */
|
|
95
|
+
located?: { baseTxIndex: number; txHash: string };
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export interface SettlementCheck {
|
|
99
|
+
name: string;
|
|
100
|
+
ok: boolean;
|
|
101
|
+
detail?: string;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export interface SettlementPointerResult {
|
|
105
|
+
ok: boolean;
|
|
106
|
+
reason?: string;
|
|
107
|
+
checks: SettlementCheck[];
|
|
108
|
+
/** The Ethereum block's time, read from the verified header; the ceiling transaction existed by it. */
|
|
109
|
+
existedBy: { blockNumber: number; blockHash: string; blockTimestamp: number } | null;
|
|
110
|
+
/** Always false here: the header is taken as given offline; see checkSettlementOnline. */
|
|
111
|
+
headerCheckedAgainstChain: false;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Offline checks of the pointer, no blobs needed. `expectBlobs` is the list of
|
|
116
|
+
* versioned hashes the ceiling record names (all of the pointer's `blobs`
|
|
117
|
+
* when the caller has no other source).
|
|
118
|
+
*/
|
|
119
|
+
export function verifySettlementPointer(pointer: SettlementPointer, pins: SettlementPins): SettlementPointerResult {
|
|
120
|
+
const checks: SettlementCheck[] = [];
|
|
121
|
+
const fail = (name: string, detail: string): SettlementPointerResult => {
|
|
122
|
+
checks.push({ name, ok: false, detail });
|
|
123
|
+
return { ok: false, reason: `${name}: ${detail}`, checks, existedBy: null, headerCheckedAgainstChain: false };
|
|
124
|
+
};
|
|
125
|
+
const pass = (name: string, detail?: string) => checks.push(detail === undefined ? { name, ok: true } : { name, ok: true, detail });
|
|
126
|
+
|
|
127
|
+
if (pointer?.version !== SETTLEMENT_VERSION) return fail("format", `not a ${SETTLEMENT_VERSION} record`);
|
|
128
|
+
const l1 = pointer.l1;
|
|
129
|
+
if (!l1 || typeof l1 !== "object") return fail("format", "no l1 section");
|
|
130
|
+
if (l1.chainId !== pins.l1ChainId) return fail("chain", `the record names chain ${l1.chainId}, expected ${pins.l1ChainId}`);
|
|
131
|
+
|
|
132
|
+
// 1. The header is the one with that hash, and its time is the bound.
|
|
133
|
+
let header;
|
|
134
|
+
try {
|
|
135
|
+
header = decodeHeader(hexToBytes(l1.blockHeader));
|
|
136
|
+
} catch (e) {
|
|
137
|
+
return fail("header", (e as Error).message);
|
|
138
|
+
}
|
|
139
|
+
if (header.hash !== l1.blockHash.toLowerCase()) return fail("header", "the header does not hash to blockHash");
|
|
140
|
+
if (header.number !== l1.blockNumber) return fail("header", `the header is block ${header.number}, the record says ${l1.blockNumber}`);
|
|
141
|
+
if (header.timestamp !== l1.blockTimestamp) return fail("header", `the header's timestamp is ${header.timestamp}, the record says ${l1.blockTimestamp}`);
|
|
142
|
+
pass("header", `Ethereum block ${header.number} hashes to its hash`);
|
|
143
|
+
|
|
144
|
+
// 2. The batcher transaction is in the block.
|
|
145
|
+
let rawTx: Uint8Array;
|
|
146
|
+
try {
|
|
147
|
+
rawTx = hexToBytes(l1.rawTx);
|
|
148
|
+
} catch (e) {
|
|
149
|
+
return fail("transaction", (e as Error).message);
|
|
150
|
+
}
|
|
151
|
+
const txHash = bytesToHex(keccak256(rawTx));
|
|
152
|
+
if (txHash !== l1.txHash.toLowerCase()) return fail("transaction", "the raw transaction does not hash to txHash");
|
|
153
|
+
const inBlock = mptVerify(hexToBytes(header.transactionsRoot), txTrieKey(l1.txIndex), l1.txInclusionProof.map(hexToBytes));
|
|
154
|
+
if (!inBlock || !bytesEqual(inBlock, rawTx)) return fail("inclusion", "the transaction is not in this block");
|
|
155
|
+
pass("inclusion", `transaction ${l1.txIndex} of block ${header.number}`);
|
|
156
|
+
|
|
157
|
+
// 3. It is a blob transaction to the batch inbox, from the batcher, listing the blobs.
|
|
158
|
+
let tx: Eip4844Tx;
|
|
159
|
+
try {
|
|
160
|
+
tx = decodeEip4844(rawTx);
|
|
161
|
+
} catch (e) {
|
|
162
|
+
return fail("transaction", (e as Error).message);
|
|
163
|
+
}
|
|
164
|
+
if (tx.chainId !== BigInt(pins.l1ChainId)) return fail("chain", `the transaction is for chain ${tx.chainId}`);
|
|
165
|
+
if (tx.to !== pins.batchInbox.toLowerCase()) return fail("inbox", `the transaction is addressed to ${tx.to}, not the batch inbox`);
|
|
166
|
+
if (tx.from !== pins.batcher.toLowerCase()) return fail("batcher", `the transaction was signed by ${tx.from}, not the batcher`);
|
|
167
|
+
const listed = new Set(tx.blobVersionedHashes.map((h) => h.toLowerCase()));
|
|
168
|
+
if (!Array.isArray(pointer.blobs) || pointer.blobs.length === 0) return fail("blobs", "the record names no blobs");
|
|
169
|
+
for (const b of pointer.blobs) {
|
|
170
|
+
const vh = b.versionedHash.toLowerCase();
|
|
171
|
+
if (!listed.has(vh)) return fail("blobs", `blob ${vh.slice(0, 18)}… is not listed by the transaction`);
|
|
172
|
+
const expect = versionedHashOf(hexToBytes(b.kzgCommitment));
|
|
173
|
+
if (expect !== vh) return fail("blobs", `blob ${vh.slice(0, 18)}…: the versioned hash is not 0x01 || SHA-256(commitment)[1:]`);
|
|
174
|
+
}
|
|
175
|
+
pass("blobs", `${pointer.blobs.length} blob commitment${pointer.blobs.length === 1 ? "" : "s"} listed by the transaction`);
|
|
176
|
+
|
|
177
|
+
return {
|
|
178
|
+
ok: true,
|
|
179
|
+
checks,
|
|
180
|
+
existedBy: { blockNumber: header.number, blockHash: header.hash, blockTimestamp: header.timestamp },
|
|
181
|
+
headerCheckedAgainstChain: false,
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/** 0x01 || SHA-256(commitment)[1:], lowercase 0x hex. */
|
|
186
|
+
export function versionedHashOf(kzgCommitment: Uint8Array): string {
|
|
187
|
+
const h = sha256(kzgCommitment);
|
|
188
|
+
h[0] = 0x01;
|
|
189
|
+
return bytesToHex(h);
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/** The one online question: is that Ethereum header the chain's own block? `getBlockHash` is injected; this module never fetches. */
|
|
193
|
+
export async function checkSettlementOnline(
|
|
194
|
+
pointer: SettlementPointer,
|
|
195
|
+
getBlockHash: (blockNumber: number) => Promise<string | null>,
|
|
196
|
+
): Promise<{ ok: boolean; detail: string }> {
|
|
197
|
+
const chainHash = await getBlockHash(pointer.l1.blockNumber);
|
|
198
|
+
if (chainHash === null) return { ok: false, detail: `the node has no block ${pointer.l1.blockNumber}` };
|
|
199
|
+
if (chainHash.toLowerCase() !== pointer.l1.blockHash.toLowerCase()) return { ok: false, detail: `the node's block ${pointer.l1.blockNumber} is ${chainHash}, not the record's header` };
|
|
200
|
+
return { ok: true, detail: `Ethereum block ${pointer.l1.blockNumber} confirmed against the node` };
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/* ── EIP-4844 (type 3) transaction decoding, network form without blobs ─── */
|
|
204
|
+
|
|
205
|
+
import { rlpDecode, rlpEncode, rlpUint, type RlpItem } from "./ceiling-evm.js";
|
|
206
|
+
import { secp256k1 } from "@noble/curves/secp256k1.js";
|
|
207
|
+
|
|
208
|
+
export interface Eip4844Tx {
|
|
209
|
+
chainId: bigint;
|
|
210
|
+
nonce: bigint;
|
|
211
|
+
to: string | null;
|
|
212
|
+
data: Uint8Array;
|
|
213
|
+
blobVersionedHashes: string[];
|
|
214
|
+
/** Address recovered from the signature, lowercase 0x hex. */
|
|
215
|
+
from: string;
|
|
216
|
+
/** keccak256 of the raw transaction: its hash on chain. */
|
|
217
|
+
hash: string;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
function asList(item: RlpItem | undefined, what: string): RlpItem[] {
|
|
221
|
+
if (!Array.isArray(item)) throw new TypeError(`${what}: expected a list`);
|
|
222
|
+
return item;
|
|
223
|
+
}
|
|
224
|
+
function asBytes(item: RlpItem | undefined, what: string): Uint8Array {
|
|
225
|
+
if (!(item instanceof Uint8Array)) throw new TypeError(`${what}: expected bytes`);
|
|
226
|
+
return item;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* Decode a signed type-3 transaction, 0x03 || rlp([chain_id, nonce,
|
|
231
|
+
* max_priority_fee_per_gas, max_fee_per_gas, gas_limit, to, value, data,
|
|
232
|
+
* access_list, max_fee_per_blob_gas, blob_versioned_hashes, y_parity, r, s]),
|
|
233
|
+
* and recover its sender. This is the network form the block's trie holds;
|
|
234
|
+
* the blob-carrying wrapper is not accepted.
|
|
235
|
+
*/
|
|
236
|
+
export function decodeEip4844(raw: Uint8Array): Eip4844Tx {
|
|
237
|
+
if (raw.length < 2 || raw[0] !== 0x03) throw new TypeError("tx: not an EIP-4844 (type 3) transaction");
|
|
238
|
+
const f = asList(rlpDecode(raw.slice(1)), "tx");
|
|
239
|
+
if (f.length !== 14) throw new TypeError("tx: a type-3 transaction has 14 fields");
|
|
240
|
+
const chainId = rlpUint(f[0], "tx.chainId");
|
|
241
|
+
const nonce = rlpUint(f[1], "tx.nonce");
|
|
242
|
+
const toBytes = asBytes(f[5], "tx.to");
|
|
243
|
+
if (toBytes.length !== 20) throw new TypeError("tx: a blob transaction names a recipient");
|
|
244
|
+
const data = asBytes(f[7], "tx.data");
|
|
245
|
+
asList(f[8], "tx.accessList");
|
|
246
|
+
const hashes = asList(f[10], "tx.blobVersionedHashes").map((h) => bytesToHex(asBytes(h, "tx.blobVersionedHashes[]")));
|
|
247
|
+
if (hashes.length === 0) throw new TypeError("tx: a blob transaction lists at least one blob");
|
|
248
|
+
const yParity = rlpUint(f[11], "tx.yParity");
|
|
249
|
+
const r = rlpUint(f[12], "tx.r");
|
|
250
|
+
const s = rlpUint(f[13], "tx.s");
|
|
251
|
+
if (yParity > 1n) throw new TypeError("tx: yParity must be 0 or 1");
|
|
252
|
+
const unsigned = rlpEncode(f.slice(0, 11));
|
|
253
|
+
const preimage = new Uint8Array(1 + unsigned.length);
|
|
254
|
+
preimage[0] = 0x03;
|
|
255
|
+
preimage.set(unsigned, 1);
|
|
256
|
+
const signingHash = keccak256(preimage);
|
|
257
|
+
const compact = hexToBytes(r.toString(16).padStart(64, "0") + s.toString(16).padStart(64, "0"));
|
|
258
|
+
const sig = secp256k1.Signature.fromBytes(compact, "compact");
|
|
259
|
+
if (sig.hasHighS()) throw new TypeError("tx: high-s signature");
|
|
260
|
+
const pub = sig.addRecoveryBit(Number(yParity)).recoverPublicKey(signingHash).toBytes(false);
|
|
261
|
+
const from = bytesToHex(keccak256(pub.slice(1)).slice(12));
|
|
262
|
+
return { chainId, nonce, to: bytesToHex(toBytes), data, blobVersionedHashes: hashes, from, hash: bytesToHex(keccak256(raw)) };
|
|
263
|
+
}
|