Skip to main content

Merkle tree for Python

Project description

merkle-py

Python implementation of Merkle tree

Github tag (latest by date) PyPI - Downloads Github last commit Github issues PyPI - License

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-2025 Cyril Dever. All rights reserved.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

merkle_py-1.2.10.tar.gz (9.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

merkle_py-1.2.10-py3-none-any.whl (8.2 kB view details)

Uploaded Python 3

File details

Details for the file merkle_py-1.2.10.tar.gz.

File metadata

  • Download URL: merkle_py-1.2.10.tar.gz
  • Upload date:
  • Size: 9.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.31.0 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.3 CPython/3.10.2

File hashes

Hashes for merkle_py-1.2.10.tar.gz
Algorithm Hash digest
SHA256 ec6e77cea3158b57f34b674c091c299a9796a4239cccb359a7cde792f84f31d5
MD5 a7eb4ca2d41b4ae25820f324f47f7be2
BLAKE2b-256 96efab35de2e24342126aaf7f00686ff0197b40b8104e8c2952da961029e3bb4

See more details on using hashes here.

File details

Details for the file merkle_py-1.2.10-py3-none-any.whl.

File metadata

  • Download URL: merkle_py-1.2.10-py3-none-any.whl
  • Upload date:
  • Size: 8.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.31.0 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.3 CPython/3.10.2

File hashes

Hashes for merkle_py-1.2.10-py3-none-any.whl
Algorithm Hash digest
SHA256 8093d7167f025e09fad43d746d4ac871f152efd1c2a00f8ef81de894ccab2763
MD5 827b7a98824fee5d3c157df139535b4e
BLAKE2b-256 0ca91f02a7a013c11162b3f3c05b6ba010b9ba38dfb828135ebe219c0f7fda7f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page