-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMerkleHashClaim.js
More file actions
53 lines (50 loc) · 1.24 KB
/
MerkleHashClaim.js
File metadata and controls
53 lines (50 loc) · 1.24 KB
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
//Includes
const Crypto = require('crypto-js');
//Get the JSON data
let JSONClaim = process.argv[2];
JSONClaim = decodeURIComponent(JSONClaim);
JSONClaim = JSON.parse(JSONClaim);
//Retrieve the data
let Depth = [];
let Hashes = [];
let CurrentDepth = 0;
for(let i in JSONClaim) {
let Keys = Object.keys(JSONClaim[i]);
if(Keys[0] != "Hash") {
let DataPoint = Keys[0] + ":" + JSONClaim[i][Keys[0]];
Hashes.push(Crypto.SHA256(DataPoint).toString());
} else {
Hashes.push(JSONClaim[i]["Hash"]);
}
let ThisDepth = JSONClaim[i]["Depth"];
Depth.push(ThisDepth);
if(ThisDepth > CurrentDepth) {
CurrentDepth = ThisDepth;
}
}
let MaxDepth = CurrentDepth;
for(let i=0; i < MaxDepth-1; i++) {
let NewHashes = [];
let NewDepth = [];
let FirstIndex = -1;
for(let k in Hashes) {
if(Depth[k] == CurrentDepth) {
if(FirstIndex == -1) {
FirstIndex = k;
} else {
NewHashes.push(Crypto.SHA256(Hashes[FirstIndex]+Hashes[k]).toString());
NewDepth.push(CurrentDepth-1);
FirstIndex = -1;
}
} else {
NewHashes.push(Hashes[k]);
NewDepth.push(Depth[k]);
}
}
//Update state
Hashes = NewHashes;
Depth = NewDepth;
CurrentDepth--;
}
console.log(Hashes);
return Hashes[0];