Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added function to generate merkle root from proof and leaf #23

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions merkletools/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,13 @@ def get_proof(self, index):

def validate_proof(self, proof, target_hash, merkle_root):
merkle_root = bytearray.fromhex(merkle_root)
proof_hash = self.generate_merkle_root_with_merkle_proof(proof, target_hash)
return proof_hash == merkle_root

def generate_merkle_root_with_merkle_proof(self, proof, target_hash):
target_hash = bytearray.fromhex(target_hash)
if len(proof) == 0:
return target_hash == merkle_root
return target_hash
else:
proof_hash = target_hash
for p in proof:
Expand All @@ -121,4 +125,4 @@ def validate_proof(self, proof, target_hash, merkle_root):
# the sibling is a right node
sibling = bytearray.fromhex(p['right'])
proof_hash = self.hash_function(proof_hash + sibling).digest()
return proof_hash == merkle_root
return proof_hash
13 changes: 0 additions & 13 deletions tests/test_merkle_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,19 +233,6 @@ def test_sha3_224():
assert is_valid == True


def test_sha3_224():
mt = MerkleTools(hash_type='sha3_224')
mt.add_leaf([
'6ed712b9472b671fd70bb950dc4ccfce197c92a7969f6bc2aa6b6d9f',
'08db5633d406804d044a3e67683e179b5ee51249ed2139c239d1e65a'
])
mt.make_tree()
assert mt.get_merkle_root() == '674bc9f53d5c666174cdd3ccb9df04768dfb7759655e7d937aef0c3a'
assert mt.get_proof(0)[0]['right'] == '08db5633d406804d044a3e67683e179b5ee51249ed2139c239d1e65a'
is_valid = mt.validate_proof(mt.get_proof(0), '6ed712b9472b671fd70bb950dc4ccfce197c92a7969f6bc2aa6b6d9f', '674bc9f53d5c666174cdd3ccb9df04768dfb7759655e7d937aef0c3a')
assert is_valid == True


def test_sha3_256():
mt = MerkleTools(hash_type='sha3_256')
mt.add_leaf([
Expand Down