Skip to content

Commit cd66860

Browse files
committed
Initial work's commit
0 parents  commit cd66860

File tree

1,771 files changed

+242387
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1,771 files changed

+242387
-0
lines changed

Bst.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
class Node{
2+
constructor(data, left = null, right = null){
3+
this.data = data;
4+
this.left = left;
5+
this.right = right;
6+
}
7+
}
8+
9+
class BST{
10+
constructor(){
11+
this.root = null;
12+
}
13+
insert(data){
14+
var newNode = new Node(data);
15+
16+
if(this.root == null){
17+
this.root = newNode;
18+
}else{
19+
this.insertNode(this.root, newNode);
20+
}
21+
}
22+
insertNode(node, newNode){
23+
if(newNode.data < node.data){
24+
if(node.left == null){
25+
node.left = newNode;
26+
}else{
27+
this.insertNode(node.left, newNode);
28+
}
29+
}else{
30+
if(node.right == null){
31+
node.right = newNode;
32+
}else{
33+
this.insertNode(node.right, newNode);
34+
}
35+
}
36+
}
37+
inorder(node){
38+
if(node!=null){
39+
this.inorder(node.left);
40+
console.log(node.data);
41+
this.inorder(node.right);
42+
}
43+
}
44+
getRootNode(){
45+
return this.root;
46+
}
47+
}
48+
49+
const BSTInstance = new BST();
50+
BSTInstance.insert(4);
51+
BSTInstance.insert(2);
52+
BSTInstance.insert(1);
53+
BSTInstance.insert(6);
54+
BSTInstance.insert(7);
55+
BSTInstance.insert(5);
56+
BSTInstance.insert(3);
57+
58+
BSTInstance.inorder(BSTInstance.getRootNode());

MerkleTree.js

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
var sha256 = require('js-sha256');
2+
3+
class MerkleHash{
4+
ComputeHash(value){
5+
var SHA256 = sha256(value);
6+
var hashValue = sha256(SHA256);
7+
return hashValue;
8+
}
9+
CompareHash(hash1, hash2){
10+
return hash1 === hash2;
11+
}
12+
CombinedHash(hash1, hash2){
13+
var finalPreHash = hash1 + hash2;
14+
var finalHash = this.ComputeHash(finalPreHash);
15+
return finalHash;
16+
}
17+
}
18+
19+
class MerkleNode{
20+
constructor(data, left = null, right = null, parent = null, direction = null){
21+
this.data = data;
22+
this.left = left;
23+
this.right = right;
24+
this.parent = parent;
25+
this.direction = direction;
26+
}
27+
CreateCombinedNode(leftNode, rightNode){
28+
var hash1 = leftNode.data;
29+
var hash2 = rightNode.data;
30+
// console.log(sha256(hash1 + hash2));
31+
var newNode = new MerkleNode(sha256(hash1 + hash2));
32+
return newNode;
33+
}
34+
}
35+
36+
class MerkleTree{
37+
constructor(){
38+
this.nodes = [];
39+
this.leaves = [];
40+
this.rootNode = '';
41+
}
42+
AppendNode(merkleNode){
43+
this.nodes.push(merkleNode);
44+
this.leaves.push(merkleNode);
45+
}
46+
AppendHash(merkleHash){
47+
var node = new MerkleNode(merkleHash);
48+
this.nodes.push(node);
49+
this.leaves.push(node);
50+
}
51+
ConstructTree(){
52+
if(this.leaves.length != 0){
53+
this.BuildTree(this.leaves);
54+
}
55+
}
56+
BuildTree(nodes){
57+
if(nodes.length == 1){
58+
this.rootNode = nodes[0];
59+
return;
60+
}else{
61+
var parents = [];
62+
63+
for(var i=0; i<nodes.length; i=i+2){
64+
var right = null;
65+
if(i + 1 < nodes.length){
66+
right = nodes[i + 1];
67+
}else{
68+
right = nodes[i];
69+
right.direction = 'left';
70+
}
71+
var parent = new MerkleNode(0);
72+
var parentNode = parent.CreateCombinedNode(nodes[i], right);
73+
parents.push(parentNode);
74+
nodes[i].parent = parentNode;
75+
nodes[i].direction = 'left';
76+
parentNode.left = nodes[i];
77+
if(right!=null){
78+
right.parent = parentNode;
79+
right.direction = 'right';
80+
parentNode.right = right;
81+
}
82+
}
83+
this.BuildTree(parents);
84+
}
85+
}
86+
FindLeaf(leafHash){
87+
for(var i=0; i<this.leaves.length; i++){
88+
if(this.leaves[i].data == leafHash){
89+
return this.leaves[i];
90+
}
91+
}
92+
return null;
93+
}
94+
AuditProof(leafHash){
95+
var auditTrail = [];
96+
var leafNode = this.FindLeaf(leafHash);
97+
if(leafNode != null){
98+
var parent = leafNode.parent;
99+
this.BuildAuditTrail(auditTrail, parent, leafNode);
100+
}
101+
return auditTrail;
102+
}
103+
BuildAuditTrail(auditTrail, parentNode, childNode){
104+
if(parentNode != null){
105+
var nextChild = parentNode.left == childNode ? parentNode.right : parentNode.left;
106+
107+
if(nextChild != null){
108+
auditTrail.push(nextChild);
109+
}
110+
if(childNode.parent!=null)
111+
this.BuildAuditTrail(auditTrail, childNode.parent.parent, childNode.parent);
112+
else{
113+
return;
114+
}
115+
}
116+
}
117+
TreeTraversal(){
118+
for(var i=0; i<this.nodes.length; i++){
119+
console.log(this.nodes[i]);
120+
}
121+
}
122+
}
123+
124+
var tree = new MerkleTree();
125+
var txn1 = 't1';
126+
var txn2 = 't2';
127+
var txn3 = 't3';
128+
var txn4 = 't4';
129+
var merkleHash = new MerkleHash();
130+
hash1 = merkleHash.ComputeHash(txn1);
131+
hash2 = merkleHash.ComputeHash(txn2);
132+
hash3 = merkleHash.ComputeHash(txn3);
133+
hash4 = merkleHash.ComputeHash(txn4);
134+
tree.AppendHash(hash1);
135+
tree.AppendHash(hash2);
136+
tree.AppendHash(hash3);
137+
tree.AppendHash(hash4);
138+
tree.ConstructTree();
139+
// console.log(tree.rootNode);
140+
tree.TreeTraversal();
141+
console.log("====================================================================================");
142+
console.log("Merkle Proof of Transaction: " + txn3);
143+
var trail = tree.AuditProof(hash3);
144+
var currentHash = hash3;
145+
for(var i=0; i<trail.length; i++){
146+
// console.log(trail[i].data + " " + trail[i].direction);
147+
if(trail[i].direction == 'left'){
148+
currentHash = sha256(trail[i].data + currentHash);
149+
}else{
150+
currentHash = sha256(currentHash + trail[i].data);
151+
}
152+
}
153+
if(tree.rootNode.data == currentHash){
154+
console.log("Transaction Valid...");
155+
console.log("Generated Hash: " + currentHash);
156+
console.log("Merkle Root: " + tree.rootNode.data);
157+
}else{
158+
console.log(currentHash);
159+
}

node_modules/.bin/atob

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node_modules/.bin/is-ci

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node_modules/.bin/mime

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node_modules/.bin/nodemon

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node_modules/.bin/nodetouch

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node_modules/.bin/nopt

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node_modules/.bin/rc

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node_modules/.bin/semver

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)