-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproof.js
61 lines (53 loc) · 1.58 KB
/
proof.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
const { ethers } = require('ethers')
const { MerkleTree } = require('merkletreejs')
const { keccak256 } = ethers//.utils
const fs = require('fs')
const args = process.argv.slice(2)
let csv_path = args[0]
let out_dir = args[1]
fs.readFile(csv_path, 'utf8', (err, data) => {
// load wl from file, filter for remove last empty row
let whitelist = data.split('\n').map(r => r.split(',')[0].trim()).filter(r => r)
console.log('total:', whitelist.length)
// get tree
let leaves = whitelist.map((addr) => keccak256(addr))
let tree = new MerkleTree(leaves, keccak256, { sortPairs: true })
let root = tree.getHexRoot()
console.log('root:', root)
// separate wl by first char after 0x
let chunk = {}
let from_time = new Date()
whitelist.forEach(addr => {
// gen proof
let leaf = keccak256(addr)
let proof = tree.getHexProof(leaf)
// add to chunk
let l = addr.toLowerCase()
let s = l[2]
if (!chunk[s]) chunk[s] = {}
chunk[s][l] = proof
})
let usage_time = (new Date()) - from_time
console.log('usage time:', usage_time/1_000, 's')
// debug
// let sum = 0
// console.log(chunk)
// console.log(whitelist.length)
// for (k in chunk) {
// let size = Object.keys(chunk[k]).length
// console.log(k, size)
// sum += size
// }
// console.log(sum)
// write to file
for (k in chunk) {
let content = JSON.stringify(chunk[k])
let out_path = `./${out_dir}/${k}.json`
fs.writeFile(out_path, content, 'utf8', (err) => {
if (err)
console.error(out_path, err);
else
console.log(out_path, 'OK');
})
}
})