-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
65 lines (54 loc) · 2.16 KB
/
index.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
55
56
57
58
59
60
61
62
63
64
65
const { MerkleTree } = require("merkletreejs");
const keccak256 = require("keccak256");
const ethers = require("ethers");
const address_list = require("./setup.js");
const fs = require("fs");
async function main() {
const addresses = formatAddresses(address_list);
// Ensure output folder exists
const output_folder = './build'
if(!fs.existsSync(output_folder)) {
fs.mkdirSync(output_folder);
}
// Create merkle tree
const buf2hex = x => "0x" + x.toString("hex");
console.log(addresses);
const leaves = addresses.map(x => keccak256(ethers.utils.getAddress(x)));
const tree = new MerkleTree(leaves, keccak256, { sort: true });
// Find the
const leaves_by_addr = {};
addresses.map(address => {
const addr_leaf = keccak256(ethers.utils.getAddress(address));
const addr_hexproof = tree.getProof(addr_leaf).map(x => buf2hex(x.data));
leaves_by_addr[address] = addr_hexproof;
});
// Make sure this stuff all worked, and then save it
const hexroot = tree.getHexRoot();
const leaf = keccak256(ethers.utils.getAddress(addresses[0]));
const hexleaf = buf2hex(leaf);
const hexproof = tree.getProof(leaf).map(x => buf2hex(x.data));
if(tree.verify(hexproof, hexleaf, hexroot)) {
const date = new Date();
const date_created = `${date.getFullYear()}_${date.getMonth()}_${date.getTime()}`
fs.writeFileSync(`${output_folder}/${date_created}_proofs.json`, JSON.stringify({ root: hexroot, leaves: leaves_by_addr }, null, 2));
console.log(`Created file: ${output_folder}/${date_created}_proofs.json`)
} else {
console.log('RIP your not suppose to get here. GL');
}
}
function formatAddresses(addresses) {
// Ensure a list exists
if(addresses.length == 0) {
throw new Error("You have no wallet address's in the setup.js file.");
}
// Remove white space and lowercase all address's
const formatted_addresses = addresses.map(addr => addr.toLowerCase().trim());
// Ensure all address are ETH address's
for(const address of formatted_addresses) {
if(!ethers.utils.isAddress(address)) {
throw new Error(`Is not a valid ETH address: ${address}`);
}
}
return formatted_addresses;
}
main().catch(err => console.error(err.message));