-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgenerateMerkleProof.js
70 lines (62 loc) · 2.1 KB
/
generateMerkleProof.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
66
67
68
69
70
const keccak256 = require('keccak256');
const toHex = require('to-hex');
const fs = require('fs');
const { MerkleTree } = require('merkletreejs')
const geneList = require('./data/NFgenesList.json');
/**
* TO DO: Add a check for a generated tree that is unbalanced. At a minimum
* give a notice of unbalaned tree and perhaps add functionality to auto-balance.
*/
let tree;
let root;
let leaf
let leafIndex;
let checkStatus;
const extract = geneList.id_symbol_primary;
async function generateMerkleTree(symbol) {
try {
tree = new MerkleTree(extract, keccak256, {sortPairs: true, sortLeaves: true, sort: true, hashLeaves: true});
root = tree.getHexRoot();
leaf = symbol;
} catch (e) {
console.log(e);
}
}
/**
* Using the selected leaf value, lookup the corresponding hash
* and index from the Merkle Tree Summary json file
*/
async function getLeafHashFromTreeSummary(symbol) {
try {
const treeSummary = JSON.parse(fs.readFileSync('./example/MerkleTreeSummary.json'));
const leafHash = treeSummary.filter(x => x.Leaf === symbol);
leafHash != 0 ? leafIndex = tree.getLeafIndex(leafHash[0].Hash) : checkStatus = 0;
} catch (e) {
console.log(e);
}
}
const checkValue = () => {
checkStatus === 0 ? console.log("Error: value does not exist within the list") : getProof(leafIndex);
}
async function getProof(value) {
try {
const leaves = tree.getHexLeaves();
const proof = tree.getHexProof(leaves[value]);
const leafValueHex = toHex(leaf);
const leafFilePath = `./example/MerkleProof_${leaf}.json`;
console.log(`Proof generated for ${leaf}`);
console.log(`Saving proof to ${leafFilePath}`);
fs.writeFileSync(leafFilePath, JSON.stringify({
"Leaf Value": leaf,
"Leaf Hex": leafValueHex,
"LeafHash": leaves[value],
"Proof": proof
}));
} catch (e) {
console.log(e);
}
}
generateMerkleTree("RPAP3")
// pass in a leaf value to generate a proof
.then(getLeafHashFromTreeSummary(leaf))
.then(checkValue)