### Install blockcerts-merkletools Source: https://github.com/blockchain-certificates/blockcerts-pymerkletools/blob/main/README.md Install the library using pip. This command fetches and installs the latest version from PyPI. ```bash pip install blockcerts-merkletools ``` -------------------------------- ### Common Usage: Creating a tree and generating proofs Source: https://github.com/blockchain-certificates/blockcerts-pymerkletools/blob/main/README.md An example demonstrating the common workflow of creating a Merkle tree, adding leaves, generating the root, and obtaining a proof for a specific leaf. ```APIDOC ## Common Usage ### Creating a tree and generating the proofs ```python mt = MerkleTools() mt.add_leaf("tierion", True) mt.add_leaf(["bitcoin", "blockchain"], True) mt.make_tree() print "root:", mt.get_merkle_root() # root: '765f15d171871b00034ee55e48ffdf76afbc44ed0bcff5c82f31351d333c2ed1' print mt.get_proof(1) # [{left: '2da7240f6c88536be72abe9f04e454c6478ee29709fc3729ddfb942f804fbf08'}, # {right: 'ef7797e13d3a75526946a3bcf00daec9fc9c9c4d51ddc7cc5df888f74dd434d1'}] print mt.validate_proof(mt.get_proof(1), mt.get_leaf(1), mt.get_merkle_root()) # True ``` ``` -------------------------------- ### Get Tree String Representation with `__str__()` Source: https://context7.com/blockchain-certificates/blockcerts-pymerkletools/llms.txt Returns a human-readable multi-line string displaying the Merkle root followed by each level of the tree with all node hashes. Returns an empty string if the tree is not yet built. ```python from blockcerts_merkletools import MerkleTools mt = MerkleTools() mt.add_leaf(['a', 'b', 'c'], do_hash=True) mt.make_tree() print(str(mt)) # # Level: 0 # Level: 1 # Level: 2 # Not ready — empty string mt2 = MerkleTools() mt2.add_leaf("x", do_hash=True) print(repr(str(mt2))) # '' ``` -------------------------------- ### Get Merkle Proof Source: https://github.com/blockchain-certificates/blockcerts-pymerkletools/blob/main/README.md Generates the proof for a leaf at a given index. The proof is an array of hash objects, each indicating its position (left/right) relative to the current hash. ```python proof = mt.get_proof(1) ``` -------------------------------- ### Get Merkle Root Source: https://github.com/blockchain-certificates/blockcerts-pymerkletools/blob/main/README.md Retrieves the Merkle root of the constructed tree as a hex string. Returns None if the tree is not ready. ```python root_value = mt.get_merkle_root(); ``` -------------------------------- ### Get Leaf Count Source: https://github.com/blockchain-certificates/blockcerts-pymerkletools/blob/main/README.md Retrieves the total number of leaves currently added to the Merkle tree. ```python leaf_count = mt.get_leaf_count(); ``` -------------------------------- ### Get Leaf Value by Index Source: https://github.com/blockchain-certificates/blockcerts-pymerkletools/blob/main/README.md Retrieves the hex string value of a leaf at a specific index in the Merkle tree. ```python leaf_value = mt.get_leaf(1) ``` -------------------------------- ### Retrieve a Specific Leaf from Merkle Tree Source: https://context7.com/blockchain-certificates/blockcerts-pymerkletools/llms.txt Get a leaf from the Merkle tree by its zero-based index. The leaf is returned as a lowercase hex string. ```python from blockcerts_merkletools import MerkleTools mt = MerkleTools() mt.add_leaf("tierion", do_hash=True) mt.add_leaf("bitcoin", do_hash=True) print(mt.get_leaf(0)) # 2da7240f6c88536be72abe9f04e454c6478ee29709fc3729ddfb942f804fbf08 print(mt.get_leaf(1)) # b84c79e1047d90c1e3f78e69e7b4fae8e7e6c46b62e0d29a7ee7e5d793e5f5b5 ``` -------------------------------- ### Common Usage: Create Tree, Generate Proofs, and Validate Source: https://github.com/blockchain-certificates/blockcerts-pymerkletools/blob/main/README.md Demonstrates a typical workflow: creating a Merkle tree with various leaf types, generating the root and a proof, and then validating that proof against the leaf and root. ```python mt = MerkleTools() mt.add_leaf("tierion", True) mt.add_leaf(["bitcoin", "blockchain"], True) mt.make_tree() print "root:", mt.get_merkle_root() # root: '765f15d171871b00034ee55e48ffdf76afbc44ed0bcff5c82f31351d333c2ed1' print mt.get_proof(1) # [{left: '2da7240f6c88536be72abe9f04e454c6478ee29709fc3729ddfb942f804fbf08'}, # {right: 'ef7797e13d3a75526946a3bcf00daec9fc9c9c4d51ddc7cc5df888f74dd434d1'}] print mt.validate_proof(mt.get_proof(1), mt.get_leaf(1), mt.get_merkle_root()) # True ``` -------------------------------- ### make_tree() Source: https://context7.com/blockchain-certificates/blockcerts-pymerkletools/llms.txt Constructs the Merkle tree using all the leaves that have been added. Once this method is called, the `is_ready` attribute is set to `True`, and the Merkle root and proofs become accessible. If the tree contains no leaves, `is_ready` will still be `True`, but `get_merkle_root()` will return `None`. The tree construction handles odd numbers of leaves by promoting the last leaf to the next level instead of duplicating it. ```APIDOC ## make_tree() — Build the Tree Constructs the Merkle tree from all currently added leaves. After this call, `is_ready` becomes `True` and both the root and proofs become available. If there are no leaves, `is_ready` is still set to `True` but `get_merkle_root()` returns `None`. Odd-numbered levels promote the lone trailing leaf rather than duplicating it. ```python from blockcerts_merkletools import MerkleTools mt = MerkleTools() mt.add_leaf(['a', 'b', 'c', 'd', 'e'], do_hash=True) print(mt.is_ready) # False mt.make_tree() print(mt.is_ready) # True print(mt.get_merkle_root()) # d71f8983ad4ee170f8129f1ebcdd7440be7798d8e1c80420bf11f1eced610dba # Empty tree mt2 = MerkleTools() mt2.make_tree() print(mt2.is_ready) # True print(mt2.get_merkle_root()) # None ``` ``` -------------------------------- ### Programmatic Readiness Check with `get_tree_ready_state()` Source: https://context7.com/blockchain-certificates/blockcerts-pymerkletools/llms.txt Returns the same boolean value as the `is_ready` property via a method call. Useful when passing a callable reference is preferred. ```python from blockcerts_merkletools import MerkleTools mt = MerkleTools() print(mt.get_tree_ready_state()) # False mt.add_leaf("hello", do_hash=True) mt.make_tree() print(mt.get_tree_ready_state()) # True ``` -------------------------------- ### MerkleTools Object Creation Source: https://github.com/blockchain-certificates/blockcerts-pymerkletools/blob/main/README.md Instantiate the MerkleTools class to begin creating a Merkle tree. The default hash algorithm is Web3.solidity_keccak. ```APIDOC ## Create MerkleTools Object ```python import blockcerts_merkletools mt = MerkleTools() # default hash algorithm is Web3.solidity_keccak ``` ``` -------------------------------- ### Create MerkleTools Object Source: https://github.com/blockchain-certificates/blockcerts-pymerkletools/blob/main/README.md Instantiate the MerkleTools class. By default, it uses Web3's solidity_keccak hashing function. ```python import blockcerts_merkletools mt = MerkleTools() # default hash algorithm is Web3.solidity_keccak ``` -------------------------------- ### is_ready Source: https://github.com/blockchain-certificates/blockcerts-pymerkletools/blob/main/README.md A boolean property that indicates whether the Merkle tree has been built and is ready to provide its root and proofs. The state becomes `True` only after `make_tree()` is called. ```APIDOC ## is_ready `.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. ```python is_ready = mt.is_ready ``` ``` -------------------------------- ### Instantiate MerkleTools with Different Hash Types Source: https://context7.com/blockchain-certificates/blockcerts-pymerkletools/llms.txt Instantiate a MerkleTools object specifying the desired hashing algorithm. Supported types include 'sha256', 'md5', and 'sha3_256'. Unsupported types will raise an exception. ```python from blockcerts_merkletools import MerkleTools # Default SHA-256 tree mt = MerkleTools() # SHA3-256 tree mt_sha3 = MerkleTools(hash_type="sha3_256") # MD5 tree (legacy/testing) mt_md5 = MerkleTools(hash_type="md5") # Unsupported type raises an exception try: bad = MerkleTools(hash_type="blake2b") except Exception as e: print(e) # `hash_type` blake2b nor supported ``` -------------------------------- ### Build Merkle Tree from Added Leaves Source: https://context7.com/blockchain-certificates/blockcerts-pymerkletools/llms.txt Construct the Merkle tree using all leaves added via `add_leaf()`. After this operation, `is_ready` becomes `True`, and the Merkle root is available. For an empty tree, `is_ready` is `True` but the root is `None`. Odd-numbered leaf counts are handled by promoting the last leaf. ```python from blockcerts_merkletools import MerkleTools mt = MerkleTools() mt.add_leaf(['a', 'b', 'c', 'd', 'e'], do_hash=True) print(mt.is_ready) # False mt.make_tree() print(mt.is_ready) # True print(mt.get_merkle_root()) # d71f8983ad4ee170f8129f1ebcdd7440be7798d8e1c80420bf11f1eced610dba # Empty tree mt2 = MerkleTools() mt2.make_tree() print(mt2.is_ready) # True print(mt2.get_merkle_root()) # None ``` -------------------------------- ### Check Tree Readiness with `is_ready` Source: https://context7.com/blockchain-certificates/blockcerts-pymerkletools/llms.txt The `is_ready` property is True only after `make_tree()` is called on a non-empty set of leaves without subsequent modifications. It becomes False when leaves are added or the tree is reset. ```python from blockcerts_merkletools import MerkleTools mt = MerkleTools() print(mt.is_ready) # False mt.add_leaf("hello", do_hash=True) print(mt.is_ready) # False mt.make_tree() print(mt.is_ready) # True mt.add_leaf("world", do_hash=True) print(mt.is_ready) # False — modified after build ``` -------------------------------- ### is_ready — Tree Readiness Property Source: https://context7.com/blockchain-certificates/blockcerts-pymerkletools/llms.txt A boolean property that indicates if the Merkle tree is ready. It is True only after `make_tree()` has been called on a non-empty set of leaves without subsequent modifications. It becomes False when leaves are added or the tree is reset. ```APIDOC ## is_ready ### Description A boolean property that is `True` only after `make_tree()` has been called on a non-empty set of leaves without subsequent modifications. Becomes `False` when leaves are added or the tree is reset. ### Usage ```python from blockcerts_merkletools import MerkleTools mt = MerkleTools() print(mt.is_ready) # False mt.add_leaf("hello", do_hash=True) print(mt.is_ready) # False mt.make_tree() print(mt.is_ready) # True mt.add_leaf("world", do_hash=True) print(mt.is_ready) # False — modified after build ``` ``` -------------------------------- ### make_tree() Source: https://github.com/blockchain-certificates/blockcerts-pymerkletools/blob/main/README.md Constructs the Merkle tree based on the leaves that have been added. This method must be called before generating proofs or retrieving the Merkle root. ```APIDOC ## make_tree() Generates the merkle tree using the leaves that have been added. ```python mt.make_tree(); ``` ``` -------------------------------- ### Generate Merkle Proof with `get_proof(index)` Source: https://context7.com/blockchain-certificates/blockcerts-pymerkletools/llms.txt Returns the sibling-path proof for the leaf at the given zero-based `index` as a list of `{"left": hex}` or `{"right": hex}` dicts. Returns None if the tree is not ready, the index is out of range, or no leaves exist. Lone nodes are skipped. ```python from blockcerts_merkletools import MerkleTools mt = MerkleTools() mt.add_leaf(["tierion", "bitcoin", "blockchain"], do_hash=True) mt.make_tree() proof = mt.get_proof(1) print(proof) # [ # {'left': '2da7240f6c88536be72abe9f04e454c6478ee29709fc3729ddfb942f804fbf08'}, # {'right': 'ef7797e13d3a75526946a3bcf00daec9fc9c9c4d51ddc7cc5df888f74dd434d1'} # ] # Proof for an out-of-range index print(mt.get_proof(99)) # None # Proof before make_tree() mt2 = MerkleTools() mt2.add_leaf("a", do_hash=True) print(mt2.get_proof(0)) # None ``` -------------------------------- ### get_proof(index) Source: https://github.com/blockchain-certificates/blockcerts-pymerkletools/blob/main/README.md Generates and returns the Merkle proof for a leaf at a specified index. The proof is returned as an array of hash objects, each indicating its position (left or right) relative to the current hash. ```APIDOC ## get_proof(index) Returns 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. ```python proof = mt.get_proof(1) ``` The 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. ``` -------------------------------- ### get_tree_ready_state() — Programmatic Readiness Check Source: https://context7.com/blockchain-certificates/blockcerts-pymerkletools/llms.txt Returns the readiness state of the Merkle tree as a boolean value via a method call. This is equivalent to the `is_ready` property. ```APIDOC ## get_tree_ready_state() ### Description Returns the same boolean value as the `is_ready` property via a method call. Useful when passing a callable reference is preferred. ### Usage ```python from blockcerts_merkletools import MerkleTools mt = MerkleTools() print(mt.get_tree_ready_state()) # False mt.add_leaf("hello", do_hash=True) mt.make_tree() print(mt.get_tree_ready_state()) # True ``` ``` -------------------------------- ### Retrieve Merkle Root with `get_merkle_root()` Source: https://context7.com/blockchain-certificates/blockcerts-pymerkletools/llms.txt Returns the Merkle root of the built tree as a hex string. Returns None if the tree is not ready or was built with no leaves. For a single leaf, the root equals the leaf itself. ```python from blockcerts_merkletools import MerkleTools mt = MerkleTools() # Before building print(mt.get_merkle_root()) # None mt.add_leaf(["tierion", "bitcoin", "blockchain"], do_hash=True) mt.make_tree() root = mt.get_merkle_root() print(root) # 765f15d171871b00034ee55e48ffdf76afbc44ed0bcff5c82f31351d333c2ed1 # Single leaf: root equals the leaf itself mt2 = MerkleTools() mt2.add_leaf(['ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb']) mt2.make_tree() print(mt2.get_merkle_root()) # ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb ``` -------------------------------- ### get_proof(index) — Generate a Merkle Proof Source: https://context7.com/blockchain-certificates/blockcerts-pymerkletools/llms.txt Generates a Merkle proof for the leaf at the specified index. Returns the sibling-path proof as a list of dictionaries, or `None` if the tree is not ready, the index is out of range, or no leaves exist. ```APIDOC ## get_proof(index) ### Description Returns the sibling-path proof for the leaf at the given zero-based `index` as a list of `{"left": hex}` or `{"right": hex}` dicts. Returns `None` if the tree is not ready, the index is out of range, or no leaves exist. Lone (odd) nodes at any level are skipped in the proof path. ### Parameters #### Path Parameters - **index** (int) - Required - The zero-based index of the leaf for which to generate the proof. ### Usage ```python from blockcerts_merkletools import MerkleTools mt = MerkleTools() mt.add_leaf(["tierion", "bitcoin", "blockchain"], do_hash=True) mt.make_tree() proof = mt.get_proof(1) print(proof) # [ # {'left': '2da7240f6c88536be72abe9f04e454c6478ee29709fc3729ddfb942f804fbf08'}, # {'right': 'ef7797e13d3a75526946a3bcf00daec9fc9c9c4d51ddc7cc5df888f74dd434d1'} # ] # Proof for an out-of-range index print(mt.get_proof(99)) # None # Proof before make_tree() mt2 = MerkleTools() mt2.add_leaf("a", do_hash=True) print(mt2.get_proof(0)) # None ``` ``` -------------------------------- ### MerkleTools Constructor Source: https://context7.com/blockchain-certificates/blockcerts-pymerkletools/llms.txt Instantiates a new Merkle tree object. You can specify the hashing algorithm to be used for all internal tree operations. Supported hash types include 'sha256' (default), 'md5', 'sha224', 'sha384', 'sha512', 'sha3_256', 'sha3_224', 'sha3_384', and 'sha3_512'. Providing an unsupported hash type will raise an exception. ```APIDOC ## MerkleTools(hash_type="sha256") — Constructor Instantiates a new Merkle tree object. The `hash_type` parameter selects the hashing algorithm for all internal tree operations. Supported values: `"sha256"` (default), `"md5"`, `"sha224"`, `"sha384"`, `"sha512"`, `"sha3_256"`, `"sha3_224"`, `"sha3_384"`, `"sha3_512"`. Raises an exception for unsupported hash types. ```python from blockcerts_merkletools import MerkleTools # Default SHA-256 tree mt = MerkleTools() # SHA3-256 tree mt_sha3 = MerkleTools(hash_type="sha3_256") # MD5 tree (legacy/testing) mt_md5 = MerkleTools(hash_type="md5") # Unsupported type raises an exception try: bad = MerkleTools(hash_type="blake2b") except Exception as e: print(e) # `hash_type` blake2b nor supported ``` ``` -------------------------------- ### Check if Tree is Ready Source: https://github.com/blockchain-certificates/blockcerts-pymerkletools/blob/main/README.md A boolean property that indicates if the Merkle tree has been built and is ready to provide its root and proofs. The state becomes False if leaves are added or the tree is reset after `make_tree()`. ```python is_ready = mt.is_ready ``` -------------------------------- ### Verify Merkle Proof with `validate_proof()` Source: https://context7.com/blockchain-certificates/blockcerts-pymerkletools/llms.txt Returns True if the supplied `proof` array correctly connects `target_hash` to `merkle_root`; False otherwise. Both hashes must be hex strings. An empty proof is valid only when the target hash equals the root (single-leaf tree). ```python from blockcerts_merkletools import MerkleTools mt = MerkleTools() mt.add_leaf([ 'ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb', '3e23e8160039594a33894f6564e1b1348bbd7a0088d42c4acb73eeaed59c009d', '2e7d2c03a9507ae265ecf5b5356885a53393a2029d241394997265a1a25aefc6', '18ac3e7343f016890c510e93f935261169d9e3f565436429830faf0934f4f8e4', '3f79bb7b435b05321651daefd374cdc681dc06faa65e374e38337b88ca046dea' ]) mt.make_tree() root = mt.get_merkle_root() # d71f8983ad4ee170f8129f1ebcdd7440be7798d8e1c80420bf11f1eced610dba # Valid proof for leaf at index 1 proof = mt.get_proof(1) is_valid = mt.validate_proof( proof, '3e23e8160039594a33894f6564e1b1348bbd7a0088d42c4acb73eeaed59c009d', root ) print(is_valid) # True # Invalid proof: wrong target hash is_valid = mt.validate_proof( proof, 'badc3e7343f016890c510e93f935261169d9e3f565436429830faf0934f4f8e4', root ) print(is_valid) # False # Valid proof for last leaf (index 4) in 5-leaf tree (odd-node promotion) proof4 = mt.get_proof(4) is_valid4 = mt.validate_proof( proof4, '3f79bb7b435b05321651daefd374cdc681dc06faa65e374e38337b88ca046dea', root ) print(is_valid4) # True ``` -------------------------------- ### validate_proof(proof, target_hash, merkle_root) Source: https://github.com/blockchain-certificates/blockcerts-pymerkletools/blob/main/README.md Validates a given Merkle proof against a target hash and the Merkle root. It returns `True` if the proof is valid and correctly links the target hash to the Merkle root, and `False` otherwise. ```APIDOC ## validate_proof(proof, target_hash, merkle_root) Returns 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. ```python proof = [ { right: '09096dbc49b7909917e13b795ebf289ace50b870440f10424af8845fb7761ea5' }, { right: 'ed2456914e48c1e17b7bd922177291ef8b7f553edf1b1f66b6fc1a076524b22f' }, { left: 'eac53dde9661daf47a428efea28c81a021c06d64f98eeabbdcff442d992153a8' }, ] target_hash = '36e0fd847d927d68475f32a94efff30812ee3ce87c7752973f4dd7476aa2e97e' merkle_root = 'b8b1f39aa2e3fc2dde37f3df04e829f514fb98369b522bfb35c663befa896766' is_valid = mt.validate_proof(proof, targetHash, merkleRoot) ``` The 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: 1. 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. 2. Hash the resulting value. 3. Concatenate the resulting hash with the next hash in the proof array, using the same left and right rules. 4. Hash that value and continue the process until you’ve gone through each item in the proof array. 5. The final hash value should equal the `merkle_root` value if the proof is valid, otherwise the proof is invalid. ``` -------------------------------- ### Reset Merkle Tree to Initial State Source: https://context7.com/blockchain-certificates/blockcerts-pymerkletools/llms.txt Clear all added leaves and reset the Merkle tree to its empty state. This action sets `is_ready` to `False` and the Merkle root becomes `None`. ```python from blockcerts_merkletools import MerkleTools mt = MerkleTools() mt.add_leaf(["a", "b", "c"], do_hash=True) mt.make_tree() print(mt.is_ready) # True print(mt.get_leaf_count()) # 3 mt.reset_tree() print(mt.is_ready) # False print(mt.get_leaf_count()) # 0 print(mt.get_merkle_root()) # None ``` -------------------------------- ### Generate Merkle Tree Source: https://github.com/blockchain-certificates/blockcerts-pymerkletools/blob/main/README.md Constructs the Merkle tree based on the leaves that have been added. This must be called before generating proofs or the root. ```python mt.make_tree(); ``` -------------------------------- ### get_merkle_root() Source: https://github.com/blockchain-certificates/blockcerts-pymerkletools/blob/main/README.md Returns the Merkle root of the constructed tree as a hex string. If the tree is not ready (i.e., `make_tree()` has not been called), it returns `None`. ```APIDOC ## get_merkle_root() Returns the merkle root of the tree as a hex string. If the tree is not ready, `None` is returned. ```python root_value = mt.get_merkle_root(); ``` ``` -------------------------------- ### Add Leaves to Merkle Tree (Hashed and Raw) Source: https://context7.com/blockchain-certificates/blockcerts-pymerkletools/llms.txt Add leaves to the Merkle tree, either as pre-hashed hex strings or raw values that the library will hash. The `do_hash=True` parameter is used for raw values. Adding leaves sets `is_ready` to `False` until `make_tree()` is called. ```python from blockcerts_merkletools import MerkleTools mt = MerkleTools() # Add pre-hashed hex leaves (sha256 of "a", "b", "c", "d", "e") mt.add_leaf([ 'ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb', # sha256("a") '3e23e8160039594a33894f6564e1b1348bbd7a0088d42c4acb73eeaed59c009d', # sha256("b") '2e7d2c03a9507ae265ecf5b5356885a53393a2029d241394997265a1a25aefc6', # sha256("c") ]) # Add raw string leaves — library hashes them automatically mt.add_leaf(['d', 'e'], do_hash=True) print(mt.get_leaf_count()) # 5 print(mt.is_ready) # False — tree not yet built # Adding a single leaf mt2 = MerkleTools() mt2.add_leaf("tierion", do_hash=True) mt2.add_leaf("bitcoin", do_hash=True) print(mt2.get_leaf_count()) # 2 ``` -------------------------------- ### __str__() — Tree String Representation Source: https://context7.com/blockchain-certificates/blockcerts-pymerkletools/llms.txt Returns a human-readable string representation of the Merkle tree, including the root and all node hashes at each level. Returns an empty string if the tree is not yet built. ```APIDOC ## __str__() ### Description Returns a human-readable multi-line string displaying the Merkle root followed by each level of the tree with all node hashes. Returns an empty string if the tree is not yet built. ### Usage ```python from blockcerts_merkletools import MerkleTools mt = MerkleTools() mt.add_leaf(['a', 'b', 'c'], do_hash=True) mt.make_tree() print(str(mt)) # # Level: 0 # Level: 1 # Level: 2 # Not ready — empty string mt2 = MerkleTools() mt2.add_leaf("x", do_hash=True) print(repr(str(mt2))) # '' ``` ``` -------------------------------- ### Validate Merkle Proof Source: https://github.com/blockchain-certificates/blockcerts-pymerkletools/blob/main/README.md Verifies if a given proof correctly connects a target hash to the Merkle root. Requires the proof array, target hash, and Merkle root, all as hex strings. ```python proof = [ { right: '09096dbc49b7909917e13b795ebf289ace50b870440f10424af8845fb7761ea5' }, { right: 'ed2456914e48c1e17b7bd922177291ef8b7f553edf1b1f66b6fc1a076524b22f' }, { left: 'eac53dde9661daf47a428efea28c81a021c06d64f98eeabbdcff442d992153a8' }, ] target_hash = '36e0fd847d927d68475f32a94efff30812ee3ce87c7752973f4dd7476aa2e97e' merkle_root = 'b8b1f39aa2e3fc2dde37f3df04e829f514fb98369b522bfb35c663befa896766' is_valid = mt.validate_proof(proof, targetHash, merkleRoot) ``` -------------------------------- ### add_leaf(values, do_hash=False) Source: https://context7.com/blockchain-certificates/blockcerts-pymerkletools/llms.txt Adds one or more leaves to the Merkle tree. The `values` parameter can accept a single value or a list/tuple of values. If `do_hash` is set to `False` (the default), the provided values must be valid hex strings. If `do_hash` is `True`, the values are treated as UTF-8 encoded strings and will be hashed by the library. After calling this method, the `is_ready` attribute of the MerkleTools object is set to `False`. ```APIDOC ## add_leaf(values, do_hash=False) — Add Leaves Adds one or more leaves to the tree. `values` can be a single value or a list/tuple. When `do_hash=False` (default), values must already be valid hex strings. When `do_hash=True`, values are UTF-8 encoded strings that will be hashed before storage. Calling this method sets `is_ready` to `False`. ```python from blockcerts_merkletools import MerkleTools mt = MerkleTools() # Add pre-hashed hex leaves (sha256 of "a", "b", "c", "d", "e") mt.add_leaf([ 'ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb', # sha256("a") '3e23e8160039594a33894f6564e1b1348bbd7a0088d42c4acb73eeaed59c009d', # sha256("b") '2e7d2c03a9507ae265ecf5b5356885a53393a2029d241394997265a1a25aefc6', # sha256("c") ]) # Add raw string leaves — library hashes them automatically mt.add_leaf(['d', 'e'], do_hash=True) print(mt.get_leaf_count()) # 5 print(mt.is_ready) # False — tree not yet built # Adding a single leaf mt2 = MerkleTools() mt2.add_leaf("tierion", do_hash=True) mt2.add_leaf("bitcoin", do_hash=True) print(mt2.get_leaf_count()) # 2 ``` ``` -------------------------------- ### reset_tree() Source: https://github.com/blockchain-certificates/blockcerts-pymerkletools/blob/main/README.md Clears all leaves from the Merkle tree, effectively resetting it to an empty state for creating a new tree. ```APIDOC ## reset_tree() Removes all the leaves from the tree, prepararing to to begin creating a new tree. ```python mt.reset_tree() ``` ``` -------------------------------- ### get_merkle_root() — Retrieve the Root Hash Source: https://context7.com/blockchain-certificates/blockcerts-pymerkletools/llms.txt Returns the Merkle root of the built tree as a hex string. Returns `None` if the tree is not ready or was built with no leaves. ```APIDOC ## get_merkle_root() ### Description Returns the Merkle root of the built tree as a hex string. Returns `None` if the tree is not ready or was built with no leaves. ### Usage ```python from blockcerts_merkletools import MerkleTools mt = MerkleTools() # Before building print(mt.get_merkle_root()) # None mt.add_leaf(["tierion", "bitcoin", "blockchain"], do_hash=True) mt.make_tree() root = mt.get_merkle_root() print(root) # 765f15d171871b00034ee55e48ffdf76afbc44ed0bcff5c82f31351d333c2ed1 # Single leaf: root equals the leaf itself mt2 = MerkleTools() mt2.add_leaf(['ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb']) mt2.make_tree() print(mt2.get_merkle_root()) # ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb ``` ``` -------------------------------- ### reset_tree() Source: https://context7.com/blockchain-certificates/blockcerts-pymerkletools/llms.txt Clears all leaves from the Merkle tree, effectively resetting it to its initial empty state. This operation also sets the `is_ready` attribute to `False`. ```APIDOC ## reset_tree() — Clear the Tree Removes all leaves and resets the tree to its empty initial state. Sets `is_ready` to `False`. ```python from blockcerts_merkletools import MerkleTools mt = MerkleTools() mt.add_leaf(["a", "b", "c"], do_hash=True) mt.make_tree() print(mt.is_ready) # True print(mt.get_leaf_count()) # 3 mt.reset_tree() print(mt.is_ready) # False print(mt.get_leaf_count()) # 0 print(mt.get_merkle_root()) # None ``` ``` -------------------------------- ### add_leaf(value, do_hash) Source: https://github.com/blockchain-certificates/blockcerts-pymerkletools/blob/main/README.md Adds a value or a list of values as leaves to the Merkle tree. Values should be provided as hex strings. The `do_hash` parameter (boolean) indicates whether the value should be hashed before being added. ```APIDOC ## add_leaf(value, do_hash) Adds a value as a leaf or a list of leafs to the tree. The value must be a hex string. ```python mt.add_leaf("0x4b39F7b0624b9dB86AD293686bc38B903142dbBc") mt.add_leaf("0x71b4a2d9B91726bdb5849D928967A1654D7F3de7") ``` ``` -------------------------------- ### Add Multiple Leaves to Merkle Tree Source: https://github.com/blockchain-certificates/blockcerts-pymerkletools/blob/main/README.md Adds multiple hex string values as leaves to the Merkle tree. The values must be valid hex strings. ```python mt.add_leaf("0x71b4a2d9B91726bdb5849D928967A1654D7F3de7") ``` -------------------------------- ### get_leaf(index) Source: https://context7.com/blockchain-certificates/blockcerts-pymerkletools/llms.txt Retrieves and returns a specific leaf from the tree based on its zero-based index. The leaf is returned as a lowercase hexadecimal string. ```APIDOC ## get_leaf(index) — Retrieve a Leaf Returns the stored leaf at the given zero-based index as a lowercase hex string. ```python from blockcerts_merkletools import MerkleTools mt = MerkleTools() mt.add_leaf("tierion", do_hash=True) mt.add_leaf("bitcoin", do_hash=True) print(mt.get_leaf(0)) # 2da7240f6c88536be72abe9f04e454c6478ee29709fc3729ddfb942f804fbf08 print(mt.get_leaf(1)) # b84c79e1047d90c1e3f78e69e7b4fae8e7e6c46b62e0d29a7ee7e5d793e5f5b5 ``` ``` -------------------------------- ### Reset Merkle Tree Source: https://github.com/blockchain-certificates/blockcerts-pymerkletools/blob/main/README.md Clears all added leaves from the Merkle tree, preparing it for a new tree construction. ```python mt.reset_tree() ``` -------------------------------- ### get_leaf(index) Source: https://github.com/blockchain-certificates/blockcerts-pymerkletools/blob/main/README.md Retrieves the value of a leaf at a specific index from the Merkle tree. The value is returned as a hex string. ```APIDOC ## get_leaf(index) Returns the value of the leaf at the given index as a hex string. ```python leaf_value = mt.get_leaf(1) ``` ``` -------------------------------- ### Count Leaves in Merkle Tree Source: https://context7.com/blockchain-certificates/blockcerts-pymerkletools/llms.txt Retrieve the total number of leaves currently added to the Merkle tree. This method returns an integer count. ```python from blockcerts_merkletools import MerkleTools mt = MerkleTools() mt.add_leaf(["tierion", "bitcoin", "blockchain"], do_hash=True) print(mt.get_leaf_count()) # 3 ``` -------------------------------- ### Add Leaf to Merkle Tree Source: https://github.com/blockchain-certificates/blockcerts-pymerkletools/blob/main/README.md Adds a single hex string value as a leaf to the Merkle tree. Ensure the value is a valid hex string. ```python mt.add_leaf("0x4b39F7b0624b9dB86AD293686bc38B903142dbBc") ``` -------------------------------- ### get_leaf_count() Source: https://github.com/blockchain-certificates/blockcerts-pymerkletools/blob/main/README.md Returns the total number of leaves currently added to the Merkle tree. ```APIDOC ## get_leaf_count() Returns the number of leaves that are currently added to the tree. ```python leaf_count = mt.get_leaf_count(); ``` ``` -------------------------------- ### get_leaf_count() Source: https://context7.com/blockchain-certificates/blockcerts-pymerkletools/llms.txt Returns the total number of leaves currently stored within the Merkle tree as an integer. ```APIDOC ## get_leaf_count() — Count Leaves Returns the integer number of leaves currently stored in the tree. ```python from blockcerts_merkletools import MerkleTools mt = MerkleTools() mt.add_leaf(["tierion", "bitcoin", "blockchain"], do_hash=True) print(mt.get_leaf_count()) # 3 ``` ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.