-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex2.js
54 lines (45 loc) · 1.72 KB
/
index2.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
42
43
44
45
46
47
48
49
50
51
52
53
54
const { MerkleTree } = require("merkletreejs");
const ethers = require("ethers");
const address_list = require("./setup.js");
const fs = require("fs");
async function main() {
// Ensure output folder exists
const output_folder = './build'
if(!fs.existsSync(output_folder)) {
fs.mkdirSync(output_folder);
}
const createLeaf = (address, amount) => {
if(!ethers.utils.isAddress(address)) {
throw new Error(`Is not a valid ETH address: ${address}`);
}
return ethers.utils.solidityKeccak256(
["string", "uint256"],
[ ethers.utils.getAddress(address), ethers.utils.parseEther(amount.toString()).toString()]
);
}
const leaves = address_list.map(([ address, amount ]) => {
return createLeaf(address, amount);
});
const tree = new MerkleTree(leaves, ethers.utils.keccak256, { sort: true });
const root = tree.getHexRoot();
// Create output data to save
let entries = [];
for(const [ address, amount ] of address_list) {
const leaf = createLeaf(address, amount);
const proof = tree.getHexProof(leaf);
entries.push({address, amount, leaf, proof});
}
// Test first entry
if(tree.verify(entries[0].proof, entries[0].leaf, root)) {
const date = new Date();
const date_created = `${date.getFullYear()}_${date.getMonth()}_${date.getTime()}`
fs.writeFileSync(`${output_folder}/${date_created}_proofs.json`, JSON.stringify({
root: root,
data: entries
}, null, 2));
console.log(`Created file: ${output_folder}/${date_created}_proofs.json`)
} else {
console.log('RIP your not suppose to get here. GL');
}
}
main().catch(err => console.error(err.message));