@layerzerolabs/lz-proof-utility 1.5.66 → 1.5.68

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 CHANGED
@@ -2,15 +2,35 @@
2
2
 
3
3
  var ethers = require('ethers');
4
4
  var invariant2 = require('tiny-invariant');
5
- var ethereumjsUtil = require('ethereumjs-util');
5
+ var ethUtils = require('ethereumjs-util');
6
6
  var nibbles = require('merkle-patricia-tree/util/nibbles');
7
7
  var ethObject = require('eth-object');
8
8
  var ethUtilLite = require('eth-util-lite');
9
9
  var promisfy = require('promisfy');
10
+ var saferBuffer = require('safer-buffer');
10
11
 
11
12
  function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
12
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
+
13
32
  var invariant2__default = /*#__PURE__*/_interopDefault(invariant2);
33
+ var ethUtils__namespace = /*#__PURE__*/_interopNamespace(ethUtils);
14
34
 
15
35
  var __defProp = Object.defineProperty;
16
36
  var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
@@ -18,7 +38,7 @@ var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require
18
38
  }) : x)(function(x) {
19
39
  if (typeof require !== "undefined")
20
40
  return require.apply(this, arguments);
21
- throw Error('Dynamic require of "' + x + '" is not supported');
41
+ throw new Error('Dynamic require of "' + x + '" is not supported');
22
42
  });
23
43
  var __export = (target, all) => {
24
44
  for (var name in all)
@@ -129,11 +149,18 @@ __export(proof_exports, {
129
149
  });
130
150
  var Tree = __require("merkle-patricia-tree");
131
151
  function getPolygonStateSyncTxHash(block) {
132
- const prefix = Buffer.from("matic-bor-receipt-");
133
- const blockNumberBytes = ethers.utils.zeroPad(ethers.utils.arrayify(ethers.BigNumber.from(block.number)), 8);
134
- const blockHashBytes = ethers.utils.arrayify(block.hash);
135
- const hash = ethers.utils.keccak256(ethers.utils.concat([prefix, blockNumberBytes, blockHashBytes]));
136
- return hash;
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
+ );
137
164
  }
138
165
  function buffer2hex(buffer) {
139
166
  return "0x" + buffer.toString("hex");
@@ -156,7 +183,7 @@ async function receiptProofFrom(network, block, transactionReceipts, transaction
156
183
  }
157
184
  serializedReceipt = serializedReceipt.serialize();
158
185
  if (siblingReceipt.type) {
159
- serializedReceipt = Buffer.concat([ethUtilLite.toBuffer(siblingReceipt.type), serializedReceipt]);
186
+ serializedReceipt = saferBuffer.Buffer.concat([ethUtilLite.toBuffer(siblingReceipt.type), serializedReceipt]);
160
187
  }
161
188
  return promisfy.promisfy(tree.put, tree)(siblingPath, serializedReceipt);
162
189
  })
@@ -200,9 +227,9 @@ function assertReceiptInclusion(receiptsRoot, expectedLogValue, rlpProof, pointe
200
227
  function assembleMPTProof(proof, block, transactionIndex) {
201
228
  const stack = proof.stack;
202
229
  const rlpNestedProof = [...proof.receiptProof].map((node) => node.map((elem) => buffer2hex(elem)));
203
- const rlpProof = rlpNestedProof.map((node) => buffer2hex(ethereumjsUtil.rlp.encode(node)));
230
+ const rlpProof = rlpNestedProof.map((node) => buffer2hex(ethUtils.rlp.encode(node)));
204
231
  const receiptIndexDeci = ethers.ethers.BigNumber.from(transactionIndex).toNumber();
205
- const encodedMerklePath = nibbles.stringToNibbles(ethereumjsUtil.rlp.encode(receiptIndexDeci));
232
+ const encodedMerklePath = nibbles.stringToNibbles(ethUtils.rlp.encode(receiptIndexDeci));
206
233
  let hashRoot = block.receiptsRoot;
207
234
  const proofDepth = stack.length;
208
235
  const targetReceipt = rlpNestedProof[rlpNestedProof.length - 1][1];
@@ -270,9 +297,9 @@ async function getReceiptProof(network, block, transactionReceipts, transactionI
270
297
  function assembleMPTProof2(proof, block, transactionIndex) {
271
298
  const stack = proof.stack;
272
299
  const rlpNestedProof = [...proof.receiptProof].map((node) => node.map((elem) => buffer2hex(elem)));
273
- const rlpProof = rlpNestedProof.map((node) => buffer2hex(ethereumjsUtil.rlp.encode(node)));
300
+ const rlpProof = rlpNestedProof.map((node) => buffer2hex(ethUtils.rlp.encode(node)));
274
301
  const receiptIndexDeci = ethers.ethers.BigNumber.from(transactionIndex).toNumber();
275
- const encodedMerklePath = nibbles.stringToNibbles(ethereumjsUtil.rlp.encode(receiptIndexDeci));
302
+ const encodedMerklePath = nibbles.stringToNibbles(ethUtils.rlp.encode(receiptIndexDeci));
276
303
  let hashRoot = block.receiptsRoot;
277
304
  const proofDepth = stack.length;
278
305
  const targetReceipt = rlpNestedProof[rlpNestedProof.length - 1][1];
@@ -1 +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","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;AA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,OAAO,MAAM,kBAAkB,QAAQ;AAAA,UAClD;AAAA,QACJ;AAAA,QACA;AACI,gBAAM,IAAI,MAAM,+BAA+B,YAAY,EAAE;AAAA,MACrE;AAAA,IACJ;AAAA,IACA;AACI,YAAM,IAAI,MAAM,mCAAmC,iBAAiB,EAAE;AAAA,EAC9E;AACJ;AAEO,SAAS,mBAAmB,YAAY,MAAM;AAEjD,QAAM,SAASA,QAAO,MAAM,gBAAgB,OAAO,CAAC,UAAU,OAAO,GAAG,KAAK,IAAI;AACjF,QAAM,OAAO,OAAO,CAAC,EAAE,MAAM,CAAC;AAG9B,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 { BigNumber, utils } from 'ethers'\nimport { promisfy } from 'promisfy'\nimport invariant from 'tiny-invariant'\n\nconst Tree = require('merkle-patricia-tree')\n\nexport function getPolygonStateSyncTxHash(block) {\n const prefix = Buffer.from('matic-bor-receipt-')\n const blockNumberBytes = utils.zeroPad(utils.arrayify(BigNumber.from(block.number)), 8)\n const blockHashBytes = utils.arrayify(block.hash)\n\n const hash = utils.keccak256(utils.concat([prefix, blockNumberBytes, blockHashBytes]))\n\n return hash\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"]}
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,EACA,WAAW;AAAA,IACP,8CAA8C;AAAA,IAC9C,8CAA8C;AAAA,IAC9C,8CAA8C;AAAA,EAClD;AAAA,EACA,KAAK;AAAA,IACD,8CAA8C;AAAA,IAC9C,8CAA8C;AAAA,IAC9C,8CAA8C;AAAA,EAClD;AAAA,EACA,eAAe;AAAA,IACX,8CAA8C;AAAA,IAC9C,8CAA8C;AAAA,IAC9C,8CAA8C;AAAA,IAC9C,8CAA8C;AAAA,EAClD;AAAA,EACA,eAAe;AAAA,IACX,8CAA8C;AAAA,IAC9C,8CAA8C;AAAA,IAC9C,8CAA8C;AAAA,EAClD;AAAA,EACA,UAAU;AAAA,IACN,8CAA8C;AAAA,IAC9C,8CAA8C;AAAA,IAC9C,8CAA8C;AAAA,EAClD;AAAA,EACA,QAAQ;AAAA,IACJ,8CAA8C;AAAA,IAC9C,8CAA8C;AAAA,IAC9C,8CAA8C;AAAA,EAClD;AAAA,EACA,kBAAkB;AAAA,IACd,8CAA8C;AAAA,IAC9C,8CAA8C;AAAA,IAC9C,8CAA8C;AAAA,IAC9C,8CAA8C;AAAA,EAClD;AAAA,E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AM,OAAO,MAAM,kBAAkB,QAAQ;AAAA,UAClF;AAAA,QACJ;AAAA,QACA,KAAK,kBAAkB,IAAI;AACvB,iBAAOA,QAAO,MAAM,gBAAgB;AAAA,YAChC,CAAC,WAAW,aAAa,SAAS;AAAA,YAClC,CAAC,MAAM,OAAO,MAAM,kBAAkB,QAAQ;AAAA,UAClD;AAAA,QACJ;AAAA,QACA;AACI,gBAAM,IAAI,MAAM,+BAA+B,cAAc;AAAA,MACrE;AAAA,IACJ;AAAA,IACA;AACI,YAAM,IAAI,MAAM,mCAAmC,mBAAmB;AAAA,EAC9E;AACJ;AAEO,SAAS,mBAAmB,YAAY,MAAM;AAEjD,QAAM,SAASA,QAAO,MAAM,gBAAgB,OAAO,CAAC,UAAU,OAAO,GAAG,KAAK,IAAI;AACjF,QAAM,OAAO,OAAO,CAAC,EAAE,MAAM,CAAC;AAG9B,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"]}
package/dist/index.mjs CHANGED
@@ -1,10 +1,12 @@
1
- import { ethers, utils, BigNumber } from 'ethers';
1
+ import { ethers } from 'ethers';
2
2
  import invariant2 from 'tiny-invariant';
3
+ import * as ethUtils from 'ethereumjs-util';
3
4
  import { rlp } from 'ethereumjs-util';
4
5
  import { stringToNibbles, matchingNibbleLength } from 'merkle-patricia-tree/util/nibbles';
5
6
  import { Receipt, Proof } from 'eth-object';
6
7
  import { encode, toBuffer } from 'eth-util-lite';
7
8
  import { promisfy } from 'promisfy';
9
+ import { Buffer } from 'safer-buffer';
8
10
 
9
11
  var __defProp = Object.defineProperty;
10
12
  var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
@@ -12,7 +14,7 @@ var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require
12
14
  }) : x)(function(x) {
13
15
  if (typeof require !== "undefined")
14
16
  return require.apply(this, arguments);
15
- throw Error('Dynamic require of "' + x + '" is not supported');
17
+ throw new Error('Dynamic require of "' + x + '" is not supported');
16
18
  });
17
19
  var __export = (target, all) => {
18
20
  for (var name in all)
@@ -123,11 +125,18 @@ __export(proof_exports, {
123
125
  });
124
126
  var Tree = __require("merkle-patricia-tree");
125
127
  function getPolygonStateSyncTxHash(block) {
126
- const prefix = Buffer.from("matic-bor-receipt-");
127
- const blockNumberBytes = utils.zeroPad(utils.arrayify(BigNumber.from(block.number)), 8);
128
- const blockHashBytes = utils.arrayify(block.hash);
129
- const hash = utils.keccak256(utils.concat([prefix, blockNumberBytes, blockHashBytes]));
130
- return hash;
128
+ return ethUtils.bufferToHex(
129
+ ethUtils.keccak256(
130
+ Buffer.concat([
131
+ ethUtils.toBuffer("matic-bor-receipt-"),
132
+ // prefix for bor receipt
133
+ ethUtils.setLengthLeft(ethUtils.toBuffer(block.number), 8),
134
+ // 8 bytes of block number (BigEndian)
135
+ ethUtils.toBuffer(block.hash)
136
+ // block hash
137
+ ])
138
+ )
139
+ );
131
140
  }
132
141
  function buffer2hex(buffer) {
133
142
  return "0x" + buffer.toString("hex");
@@ -1 +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","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,EACA,WAAW;AAAA,IACP,8CAA8C;AAAA,IAC9C,8CAA8C;AAAA,IAC9C,8CAA8C;AAAA,EAClD;AAAA,EACA,KAAK;AAAA,IACD,8CAA8C;AAAA,IAC9C,8CAA8C;AAAA,IAC9C,8CAA8C;AAAA,EAClD;AAAA,EACA,eAAe;AAAA,IACX,8CAA8C;AAAA,IAC9C,8CAA8C;AAAA,IAC9C,8CAA8C;AAAA,IAC9C,8CAA8C;AAAA,EAClD;AAAA,EACA,eAAe;AAAA,IACX,8CAA8C;AAAA,IAC9C,8CAA8C;AAAA,IAC9C,8CAA8C;AAAA,EAClD;AAAA,EACA,UAAU;AAAA,IACN,8CAA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,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 { BigNumber, utils } from 'ethers'\nimport { promisfy } from 'promisfy'\nimport invariant from 'tiny-invariant'\n\nconst Tree = require('merkle-patricia-tree')\n\nexport function getPolygonStateSyncTxHash(block) {\n const prefix = Buffer.from('matic-bor-receipt-')\n const blockNumberBytes = utils.zeroPad(utils.arrayify(BigNumber.from(block.number)), 8)\n const blockHashBytes = utils.arrayify(block.hash)\n\n const hash = utils.keccak256(utils.concat([prefix, blockNumberBytes, blockHashBytes]))\n\n return hash\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"]}
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,EACA,WAAW;AAAA,IACP,8CAA8C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AAC,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"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@layerzerolabs/lz-proof-utility",
3
- "version": "1.5.66",
3
+ "version": "1.5.68",
4
4
  "description": "LayerZero Proof Utility",
5
5
  "license": "BUSL-1.1",
6
6
  "exports": {
@@ -16,7 +16,7 @@
16
16
  "scripts": {
17
17
  "build": "$npm_execpath build-ts",
18
18
  "build-ts": "$npm_execpath tsc --noEmit && $npm_execpath tsup",
19
- "clean": "rimraf .turbo dist",
19
+ "clean": "$npm_execpath rimraf .turbo && $npm_execpath rimraf dist",
20
20
  "format": "$npm_execpath prettier --ignore-path $(git rev-parse --show-toplevel)/.prettierignore --write ",
21
21
  "lint": "TIMING=1 $npm_execpath eslint --no-error-on-unmatched-pattern src/**/*.ts --fix"
22
22
  },
@@ -24,7 +24,7 @@
24
24
  "@types/safer-buffer": "^2.1.0",
25
25
  "eth-object": "^1.0.3",
26
26
  "eth-util-lite": "^1.0.1",
27
- "ethereumjs-util": "^7.1.5",
27
+ "ethereumjs-util": "^5.2.0",
28
28
  "ethers": "^5.7.2",
29
29
  "merkle-patricia-tree": "github:zmitton/merkle-patricia-tree#build",
30
30
  "promisfy": "^1.2.0",
@@ -32,13 +32,13 @@
32
32
  "tiny-invariant": "^1.3.1"
33
33
  },
34
34
  "devDependencies": {
35
- "@layerzerolabs-internal/tsup-config": "^1.5.66",
36
- "@layerzerolabs-internal/typescript-config": "^1.5.66",
37
- "@layerzerolabs/eslint-config-next": "^1.5.66",
38
- "eslint": "^8.49.0",
39
- "rimraf": "^5.0.0",
40
- "tsup": "^7.2.0",
41
- "typescript": "~5.2.2"
35
+ "@layerzerolabs-internal/tsup-config": "^1.5.68",
36
+ "@layerzerolabs-internal/typescript-config": "^1.5.68",
37
+ "@layerzerolabs/eslint-config-next": "^1.5.68",
38
+ "eslint": "^8.17.0",
39
+ "rimraf": "^3.0.2",
40
+ "tsup": "^6.7.0",
41
+ "typescript": "^5.1.3"
42
42
  },
43
43
  "publishConfig": {
44
44
  "access": "restricted"
package/dist/index.d.mts DELETED
@@ -1,131 +0,0 @@
1
- declare const NETWORKS: string[];
2
- declare const OutboundProofType: {
3
- MPT: number;
4
- FP: number;
5
- };
6
- declare const EVMUtilityVersion: {
7
- V1: number;
8
- V2: number;
9
- V3: number;
10
- V4: number;
11
- };
12
- declare const UTILS_VERSION_LOOKUP: {
13
- arbitrum: {
14
- '0x4ee2f9b7cf3a68966c370f3eb2c16613d3235245': number;
15
- '0x50500ccd11e29f14008c7778a6ced655727a21c3': number;
16
- '0x5b23e2bae5c5f00e804ea2c4c9abe601604378fa': number;
17
- };
18
- avalanche: {
19
- '0x4ee2f9b7cf3a68966c370f3eb2c16613d3235245': number;
20
- '0x50500ccd11e29f14008c7778a6ced655727a21c3': number;
21
- '0xcbd35a9b849342ad34a71e072d9947d4afb4e164': number;
22
- };
23
- bsc: {
24
- '0x4ee2f9b7cf3a68966c370f3eb2c16613d3235245': number;
25
- '0x50500ccd11e29f14008c7778a6ced655727a21c3': number;
26
- '0xcff08a35a5f27f306e2da99ff198db90f13def77': number;
27
- };
28
- 'bsc-testnet': {
29
- '0xfc256f1007e67abbd027536818142823b4596a24': number;
30
- '0x1de92e6b8e8b6b150ea8588fe387a4f5fb4da778': number;
31
- '0x76860669184330e6966a61188cfbd13486c7a6a6': number;
32
- '0x4652b61781f5ee79dad354ab5d249aa2b99b4def': number;
33
- };
34
- 'bsc-sandbox': {
35
- '0xf96093134adf151889ed72517979b2cd7144a831': number;
36
- '0x1751181f9af72b37934006bbe989238498067563': number;
37
- '0x8953031e74a172586e89ddb40b7d269b9186e74c': number;
38
- };
39
- ethereum: {
40
- '0x2d61dcdd36f10b22176e0433b86f74567d529aaa': number;
41
- '0x50500ccd11e29f14008c7778a6ced655727a21c3': number;
42
- '0xe9ae261d3aff7d3fccf38fa2d612dd3897e07b2d': number;
43
- };
44
- fantom: {
45
- '0x3c2269811836af69497e5f486a85d7316753cf62': number;
46
- '0x50500ccd11e29f14008c7778a6ced655727a21c3': number;
47
- '0x6f1686189f32e78f1d83e7c6ed433fcebc3a5b51': number;
48
- };
49
- 'fantom-testnet': {
50
- '0x0c3d09a7d86652f5599b836ce3db685a86513487': number;
51
- '0x7669ac2bb50d567fd4bfb1cb5154b79c1e5e4a92': number;
52
- '0xdafbd7a68e4ddcaa864c6a1171bd6030d0ca9a43': number;
53
- '0x843986e31e0e3fea5eaf01086f36e7d85c458bb1': number;
54
- };
55
- 'fantom-sandbox': {
56
- '0x54f51642779b9dbf48cc653c40499c82b8f261b3': number;
57
- '0x01de566a13dc4be329351d930b0d9a569a495e46': number;
58
- '0xf96093134adf151889ed72517979b2cd7144a831': number;
59
- };
60
- 'avalanche-testnet': {
61
- '0xa7ac9fadbe9f51e7aa96751aa53f4cbb8a07b9ba': number;
62
- '0x08c2e2b98d35973a4b2ca7ce8815cdb7c6f474f0': number;
63
- '0x0a833b15305ce8b64946098db286710ace7bbe94': number;
64
- '0xd81a1c5c52495a23f75d7269c17c73cebea61233': number;
65
- };
66
- 'avalanche-sandbox': {
67
- '0x8953031e74a172586e89ddb40b7d269b9186e74c': number;
68
- '0x4d7248986395d24ec777b16ca3fa32de64ec0330': number;
69
- '0x4489a462f812d6dca0bde380ebdac12eb5a9e85a': number;
70
- };
71
- 'polygon-testnet': {
72
- '0x2c7b02ffbc9602e8c04901609ef17cb1c4c2a7f7': number;
73
- '0x3f6055024105d22b9400e025a1ca3259bd8b1893': number;
74
- '0xcb97d107a87c5e172e075de94d7ef4498f6f06d2': number;
75
- '0xd8b2de57ccfbbd5cd1713542cb6790fd2e33fed6': number;
76
- };
77
- 'polygon-sandbox': {
78
- '0x369cc088dcedaaad27df0185c4f78caae42ff942': number;
79
- '0x46f703c2a92874d5d526878a2ccc44e9431720a5': number;
80
- '0xee404727abfca5d08a2480b0b881745f25362828': number;
81
- };
82
- optimism: {
83
- '0x4ee2f9b7cf3a68966c370f3eb2c16613d3235245': number;
84
- '0x50500ccd11e29f14008c7778a6ced655727a21c3': number;
85
- '0x5b23e2bae5c5f00e804ea2c4c9abe601604378fa': number;
86
- };
87
- polygon: {
88
- '0x4ee2f9b7cf3a68966c370f3eb2c16613d3235245': number;
89
- '0x50500ccd11e29f14008c7778a6ced655727a21c3': number;
90
- '0x6f1686189f32e78f1d83e7c6ed433fcebc3a5b51': number;
91
- };
92
- };
93
-
94
- declare function getFeatherProof(utilsVersion: any, emitterAddress: any, packetPayload: any): {
95
- proof: any;
96
- };
97
- declare function getReceiptProof(network: any, block: any, transactionReceipts: any, transactionIndex: any, outboundProofType: any, utilsVersion: any): Promise<{
98
- receiptRoot: any;
99
- proof: string[];
100
- pointers: number[];
101
- receiptSlotIndex: any;
102
- } | {
103
- receiptRoot: any;
104
- proof: string[];
105
- receiptSlotIndex: number[];
106
- }>;
107
- declare function encodeParams(proof: any, outboundProofType: any, utilsVersion: any, logIndex: any, srcEndpointId?: undefined): string;
108
- declare function getLayerZeroPacket(srcChainId: any, _log: any): {
109
- srcChainId: any;
110
- dstChainId: number;
111
- nonce: number;
112
- dstAddress: string;
113
- srcAddress: string;
114
- ulnAddress: string;
115
- payload: string;
116
- };
117
-
118
- declare const proof_encodeParams: typeof encodeParams;
119
- declare const proof_getFeatherProof: typeof getFeatherProof;
120
- declare const proof_getLayerZeroPacket: typeof getLayerZeroPacket;
121
- declare const proof_getReceiptProof: typeof getReceiptProof;
122
- declare namespace proof {
123
- export {
124
- proof_encodeParams as encodeParams,
125
- proof_getFeatherProof as getFeatherProof,
126
- proof_getLayerZeroPacket as getLayerZeroPacket,
127
- proof_getReceiptProof as getReceiptProof,
128
- };
129
- }
130
-
131
- export { EVMUtilityVersion, NETWORKS, OutboundProofType, UTILS_VERSION_LOOKUP, proof as proofUtils };