diff --git a/pyproject.toml b/pyproject.toml index 6a1d014..c5c95a2 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -15,6 +15,9 @@ classifiers = [ ] requires-python = ">=3.9" dependencies = [ + "pydantic", + "eth-utils", + "eth-hash[pycryptodome]" ] [project.urls] @@ -229,8 +232,8 @@ dependencies = [ "eth-utils", "pytest", "pytest-cov", + "rich" ] -installer = "uv" [tool.hatch.envs.test.scripts] test = "pytest" diff --git a/src/mantaray_py/__init__.py b/src/mantaray_py/__init__.py index 2ff79ed..754c390 100644 --- a/src/mantaray_py/__init__.py +++ b/src/mantaray_py/__init__.py @@ -58,7 +58,7 @@ def init_manifest_node(options: dict = None) -> MantarayNode: if 'obfuscationKey' in options: manifest_node.set_obfuscation_key(options['obfuscationKey']) else: - manifest_node.set_obfuscation_key(None) + manifest_node.set_obfuscation_key(gen_32_bytes()) return manifest_node diff --git a/src/mantaray_py/node.py b/src/mantaray_py/node.py index 5063f59..b585b17 100644 --- a/src/mantaray_py/node.py +++ b/src/mantaray_py/node.py @@ -11,7 +11,8 @@ StorageLoader, StorageSaver, ) -from mantaray_py.utils import IndexBytes, check_reference, common, encrypt_decrypt, equal_bytes, keccak256_hash +from mantaray_py.utils import IndexBytes, check_reference, common, encrypt_decrypt, equal_bytes +from eth_utils import keccak PATH_SEPARATOR = "/" PATH_SEPARATOR_BYTE = 47 @@ -450,11 +451,10 @@ def serialize(self) -> bytes: for fork_index in self.forks.keys(): index.set_byte(int(fork_index)) index_bytes = index.get_bytes() - # Forks - fork_serializations: list[bytes] = [] + fork_serializations: bytearray = [] - for byte in index: + for byte in index.get_bytes(): fork = self.forks.get(byte) if fork is None: msg = f"Fork indexing error: fork has not found under {byte} index" @@ -693,7 +693,7 @@ def serialize_version(version: Union[MarshalVersion, str]) -> bytes: """ version_name = "mantaray" version_separator = ":" - hash_bytes = keccak256_hash(version_name + version_separator + version) + hash_bytes = keccak(text=(version_name + version_separator + version)) return hash_bytes[:31] @@ -713,8 +713,9 @@ def serialize_reference_len(entry: Reference) -> bytes: msg = f"Wrong referenceLength. It can be only 32 or 64. Got: {reference_len}" raise ValueError(msg) + print(reference_len) # Serialize the reference length into a single byte - return int.to_bytes(len(entry), byteorder="big", signed=False) # type: ignore + return (reference_len).to_bytes(reference_len, byteorder="big", signed=False) # type: ignore def load_all_nodes(storage_loader: StorageLoader, node: MantarayNode) -> None: diff --git a/src/mantaray_py/utils.py b/src/mantaray_py/utils.py index e2551d8..47f2d10 100644 --- a/src/mantaray_py/utils.py +++ b/src/mantaray_py/utils.py @@ -9,14 +9,9 @@ class IndexBytes(BaseModel): - bytes_data: bytearray = Field(..., min_length=32, max_length=32) + bytes_data: bytearray = Field(bytearray(32), min_length=32, max_length=32) model_config = ConfigDict(arbitrary_types_allowed=True) - def __init__(self, **data: dict) -> None: - super().__init__(**data) - # Initialize bytes_data with zeros - self.bytes_data = bytearray(32) - def set_byte(self, byte: int) -> None: """Set a byte value.""" if byte > 255: # noqa: PLR2004 diff --git a/tests/conftest.py b/tests/conftest.py index 29b3614..658a144 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,9 +1,8 @@ from mantaray_py import MantarayNode, equal_nodes, gen_32_bytes -import pytest +from pytest import fixture -@pytest.fixture +@fixture def get_sample_mantaray_node() -> dict[MantarayNode, bytes]: - node = MantarayNode() random_address = gen_32_bytes() node.set_entry(random_address) diff --git a/tests/test_unit.py b/tests/test_unit.py index 17b7bbc..5a6bb26 100644 --- a/tests/test_unit.py +++ b/tests/test_unit.py @@ -1,10 +1,14 @@ from mantaray_py import MantarayNode, equal_nodes, gen_32_bytes, check_for_separator, init_manifest_node import pytest +from rich.console import Console +console = Console() -def test_single_manaray_node_with_a_random_address(get_sample_mantaray_node): +def test_single_manaray_node_with_a_random_address(): node = init_manifest_node() random_address = gen_32_bytes() + console.print(random_address) + node.set_entry(random_address) serialized = node.serialize()