{"id":37068080,"url":"https://github.com/f8n/solidity-pymerkletools","last_synced_at":"2026-01-14T08:00:03.701Z","repository":{"id":63992484,"uuid":"572356192","full_name":"f8n/solidity-pymerkletools","owner":"f8n","description":"Python tools for creating Merkle trees, generating Merkle proofs, and verification of Merkle proofs. This fork specifically supports hashing the same way as solidity.","archived":false,"fork":true,"pushed_at":"2023-08-21T16:32:27.000Z","size":49,"stargazers_count":0,"open_issues_count":1,"forks_count":4,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-11-27T13:48:57.790Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":false,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":"Tierion/pymerkletools","license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/f8n.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-11-30T04:56:28.000Z","updated_at":"2022-11-30T05:10:40.000Z","dependencies_parsed_at":"2023-01-22T22:45:54.455Z","dependency_job_id":null,"html_url":"https://github.com/f8n/solidity-pymerkletools","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/f8n/solidity-pymerkletools","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/f8n%2Fsolidity-pymerkletools","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/f8n%2Fsolidity-pymerkletools/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/f8n%2Fsolidity-pymerkletools/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/f8n%2Fsolidity-pymerkletools/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/f8n","download_url":"https://codeload.github.com/f8n/solidity-pymerkletools/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/f8n%2Fsolidity-pymerkletools/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28413527,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-14T05:26:33.345Z","status":"ssl_error","status_checked_at":"2026-01-14T05:21:57.251Z","response_time":107,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2026-01-14T08:00:03.023Z","updated_at":"2026-01-14T08:00:03.693Z","avatar_url":"https://github.com/f8n.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# pymerkletools\n[![PyPI version](https://badge.fury.io/py/solidity-merkletools.svg)](https://badge.fury.io/py/solidity-merkletools) [![Build Status](https://travis-ci.org/f8n/solidity-merkletools.svg?branch=main)](https://travis-ci.org/f8n/solidity-merkletools)\n\nThis is a Python port of [merkle-tools](https://github.com/tierion/merkle-tools). Then it was forked to support using Web3's solidity keccak hashing function.\n\nTools for creating Merkle trees, generating merkle proofs, and verification of merkle proofs.\n\n## Installation\n\n```\npip install solidity-merkletools\n```\n\n### Create MerkleTools Object\n\n```python\nimport solidity_merkletools\n\nmt = MerkleTools()  # default hash algorithm is Web3.solidityKeccak\n```\n\n## Methods\n\n### add_leaf(value, do_hash)\n\nAdds a value as a leaf or a list of leafs to the tree. The value must be a hex string.\n\n```python\nmt.add_leaf(\"0x4b39F7b0624b9dB86AD293686bc38B903142dbBc\")\nmt.add_leaf(\"0x71b4a2d9B91726bdb5849D928967A1654D7F3de7\")\n```\n\n### get_leaf_count()\n\nReturns the number of leaves that are currently added to the tree. \n\n```python\nleaf_count =  mt.get_leaf_count();\n```\n\n### get_leaf(index)\n\nReturns the value of the leaf at the given index as a hex string.\n\n```python\nleaf_value =  mt.get_leaf(1)\n```\n\n### reset_tree()\n\nRemoves all the leaves from the tree, prepararing to to begin creating a new tree.\n\n```python\nmt.reset_tree()\n```\n\n### make_tree()\n\nGenerates the merkle tree using the leaves that have been added.\n\n```python\nmt.make_tree();\n```\n\n### is_ready \n\n`.is_ready` is a boolean property indicating if the tree is built and ready to supply its root and proofs. The `is_ready` state is `True` only after calling 'make_tree()'.  Adding leaves or resetting the tree will change the ready state to False.\n\n```python\nis_ready = mt.is_ready \n```\n\n### get_merkle_root()\n\nReturns the merkle root of the tree as a hex string. If the tree is not ready, `None` is returned.\n\n```python\nroot_value = mt.get_merkle_root();\n```\n\n### get_proof(index)\n\nReturns the proof as an array of hash objects for the leaf at the given index. If the tree is not ready or no leaf exists at the given index, null is returned.  \n\n```python\nproof = mt.get_proof(1)\n```\n\nThe proof array contains a set of merkle sibling objects. Each object contains the sibling hash, with the key value of either right or left. The right or left value tells you where that sibling was in relation to the current hash being evaluated. This information is needed for proof validation, as explained in the following section.\n\n### validate_proof(proof, target_hash, merkle_root)\n\nReturns a boolean indicating whether or not the proof is valid and correctly connects the `target_hash` to the `merkle_root`. `proof` is a proof array as supplied by the `get_proof` method. The `target_hash` and `merkle_root` parameters must be a hex strings.\n\n```python\nproof = [\n   { right: '09096dbc49b7909917e13b795ebf289ace50b870440f10424af8845fb7761ea5' },\n   { right: 'ed2456914e48c1e17b7bd922177291ef8b7f553edf1b1f66b6fc1a076524b22f' },\n   { left: 'eac53dde9661daf47a428efea28c81a021c06d64f98eeabbdcff442d992153a8' },\n]\ntarget_hash = '36e0fd847d927d68475f32a94efff30812ee3ce87c7752973f4dd7476aa2e97e'\nmerkle_root = 'b8b1f39aa2e3fc2dde37f3df04e829f514fb98369b522bfb35c663befa896766'\n\nis_valid = mt.validate_proof(proof, targetHash, merkleRoot)\n```\n\nThe proof process uses all the proof objects in the array to attempt to prove a relationship between the `target_hash` and the `merkle_root` values. The steps to validate a proof are:\n\n1. Concatenate `target_hash` and the first hash in the proof array. The right or left designation specifies which side of the concatenation that the proof hash value should be on.\n2. Hash the resulting value.\n3. Concatenate the resulting hash with the next hash in the proof array, using the same left and right rules.\n4. Hash that value and continue the process until you’ve gone through each item in the proof array.\n5. The final hash value should equal the `merkle_root` value if the proof is valid, otherwise the proof is invalid.\n\n## Common Usage\n\n### Creating a tree and generating the proofs\n\n```python\nmt = MerkleTools()\n\nmt.add_leaf(\"tierion\", True)\nmt.add_leaf([\"bitcoin\", \"blockchain\"], True)\n\nmt.make_tree()\n\nprint \"root:\", mt.get_merkle_root()  # root: '765f15d171871b00034ee55e48ffdf76afbc44ed0bcff5c82f31351d333c2ed1'\n\nprint mt.get_proof(1)  # [{left: '2da7240f6c88536be72abe9f04e454c6478ee29709fc3729ddfb942f804fbf08'},\n                       #  {right: 'ef7797e13d3a75526946a3bcf00daec9fc9c9c4d51ddc7cc5df888f74dd434d1'}] \n\nprint mt.validate_proof(mt.get_proof(1), mt.get_leaf(1), mt.get_merkle_root())  # True\n```\n\n## Notes\n\n### About tree generation\n\n1. Internally, leaves are stored as `bytearray`. When the tree is built, it is generated by hashing together the `bytearray` values. \n2. Lonely leaf nodes are promoted to the next level up, as depicted below.\n\n                         ROOT=Hash(H+E)\n                         /        \\\n                        /          \\\n                 H=Hash(F+G)        E\n                 /       \\           \\\n                /         \\           \\\n         F=Hash(A+B)    G=Hash(C+D)    E\n          /     \\        /     \\        \\\n         /       \\      /       \\        \\\n\n        A         B    C         D        E","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ff8n%2Fsolidity-pymerkletools","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ff8n%2Fsolidity-pymerkletools","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ff8n%2Fsolidity-pymerkletools/lists"}