# merkle-py
_Python implementation of Merkle tree_
![Github tag (latest by date)](https://img.shields.io/github/v/tag/cyrildever/merkle-trees)
![PyPI - Downloads](https://img.shields.io/pypi/dm/merkle-py)
![Github last commit](https://img.shields.io/github/last-commit/cyrildever/merkle-trees)
![Github issues](https://img.shields.io/github/issues/cyrildever/merkle-trees)
![PyPI - License](https://img.shields.io/pypi/l/merkle-py)
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](../go/README.md), [Scala](../scala/README.md) and [TypeScript](../ts/README.md).
### Usage
```console
$ pip install merkle-py
```
Here are some simple examples of how it works:
```python
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
```console
$ 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](LICENSE) file.
<hr />
© 2022-2024 Cyril Dever. All rights reserved.
Raw data
{
"_id": null,
"home_page": "https://github.com/cyrildever/merkle-trees/tree/master/packages/py",
"name": "merkle-py",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.6",
"maintainer_email": null,
"keywords": "python, merkle-tree, merkle",
"author": "Cyril Dever",
"author_email": null,
"download_url": "https://files.pythonhosted.org/packages/c5/ce/6def12ab6953d72b458a19de2bb07b1f8dc6065fa39da4647aa11808cbe3/merkle_py-1.2.9.tar.gz",
"platform": null,
"description": "# merkle-py\n_Python implementation of Merkle tree_\n\n![Github tag (latest by date)](https://img.shields.io/github/v/tag/cyrildever/merkle-trees)\n![PyPI - Downloads](https://img.shields.io/pypi/dm/merkle-py)\n![Github last commit](https://img.shields.io/github/last-commit/cyrildever/merkle-trees)\n![Github issues](https://img.shields.io/github/issues/cyrildever/merkle-trees)\n![PyPI - License](https://img.shields.io/pypi/l/merkle-py)\n\nThis library defines my special implementation in Python of the notorious Merkle trees. Feel free to use it (with the appropriate credits).\n\nOther implementations include: [Go](../go/README.md), [Scala](../scala/README.md) and [TypeScript](../ts/README.md).\n\n### Usage\n\n```console\n$ pip install merkle-py\n```\n\nHere are some simple examples of how it works:\n```python\nfrom merklepy import MerkleTree, MerkleTreeOptions, build_hash_function, SHA_256\n\noptions1 = MerkleTreeOptions(doubleHash= True, engine='sha-256', sort=True)\ntree1 = MerkleTree(options1)\n\n# Build a tree from the raw data\nproofs1 = tree1.add_leaves(True, '1', '2', '3')\nroot_hash = tree1.get_root_hash()\nassert(tree1.depth() == 1)\n\njson = tree1.to_json()\n\n# Build another identical tree from the JSON of the first one\ntree2 = tree_from(json)\nassert(tree1.size() == tree2.size())\nsha256 = build_hash_function(SHA_256)\nassert(tree2.size() == proofs1[0].size)\nassert(tree2.validate_proof(proofs1[0], sha256('1'), root_hash) == True)\n\n# Enrich with new hashed data\nproofs2 = tree2.add_leaves(False,\n bytes.fromhex('1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef'),\n bytes.fromhex('abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789')\n)\nassert(tree2.size() == 5)\nassert(tree2.depth() == 2)\n\n# Because the size of the tree has changed, and so has the root hash\nassert(proofs1[0].to_string() != proofs2[0].to_string() and tree2.validate_proof(proofs1[0], sha256('1'), root_hash) == False)\n```\n\n#### Important note\n\nAs 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. \\\nIn other words, this implementation is either not made for a growing tree, or should take this behaviour into account when issuing and verifying proofs.\n\n\n\n### Tests\n\n```console\n$ git clone https://github.com/cyrildever/merkle-trees.git\n$ cd merkle-trees/packages/py/\n$ pip install -e .\n$ python3 -m unittest discover\n```\n\n\n### License\n\nThis library is distributed under a MIT license. \\\nSee the [LICENSE](LICENSE) file.\n\n\n<hr />\n© 2022-2024 Cyril Dever. All rights reserved.\n",
"bugtrack_url": null,
"license": null,
"summary": "Merkle tree for Python",
"version": "1.2.9",
"project_urls": {
"Bug Tracker": "https://github.com/cyrildever/merkle-trees/issues",
"Homepage": "https://github.com/cyrildever/merkle-trees/tree/master/packages/py"
},
"split_keywords": [
"python",
" merkle-tree",
" merkle"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "4aa9a9c4731e9759e42ee71e72f514687052585dd3cb64ed3a13900aed5eae6b",
"md5": "151673d1a2029512f768e810a53abec8",
"sha256": "c78f0d8868ab1bdffd6dde6d26eb65197ebdd7ad95f6e17aa0622fd25db43066"
},
"downloads": -1,
"filename": "merkle_py-1.2.9-py3-none-any.whl",
"has_sig": false,
"md5_digest": "151673d1a2029512f768e810a53abec8",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.6",
"size": 8160,
"upload_time": "2024-12-05T09:09:57",
"upload_time_iso_8601": "2024-12-05T09:09:57.947777Z",
"url": "https://files.pythonhosted.org/packages/4a/a9/a9c4731e9759e42ee71e72f514687052585dd3cb64ed3a13900aed5eae6b/merkle_py-1.2.9-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "c5ce6def12ab6953d72b458a19de2bb07b1f8dc6065fa39da4647aa11808cbe3",
"md5": "d35182252882f7a474f20478b38a3bc5",
"sha256": "ae4e74eb9a0dbe3397f21c456d121e1d358bdd4b11cc945fbaf9d715ae87b237"
},
"downloads": -1,
"filename": "merkle_py-1.2.9.tar.gz",
"has_sig": false,
"md5_digest": "d35182252882f7a474f20478b38a3bc5",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.6",
"size": 9156,
"upload_time": "2024-12-05T09:09:59",
"upload_time_iso_8601": "2024-12-05T09:09:59.553578Z",
"url": "https://files.pythonhosted.org/packages/c5/ce/6def12ab6953d72b458a19de2bb07b1f8dc6065fa39da4647aa11808cbe3/merkle_py-1.2.9.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-12-05 09:09:59",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "cyrildever",
"github_project": "merkle-trees",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "merkle-py"
}