Skip to content

Instantly share code, notes, and snippets.

@gaxiiiiiiiiiiii
Created October 31, 2018 08:41
Show Gist options
  • Save gaxiiiiiiiiiiii/b06b47f606c40614a213d7fe7d6394d3 to your computer and use it in GitHub Desktop.
Save gaxiiiiiiiiiiii/b06b47f606c40614a213d7fe7d6394d3 to your computer and use it in GitHub Desktop.
class Tree:
def __init__(self, leaves):
self.leaves = [Node(leaf) for leaf in leaves]
self.layer = self.leaves[::]
self.root = None
self.build_tree()
def build_layer(self):
new_layer = []
if len(self.layer) % 2 == 1:
self.layer.append(self.layer[-1])
for i in range(0, len(self.layer), 2):
left = self.layer[i]
right = self.layer[i+1]
parent = Node(left.hash + right.hash)
left.parent = parent
left.sibling = right
left.position = "left"
right.parent = parent
right.sibling = left
right.position = "right"
parent.left = left
parent.right = right
new_layer.append(parent)
self.layer = new_layer
def build_tree(self):
while len(self.layer) > 1:
self.build_layer()
self.root = self.layer[0].hash
def search(self, data):
target = None
hash_value = sha256(data.encode()).hexdigest()
for node in self.leaves:
if node.hash == hash_value:
target = node
return target
def get_pass(self, data):
target = self.search(data)
markle_pass = []
if not(target):
return
markle_pass.append(target.hash)
while target.parent:
sibling = target.sibling
markle_pass.append((sibling.hash, sibling.position))
target = target.parent
return markle_pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment