@layerzerolabs/lz-proof-utility 1.5.25 → 1.5.27

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/index.cjs ADDED
@@ -0,0 +1,488 @@
1
+ 'use strict';
2
+
3
+ var ethers = require('ethers');
4
+ var invariant2 = require('tiny-invariant');
5
+ var ethUtils = require('ethereumjs-util');
6
+ var nibbles = require('merkle-patricia-tree/util/nibbles');
7
+ var ethObject = require('eth-object');
8
+ var ethUtilLite = require('eth-util-lite');
9
+ var promisfy = require('promisfy');
10
+ var saferBuffer = require('safer-buffer');
11
+
12
+ function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
13
+
14
+ function _interopNamespace(e) {
15
+ if (e && e.__esModule) return e;
16
+ var n = Object.create(null);
17
+ if (e) {
18
+ Object.keys(e).forEach(function (k) {
19
+ if (k !== 'default') {
20
+ var d = Object.getOwnPropertyDescriptor(e, k);
21
+ Object.defineProperty(n, k, d.get ? d : {
22
+ enumerable: true,
23
+ get: function () { return e[k]; }
24
+ });
25
+ }
26
+ });
27
+ }
28
+ n.default = e;
29
+ return Object.freeze(n);
30
+ }
31
+
32
+ var invariant2__default = /*#__PURE__*/_interopDefault(invariant2);
33
+ var ethUtils__namespace = /*#__PURE__*/_interopNamespace(ethUtils);
34
+
35
+ var __defProp = Object.defineProperty;
36
+ var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
37
+ get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
38
+ }) : x)(function(x) {
39
+ if (typeof require !== "undefined")
40
+ return require.apply(this, arguments);
41
+ throw new Error('Dynamic require of "' + x + '" is not supported');
42
+ });
43
+ var __export = (target, all) => {
44
+ for (var name in all)
45
+ __defProp(target, name, { get: all[name], enumerable: true });
46
+ };
47
+
48
+ // src/constants.ts
49
+ var NETWORKS = ["default", "hardhat", "arbitrum", "polygon", "harmony"];
50
+ var OutboundProofType = {
51
+ MPT: 1,
52
+ FP: 2
53
+ };
54
+ var EVMUtilityVersion = {
55
+ V1: 1,
56
+ V2: 2,
57
+ V3: 3,
58
+ V4: 4
59
+ };
60
+ var UTILS_VERSION_LOOKUP = {
61
+ arbitrum: {
62
+ "0x4ee2f9b7cf3a68966c370f3eb2c16613d3235245": 1,
63
+ "0x50500ccd11e29f14008c7778a6ced655727a21c3": 2,
64
+ "0x5b23e2bae5c5f00e804ea2c4c9abe601604378fa": 2
65
+ },
66
+ avalanche: {
67
+ "0x4ee2f9b7cf3a68966c370f3eb2c16613d3235245": 1,
68
+ "0x50500ccd11e29f14008c7778a6ced655727a21c3": 2,
69
+ "0xcbd35a9b849342ad34a71e072d9947d4afb4e164": 2
70
+ },
71
+ bsc: {
72
+ "0x4ee2f9b7cf3a68966c370f3eb2c16613d3235245": 1,
73
+ "0x50500ccd11e29f14008c7778a6ced655727a21c3": 2,
74
+ "0xcff08a35a5f27f306e2da99ff198db90f13def77": 2
75
+ },
76
+ "bsc-testnet": {
77
+ "0xfc256f1007e67abbd027536818142823b4596a24": 1,
78
+ "0x1de92e6b8e8b6b150ea8588fe387a4f5fb4da778": 2,
79
+ "0x76860669184330e6966a61188cfbd13486c7a6a6": 2,
80
+ "0x4652b61781f5ee79dad354ab5d249aa2b99b4def": 2
81
+ },
82
+ "bsc-sandbox": {
83
+ "0xf96093134adf151889ed72517979b2cd7144a831": 1,
84
+ "0x1751181f9af72b37934006bbe989238498067563": 2,
85
+ "0x8953031e74a172586e89ddb40b7d269b9186e74c": 2
86
+ },
87
+ ethereum: {
88
+ "0x2d61dcdd36f10b22176e0433b86f74567d529aaa": 1,
89
+ "0x50500ccd11e29f14008c7778a6ced655727a21c3": 2,
90
+ "0xe9ae261d3aff7d3fccf38fa2d612dd3897e07b2d": 2
91
+ },
92
+ fantom: {
93
+ "0x3c2269811836af69497e5f486a85d7316753cf62": 1,
94
+ "0x50500ccd11e29f14008c7778a6ced655727a21c3": 2,
95
+ "0x6f1686189f32e78f1d83e7c6ed433fcebc3a5b51": 2
96
+ },
97
+ "fantom-testnet": {
98
+ "0x0c3d09a7d86652f5599b836ce3db685a86513487": 1,
99
+ "0x7669ac2bb50d567fd4bfb1cb5154b79c1e5e4a92": 2,
100
+ "0xdafbd7a68e4ddcaa864c6a1171bd6030d0ca9a43": 2,
101
+ "0x843986e31e0e3fea5eaf01086f36e7d85c458bb1": 2
102
+ },
103
+ "fantom-sandbox": {
104
+ "0x54f51642779b9dbf48cc653c40499c82b8f261b3": 1,
105
+ "0x01de566a13dc4be329351d930b0d9a569a495e46": 2,
106
+ "0xf96093134adf151889ed72517979b2cd7144a831": 2
107
+ },
108
+ "avalanche-testnet": {
109
+ "0xa7ac9fadbe9f51e7aa96751aa53f4cbb8a07b9ba": 1,
110
+ "0x08c2e2b98d35973a4b2ca7ce8815cdb7c6f474f0": 2,
111
+ "0x0a833b15305ce8b64946098db286710ace7bbe94": 2,
112
+ "0xd81a1c5c52495a23f75d7269c17c73cebea61233": 2
113
+ },
114
+ "avalanche-sandbox": {
115
+ "0x8953031e74a172586e89ddb40b7d269b9186e74c": 1,
116
+ "0x4d7248986395d24ec777b16ca3fa32de64ec0330": 2,
117
+ "0x4489a462f812d6dca0bde380ebdac12eb5a9e85a": 2
118
+ },
119
+ "polygon-testnet": {
120
+ "0x2c7b02ffbc9602e8c04901609ef17cb1c4c2a7f7": 1,
121
+ "0x3f6055024105d22b9400e025a1ca3259bd8b1893": 2,
122
+ "0xcb97d107a87c5e172e075de94d7ef4498f6f06d2": 2,
123
+ "0xd8b2de57ccfbbd5cd1713542cb6790fd2e33fed6": 2
124
+ },
125
+ "polygon-sandbox": {
126
+ "0x369cc088dcedaaad27df0185c4f78caae42ff942": 1,
127
+ "0x46f703c2a92874d5d526878a2ccc44e9431720a5": 2,
128
+ "0xee404727abfca5d08a2480b0b881745f25362828": 2
129
+ },
130
+ optimism: {
131
+ "0x4ee2f9b7cf3a68966c370f3eb2c16613d3235245": 1,
132
+ "0x50500ccd11e29f14008c7778a6ced655727a21c3": 2,
133
+ "0x5b23e2bae5c5f00e804ea2c4c9abe601604378fa": 2
134
+ },
135
+ polygon: {
136
+ "0x4ee2f9b7cf3a68966c370f3eb2c16613d3235245": 1,
137
+ "0x50500ccd11e29f14008c7778a6ced655727a21c3": 2,
138
+ "0x6f1686189f32e78f1d83e7c6ed433fcebc3a5b51": 2
139
+ }
140
+ };
141
+
142
+ // src/proof.ts
143
+ var proof_exports = {};
144
+ __export(proof_exports, {
145
+ encodeParams: () => encodeParams,
146
+ getFeatherProof: () => getFeatherProof,
147
+ getLayerZeroPacket: () => getLayerZeroPacket,
148
+ getReceiptProof: () => getReceiptProof3
149
+ });
150
+ var Tree = __require("merkle-patricia-tree");
151
+ function getPolygonStateSyncTxHash(block) {
152
+ return ethUtils__namespace.bufferToHex(
153
+ ethUtils__namespace.keccak256(
154
+ saferBuffer.Buffer.concat([
155
+ ethUtils__namespace.toBuffer("matic-bor-receipt-"),
156
+ // prefix for bor receipt
157
+ ethUtils__namespace.setLengthLeft(ethUtils__namespace.toBuffer(block.number), 8),
158
+ // 8 bytes of block number (BigEndian)
159
+ ethUtils__namespace.toBuffer(block.hash)
160
+ // block hash
161
+ ])
162
+ )
163
+ );
164
+ }
165
+ function buffer2hex(buffer) {
166
+ return "0x" + buffer.toString("hex");
167
+ }
168
+ async function receiptProofFrom(network, block, transactionReceipts, transactionIndex) {
169
+ if (network === "polygon") {
170
+ const ignoredTxnHash = getPolygonStateSyncTxHash(block);
171
+ transactionReceipts = transactionReceipts.filter((receipt) => receipt.transactionHash !== ignoredTxnHash);
172
+ }
173
+ const tree = new Tree();
174
+ await Promise.all(
175
+ transactionReceipts.map((siblingReceipt, index) => {
176
+ const siblingPath = ethUtilLite.encode(index);
177
+ if (network === "harmony" && index >= block.transactions.length) {
178
+ siblingReceipt.type = 0;
179
+ }
180
+ let serializedReceipt = ethObject.Receipt.fromRpc(siblingReceipt);
181
+ if (network === "arbitrum") {
182
+ serializedReceipt[0] = ethUtilLite.toBuffer(0);
183
+ }
184
+ serializedReceipt = serializedReceipt.serialize();
185
+ if (siblingReceipt.type) {
186
+ serializedReceipt = saferBuffer.Buffer.concat([ethUtilLite.toBuffer(siblingReceipt.type), serializedReceipt]);
187
+ }
188
+ return promisfy.promisfy(tree.put, tree)(siblingPath, serializedReceipt);
189
+ })
190
+ );
191
+ const [_, __, stack] = await promisfy.promisfy(tree.findPath, tree)(ethUtilLite.encode(transactionIndex));
192
+ const receiptRootFromBlock = block.receiptsRoot.slice(2);
193
+ invariant2__default.default(
194
+ receiptRootFromBlock === tree._root.toString("hex"),
195
+ "receiptRoot from rpc block != receipt root we built"
196
+ );
197
+ return {
198
+ stack,
199
+ receiptProof: ethObject.Proof.fromStack(stack)
200
+ };
201
+ }
202
+
203
+ // src/mpt/v1.ts
204
+ function findPointer(fullBytes, currentNodeElement) {
205
+ const fullBytesHex = fullBytes.toString("hex");
206
+ const slicedBytesHex = currentNodeElement.toString("hex");
207
+ const result = fullBytesHex.indexOf(slicedBytesHex);
208
+ invariant2__default.default(result >= 0, "wrong index");
209
+ return (result - 2) / 2;
210
+ }
211
+ function assertReceiptInclusion(receiptsRoot, expectedLogValue, rlpProof, pointers) {
212
+ let nextRoot = receiptsRoot;
213
+ const proofDepth = rlpProof.length;
214
+ let pointer;
215
+ let proofBytes;
216
+ for (let i = 0; i < proofDepth; i++) {
217
+ proofBytes = rlpProof[i];
218
+ if (nextRoot !== ethers.ethers.utils.keccak256(proofBytes))
219
+ return false;
220
+ if (i < pointers.length) {
221
+ pointer = 2 + pointers[i] * 2;
222
+ nextRoot = "0x" + proofBytes.substring(pointer, pointer + 64);
223
+ }
224
+ }
225
+ return true;
226
+ }
227
+ function assembleMPTProof(proof, block, transactionIndex) {
228
+ const stack = proof.stack;
229
+ const rlpNestedProof = [...proof.receiptProof].map((node) => node.map((elem) => buffer2hex(elem)));
230
+ const rlpProof = rlpNestedProof.map((node) => buffer2hex(ethUtils.rlp.encode(node)));
231
+ const receiptIndexDeci = ethers.ethers.BigNumber.from(transactionIndex).toNumber();
232
+ const encodedMerklePath = nibbles.stringToNibbles(ethUtils.rlp.encode(receiptIndexDeci));
233
+ let hashRoot = block.receiptsRoot;
234
+ const proofDepth = stack.length;
235
+ const targetReceipt = rlpNestedProof[rlpNestedProof.length - 1][1];
236
+ let proofPathCounter = 0;
237
+ const totalPathLength = encodedMerklePath.length;
238
+ const pointers = [];
239
+ let receiptSlotIndex;
240
+ for (let i = 0; i < proofDepth; i++) {
241
+ invariant2__default.default(
242
+ proofPathCounter <= totalPathLength,
243
+ `proofPathCounter wrong at ${proofPathCounter}, where totalPathLength = ${totalPathLength}`
244
+ );
245
+ const thisNode = stack[i];
246
+ const thisNodeValue = thisNode.raw;
247
+ const thisNodeRlp = rlpProof[i];
248
+ invariant2__default.default(
249
+ ethers.ethers.utils.keccak256(thisNodeRlp) === hashRoot,
250
+ `invalid hashlink at depth ${i} | proofPathCounter ${proofPathCounter} | totalPathLength = ${totalPathLength}`
251
+ );
252
+ if (thisNode.type === "branch") {
253
+ if (proofPathCounter === totalPathLength) {
254
+ invariant2__default.default(ethers.ethers.utils.keccak256(thisNodeValue[16]) === targetReceipt, "invalid branch value node");
255
+ receiptSlotIndex = 16;
256
+ } else {
257
+ const nextNibble = encodedMerklePath[proofPathCounter];
258
+ invariant2__default.default(
259
+ nextNibble <= 16,
260
+ `invalid nibble at ${nextNibble} | proofPathCounter ${proofPathCounter} | totalPathLength = ${totalPathLength}`
261
+ );
262
+ hashRoot = buffer2hex(thisNodeValue[nextNibble]);
263
+ proofPathCounter += 1;
264
+ pointers.push(findPointer(thisNodeRlp, thisNodeValue[nextNibble]));
265
+ }
266
+ } else if (thisNode.type === "leaf" || thisNode.type === "extention") {
267
+ const progressKey = encodedMerklePath.slice(0, proofPathCounter);
268
+ const keyRemainder = encodedMerklePath.slice(nibbles.matchingNibbleLength(progressKey, encodedMerklePath));
269
+ proofPathCounter += nibbles.matchingNibbleLength(keyRemainder, stack[i].key);
270
+ if (thisNode.type === "leaf") {
271
+ invariant2__default.default(proofPathCounter === totalPathLength, "invalid leaf node");
272
+ invariant2__default.default(
273
+ ethers.ethers.utils.keccak256(thisNodeValue[1]) === ethers.ethers.utils.keccak256(targetReceipt),
274
+ "wrong leaf value"
275
+ );
276
+ receiptSlotIndex = 1;
277
+ } else if (thisNode.type === "extention") {
278
+ hashRoot = buffer2hex(thisNodeValue[1]);
279
+ pointers.push(findPointer(thisNodeRlp, thisNodeValue[1]));
280
+ }
281
+ } else {
282
+ throw `unsupported node type in MPT ${thisNode} ${encodedMerklePath}`;
283
+ }
284
+ }
285
+ invariant2__default.default(assertReceiptInclusion(block.receiptsRoot, targetReceipt, rlpProof, pointers), "MPTLite local fails");
286
+ return {
287
+ receiptRoot: block.receiptsRoot,
288
+ proof: rlpProof,
289
+ pointers,
290
+ receiptSlotIndex
291
+ };
292
+ }
293
+ async function getReceiptProof(network, block, transactionReceipts, transactionIndex) {
294
+ const proof = await receiptProofFrom(network, block, transactionReceipts, transactionIndex);
295
+ return assembleMPTProof(proof, block, transactionIndex);
296
+ }
297
+ function assembleMPTProof2(proof, block, transactionIndex) {
298
+ const stack = proof.stack;
299
+ const rlpNestedProof = [...proof.receiptProof].map((node) => node.map((elem) => buffer2hex(elem)));
300
+ const rlpProof = rlpNestedProof.map((node) => buffer2hex(ethUtils.rlp.encode(node)));
301
+ const receiptIndexDeci = ethers.ethers.BigNumber.from(transactionIndex).toNumber();
302
+ const encodedMerklePath = nibbles.stringToNibbles(ethUtils.rlp.encode(receiptIndexDeci));
303
+ let hashRoot = block.receiptsRoot;
304
+ const proofDepth = stack.length;
305
+ const targetReceipt = rlpNestedProof[rlpNestedProof.length - 1][1];
306
+ let proofPathCounter = 0;
307
+ const totalPathLength = encodedMerklePath.length;
308
+ const path = [];
309
+ for (let i = 0; i < proofDepth; i++) {
310
+ invariant2__default.default(
311
+ proofPathCounter <= totalPathLength,
312
+ `proofPathCounter wrong at ${proofPathCounter}, where totalPathLength = ${totalPathLength}`
313
+ );
314
+ const thisNode = stack[i];
315
+ const thisNodeValue = thisNode.raw;
316
+ const thisNodeRlp = rlpProof[i];
317
+ invariant2__default.default(
318
+ ethers.ethers.utils.keccak256(thisNodeRlp) === hashRoot,
319
+ `invalid hashlink at depth ${i} | proofPathCounter ${proofPathCounter} | totalPathLength = ${totalPathLength}`
320
+ );
321
+ if (thisNode.type === "branch") {
322
+ if (proofPathCounter === totalPathLength) {
323
+ invariant2__default.default(ethers.ethers.utils.keccak256(thisNodeValue[16]) === targetReceipt, "invalid branch value node");
324
+ path[i] = 16;
325
+ console.warn(`a branch node 16 value type ${transactionIndex} receipt root ${block.receiptsRoot}`);
326
+ } else {
327
+ const nextNibble = encodedMerklePath[proofPathCounter];
328
+ invariant2__default.default(
329
+ nextNibble <= 16,
330
+ `invalid nibble at ${nextNibble} | proofPathCounter ${proofPathCounter} | totalPathLength = ${totalPathLength}`
331
+ );
332
+ hashRoot = buffer2hex(thisNodeValue[nextNibble]);
333
+ proofPathCounter += 1;
334
+ path[i] = nextNibble;
335
+ }
336
+ } else if (thisNode.type === "leaf" || thisNode.type === "extention") {
337
+ const progressKey = encodedMerklePath.slice(0, proofPathCounter);
338
+ const keyRemainder = encodedMerklePath.slice(nibbles.matchingNibbleLength(progressKey, encodedMerklePath));
339
+ proofPathCounter += nibbles.matchingNibbleLength(keyRemainder, stack[i].key);
340
+ if (thisNode.type === "leaf") {
341
+ invariant2__default.default(proofPathCounter === totalPathLength, "invalid leaf node");
342
+ invariant2__default.default(
343
+ ethers.ethers.utils.keccak256(thisNodeValue[1]) === ethers.ethers.utils.keccak256(targetReceipt),
344
+ "wrong leaf value"
345
+ );
346
+ path[i] = 1;
347
+ } else if (thisNode.type === "extention") {
348
+ path[i] = 1;
349
+ hashRoot = buffer2hex(thisNodeValue[1]);
350
+ }
351
+ } else {
352
+ throw `unsupported node type in MPT ${thisNode} ${encodedMerklePath}`;
353
+ }
354
+ }
355
+ invariant2__default.default(assertReceiptInclusion2(block.receiptsRoot, targetReceipt, rlpProof, path, stack), "MPTLite local fails");
356
+ return {
357
+ receiptRoot: block.receiptsRoot,
358
+ proof: rlpProof,
359
+ receiptSlotIndex: path
360
+ };
361
+ }
362
+ function assertReceiptInclusion2(receiptsRoot, expectedLogValue, rlpProof, path, stack) {
363
+ let nextRoot = receiptsRoot;
364
+ const proofDepth = rlpProof.length;
365
+ for (let i = 0; i < proofDepth; i++) {
366
+ const proofBytes = rlpProof[i];
367
+ const thisNodeValue = stack[i].raw;
368
+ if (nextRoot !== ethers.ethers.utils.keccak256(proofBytes))
369
+ return false;
370
+ if (i < proofDepth - 1) {
371
+ nextRoot = buffer2hex(thisNodeValue[path[i]]);
372
+ }
373
+ }
374
+ return true;
375
+ }
376
+ async function getReceiptProof2(network, block, transactionReceipts, transactionIndex) {
377
+ const proof = await receiptProofFrom(network, block, transactionReceipts, transactionIndex);
378
+ return assembleMPTProof2(proof, block, transactionIndex);
379
+ }
380
+
381
+ // src/proof.ts
382
+ function getFeatherProof(utilsVersion, emitterAddress, packetPayload) {
383
+ switch (utilsVersion) {
384
+ case EVMUtilityVersion.V1: {
385
+ const contractAddrByte32 = ethers.ethers.utils.hexZeroPad(emitterAddress, 32);
386
+ return {
387
+ proof: ethers.ethers.utils.solidityPack(["bytes32", "bytes"], [contractAddrByte32, packetPayload])
388
+ };
389
+ }
390
+ case EVMUtilityVersion.V2: {
391
+ return {
392
+ proof: packetPayload
393
+ };
394
+ }
395
+ default:
396
+ throw new Error(`Unknown utility version ${utilsVersion}`);
397
+ }
398
+ }
399
+ async function getReceiptProof3(network, block, transactionReceipts, transactionIndex, outboundProofType, utilsVersion) {
400
+ invariant2__default.default(NETWORKS.includes(network), `Unsupported network: ${network}`);
401
+ switch (outboundProofType) {
402
+ case OutboundProofType.MPT: {
403
+ switch (utilsVersion) {
404
+ case EVMUtilityVersion.V1: {
405
+ return await getReceiptProof(network, block, transactionReceipts, transactionIndex);
406
+ }
407
+ case EVMUtilityVersion.V2:
408
+ case EVMUtilityVersion.V4: {
409
+ return await getReceiptProof2(network, block, transactionReceipts, transactionIndex);
410
+ }
411
+ case EVMUtilityVersion.V3: {
412
+ const proof = await getReceiptProof2(network, block, transactionReceipts, transactionIndex);
413
+ proof["blockHash"] = block.hash;
414
+ return proof;
415
+ }
416
+ default:
417
+ throw new Error(`Unsupported utility version ${utilsVersion}`);
418
+ }
419
+ }
420
+ default:
421
+ throw new Error(`Unsupported Outbound Proof Type ${outboundProofType}`);
422
+ }
423
+ }
424
+ function encodeParams(proof, outboundProofType, utilsVersion, logIndex, srcEndpointId = void 0) {
425
+ switch (outboundProofType) {
426
+ case OutboundProofType.MPT: {
427
+ switch (utilsVersion) {
428
+ case EVMUtilityVersion.V1: {
429
+ return ethers.ethers.utils.defaultAbiCoder.encode(
430
+ ["uint16", "bytes[]", "uint256[]", "uint256", "uint256"],
431
+ [srcEndpointId, proof.proof, proof.pointers, proof.receiptSlotIndex, logIndex]
432
+ );
433
+ }
434
+ case EVMUtilityVersion.V2: {
435
+ return ethers.ethers.utils.defaultAbiCoder.encode(
436
+ ["uint16", "bytes[]", "uint256[]", "uint256"],
437
+ [srcEndpointId, proof.proof, proof.receiptSlotIndex, logIndex]
438
+ );
439
+ }
440
+ case EVMUtilityVersion.V3: {
441
+ return ethers.ethers.utils.defaultAbiCoder.encode(
442
+ ["uint16", "bytes32", "bytes[]", "uint256[]", "uint256"],
443
+ [srcEndpointId, proof.blockHash, proof.proof, proof.receiptSlotIndex, logIndex]
444
+ );
445
+ }
446
+ case EVMUtilityVersion.V4: {
447
+ return ethers.ethers.utils.defaultAbiCoder.encode(
448
+ ["bytes[]", "uint256[]", "uint256"],
449
+ [proof.proof, proof.receiptSlotIndex, logIndex]
450
+ );
451
+ }
452
+ default:
453
+ throw new Error(`Unsupported utility version ${utilsVersion}`);
454
+ }
455
+ }
456
+ default:
457
+ throw new Error(`Unsupported Outbound Proof Type ${outboundProofType}`);
458
+ }
459
+ }
460
+ function getLayerZeroPacket(srcChainId, _log) {
461
+ const packet = ethers.ethers.utils.defaultAbiCoder.decode(["uint16", "bytes"], _log.data);
462
+ const data = packet[1].slice(2);
463
+ const dstChainId = parseInt(packet[0]);
464
+ const nonce = ethers.ethers.BigNumber.from("0x" + data.slice(0, 16)).toNumber();
465
+ const srcAddress = "0x" + data.slice(16, 56);
466
+ const dstAddress = "0x" + data.slice(56, 96);
467
+ let payload = "0x";
468
+ if (data.length > 96) {
469
+ payload = "0x" + data.slice(96, data.length);
470
+ }
471
+ return {
472
+ srcChainId,
473
+ dstChainId,
474
+ nonce,
475
+ dstAddress,
476
+ srcAddress,
477
+ ulnAddress: ethers.ethers.utils.defaultAbiCoder.encode(["address"], [_log.address]),
478
+ payload
479
+ };
480
+ }
481
+
482
+ exports.EVMUtilityVersion = EVMUtilityVersion;
483
+ exports.NETWORKS = NETWORKS;
484
+ exports.OutboundProofType = OutboundProofType;
485
+ exports.UTILS_VERSION_LOOKUP = UTILS_VERSION_LOOKUP;
486
+ exports.proofUtils = proof_exports;
487
+ //# sourceMappingURL=out.js.map
488
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/constants.ts","../src/proof.ts","../src/mpt/v1.ts","../src/mpt/common.ts","../src/mpt/v2.ts"],"names":["getReceiptProof","ethers","invariant","Buffer","rlp","matchingNibbleLength","stringToNibbles","assembleMPTProof","assertReceiptInclusion"],"mappings":";;;;;;;;;;;;;;AAAO,IAAM,WAAW,CAAC,WAAW,WAAW,YAAY,WAAW,SAAS;AAExE,IAAM,oBAAoB;AAAA,EAC7B,KAAK;AAAA,EACL,IAAI;AACR;AAEO,IAAM,oBAAoB;AAAA,EAC7B,IAAI;AAAA,EACJ,IAAI;AAAA,EACJ,IAAI;AAAA,EACJ,IAAI;AACR;AAEO,IAAM,uBAAuB;AAAA,EAChC,UAAU;AAAA,IACN,8CAA8C;AAAA,IAC9C,8CAA8C;AAAA,IAC9C,8CAA8C;AAAA,EAClD;AAAA,EAC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,QAAM,aAAa,SAAS,OAAO,CAAC,CAAC;AACrC,QAAM,QAAQA,QAAO,UAAU,KAAK,OAAO,KAAK,MAAM,GAAG,EAAE,CAAC,EAAE,SAAS;AACvE,QAAM,aAAa,OAAO,KAAK,MAAM,IAAI,EAAE;AAC3C,QAAM,aAAa,OAAO,KAAK,MAAM,IAAI,EAAE;AAE3C,MAAI,UAAU;AACd,MAAI,KAAK,SAAS,IAAI;AAElB,cAAU,OAAO,KAAK,MAAM,IAAI,KAAK,MAAM;AAAA,EAC/C;AAEA,SAAO;AAAA,IACH;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA,YAAYA,QAAO,MAAM,gBAAgB,OAAO,CAAC,SAAS,GAAG,CAAC,KAAK,OAAO,CAAC;AAAA,IAC3E;AAAA,EACJ;AACJ","sourcesContent":["export const NETWORKS = ['default', 'hardhat', 'arbitrum', 'polygon', 'harmony']\n\nexport const OutboundProofType = {\n MPT: 1,\n FP: 2,\n}\n\nexport const EVMUtilityVersion = {\n V1: 1,\n V2: 2,\n V3: 3,\n V4: 4,\n}\n\nexport const UTILS_VERSION_LOOKUP = {\n arbitrum: {\n '0x4ee2f9b7cf3a68966c370f3eb2c16613d3235245': 1,\n '0x50500ccd11e29f14008c7778a6ced655727a21c3': 2,\n '0x5b23e2bae5c5f00e804ea2c4c9abe601604378fa': 2,\n },\n avalanche: {\n '0x4ee2f9b7cf3a68966c370f3eb2c16613d3235245': 1,\n '0x50500ccd11e29f14008c7778a6ced655727a21c3': 2,\n '0xcbd35a9b849342ad34a71e072d9947d4afb4e164': 2,\n },\n bsc: {\n '0x4ee2f9b7cf3a68966c370f3eb2c16613d3235245': 1,\n '0x50500ccd11e29f14008c7778a6ced655727a21c3': 2,\n '0xcff08a35a5f27f306e2da99ff198db90f13def77': 2,\n },\n 'bsc-testnet': {\n '0xfc256f1007e67abbd027536818142823b4596a24': 1,\n '0x1de92e6b8e8b6b150ea8588fe387a4f5fb4da778': 2,\n '0x76860669184330e6966a61188cfbd13486c7a6a6': 2,\n '0x4652b61781f5ee79dad354ab5d249aa2b99b4def': 2,\n },\n 'bsc-sandbox': {\n '0xf96093134adf151889ed72517979b2cd7144a831': 1,\n '0x1751181f9af72b37934006bbe989238498067563': 2,\n '0x8953031e74a172586e89ddb40b7d269b9186e74c': 2,\n },\n ethereum: {\n '0x2d61dcdd36f10b22176e0433b86f74567d529aaa': 1,\n '0x50500ccd11e29f14008c7778a6ced655727a21c3': 2,\n '0xe9ae261d3aff7d3fccf38fa2d612dd3897e07b2d': 2,\n },\n fantom: {\n '0x3c2269811836af69497e5f486a85d7316753cf62': 1,\n '0x50500ccd11e29f14008c7778a6ced655727a21c3': 2,\n '0x6f1686189f32e78f1d83e7c6ed433fcebc3a5b51': 2,\n },\n 'fantom-testnet': {\n '0x0c3d09a7d86652f5599b836ce3db685a86513487': 1,\n '0x7669ac2bb50d567fd4bfb1cb5154b79c1e5e4a92': 2,\n '0xdafbd7a68e4ddcaa864c6a1171bd6030d0ca9a43': 2,\n '0x843986e31e0e3fea5eaf01086f36e7d85c458bb1': 2,\n },\n 'fantom-sandbox': {\n '0x54f51642779b9dbf48cc653c40499c82b8f261b3': 1,\n '0x01de566a13dc4be329351d930b0d9a569a495e46': 2,\n '0xf96093134adf151889ed72517979b2cd7144a831': 2,\n },\n 'avalanche-testnet': {\n '0xa7ac9fadbe9f51e7aa96751aa53f4cbb8a07b9ba': 1,\n '0x08c2e2b98d35973a4b2ca7ce8815cdb7c6f474f0': 2,\n '0x0a833b15305ce8b64946098db286710ace7bbe94': 2,\n '0xd81a1c5c52495a23f75d7269c17c73cebea61233': 2,\n },\n 'avalanche-sandbox': {\n '0x8953031e74a172586e89ddb40b7d269b9186e74c': 1,\n '0x4d7248986395d24ec777b16ca3fa32de64ec0330': 2,\n '0x4489a462f812d6dca0bde380ebdac12eb5a9e85a': 2,\n },\n 'polygon-testnet': {\n '0x2c7b02ffbc9602e8c04901609ef17cb1c4c2a7f7': 1,\n '0x3f6055024105d22b9400e025a1ca3259bd8b1893': 2,\n '0xcb97d107a87c5e172e075de94d7ef4498f6f06d2': 2,\n '0xd8b2de57ccfbbd5cd1713542cb6790fd2e33fed6': 2,\n },\n 'polygon-sandbox': {\n '0x369cc088dcedaaad27df0185c4f78caae42ff942': 1,\n '0x46f703c2a92874d5d526878a2ccc44e9431720a5': 2,\n '0xee404727abfca5d08a2480b0b881745f25362828': 2,\n },\n optimism: {\n '0x4ee2f9b7cf3a68966c370f3eb2c16613d3235245': 1,\n '0x50500ccd11e29f14008c7778a6ced655727a21c3': 2,\n '0x5b23e2bae5c5f00e804ea2c4c9abe601604378fa': 2,\n },\n polygon: {\n '0x4ee2f9b7cf3a68966c370f3eb2c16613d3235245': 1,\n '0x50500ccd11e29f14008c7778a6ced655727a21c3': 2,\n '0x6f1686189f32e78f1d83e7c6ed433fcebc3a5b51': 2,\n },\n}\n","import { ethers } from 'ethers'\nimport invariant from 'tiny-invariant'\n\nimport { EVMUtilityVersion, NETWORKS, OutboundProofType } from './constants'\nimport { getReceiptProof as getReceiptProofV1 } from './mpt/v1'\nimport { getReceiptProof as getReceiptProofV2 } from './mpt/v2'\n\nexport function getFeatherProof(utilsVersion, emitterAddress, packetPayload) {\n switch (utilsVersion) {\n case EVMUtilityVersion.V1: {\n const contractAddrByte32 = ethers.utils.hexZeroPad(emitterAddress, 32)\n return {\n proof: ethers.utils.solidityPack(['bytes32', 'bytes'], [contractAddrByte32, packetPayload]),\n }\n }\n case EVMUtilityVersion.V2: {\n return {\n proof: packetPayload,\n }\n }\n default:\n throw new Error(`Unknown utility version ${utilsVersion}`)\n }\n}\n\nexport async function getReceiptProof(\n network,\n block,\n transactionReceipts,\n transactionIndex,\n outboundProofType,\n utilsVersion\n) {\n invariant(NETWORKS.includes(network), `Unsupported network: ${network}`)\n\n switch (outboundProofType) {\n case OutboundProofType.MPT: {\n switch (utilsVersion) {\n case EVMUtilityVersion.V1: {\n return await getReceiptProofV1(network, block, transactionReceipts, transactionIndex)\n }\n case EVMUtilityVersion.V2:\n case EVMUtilityVersion.V4: {\n return await getReceiptProofV2(network, block, transactionReceipts, transactionIndex)\n }\n case EVMUtilityVersion.V3: {\n const proof = await getReceiptProofV2(network, block, transactionReceipts, transactionIndex)\n proof['blockHash'] = block.hash\n return proof\n }\n default:\n throw new Error(`Unsupported utility version ${utilsVersion}`)\n }\n }\n default:\n throw new Error(`Unsupported Outbound Proof Type ${outboundProofType}`)\n }\n}\n\nexport function encodeParams(proof, outboundProofType, utilsVersion, logIndex, srcEndpointId = undefined) {\n switch (outboundProofType) {\n case OutboundProofType.MPT: {\n switch (utilsVersion) {\n case EVMUtilityVersion.V1: {\n return ethers.utils.defaultAbiCoder.encode(\n ['uint16', 'bytes[]', 'uint256[]', 'uint256', 'uint256'],\n [srcEndpointId, proof.proof, proof.pointers, proof.receiptSlotIndex, logIndex]\n )\n }\n case EVMUtilityVersion.V2: {\n return ethers.utils.defaultAbiCoder.encode(\n ['uint16', 'bytes[]', 'uint256[]', 'uint256'],\n [srcEndpointId, proof.proof, proof.receiptSlotIndex, logIndex]\n )\n }\n case EVMUtilityVersion.V3: {\n return ethers.utils.defaultAbiCoder.encode(\n ['uint16', 'bytes32', 'bytes[]', 'uint256[]', 'uint256'],\n [srcEndpointId, proof.blockHash, proof.proof, proof.receiptSlotIndex, logIndex]\n )\n }\n case EVMUtilityVersion.V4: {\n return ethers.utils.defaultAbiCoder.encode(\n ['bytes[]', 'uint256[]', 'uint256'],\n [proof.proof, proof.receiptSlotIndex, logIndex]\n )\n }\n default:\n throw new Error(`Unsupported utility version ${utilsVersion}`)\n }\n }\n default:\n throw new Error(`Unsupported Outbound Proof Type ${outboundProofType}`)\n }\n}\n\nexport function getLayerZeroPacket(srcChainId, _log) {\n //todo: what if src address differs from 20 bytes\n const packet = ethers.utils.defaultAbiCoder.decode(['uint16', 'bytes'], _log.data)\n const data = packet[1].slice(2)\n\n //todo: what if dst address differs from 20 bytes\n const dstChainId = parseInt(packet[0])\n const nonce = ethers.BigNumber.from('0x' + data.slice(0, 16)).toNumber()\n const srcAddress = '0x' + data.slice(16, 56)\n const dstAddress = '0x' + data.slice(56, 96)\n\n let payload = '0x'\n if (data.length > 96) {\n //has payload\n payload = '0x' + data.slice(96, data.length)\n }\n\n return {\n srcChainId,\n dstChainId,\n nonce,\n dstAddress,\n srcAddress,\n ulnAddress: ethers.utils.defaultAbiCoder.encode(['address'], [_log.address]),\n payload,\n }\n}\n","import { rlp } from 'ethereumjs-util'\nimport { ethers } from 'ethers'\nimport { matchingNibbleLength, stringToNibbles } from 'merkle-patricia-tree/util/nibbles'\nimport invariant from 'tiny-invariant'\n\nimport { buffer2hex, receiptProofFrom } from './common'\n\nfunction findPointer(fullBytes, currentNodeElement) {\n const fullBytesHex = fullBytes.toString('hex')\n const slicedBytesHex = currentNodeElement.toString('hex')\n const result = fullBytesHex.indexOf(slicedBytesHex)\n invariant(result >= 0, 'wrong index')\n return (result - 2) / 2\n}\n\n/*\nrunning MPT locally, strictly the same as solidity file\n */\nfunction assertReceiptInclusion(receiptsRoot, expectedLogValue, rlpProof, pointers) {\n let nextRoot = receiptsRoot\n const proofDepth = rlpProof.length\n let pointer\n let proofBytes\n for (let i = 0; i < proofDepth; i++) {\n proofBytes = rlpProof[i]\n if (nextRoot !== ethers.utils.keccak256(proofBytes)) return false\n // load 32 bytes from the proofBytes\n if (i < pointers.length) {\n pointer = 2 + pointers[i] * 2\n nextRoot = '0x' + proofBytes.substring(pointer, pointer + 64)\n }\n }\n return true\n}\n\nfunction assembleMPTProof(proof, block, transactionIndex) {\n const stack = proof.stack\n\n // convert receipt Proof L2 nested contents into hex\n const rlpNestedProof = [...proof.receiptProof].map((node) => node.map((elem) => buffer2hex(elem)))\n const rlpProof = rlpNestedProof.map((node) => buffer2hex(rlp.encode(node)))\n\n // decimal of transaction index\n const receiptIndexDeci = ethers.BigNumber.from(transactionIndex).toNumber()\n const encodedMerklePath = stringToNibbles(rlp.encode(receiptIndexDeci)) // if index = 252, encoded = 81fc\n\n // prepare the data for offline traversal\n let hashRoot = block.receiptsRoot\n const proofDepth = stack.length\n // const targetReceipt = stack[proofDepth - 1].value;\n const targetReceipt = rlpNestedProof[rlpNestedProof.length - 1][1]\n let proofPathCounter = 0\n const totalPathLength = encodedMerklePath.length\n const pointers: number[] = []\n let receiptSlotIndex\n for (let i = 0; i < proofDepth; i++) {\n // assert the path depth constaints\n invariant(\n proofPathCounter <= totalPathLength,\n `proofPathCounter wrong at ${proofPathCounter}, where totalPathLength = ${totalPathLength}`\n )\n\n // thisNodeValue in array form, easier to retrieve data\n const thisNode = stack[i]\n const thisNodeValue = thisNode.raw\n // convert the elem first then hex again, it is == rlpNestedProof[i]\n const thisNodeRlp = rlpProof[i]\n invariant(\n ethers.utils.keccak256(thisNodeRlp) === hashRoot,\n `invalid hashlink at depth ${i} | proofPathCounter ${proofPathCounter} | totalPathLength = ${totalPathLength}`\n )\n\n if (thisNode.type === 'branch') {\n // branch node\n if (proofPathCounter === totalPathLength) {\n // has reach the end, assert targetReceipt ==\n invariant(ethers.utils.keccak256(thisNodeValue[16]) === targetReceipt, 'invalid branch value node')\n receiptSlotIndex = 16\n } else {\n // a normal branch node, step down\n const nextNibble = encodedMerklePath[proofPathCounter]\n invariant(\n nextNibble <= 16,\n `invalid nibble at ${nextNibble} | proofPathCounter ${proofPathCounter} | totalPathLength = ${totalPathLength}`\n )\n // hashRoot = buffer2hex(thisNode._branches[nextNibble])\n hashRoot = buffer2hex(thisNodeValue[nextNibble])\n proofPathCounter += 1\n // retrieve the pointer for the branch value\n pointers.push(findPointer(thisNodeRlp, thisNodeValue[nextNibble]))\n }\n } else if (thisNode.type === 'leaf' || thisNode.type === 'extention') {\n const progressKey = encodedMerklePath.slice(0, proofPathCounter)\n const keyRemainder = encodedMerklePath.slice(matchingNibbleLength(progressKey, encodedMerklePath))\n proofPathCounter += matchingNibbleLength(keyRemainder, stack[i].key)\n if (thisNode.type === 'leaf') {\n invariant(proofPathCounter === totalPathLength, 'invalid leaf node')\n invariant(\n ethers.utils.keccak256(thisNodeValue[1]) === ethers.utils.keccak256(targetReceipt),\n 'wrong leaf value'\n )\n receiptSlotIndex = 1\n } else if (thisNode.type === 'extention') {\n hashRoot = buffer2hex(thisNodeValue[1])\n // retrieve the pointer for the leaf node\n pointers.push(findPointer(thisNodeRlp, thisNodeValue[1]))\n }\n } else {\n throw `unsupported node type in MPT ${thisNode} ${encodedMerklePath}`\n }\n }\n\n invariant(assertReceiptInclusion(block.receiptsRoot, targetReceipt, rlpProof, pointers), 'MPTLite local fails')\n\n return {\n receiptRoot: block.receiptsRoot,\n proof: rlpProof,\n pointers: pointers,\n receiptSlotIndex: receiptSlotIndex,\n }\n}\n\nexport async function getReceiptProof(network, block, transactionReceipts, transactionIndex) {\n const proof = await receiptProofFrom(network, block, transactionReceipts, transactionIndex)\n return assembleMPTProof(proof, block, transactionIndex)\n}\n","// getStateSyncTxHash returns block's tx hash for state-sync receipt\n// Bor blockchain includes extra receipt/tx for state-sync logs,\n// but it is not included in transactionRoot or receiptRoot.\n// So, while calculating proof, we have to exclude them.\n//\n// This is derived from block's hash and number\n// state-sync tx hash = keccak256(\"matic-bor-receipt-\" + block.number + block.hash)\nimport { Proof, Receipt } from 'eth-object'\nimport { encode, toBuffer } from 'eth-util-lite'\nimport * as ethUtils from 'ethereumjs-util'\nimport { promisfy } from 'promisfy'\nimport { Buffer } from 'safer-buffer'\nimport invariant from 'tiny-invariant'\n\nconst Tree = require('merkle-patricia-tree')\n\nexport function getPolygonStateSyncTxHash(block) {\n return ethUtils.bufferToHex(\n ethUtils.keccak256(\n Buffer.concat([\n ethUtils.toBuffer('matic-bor-receipt-'), // prefix for bor receipt\n ethUtils.setLengthLeft(ethUtils.toBuffer(block.number), 8), // 8 bytes of block number (BigEndian)\n ethUtils.toBuffer(block.hash), // block hash\n ])\n )\n )\n}\n\nexport function buffer2hex(buffer) {\n return '0x' + buffer.toString('hex')\n}\n\nexport async function receiptProofFrom(network, block, transactionReceipts, transactionIndex) {\n // handle the polygon special receipt\n if (network === 'polygon') {\n const ignoredTxnHash = getPolygonStateSyncTxHash(block)\n transactionReceipts = transactionReceipts.filter((receipt) => receipt.transactionHash !== ignoredTxnHash)\n }\n\n const tree = new Tree()\n await Promise.all(\n transactionReceipts.map((siblingReceipt, index) => {\n const siblingPath = encode(index)\n\n if (network === 'harmony' && index >= block.transactions.length) {\n // void staking receipt type, which works differently from EIP2718\n siblingReceipt.type = 0\n }\n\n let serializedReceipt = Receipt.fromRpc(siblingReceipt)\n\n //handles the arbitrum receipt\n if (network === 'arbitrum') {\n //todo: will not need this in aribtrum nitro\n serializedReceipt[0] = toBuffer(0)\n }\n serializedReceipt = serializedReceipt.serialize()\n\n // if type is defined, concat type and RLP buffer seperately (for receipts/transactions following EIP2718)\n if (siblingReceipt.type) {\n serializedReceipt = Buffer.concat([toBuffer(siblingReceipt.type), serializedReceipt])\n }\n\n return promisfy(tree.put, tree)(siblingPath, serializedReceipt)\n })\n )\n\n const [_, __, stack] = await promisfy(tree.findPath, tree)(encode(transactionIndex))\n\n // assert the tree root\n const receiptRootFromBlock = block.receiptsRoot.slice(2)\n invariant(\n receiptRootFromBlock === tree._root.toString('hex'),\n 'receiptRoot from rpc block != receipt root we built'\n )\n\n return {\n stack: stack,\n receiptProof: Proof.fromStack(stack),\n }\n}\n","import { rlp } from 'ethereumjs-util'\nimport { ethers } from 'ethers'\nimport { matchingNibbleLength, stringToNibbles } from 'merkle-patricia-tree/util/nibbles'\nimport invariant from 'tiny-invariant'\n\nimport { buffer2hex, receiptProofFrom } from './common'\n\nfunction assembleMPTProof(proof, block, transactionIndex) {\n const stack = proof.stack\n\n // convert receipt Proof L2 nested contents into hex\n const rlpNestedProof = [...proof.receiptProof].map((node) => node.map((elem) => buffer2hex(elem)))\n const rlpProof = rlpNestedProof.map((node) => buffer2hex(rlp.encode(node)))\n\n // decimal of transaction index\n const receiptIndexDeci = ethers.BigNumber.from(transactionIndex).toNumber()\n const encodedMerklePath = stringToNibbles(rlp.encode(receiptIndexDeci)) // if index = 252, encoded = 81fc\n\n // prepare the data for offline traversal\n let hashRoot = block.receiptsRoot\n const proofDepth = stack.length\n // const targetReceipt = stack[proofDepth - 1].value;\n const targetReceipt = rlpNestedProof[rlpNestedProof.length - 1][1]\n let proofPathCounter = 0\n const totalPathLength = encodedMerklePath.length\n const path: number[] = []\n for (let i = 0; i < proofDepth; i++) {\n // console.log(`hashroot at ${i} : ${hashRoot}`)\n // console.log(` rlpProof at ${rlpProof[i]}`)\n\n // assert the path depth constraints\n invariant(\n proofPathCounter <= totalPathLength,\n `proofPathCounter wrong at ${proofPathCounter}, where totalPathLength = ${totalPathLength}`\n )\n\n // thisNodeValue in array form, easier to retrieve data\n const thisNode = stack[i]\n const thisNodeValue = thisNode.raw\n // convert the elem first then hex again, it is == rlpNestedProof[i]\n const thisNodeRlp = rlpProof[i]\n invariant(\n ethers.utils.keccak256(thisNodeRlp) === hashRoot,\n `invalid hashlink at depth ${i} | proofPathCounter ${proofPathCounter} | totalPathLength = ${totalPathLength}`\n )\n\n if (thisNode.type === 'branch') {\n // branch node\n if (proofPathCounter === totalPathLength) {\n // has reach the end, assert targetReceipt ==\n invariant(ethers.utils.keccak256(thisNodeValue[16]) === targetReceipt, 'invalid branch value node')\n path[i] = 16\n console.warn(`a branch node 16 value type ${transactionIndex} receipt root ${block.receiptsRoot}`)\n } else {\n // a normal branch node, step down\n const nextNibble = encodedMerklePath[proofPathCounter]\n invariant(\n nextNibble <= 16,\n `invalid nibble at ${nextNibble} | proofPathCounter ${proofPathCounter} | totalPathLength = ${totalPathLength}`\n )\n // hashRoot = buffer2hex(thisNode._branches[nextNibble])\n hashRoot = buffer2hex(thisNodeValue[nextNibble])\n proofPathCounter += 1\n // retrieve the pointer for the branch value\n path[i] = nextNibble\n }\n } else if (thisNode.type === 'leaf' || thisNode.type === 'extention') {\n const progressKey = encodedMerklePath.slice(0, proofPathCounter)\n const keyRemainder = encodedMerklePath.slice(matchingNibbleLength(progressKey, encodedMerklePath))\n proofPathCounter += matchingNibbleLength(keyRemainder, stack[i].key)\n if (thisNode.type === 'leaf') {\n invariant(proofPathCounter === totalPathLength, 'invalid leaf node')\n invariant(\n ethers.utils.keccak256(thisNodeValue[1]) === ethers.utils.keccak256(targetReceipt),\n 'wrong leaf value'\n )\n path[i] = 1\n } else if (thisNode.type === 'extention') {\n path[i] = 1\n hashRoot = buffer2hex(thisNodeValue[1])\n // retrieve the pointer for the leaf node\n }\n } else {\n throw `unsupported node type in MPT ${thisNode} ${encodedMerklePath}`\n }\n }\n\n invariant(assertReceiptInclusion(block.receiptsRoot, targetReceipt, rlpProof, path, stack), 'MPTLite local fails')\n\n return {\n receiptRoot: block.receiptsRoot,\n proof: rlpProof,\n receiptSlotIndex: path,\n }\n}\n\n/*\nrunning MPT locally, strictly the same as solidity file\n */\nfunction assertReceiptInclusion(receiptsRoot, expectedLogValue, rlpProof, path, stack) {\n let nextRoot = receiptsRoot\n const proofDepth = rlpProof.length\n for (let i = 0; i < proofDepth; i++) {\n const proofBytes = rlpProof[i]\n const thisNodeValue = stack[i].raw\n if (nextRoot !== ethers.utils.keccak256(proofBytes)) return false\n if (i < proofDepth - 1) {\n nextRoot = buffer2hex(thisNodeValue[path[i]])\n }\n }\n return true\n}\n\nexport async function getReceiptProof(network, block, transactionReceipts, transactionIndex) {\n const proof = await receiptProofFrom(network, block, transactionReceipts, transactionIndex)\n return assembleMPTProof(proof, block, transactionIndex)\n}\n"]}