Skip to content

Instantly share code, notes, and snippets.

@brandomr
Created December 6, 2016 02:01
Show Gist options
  • Save brandomr/f5f325a0e9161d481714efe00d846880 to your computer and use it in GitHub Desktop.
Save brandomr/f5f325a0e9161d481714efe00d846880 to your computer and use it in GitHub Desktop.
implement merkle tree in python
from hashlib import sha256 as sha
def chunks(l, n):
"""Yield successive n-sized chunks from l."""
for i in range(0, len(l), n):
yield l[i:i + n]
def m_tree(transactions):
"""Takes an array of transactions and computes a Merkle root"""
sub_t = []
for i in chunks(transactions,2):
if len(i) == 2:
hash = sha(str(i[0]+i[1])).hexdigest()
else:
hash = sha(str(i[0]+i[0])).hexdigest()
sub_t.append(hash)
print sub_t
if len(sub_t) == 1:
return sub_t[0]
else:
return m_tree(sub_t)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment