merkle-py


Namemerkle-py JSON
Version 1.2.7 PyPI version JSON
download
home_pagehttps://github.com/cyrildever/merkle-trees/tree/master/packages/py
SummaryMerkle tree for Python
upload_time2024-02-08 10:46:23
maintainer
docs_urlNone
authorCyril Dever
requires_python>=3.6
license
keywords python merkle-tree merkle
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # 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 />
&copy; 2022-2023 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": "",
    "docs_url": null,
    "requires_python": ">=3.6",
    "maintainer_email": "",
    "keywords": "python,merkle-tree,merkle",
    "author": "Cyril Dever",
    "author_email": "",
    "download_url": "https://files.pythonhosted.org/packages/3b/86/269f03a03641b843b891a5a97e0d9f7a666a32c0b6d36a40420599fd9c51/merkle-py-1.2.7.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&copy; 2022-2023 Cyril Dever. All rights reserved.\n",
    "bugtrack_url": null,
    "license": "",
    "summary": "Merkle tree for Python",
    "version": "1.2.7",
    "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": "7ffd3417e9e317afb899cdd528d13f454678d86b5db9b1cb80ceeb5f02294245",
                "md5": "4d0e1ba1f9dff9a22e8069a0d3acd3ac",
                "sha256": "44d8947cc40202391982b48f8d3666ed9859895548ab3b798074f704da239fa1"
            },
            "downloads": -1,
            "filename": "merkle_py-1.2.7-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "4d0e1ba1f9dff9a22e8069a0d3acd3ac",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.6",
            "size": 8160,
            "upload_time": "2024-02-08T10:46:21",
            "upload_time_iso_8601": "2024-02-08T10:46:21.621337Z",
            "url": "https://files.pythonhosted.org/packages/7f/fd/3417e9e317afb899cdd528d13f454678d86b5db9b1cb80ceeb5f02294245/merkle_py-1.2.7-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "3b86269f03a03641b843b891a5a97e0d9f7a666a32c0b6d36a40420599fd9c51",
                "md5": "5bb9c7d11f3ea1ae85e1bb8756e159aa",
                "sha256": "c76540416d51ce6959c70577badde9601e1d7ffb91a486aa140803b352930697"
            },
            "downloads": -1,
            "filename": "merkle-py-1.2.7.tar.gz",
            "has_sig": false,
            "md5_digest": "5bb9c7d11f3ea1ae85e1bb8756e159aa",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.6",
            "size": 9143,
            "upload_time": "2024-02-08T10:46:23",
            "upload_time_iso_8601": "2024-02-08T10:46:23.170664Z",
            "url": "https://files.pythonhosted.org/packages/3b/86/269f03a03641b843b891a5a97e0d9f7a666a32c0b6d36a40420599fd9c51/merkle-py-1.2.7.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-02-08 10:46:23",
    "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"
}
        
Elapsed time: 0.42035s