forked from 0xPolygon/fx-portal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpayload.ts
196 lines (179 loc) · 5.27 KB
/
payload.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import { ethers } from "hardhat";
import { ProofUtil } from "./proof_util";
import { IBaseBlock } from "./interface";
import ethUtils from "ethereumjs-util";
import { MerkleTree } from "./merkle_tree";
function getLogIndex_(logEventSig: string, receipt: any) {
let logIndex = -1;
switch (logEventSig) {
case "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef":
case "0xf94915c6d1fd521cee85359239227480c7e8776d7caf1fc3bacad5c269b66a14":
logIndex = receipt.logs.findIndex(
(log: any) =>
log.topics[0].toLowerCase() === logEventSig.toLowerCase() &&
log.topics[2].toLowerCase() ===
"0x0000000000000000000000000000000000000000000000000000000000000000"
);
break;
case "0xc3d58168c5ae7397731d063d5bbf3d657854427343f4c083240f7aacaa2d0f62":
case "0x4a39dc06d4c0dbc64b70af90fd698a233a518aa5d07e595d983b8c0526c8f7fb":
logIndex = receipt.logs.findIndex(
(log: any) =>
log.topics[0].toLowerCase() === logEventSig.toLowerCase() &&
log.topics[3].toLowerCase() ===
"0x0000000000000000000000000000000000000000000000000000000000000000"
);
break;
default:
logIndex = receipt.logs.findIndex(
(log: any) => log.topics[0].toLowerCase() === logEventSig.toLowerCase()
);
}
if (logIndex < 0) {
throw new Error("Log not found in receipt");
}
return logIndex;
}
function encodePayload_(
headerNumber: any,
buildBlockProof: any,
blockNumber: any,
timestamp: any,
transactionsRoot: any,
receiptsRoot: any,
receipt: any,
receiptParentNodes: any,
path: any,
logIndex: any
) {
return ethUtils.bufferToHex(
ethUtils.rlp.encode([
headerNumber,
buildBlockProof,
blockNumber,
timestamp,
ethUtils.bufferToHex(transactionsRoot),
ethUtils.bufferToHex(receiptsRoot),
ethUtils.bufferToHex(receipt),
ethUtils.bufferToHex(ethUtils.rlp.encode(receiptParentNodes)),
ethUtils.bufferToHex(Buffer.concat([Buffer.from("00", "hex"), path])),
logIndex,
])
);
}
function buildBlockProof(
maticWeb3: any,
startBlock: number,
endBlock: number,
blockNumber: number
) {
return ProofUtil.getFastMerkleProof(
maticWeb3,
blockNumber,
startBlock,
endBlock
).then((proof) => {
return ethUtils.bufferToHex(
Buffer.concat(
proof.map((p) => {
return ethUtils.toBuffer(p);
})
)
);
});
}
export async function getHeaders(
start: number,
end: number,
provider: typeof ethers.provider
) {
if (start >= end) {
return [];
}
let current = start;
let p = [];
let result = [];
while (current <= end) {
p = [];
for (let i = 0; i < 10 && current <= end; i++) {
p.push(
ethers.provider.send("eth_getBlockByNumber", [
ethers.utils.hexValue(current),
true,
])
);
current++;
}
if (p.length > 0) {
result.push(...(await Promise.all(p)));
}
}
return result.map(getBlockHeader);
}
export function getBlockHeader(block: IBaseBlock) {
// const n = new ethUtils.BN(block.number).toArrayLike(Buffer, 'be', 32)
// const ts = new ethUtils.BN(block.timestamp).toArrayLike(Buffer, 'be', 32)
const n = ethUtils.toBuffer(ethUtils.setLengthLeft(block.number, 32));
const ts = ethUtils.toBuffer(ethUtils.setLengthLeft(block.timestamp, 32));
const txRoot = ethUtils.toBuffer(block.transactionsRoot);
const receiptsRoot = ethUtils.toBuffer(block.receiptsRoot);
return ethUtils.keccak256(Buffer.concat([n, ts, txRoot, receiptsRoot]));
}
export async function buildPayloadForExit(
burnTxHash: string,
provider = ethers.provider,
logEventSig = "0x8c5261668696ce22758910d05bab8f186d6eb247ceac2af2e82c7dc17669b036",
isFast = false
) {
const requestConcurrency = 0;
const receipt = await provider.getTransactionReceipt(burnTxHash);
const block = await provider.send("eth_getBlockByNumber", [
ethers.utils.hexValue(receipt.blockNumber),
true,
]);
const rootBlockInfo = {
start: parseInt(block.number) - 1,
end: parseInt(block.number),
headerBlockNumber: 500,
};
const headers = await getHeaders(
rootBlockInfo.start,
rootBlockInfo.end,
provider
);
const tree = new MerkleTree(headers);
const fullBlockHeader = getBlockHeader(
(await provider.send("eth_getBlockByNumber", [
ethers.utils.hexValue(block.number),
true,
])) as unknown as IBaseBlock
);
const blockProofBuffer = tree.getProof(fullBlockHeader);
const blockProof = ethUtils.bufferToHex(Buffer.concat(blockProofBuffer));
const receiptProof: any = await ProofUtil.getReceiptProof(
receipt,
block,
requestConcurrency,
await Promise.all(
block.transactions.map((tx: any) =>
provider.getTransactionReceipt(tx.hash)
)
)
);
const logIndex = getLogIndex_(logEventSig, receipt);
return {
burnProof: encodePayload_(
rootBlockInfo.headerBlockNumber,
blockProof,
block.number,
block.timestamp,
Buffer.from(block.transactionsRoot.slice(2), "hex"),
Buffer.from(block.receiptsRoot.slice(2), "hex"),
ProofUtil.getReceiptBytes(receipt), // rlp encoded
receiptProof.parentNodes,
receiptProof.path,
logIndex
),
root: ethUtils.bufferToHex(tree.getRoot()),
};
}