Merkle tree for Python
Project description
merkle-py
Python implementation of Merkle tree
This library defines my special implementation in Python of the notorious Merkle trees. Feel free to use it (with the appropriate credits).
Other implementations include: Go, Scala and TypeScript.
Usage
$ pip install merkle-py
Here are some simple examples of how it works:
from merklepy.tree import MerkleTree, MerkleTreeOptions
from merklepy.hash import build_hash_function, SHA_256
options1 = MerkleTreeOptions(doubleHash= True, engine='sha-256', sort=True)
tree1 = MerkleTree(options1)
# Build a tree from the raw data
proofs1 = tree1.add_leaves(True, '1', '2', '3')
root_hash = tree1.get_root_hash()
assert(tree1.depth() == 1)
json = tree1.to_json()
# Build another identical tree from the JSON of the first one
tree2 = tree_from(json)
assert(tree1.size() == tree2.size())
sha256 = build_hash_function(SHA_256)
assert(tree2.size() == proofs1[0].size)
assert(tree2.validate_proof(proofs1[0], sha256('1'), root_hash) == True)
# Enrich with new hashed data
proofs2 = tree2.add_leaves(False,
bytes.fromhex('1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef'),
bytes.fromhex('abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789')
)
assert(tree2.size() == 5)
assert(tree2.depth() == 2)
# Because the size of the tree has changed, and so has the root hash
assert(proofs1[0].to_string() != proofs2[0].to_string() and tree2.validate_proof(proofs1[0], sha256('1'), root_hash) == False)
Important note
As you can see from the examples above, for a continuously growing Merkle tree, proofs may not work at all time. You may need either a new proof from the latest tree, or rebuild the old tree, hence the size
attribute passed within the MerkleProof
instance. If you don't use a sorted tree and keep a record of the leaves' hashes in the order they were included in the tree, this allows you to rebuild the corresponding tree and therefore use any proof at any time.
In other words, this implementation is either not made for a growing tree, or should take this behaviour into account when issuing and verifying proofs.
Tests
$ python3 -m unittest discover
License
This library is distributed under an MIT license. See the LICENSE file.
© 2022 Cyril Dever. All rights reserved.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for merkle_py-0.2.3-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ccc4ce1943f17f68e2d51437bd112b3c437f8ba28817752b3db6c03354cac472 |
|
MD5 | 3322b6092076e9f71cd75a929f5ca107 |
|
BLAKE2b-256 | 66772ca542b4239c3cef09eb2c85e55e80420eff9ecfcbf57f3069bf545902c1 |