# 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/93/ca/586e53cef560f9a754528adeae7be83e8454227b8104960aa259f0bbec65/merkle_py-1.2.8.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.8",
"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": "6cea28ef0b558b0741f4af97768bb39fa86d39a896be08352225b20d8f18c32a",
"md5": "5e680afd3457c3bd79e6b55855c763a6",
"sha256": "e3d364c1f635e2d08ba94cf30920573b05559a646ddb104dd25cfeaf4ec8a222"
},
"downloads": -1,
"filename": "merkle_py-1.2.8-py3-none-any.whl",
"has_sig": false,
"md5_digest": "5e680afd3457c3bd79e6b55855c763a6",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.6",
"size": 8160,
"upload_time": "2024-09-11T17:08:27",
"upload_time_iso_8601": "2024-09-11T17:08:27.476425Z",
"url": "https://files.pythonhosted.org/packages/6c/ea/28ef0b558b0741f4af97768bb39fa86d39a896be08352225b20d8f18c32a/merkle_py-1.2.8-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "93ca586e53cef560f9a754528adeae7be83e8454227b8104960aa259f0bbec65",
"md5": "a411ea8407a7c4032ec13e227848838f",
"sha256": "5e9db57405f40a3b4589f14da997a60c4d10991946cbaced0df70e68f5d7e9f4"
},
"downloads": -1,
"filename": "merkle_py-1.2.8.tar.gz",
"has_sig": false,
"md5_digest": "a411ea8407a7c4032ec13e227848838f",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.6",
"size": 9141,
"upload_time": "2024-09-11T17:08:29",
"upload_time_iso_8601": "2024-09-11T17:08:29.409203Z",
"url": "https://files.pythonhosted.org/packages/93/ca/586e53cef560f9a754528adeae7be83e8454227b8104960aa259f0bbec65/merkle_py-1.2.8.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-09-11 17:08:29",
"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"
}