Skip to main content

blockcerts-pymerkletools

PyPI version

This is a Python port of solidity-pymerkletools, which supports Web3's solidity keccak hashing function. This fork supports updated dependencies per Blockcerts project requirements.

Tools for creating Merkle trees, generating merkle proofs, and verification of merkle proofs.

Installation

pip install blockcerts-merkletools

Create MerkleTools Object

import blockcerts_merkletools

mt = MerkleTools()  # default hash algorithm is Web3.solidity_keccak

Methods

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.

mt.add_leaf("0x4b39F7b0624b9dB86AD293686bc38B903142dbBc")
mt.add_leaf("0x71b4a2d9B91726bdb5849D928967A1654D7F3de7")

get_leaf_count()

Returns the number of leaves that are currently added to the tree.

leaf_count =  mt.get_leaf_count();

get_leaf(index)

Returns the value of the leaf at the given index as a hex string.

leaf_value =  mt.get_leaf(1)

reset_tree()

Removes all the leaves from the tree, prepararing to to begin creating a new tree.

mt.reset_tree()

make_tree()

Generates the merkle tree using the leaves that have been added.

mt.make_tree();

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.

is_ready = mt.is_ready 

get_merkle_root()

Returns the merkle root of the tree as a hex string. If the tree is not ready, None is returned.

root_value = mt.get_merkle_root();

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.

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.

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.

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.

Common Usage

Creating a tree and generating the proofs

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

Notes

About tree generation

  1. Internally, leaves are stored as bytearray. When the tree is built, it is generated by hashing together the bytearray values.

  2. Lonely leaf nodes are promoted to the next level up, as depicted below.

                      ROOT=Hash(H+E)
                      /        \
                     /          \
              H=Hash(F+G)        E
              /       \           \
             /         \           \
      F=Hash(A+B)    G=Hash(C+D)    E
       /     \        /     \        \
      /       \      /       \        \
    
     A         B    C         D        E
    

Metadata

Release files for blockcerts-merkletools 1.0.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for blockcerts-merkletools 1.0.5
File Size Uploaded
blockcerts_merkletools-1.0.5.tar.gz 9.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for blockcerts-merkletools 1.0.5
File Interpreter ABI Platform
blockcerts_merkletools-1.0.5-py3-none-any.whl Python 3 none any Details

Total release size: 15.0 kB

Release files / blockcerts_merkletools-1.0.5.tar.gz

Download URL blockcerts_merkletools-1.0.5.tar.gz
Size 9.0 kB
Tags Source
SHA-256 checksum
How to use checksums
a46f9db0a728212d3d3be870fb32646760dfd654dab647ecd2719e4c5e3bd0b8
BLAKE2b-256 checksum
How to use checksums
3e2c80ab69d098cfb3500db0279ae1b396d77a65d2fba8d5bf8dde31d55d8ee4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.3

Release files / blockcerts_merkletools-1.0.5-py3-none-any.whl

Download URL blockcerts_merkletools-1.0.5-py3-none-any.whl
Size 6.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
561136514f40957772ccf0a03a631f860432b661e6b66604720c76b2cabf1eb3
BLAKE2b-256 checksum
How to use checksums
b9a0bf969d0a5ebe3a7476204fcaf44c32f1dc1538d85d6389b52f077b79e185
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.3

Release history Release notifications | RSS feed

This release

1.0.5 This release

2 release files

1.0.4

2 release files

1.0.3

2 release files

1.0.2

2 release files

1.0.1

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page