-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMerkleTree.py
More file actions
75 lines (60 loc) · 2.41 KB
/
MerkleTree.py
File metadata and controls
75 lines (60 loc) · 2.41 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import hashlib
import binascii
class MerkleTree(object):
def __init__(self, hash_type):
hash_type = "SHA256"
hash_type = hash_type.lower()
self.hash_function = getattr(hashlib, hash_type)
self.reset_tree()
def reset_tree(self):
self.leaves = list()
self.levels = None
self.tree_ready = False
def _to_hex(self, x):
try: # python3
return x.hex()
except: # python2
return binascii.hexlify(x)
def add_leaf(self, values, do_hash=False):#do hash mean - value should be hashed before adding them as leaf nodes.
self.tree_ready = False
if not isinstance(values, tuple) and not isinstance(values, list): # checks if it is a single leaf
values = [values]
for v in values:
if do_hash:
v = v.encode('utf-8')
v = self.hash_function(v).hexdigest()
v = bytearray.fromhex(v) #transaction details should be visible in the block.
self.leaves.append(v) #adding the leaf to the leaves
def get_leaf(self, index):
return self._to_hex(self.leaves[index])
def get_leaf_count(self):
return len(self.leaves)
def get_tree_ready_state(self):
return self.tree_ready
def _calculate_next_level(self):
solo_leaf = None
N = len(self.levels[0]) # number of leaves on the level
if N % 2 == 1: # if odd number of leaves on the level
solo_leaf = self.levels[0][-1]
N -= 1
new_level = []
for l, r in zip(self.levels[0][0:N:2], self.levels[0][1:N:2]):#l and r child nodes.
new_level.append(self.hash_function(l+r).digest())
if solo_leaf is not None:
new_level.append(self.hash_function(solo_leaf+solo_leaf).digest()) #hashing the solo node by duplicating it.
self.levels = [new_level, ] + self.levels # prepend new level
def make_tree(self):
self.tree_ready = False
if self.get_leaf_count() > 0:
self.levels = [self.leaves, ]
while len(self.levels[0]) > 1:
self._calculate_next_level()
self.tree_ready = True
def generate_merkle_root(self):
if self.tree_ready:
if self.levels is not None:
return self._to_hex(self.levels[0][0])
else:
return None
else:
return None