-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgenerateMerkleTree.js
87 lines (77 loc) · 2.7 KB
/
generateMerkleTree.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
const keccak256 = require('keccak256');
const fs = require('fs');
const { MerkleTree } = require('merkletreejs')
const geneList = require('./data/NFgenesList.json');
const geneSymbols = [];
const leafValues = [];
let tree;
/**
* 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.
*/
const generateGeneSymbolList = async () => {
/**
* Creates a json file with the list of gene symbols
*/
try {
console.log('Saving gene symbol list to GeneSymbolList.json');
// Iterate through the gene list object and extract the gene symbols
geneList.id_symbol_primary.map(item => {
geneSymbols.push(item);
});
fs.writeFileSync('example/GeneSymbolList.json', JSON.stringify(geneSymbols));
} catch (e) {
console.log(e);
}
}
const generateMerkleTree = async () => {
/**
* Generate a Merkle Tree based on the geneSymbols array
*/
try {
console.log(`\nGenerating a Merkle Tree...`);
tree = new MerkleTree(geneSymbols, keccak256, {sortPairs: true, sortLeaves: true, sort: true, hashLeaves: true});
// For a short list, you can log it out, but not recommended for large lists
// console.log('Tree:\n', tree.toString());
fs.writeFileSync('./example/MerkleTree.txt', tree.toString());
console.log(`Merkle tree generated.
\nRoot hash is ${tree.getHexRoot()}
\nTree Summary:
\n Leaf Count: ${tree.getLeafCount()}
\n Layer Count: ${tree.getLayerCount()}
\n Tree Depth: ${tree.getDepth()}
\nSaving to MerkleTree.txt
`);
} catch (e) {
console.log(e);
}
}
const generateMerkleRoot = async () => {
/**
* Create text file called 'MerkleTreeRoot' that contains
* the Merkle tree root hash.
*/
try {
fs.writeFileSync('./example/MerkleTreeRoot.json', JSON.stringify(tree.getHexRoot()));
} catch (e) {
console.log(e);
}
}
const generateTreeSummary = async () => {
for (let i = 0; i < geneSymbols.length; i ++) {
let currentHash;
let currentValue;
currentValue = geneSymbols[i];
currentHash = `0x${keccak256(geneSymbols[i]).toString('hex')}`;
/*
* Create an object for each gene containing:
* gene symbol, keccak256 hash, leaf index
*/
leafValues.push({ "Leaf": currentValue, "Hash": currentHash})
}
fs.writeFileSync('./example/MerkleTreeSummary.json', JSON.stringify(leafValues));
}
generateGeneSymbolList()
.then(generateMerkleTree())
.then(generateMerkleRoot())
.then(generateTreeSummary())