@layerzerolabs/lz-proof-utility 3.0.15 → 3.0.16
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/CHANGELOG.md +6 -0
- package/README.md +110 -0
- package/dist/index.cjs +6 -0
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.mts +54 -0
- package/dist/index.d.ts +54 -0
- package/dist/index.mjs +6 -0
- package/dist/index.mjs.map +1 -1
- package/package.json +3 -3
package/CHANGELOG.md
CHANGED
package/README.md
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
# @layerzerolabs/lz-proof-utility
|
|
2
|
+
|
|
3
|
+
The LayerZero Proof Utility package provides a set of essential utilities and modules to facilitate the generation and verification of proofs for various blockchain networks. It includes functions for generating receipt proofs, encoding parameters, and handling state-sync transactions.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- **Proof Generation**: Functions for generating receipt proofs and feather proofs.
|
|
8
|
+
- **Parameter Encoding**: Functions for encoding proof parameters.
|
|
9
|
+
- **State-Sync Handling**: Functions for handling state-sync transactions in Polygon blocks.
|
|
10
|
+
|
|
11
|
+
## Installation
|
|
12
|
+
|
|
13
|
+
To install the LayerZero Proof Utility package, you can use npm or yarn:
|
|
14
|
+
|
|
15
|
+
```sh
|
|
16
|
+
npm install @layerzerolabs/lz-proof-utility
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
or
|
|
20
|
+
|
|
21
|
+
```sh
|
|
22
|
+
yarn add @layerzerolabs/lz-proof-utility
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## Usage
|
|
26
|
+
|
|
27
|
+
### Proof Generation
|
|
28
|
+
|
|
29
|
+
#### getReceiptProof
|
|
30
|
+
|
|
31
|
+
Retrieves the receipt proof based on the provided parameters.
|
|
32
|
+
|
|
33
|
+
```typescript
|
|
34
|
+
import { getReceiptProof } from "@layerzerolabs/lz-proof-utility"
|
|
35
|
+
|
|
36
|
+
const network = "mainnet"
|
|
37
|
+
const block = { /* block object */ }
|
|
38
|
+
const transactionReceipts = [ /* array of transaction receipts */ ]
|
|
39
|
+
const transactionIndex = 0
|
|
40
|
+
const outboundProofType = 1 // MPT
|
|
41
|
+
const utilsVersion = 1 // V1
|
|
42
|
+
|
|
43
|
+
getReceiptProof(network, block, transactionReceipts, transactionIndex, outboundProofType, utilsVersion).then((proof) => {
|
|
44
|
+
console.log(`Receipt Proof: ${proof}`)
|
|
45
|
+
})
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
#### getFeatherProof
|
|
49
|
+
|
|
50
|
+
Generates a feather proof based on the utility version.
|
|
51
|
+
|
|
52
|
+
```typescript
|
|
53
|
+
import { getFeatherProof } from "@layerzerolabs/lz-proof-utility"
|
|
54
|
+
|
|
55
|
+
const utilsVersion = 1 // V1
|
|
56
|
+
const emitterAddress = "0x1234567890abcdef1234567890abcdef12345678"
|
|
57
|
+
const packetPayload = "0xabcdef"
|
|
58
|
+
|
|
59
|
+
const proof = getFeatherProof(utilsVersion, emitterAddress, packetPayload)
|
|
60
|
+
console.log(`Feather Proof: ${proof.proof}`)
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
### Parameter Encoding
|
|
64
|
+
|
|
65
|
+
#### encodeParams
|
|
66
|
+
|
|
67
|
+
Encodes the parameters for the proof.
|
|
68
|
+
|
|
69
|
+
```typescript
|
|
70
|
+
import { encodeParams } from "@layerzerolabs/lz-proof-utility"
|
|
71
|
+
|
|
72
|
+
const proof = { /* proof object */ }
|
|
73
|
+
const outboundProofType = 1 // MPT
|
|
74
|
+
const utilsVersion = 1 // V1
|
|
75
|
+
const logIndex = 0
|
|
76
|
+
const srcEndpointId = 1
|
|
77
|
+
|
|
78
|
+
const encodedParams = encodeParams(proof, outboundProofType, utilsVersion, logIndex, srcEndpointId)
|
|
79
|
+
console.log(`Encoded Params: ${encodedParams}`)
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
### State-Sync Handling
|
|
83
|
+
|
|
84
|
+
#### getPolygonStateSyncTxHash
|
|
85
|
+
|
|
86
|
+
Returns the transaction hash for the state-sync receipt in a Polygon block.
|
|
87
|
+
|
|
88
|
+
```typescript
|
|
89
|
+
import { getPolygonStateSyncTxHash } from "@layerzerolabs/lz-proof-utility"
|
|
90
|
+
|
|
91
|
+
const block = { /* block object */ }
|
|
92
|
+
|
|
93
|
+
const txHash = getPolygonStateSyncTxHash(block)
|
|
94
|
+
console.log(`State-Sync Tx Hash: ${txHash}`)
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
### Utility Functions
|
|
98
|
+
|
|
99
|
+
#### buffer2hex
|
|
100
|
+
|
|
101
|
+
Converts a buffer to a hex string.
|
|
102
|
+
|
|
103
|
+
```typescript
|
|
104
|
+
import { buffer2hex } from "@layerzerolabs/lz-proof-utility"
|
|
105
|
+
|
|
106
|
+
const buffer = Buffer.from("example")
|
|
107
|
+
|
|
108
|
+
const hexString = buffer2hex(buffer)
|
|
109
|
+
console.log(`Hex String: ${hexString}`)
|
|
110
|
+
```
|
package/dist/index.cjs
CHANGED
|
@@ -23,7 +23,13 @@ var __export = (target, all) => {
|
|
|
23
23
|
// src/constants.ts
|
|
24
24
|
var NETWORKS = ["default", "hardhat", "arbitrum", "polygon", "harmony"];
|
|
25
25
|
var OutboundProofType = {
|
|
26
|
+
/**
|
|
27
|
+
* Merkle Patricia Trie proof type.
|
|
28
|
+
*/
|
|
26
29
|
MPT: 1,
|
|
30
|
+
/**
|
|
31
|
+
* Feather proof type.
|
|
32
|
+
*/
|
|
27
33
|
FP: 2
|
|
28
34
|
};
|
|
29
35
|
var EVMUtilityVersion = {
|
package/dist/index.cjs.map
CHANGED
|
@@ -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","assertReceipt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Ae,MAAM,WAAW,MAAM,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,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): { proof: string } {\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 getReceiptProofV1(network, block, transactionReceipts, transactionIndex)\n }\n case EVMUtilityVersion.V2:\n case EVMUtilityVersion.V4: {\n return getReceiptProofV2(network, block, transactionReceipts, transactionIndex)\n }\n case EVMUtilityVersion.V3: {\n const proof = await getReceiptProofV2(network, block, transactionReceipts, transactionIndex)\n return {\n ...proof,\n blockHash: block.hash,\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 encodeParams(proof, outboundProofType, utilsVersion, logIndex, srcEndpointId?: number): string {\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): number {\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): boolean {\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\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 CheckpointTrie from 'merkle-patricia-tree'\nimport { promisfy } from 'promisfy'\nimport invariant from 'tiny-invariant'\n\nexport function getPolygonStateSyncTxHash(block): string {\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): string {\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 CheckpointTrie()\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\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): boolean {\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","rlp","matchingNibbleLength","stringToNibbles","assembleMPTProof","assertReceiptInclusion"],"mappings":";;;;;;;AAGO,IAAM,WAAW,CAAC,WAAW,WAAW,YAAY,WAAW,SAAS;AAKxE,IAAM,oBAAoB;AAAA;AAAA;AAAA;AAAA,EAI7B,KAAK;AAAA;AAAA;AAAA;AAAA,EAIL,IAAI;AACR;AAKO,IAAM,oBAAoB;AAAA,EAC7B,IAAI;AAAA,EACJ,IAAI;AAAA,EACJ,IAAI;AAAA,EACJ,IAAI;AACR;AAKO,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,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AI,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":["/**\n * Supported networks.\n */\nexport const NETWORKS = ['default', 'hardhat', 'arbitrum', 'polygon', 'harmony']\n\n/**\n * Enum representing outbound proof types.\n */\nexport const OutboundProofType = {\n /**\n * Merkle Patricia Trie proof type.\n */\n MPT: 1,\n /**\n * Feather proof type.\n */\n FP: 2,\n}\n\n/**\n * Enum representing EVM utility versions.\n */\nexport const EVMUtilityVersion = {\n V1: 1,\n V2: 2,\n V3: 3,\n V4: 4,\n}\n\n/**\n * Lookup table for utility versions by network and address.\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\n/**\n * Generates a feather proof based on the utility version.\n *\n * @param {number} utilsVersion - The utility version.\n * @param {string} emitterAddress - The emitter address.\n * @param {string} packetPayload - The packet payload.\n * @returns {{ proof: string }} The generated proof.\n */\nexport function getFeatherProof(utilsVersion, emitterAddress, packetPayload): { proof: string } {\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\n/**\n * Retrieves the receipt proof based on the provided parameters.\n *\n * @param {string} network - The network name.\n * @param {object} block - The block object.\n * @param {Array} transactionReceipts - The transaction receipts.\n * @param {number} transactionIndex - The transaction index.\n * @param {number} outboundProofType - The outbound proof type.\n * @param {number} utilsVersion - The utility version.\n * @returns {Promise<object>} The receipt proof.\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 getReceiptProofV1(network, block, transactionReceipts, transactionIndex)\n }\n case EVMUtilityVersion.V2:\n case EVMUtilityVersion.V4: {\n return getReceiptProofV2(network, block, transactionReceipts, transactionIndex)\n }\n case EVMUtilityVersion.V3: {\n const proof = await getReceiptProofV2(network, block, transactionReceipts, transactionIndex)\n return {\n ...proof,\n blockHash: block.hash,\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\n/**\n * Encodes the parameters for the proof.\n *\n * @param {object} proof - The proof object.\n * @param {number} outboundProofType - The outbound proof type.\n * @param {number} utilsVersion - The utility version.\n * @param {number} logIndex - The log index.\n * @param {number} [srcEndpointId] - The source endpoint ID.\n * @returns {string} The encoded parameters.\n */\nexport function encodeParams(proof, outboundProofType, utilsVersion, logIndex, srcEndpointId?: number): string {\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\n/**\n * Retrieves the LayerZero packet from the log data.\n *\n * @param {number} srcChainId - The source chain ID.\n * @param {object} _log - The log object.\n * @returns {object} The LayerZero packet.\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\n/**\n * Finds the pointer in the full bytes.\n *\n * @param {Buffer} fullBytes - The full bytes.\n * @param {Buffer} currentNodeElement - The current node element.\n * @returns {number} The pointer.\n */\nfunction findPointer(fullBytes, currentNodeElement): number {\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 */\n\n/**\n * Asserts the inclusion of a receipt in the MPT.\n *\n * @param {string} receiptsRoot - The root of the receipts.\n * @param {string} expectedLogValue - The expected log value.\n * @param {Array} rlpProof - The RLP encoded proof.\n * @param {Array} pointers - The pointers in the MPT.\n * @returns {boolean} True if the receipt is included, false otherwise.\n */\nfunction assertReceiptInclusion(receiptsRoot, expectedLogValue, rlpProof, pointers): boolean {\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\n/**\n * Assembles a Merkle Patricia Tree (MPT) proof.\n *\n * @param {object} proof - The proof object.\n * @param {object} block - The block object.\n * @param {number} transactionIndex - The transaction index.\n * @returns {object} The assembled MPT proof.\n */\nfunction assembleMPTProof(proof, block, transactionIndex) {\n const { stack } = proof\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\n/**\n * Retrieves the receipt proof.\n *\n * @param {string} network - The network.\n * @param {object} block - The block object.\n * @param {Array} transactionReceipts - The transaction receipts.\n * @param {number} transactionIndex - The transaction index.\n * @returns {Promise<object>} A promise that resolves to the receipt proof.\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 CheckpointTrie from 'merkle-patricia-tree'\nimport { promisfy } from 'promisfy'\nimport invariant from 'tiny-invariant'\n\n/**\n * Returns the transaction hash for the state-sync receipt in a Polygon block.\n *\n * @param {object} block - The block object.\n * @returns {string} The transaction hash for the state-sync receipt.\n */\nexport function getPolygonStateSyncTxHash(block): string {\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\n/**\n * Converts a buffer to a hex string.\n *\n * @param {Buffer} buffer - The buffer to convert.\n * @returns {string} The hex string.\n */\nexport function buffer2hex(buffer): string {\n return '0x' + buffer.toString('hex')\n}\n\n/**\n * Generates a receipt proof from the given network, block, transaction receipts, and transaction index.\n *\n * @param {string} network - The network name.\n * @param {object} block - The block object.\n * @param {object[]} transactionReceipts - The transaction receipts.\n * @param {number} transactionIndex - The transaction index.\n * @returns {Promise<object>} A promise that resolves to an object containing the receipt proof and stack.\n * @throws {Error} If the receipt root from the RPC block does not match the receipt root built.\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 CheckpointTrie()\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\n/**\n * Assembles a Merkle Patricia Tree (MPT) proof.\n *\n * @param {object} proof - The proof object.\n * @param {object} block - The block object.\n * @param {number} transactionIndex - The transaction index.\n * @returns {object} The assembled MPT proof.\n */\nfunction assembleMPTProof(proof, block, transactionIndex) {\n const { stack } = proof\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 */\n\n/**\n * Asserts the inclusion of a receipt in the MPT.\n *\n * @param {string} receiptsRoot - The root of the receipts.\n * @param {string} expectedLogValue - The expected log value.\n * @param {Array} rlpProof - The RLP encoded proof.\n * @param {Array} path - The path in the MPT.\n * @param {Array} stack - The stack of nodes.\n * @returns {boolean} True if the receipt is included, false otherwise.\n */\nfunction assertReceiptInclusion(receiptsRoot, expectedLogValue, rlpProof, path, stack): boolean {\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\n/**\n * Retrieves the receipt proof.\n *\n * @param {string} network - The network.\n * @param {object} block - The block object.\n * @param {Array} transactionReceipts - The transaction receipts.\n * @param {number} transactionIndex - The transaction index.\n * @returns {Promise<object>} A promise that resolves to the receipt proof.\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.d.mts
CHANGED
|
@@ -1,14 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Supported networks.
|
|
3
|
+
*/
|
|
1
4
|
declare const NETWORKS: string[];
|
|
5
|
+
/**
|
|
6
|
+
* Enum representing outbound proof types.
|
|
7
|
+
*/
|
|
2
8
|
declare const OutboundProofType: {
|
|
9
|
+
/**
|
|
10
|
+
* Merkle Patricia Trie proof type.
|
|
11
|
+
*/
|
|
3
12
|
MPT: number;
|
|
13
|
+
/**
|
|
14
|
+
* Feather proof type.
|
|
15
|
+
*/
|
|
4
16
|
FP: number;
|
|
5
17
|
};
|
|
18
|
+
/**
|
|
19
|
+
* Enum representing EVM utility versions.
|
|
20
|
+
*/
|
|
6
21
|
declare const EVMUtilityVersion: {
|
|
7
22
|
V1: number;
|
|
8
23
|
V2: number;
|
|
9
24
|
V3: number;
|
|
10
25
|
V4: number;
|
|
11
26
|
};
|
|
27
|
+
/**
|
|
28
|
+
* Lookup table for utility versions by network and address.
|
|
29
|
+
*/
|
|
12
30
|
declare const UTILS_VERSION_LOOKUP: {
|
|
13
31
|
arbitrum: {
|
|
14
32
|
'0x4ee2f9b7cf3a68966c370f3eb2c16613d3235245': number;
|
|
@@ -91,9 +109,28 @@ declare const UTILS_VERSION_LOOKUP: {
|
|
|
91
109
|
};
|
|
92
110
|
};
|
|
93
111
|
|
|
112
|
+
/**
|
|
113
|
+
* Generates a feather proof based on the utility version.
|
|
114
|
+
*
|
|
115
|
+
* @param {number} utilsVersion - The utility version.
|
|
116
|
+
* @param {string} emitterAddress - The emitter address.
|
|
117
|
+
* @param {string} packetPayload - The packet payload.
|
|
118
|
+
* @returns {{ proof: string }} The generated proof.
|
|
119
|
+
*/
|
|
94
120
|
declare function getFeatherProof(utilsVersion: any, emitterAddress: any, packetPayload: any): {
|
|
95
121
|
proof: string;
|
|
96
122
|
};
|
|
123
|
+
/**
|
|
124
|
+
* Retrieves the receipt proof based on the provided parameters.
|
|
125
|
+
*
|
|
126
|
+
* @param {string} network - The network name.
|
|
127
|
+
* @param {object} block - The block object.
|
|
128
|
+
* @param {Array} transactionReceipts - The transaction receipts.
|
|
129
|
+
* @param {number} transactionIndex - The transaction index.
|
|
130
|
+
* @param {number} outboundProofType - The outbound proof type.
|
|
131
|
+
* @param {number} utilsVersion - The utility version.
|
|
132
|
+
* @returns {Promise<object>} The receipt proof.
|
|
133
|
+
*/
|
|
97
134
|
declare function getReceiptProof(network: any, block: any, transactionReceipts: any, transactionIndex: any, outboundProofType: any, utilsVersion: any): Promise<{
|
|
98
135
|
receiptRoot: any;
|
|
99
136
|
proof: string[];
|
|
@@ -109,7 +146,24 @@ declare function getReceiptProof(network: any, block: any, transactionReceipts:
|
|
|
109
146
|
proof: string[];
|
|
110
147
|
receiptSlotIndex: number[];
|
|
111
148
|
}>;
|
|
149
|
+
/**
|
|
150
|
+
* Encodes the parameters for the proof.
|
|
151
|
+
*
|
|
152
|
+
* @param {object} proof - The proof object.
|
|
153
|
+
* @param {number} outboundProofType - The outbound proof type.
|
|
154
|
+
* @param {number} utilsVersion - The utility version.
|
|
155
|
+
* @param {number} logIndex - The log index.
|
|
156
|
+
* @param {number} [srcEndpointId] - The source endpoint ID.
|
|
157
|
+
* @returns {string} The encoded parameters.
|
|
158
|
+
*/
|
|
112
159
|
declare function encodeParams(proof: any, outboundProofType: any, utilsVersion: any, logIndex: any, srcEndpointId?: number): string;
|
|
160
|
+
/**
|
|
161
|
+
* Retrieves the LayerZero packet from the log data.
|
|
162
|
+
*
|
|
163
|
+
* @param {number} srcChainId - The source chain ID.
|
|
164
|
+
* @param {object} _log - The log object.
|
|
165
|
+
* @returns {object} The LayerZero packet.
|
|
166
|
+
*/
|
|
113
167
|
declare function getLayerZeroPacket(srcChainId: any, _log: any): {
|
|
114
168
|
srcChainId: any;
|
|
115
169
|
dstChainId: number;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,14 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Supported networks.
|
|
3
|
+
*/
|
|
1
4
|
declare const NETWORKS: string[];
|
|
5
|
+
/**
|
|
6
|
+
* Enum representing outbound proof types.
|
|
7
|
+
*/
|
|
2
8
|
declare const OutboundProofType: {
|
|
9
|
+
/**
|
|
10
|
+
* Merkle Patricia Trie proof type.
|
|
11
|
+
*/
|
|
3
12
|
MPT: number;
|
|
13
|
+
/**
|
|
14
|
+
* Feather proof type.
|
|
15
|
+
*/
|
|
4
16
|
FP: number;
|
|
5
17
|
};
|
|
18
|
+
/**
|
|
19
|
+
* Enum representing EVM utility versions.
|
|
20
|
+
*/
|
|
6
21
|
declare const EVMUtilityVersion: {
|
|
7
22
|
V1: number;
|
|
8
23
|
V2: number;
|
|
9
24
|
V3: number;
|
|
10
25
|
V4: number;
|
|
11
26
|
};
|
|
27
|
+
/**
|
|
28
|
+
* Lookup table for utility versions by network and address.
|
|
29
|
+
*/
|
|
12
30
|
declare const UTILS_VERSION_LOOKUP: {
|
|
13
31
|
arbitrum: {
|
|
14
32
|
'0x4ee2f9b7cf3a68966c370f3eb2c16613d3235245': number;
|
|
@@ -91,9 +109,28 @@ declare const UTILS_VERSION_LOOKUP: {
|
|
|
91
109
|
};
|
|
92
110
|
};
|
|
93
111
|
|
|
112
|
+
/**
|
|
113
|
+
* Generates a feather proof based on the utility version.
|
|
114
|
+
*
|
|
115
|
+
* @param {number} utilsVersion - The utility version.
|
|
116
|
+
* @param {string} emitterAddress - The emitter address.
|
|
117
|
+
* @param {string} packetPayload - The packet payload.
|
|
118
|
+
* @returns {{ proof: string }} The generated proof.
|
|
119
|
+
*/
|
|
94
120
|
declare function getFeatherProof(utilsVersion: any, emitterAddress: any, packetPayload: any): {
|
|
95
121
|
proof: string;
|
|
96
122
|
};
|
|
123
|
+
/**
|
|
124
|
+
* Retrieves the receipt proof based on the provided parameters.
|
|
125
|
+
*
|
|
126
|
+
* @param {string} network - The network name.
|
|
127
|
+
* @param {object} block - The block object.
|
|
128
|
+
* @param {Array} transactionReceipts - The transaction receipts.
|
|
129
|
+
* @param {number} transactionIndex - The transaction index.
|
|
130
|
+
* @param {number} outboundProofType - The outbound proof type.
|
|
131
|
+
* @param {number} utilsVersion - The utility version.
|
|
132
|
+
* @returns {Promise<object>} The receipt proof.
|
|
133
|
+
*/
|
|
97
134
|
declare function getReceiptProof(network: any, block: any, transactionReceipts: any, transactionIndex: any, outboundProofType: any, utilsVersion: any): Promise<{
|
|
98
135
|
receiptRoot: any;
|
|
99
136
|
proof: string[];
|
|
@@ -109,7 +146,24 @@ declare function getReceiptProof(network: any, block: any, transactionReceipts:
|
|
|
109
146
|
proof: string[];
|
|
110
147
|
receiptSlotIndex: number[];
|
|
111
148
|
}>;
|
|
149
|
+
/**
|
|
150
|
+
* Encodes the parameters for the proof.
|
|
151
|
+
*
|
|
152
|
+
* @param {object} proof - The proof object.
|
|
153
|
+
* @param {number} outboundProofType - The outbound proof type.
|
|
154
|
+
* @param {number} utilsVersion - The utility version.
|
|
155
|
+
* @param {number} logIndex - The log index.
|
|
156
|
+
* @param {number} [srcEndpointId] - The source endpoint ID.
|
|
157
|
+
* @returns {string} The encoded parameters.
|
|
158
|
+
*/
|
|
112
159
|
declare function encodeParams(proof: any, outboundProofType: any, utilsVersion: any, logIndex: any, srcEndpointId?: number): string;
|
|
160
|
+
/**
|
|
161
|
+
* Retrieves the LayerZero packet from the log data.
|
|
162
|
+
*
|
|
163
|
+
* @param {number} srcChainId - The source chain ID.
|
|
164
|
+
* @param {object} _log - The log object.
|
|
165
|
+
* @returns {object} The LayerZero packet.
|
|
166
|
+
*/
|
|
113
167
|
declare function getLayerZeroPacket(srcChainId: any, _log: any): {
|
|
114
168
|
srcChainId: any;
|
|
115
169
|
dstChainId: number;
|
package/dist/index.mjs
CHANGED
|
@@ -16,7 +16,13 @@ var __export = (target, all) => {
|
|
|
16
16
|
// src/constants.ts
|
|
17
17
|
var NETWORKS = ["default", "hardhat", "arbitrum", "polygon", "harmony"];
|
|
18
18
|
var OutboundProofType = {
|
|
19
|
+
/**
|
|
20
|
+
* Merkle Patricia Trie proof type.
|
|
21
|
+
*/
|
|
19
22
|
MPT: 1,
|
|
23
|
+
/**
|
|
24
|
+
* Feather proof type.
|
|
25
|
+
*/
|
|
20
26
|
FP: 2
|
|
21
27
|
};
|
|
22
28
|
var EVMUtilityVersion = {
|
package/dist/index.mjs.map
CHANGED
|
@@ -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,8CAA8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AAE;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): { proof: string } {\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 getReceiptProofV1(network, block, transactionReceipts, transactionIndex)\n }\n case EVMUtilityVersion.V2:\n case EVMUtilityVersion.V4: {\n return getReceiptProofV2(network, block, transactionReceipts, transactionIndex)\n }\n case EVMUtilityVersion.V3: {\n const proof = await getReceiptProofV2(network, block, transactionReceipts, transactionIndex)\n return {\n ...proof,\n blockHash: block.hash,\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 encodeParams(proof, outboundProofType, utilsVersion, logIndex, srcEndpointId?: number): string {\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): number {\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): boolean {\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\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 CheckpointTrie from 'merkle-patricia-tree'\nimport { promisfy } from 'promisfy'\nimport invariant from 'tiny-invariant'\n\nexport function getPolygonStateSyncTxHash(block): string {\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): string {\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 CheckpointTrie()\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\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): boolean {\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","rlp","matchingNibbleLength","string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n\n/**\n * Enum representing outbound proof types.\n */\nexport const OutboundProofType = {\n /**\n * Merkle Patricia Trie proof type.\n */\n MPT: 1,\n /**\n * Feather proof type.\n */\n FP: 2,\n}\n\n/**\n * Enum representing EVM utility versions.\n */\nexport const EVMUtilityVersion = {\n V1: 1,\n V2: 2,\n V3: 3,\n V4: 4,\n}\n\n/**\n * Lookup table for utility versions by network and address.\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\n/**\n * Generates a feather proof based on the utility version.\n *\n * @param {number} utilsVersion - The utility version.\n * @param {string} emitterAddress - The emitter address.\n * @param {string} packetPayload - The packet payload.\n * @returns {{ proof: string }} The generated proof.\n */\nexport function getFeatherProof(utilsVersion, emitterAddress, packetPayload): { proof: string } {\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\n/**\n * Retrieves the receipt proof based on the provided parameters.\n *\n * @param {string} network - The network name.\n * @param {object} block - The block object.\n * @param {Array} transactionReceipts - The transaction receipts.\n * @param {number} transactionIndex - The transaction index.\n * @param {number} outboundProofType - The outbound proof type.\n * @param {number} utilsVersion - The utility version.\n * @returns {Promise<object>} The receipt proof.\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 getReceiptProofV1(network, block, transactionReceipts, transactionIndex)\n }\n case EVMUtilityVersion.V2:\n case EVMUtilityVersion.V4: {\n return getReceiptProofV2(network, block, transactionReceipts, transactionIndex)\n }\n case EVMUtilityVersion.V3: {\n const proof = await getReceiptProofV2(network, block, transactionReceipts, transactionIndex)\n return {\n ...proof,\n blockHash: block.hash,\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\n/**\n * Encodes the parameters for the proof.\n *\n * @param {object} proof - The proof object.\n * @param {number} outboundProofType - The outbound proof type.\n * @param {number} utilsVersion - The utility version.\n * @param {number} logIndex - The log index.\n * @param {number} [srcEndpointId] - The source endpoint ID.\n * @returns {string} The encoded parameters.\n */\nexport function encodeParams(proof, outboundProofType, utilsVersion, logIndex, srcEndpointId?: number): string {\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\n/**\n * Retrieves the LayerZero packet from the log data.\n *\n * @param {number} srcChainId - The source chain ID.\n * @param {object} _log - The log object.\n * @returns {object} The LayerZero packet.\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\n/**\n * Finds the pointer in the full bytes.\n *\n * @param {Buffer} fullBytes - The full bytes.\n * @param {Buffer} currentNodeElement - The current node element.\n * @returns {number} The pointer.\n */\nfunction findPointer(fullBytes, currentNodeElement): number {\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 */\n\n/**\n * Asserts the inclusion of a receipt in the MPT.\n *\n * @param {string} receiptsRoot - The root of the receipts.\n * @param {string} expectedLogValue - The expected log value.\n * @param {Array} rlpProof - The RLP encoded proof.\n * @param {Array} pointers - The pointers in the MPT.\n * @returns {boolean} True if the receipt is included, false otherwise.\n */\nfunction assertReceiptInclusion(receiptsRoot, expectedLogValue, rlpProof, pointers): boolean {\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\n/**\n * Assembles a Merkle Patricia Tree (MPT) proof.\n *\n * @param {object} proof - The proof object.\n * @param {object} block - The block object.\n * @param {number} transactionIndex - The transaction index.\n * @returns {object} The assembled MPT proof.\n */\nfunction assembleMPTProof(proof, block, transactionIndex) {\n const { stack } = proof\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\n/**\n * Retrieves the receipt proof.\n *\n * @param {string} network - The network.\n * @param {object} block - The block object.\n * @param {Array} transactionReceipts - The transaction receipts.\n * @param {number} transactionIndex - The transaction index.\n * @returns {Promise<object>} A promise that resolves to the receipt proof.\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 CheckpointTrie from 'merkle-patricia-tree'\nimport { promisfy } from 'promisfy'\nimport invariant from 'tiny-invariant'\n\n/**\n * Returns the transaction hash for the state-sync receipt in a Polygon block.\n *\n * @param {object} block - The block object.\n * @returns {string} The transaction hash for the state-sync receipt.\n */\nexport function getPolygonStateSyncTxHash(block): string {\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\n/**\n * Converts a buffer to a hex string.\n *\n * @param {Buffer} buffer - The buffer to convert.\n * @returns {string} The hex string.\n */\nexport function buffer2hex(buffer): string {\n return '0x' + buffer.toString('hex')\n}\n\n/**\n * Generates a receipt proof from the given network, block, transaction receipts, and transaction index.\n *\n * @param {string} network - The network name.\n * @param {object} block - The block object.\n * @param {object[]} transactionReceipts - The transaction receipts.\n * @param {number} transactionIndex - The transaction index.\n * @returns {Promise<object>} A promise that resolves to an object containing the receipt proof and stack.\n * @throws {Error} If the receipt root from the RPC block does not match the receipt root built.\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 CheckpointTrie()\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\n/**\n * Assembles a Merkle Patricia Tree (MPT) proof.\n *\n * @param {object} proof - The proof object.\n * @param {object} block - The block object.\n * @param {number} transactionIndex - The transaction index.\n * @returns {object} The assembled MPT proof.\n */\nfunction assembleMPTProof(proof, block, transactionIndex) {\n const { stack } = proof\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 */\n\n/**\n * Asserts the inclusion of a receipt in the MPT.\n *\n * @param {string} receiptsRoot - The root of the receipts.\n * @param {string} expectedLogValue - The expected log value.\n * @param {Array} rlpProof - The RLP encoded proof.\n * @param {Array} path - The path in the MPT.\n * @param {Array} stack - The stack of nodes.\n * @returns {boolean} True if the receipt is included, false otherwise.\n */\nfunction assertReceiptInclusion(receiptsRoot, expectedLogValue, rlpProof, path, stack): boolean {\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\n/**\n * Retrieves the receipt proof.\n *\n * @param {string} network - The network.\n * @param {object} block - The block object.\n * @param {Array} transactionReceipts - The transaction receipts.\n * @param {number} transactionIndex - The transaction index.\n * @returns {Promise<object>} A promise that resolves to the receipt proof.\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": "3.0.
|
|
3
|
+
"version": "3.0.16",
|
|
4
4
|
"description": "LayerZero Proof Utility",
|
|
5
5
|
"license": "BUSL-1.1",
|
|
6
6
|
"exports": {
|
|
@@ -30,8 +30,8 @@
|
|
|
30
30
|
"tiny-invariant": "^1.3.1"
|
|
31
31
|
},
|
|
32
32
|
"devDependencies": {
|
|
33
|
-
"@layerzerolabs/tsup-config-next": "^3.0.
|
|
34
|
-
"@layerzerolabs/typescript-config-next": "^3.0.
|
|
33
|
+
"@layerzerolabs/tsup-config-next": "^3.0.16",
|
|
34
|
+
"@layerzerolabs/typescript-config-next": "^3.0.16",
|
|
35
35
|
"@types/safer-buffer": "^2.1.3",
|
|
36
36
|
"rimraf": "^5.0.5",
|
|
37
37
|
"tsup": "^8.0.1",
|