-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmerkle.js
42 lines (32 loc) · 1.19 KB
/
merkle.js
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
const keccak256 = require("keccak256");
const { MerkleTree } = require("merkletreejs");
const Web3 = require("web3");
const web3 = new Web3();
const fs = require("fs");
let CHAIN_ID = process.env.CHAIN_ID || 4;
let INPUT = process.env.INPUT || "addrs.txt";
let addresses = fs.readFileSync(INPUT).toString().split("\n").filter(n => n);
const leafNodes = addresses.map((item) =>
keccak256(
Buffer.concat([
Buffer.from(web3.eth.abi.encodeParameter("uint256", item.toLowerCase()).replace("0x", ""), "hex"),
Buffer.from(web3.eth.abi.encodeParameter("uint256", CHAIN_ID).replace("0x", ""), "hex"),
])
)
);
const merkleTree = new MerkleTree(leafNodes, keccak256, { sortPairs: true });
console.log("---------");
console.log("Merke Tree");
console.log("---------");
console.log(merkleTree.toString());
console.log("---------");
console.log("Merkle Root: " + merkleTree.getHexRoot());
let result = {};
for(var index in leafNodes) {
const node = leafNodes[index];
const addr = addresses[index];
const proofs = merkleTree.getHexProof(node);
result[addr] = proofs;
}
fs.writeFileSync("proofs.json", JSON.stringify(result, undefined, 4));
console.log("Saved to proofs.json");