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 import MerkleTree, MerkleTreeOptions, 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
$ git clone https://github.com/cyrildever/merkle-trees.git
$ cd merkle-trees/packages/py/
$ pip install -e .
$ python3 -m unittest discover
License
This library is distributed under a MIT license.
See the LICENSE file.
© 2022-2026 Cyril Dever. All rights reserved.
Metadata
Release files for merkle-py 1.2.11
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| merkle_py-1.2.11.tar.gz | 9.2 kB | Without provenance | Details for merkle_py-1.2.11.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| merkle_py-1.2.11-py3-none-any.whl | Python 3 | none | any | Details for merkle_py-1.2.11-py3-none-any.whl |
Total release size: 17.4 kB
Release files / merkle_py-1.2.11.tar.gz
| Download URL | merkle_py-1.2.11.tar.gz |
|---|---|
| Size | 9.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
83ecea6ca0684035a0cf75bd9675d937517baf918ac9ef0b57d07303387ab08f
|
|
BLAKE2b-256 checksum How to use checksums |
fe47740a8ccc5f4016eb4218c802e2f4c64b45525e432478c9b24fa9b964e62a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.10.2
|
Release files / merkle_py-1.2.11-py3-none-any.whl
| Download URL | merkle_py-1.2.11-py3-none-any.whl |
|---|---|
| Size | 8.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
1c5f5ce01aa7f39fdb285923a3dd5e7cdddefafac2b22646876e3d64bc631407
|
|
BLAKE2b-256 checksum How to use checksums |
e44af89a2b6f00acfc6d66124b5de9c9900bb759c4767799b48b4f35f1dbe731
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.10.2
|
Security for merkle-py 1.2.11
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.