Skip to content

Commit

Permalink
Remove web3py from dependencies (#14)
Browse files Browse the repository at this point in the history
* Remove web3py from dependencies

Signed-off-by: cyc60 <avsysoev60@gmail.com>

* Review fix

Signed-off-by: cyc60 <avsysoev60@gmail.com>

---------

Signed-off-by: cyc60 <avsysoev60@gmail.com>
  • Loading branch information
cyc60 authored Oct 29, 2024
1 parent 99eb00d commit c5378e4
Show file tree
Hide file tree
Showing 6 changed files with 398 additions and 1,661 deletions.
7 changes: 3 additions & 4 deletions multiproof/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
from itertools import pairwise
from typing import Any

from web3 import Web3

from multiproof.bytes import compare_bytes, concat_bytes, equals_bytes
from multiproof.utils import keccak


@dataclass
Expand All @@ -17,9 +16,9 @@ class CoreMultiProof:

def hash_pair(a: bytes, b: bytes) -> bytes:
if compare_bytes(a, b) < 0:
return Web3.keccak(concat_bytes(a, b))
return keccak(concat_bytes(a, b))
# pylint: disable=arguments-out-of-order
return Web3.keccak(concat_bytes(b, a))
return keccak(concat_bytes(b, a))


def left_child_index(i: int) -> int:
Expand Down
11 changes: 4 additions & 7 deletions multiproof/standard.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@

from eth_abi import encode as abi_encode
from eth_typing import HexStr
from web3 import Web3

from multiproof.bytes import compare_bytes, equals_bytes, hex_to_bytes, to_hex
from multiproof.core import (CoreMultiProof, get_multi_proof, get_proof,
is_valid_merkle_tree, left_child_index,
make_merkle_tree, process_multi_proof,
process_proof, right_child_index)
from multiproof.utils import check_bounds
from multiproof.utils import check_bounds, keccak

T = TypeVar('T')

Expand Down Expand Up @@ -48,7 +47,7 @@ class HashedValue(Generic[T]):


def standard_leaf_hash(values: T, types: list[str]) -> bytes:
return Web3.keccak(Web3.keccak(abi_encode(types, values))) # type: ignore
return keccak(keccak(abi_encode(types, values))) # type: ignore


class StandardMerkleTree(Generic[T]):
Expand All @@ -67,7 +66,7 @@ def __init__(self, tree: list[bytes], values: list[LeafValue[T]], leaf_encoding:

@staticmethod
def of(
values: list[T], leaf_encoding: list[str], sort_leaves: bool = True
values: list[T], leaf_encoding: list[str], sort_leaves: bool = True
) -> 'StandardMerkleTree[T]':
hashed_values: list[HashedValue[T]] = []
for index, value in enumerate(values):
Expand Down Expand Up @@ -100,9 +99,7 @@ def load(data: StandardMerkleTreeData[T]) -> 'StandardMerkleTree[T]':
)

@staticmethod
def verify(
root: HexStr, leaf_encoding: list[str], leaf_value: T, proof: list[HexStr]
) -> bool:
def verify(root: HexStr, leaf_encoding: list[str], leaf_value: T, proof: list[HexStr]) -> bool:
leaf_hash = standard_leaf_hash(leaf_value, leaf_encoding)
implied_root = process_proof(leaf_hash, [hex_to_bytes(x) for x in proof])
return equals_bytes(implied_root, hex_to_bytes(root))
Expand Down
5 changes: 2 additions & 3 deletions multiproof/tests/test_standard.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import pytest
from web3 import Web3

from multiproof.bytes import to_hex
from multiproof.standard import (LeafValue, StandardMerkleTree,
StandardMerkleTreeData)
from multiproof.utils import keccak

ZERO_BYTES = bytearray(32)
ZERO = to_hex(ZERO_BYTES)
Expand All @@ -16,7 +16,6 @@ def make_tree(s: str, sort_leaves: bool = True) -> tuple[list[list[str]], Standa


class TestStandartTestCase:

@pytest.mark.parametrize('sort_leaves', (True, False))
def test_valid_single_proofs(self, sort_leaves):
"generates valid single proofs for all leaves"
Expand Down Expand Up @@ -110,7 +109,7 @@ def test_reject_malformed(self):
tree2 = StandardMerkleTree.load(
StandardMerkleTreeData(
format='standard-v1',
tree=[ZERO, ZERO, to_hex(Web3.keccak(Web3.keccak(ZERO_BYTES)))],
tree=[ZERO, ZERO, to_hex(keccak(keccak(ZERO_BYTES)))],
values=[LeafValue(value=['0'], tree_index=2)],
leaf_encoding=['uint256'],
)
Expand Down
25 changes: 25 additions & 0 deletions multiproof/utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,28 @@
from typing import Optional

from eth_typing import HexStr, Primitives
from eth_utils import keccak as eth_utils_keccak
from eth_utils import to_bytes


def check_bounds(array: list, index: int) -> None:
if index < 0 or index >= len(array):
raise ValueError("Index out of bounds")


def keccak(
primitive: Optional[Primitives] = None,
text: Optional[str] = None,
hexstr: Optional[HexStr] = None,
) -> bytes:
""" Taken from web3py """
if isinstance(primitive, (bytes, int, type(None))):
input_bytes = to_bytes(primitive, hexstr=hexstr, text=text)
return eth_utils_keccak(input_bytes)

raise TypeError(
f"You called keccak with first arg {primitive!r} and keywords "
f"{{'text': {text!r}, 'hexstr': {hexstr!r}}}. You must call it with "
"one of these approaches: keccak(text='txt'), keccak(hexstr='0x747874'), "
"keccak(b'\\x74\\x78\\x74'), or keccak(0x747874)."
)
Loading

0 comments on commit c5378e4

Please sign in to comment.