Skip to main content

Python port of merkletreejs

Project description

                _   _     _
  _ __  ___ _ _| |_| |___| |_ _ _ ___ ___ _ __ _  _
 | '  \/ -_) '_| / / / -_)  _| '_/ -_) -_) '_ \ || |
 |_|_|_\___|_| |_\_\_\___|\__|_| \___\___| .__/\_, |
                                         |_|   |__/

MerkleTree.py

A Python port of merkletreejs. :warning: Currently EXTREMELY unstable and doesn't work for many cases yet. :warning:

PyPI version

Installation

pip install merkletreepy

Working Code Examples

sha256

from merkletreepy import MerkleTree
import hashlib

def sha256(x):
    return hashlib.sha256(x).digest()

leaves = [sha256(leaf.encode()) for leaf in "abc"]
tree = MerkleTree(leaves, sha256)
root = tree.get_root()
leaf = sha256("a".encode())
bad_leaf = sha256("x".encode())
proof = tree.get_proof(leaf)
tree.verify(proof, leaf, root)      # returns True
tree.verify(proof, bad_leaf, root)  # returns False

keccak256 (as implemented in the Solidity language)

from merkletreepy import MerkleTree
import Web3

def hash_function(x):
    return Web3.keccak(text=x).hex()

leaves = [hash_function(leaf) for leaf in "abc"]
tree = MerkleTree(leaves, sha256)
root = tree.get_root()
leaf = sha256("a")
bad_leaf = sha256("x")
proof = tree.get_proof(leaf)
tree.verify(proof, leaf, root)      # returns True
tree.verify(proof, bad_leaf, root)  # returns False

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

merkletreepy-0.0.3.tar.gz (4.3 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page