-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMerkleTree.ts
61 lines (48 loc) · 1.37 KB
/
MerkleTree.ts
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
import createHash from "./utils/createHash";
type Leaf = {
hash: string;
};
type Tree = Leaf[][];
class MerkleTree {
private tree: Tree;
constructor(data: string[]) {
// Checking for data
if (!Array.isArray(data) || data.some((value) => typeof value !== "string")) {
throw new Error("Invalid data");
}
// Initializing the 1st level of tree
this.tree = [data.map((value: string) => ({ hash: createHash(value) }))];
if (this.tree[0].length !== 1) {
this.generateTree();
}
}
generateTree() {
const currentTopLevel = this.tree[0];
let nextLevel: Leaf[] = [];
for (let i = 0; i < currentTopLevel.length; i += 2) {
// Current Leaf is the last on current level
if (!currentTopLevel[i + 1]) {
nextLevel = [...nextLevel, currentTopLevel[i]];
continue;
}
// Add a new Leaf with the neighbour hash
nextLevel = [...nextLevel, { hash: createHash(`${currentTopLevel[i].hash}${currentTopLevel[i + 1].hash}`) }];
}
// Append top-level in tree
this.tree = [nextLevel, ...this.tree];
// Continue if we have not calculated a root-node yet
if (this.tree[0].length > 1) {
this.generateTree();
}
}
root() {
return this.level(0);
}
height() {
return this.tree.length;
}
level(index: number) {
return this.tree[index];
}
}
export default MerkleTree;