Skip to content

Commit

Permalink
adding support for legacy namedtuples
Browse files Browse the repository at this point in the history
  • Loading branch information
elicbarbieri committed Apr 24, 2024
1 parent 2894d82 commit 51bc4c4
Show file tree
Hide file tree
Showing 5 changed files with 718 additions and 10 deletions.
15 changes: 9 additions & 6 deletions starknet_abi/dispatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,18 @@
from starknet_abi.decoding_types import DecodedEvent, DecodedFunction
from starknet_abi.exceptions import InvalidCalldataError

consistent_hash = hashlib.md5()
# Python's builtin hash() function is seeded with a random value at startup, so it is not consistent across runs
# Using a consistent hash allows a DecodingDispatcher to be pickled and cached between uses


def _id_hash(id_str: str) -> bytes:
consistent_hash.update(id_str.encode())
"""
Python's builtin hash() function is seeded with a random value at interpreter startup, so it is not
consistent across runs. Using a consistent hash allows a DecodingDispatcher to be pickled and
cached between uses
"""

h = hashlib.md5()
h.update(id_str.encode())
# MD5 returns a 16byte digest. We only need the last 8
return consistent_hash.digest()[-8:]
return h.digest()[-8:]


@dataclass(slots=True)
Expand Down
20 changes: 18 additions & 2 deletions starknet_abi/parse.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import re
from collections import defaultdict
from graphlib import TopologicalSorter
from typing import Any
Expand Down Expand Up @@ -178,7 +179,15 @@ def _parse_tuple(
:return:
"""

stripped_tuple = abi_type[1:-1].replace(" ", "") # Remove Outer Parentheses
def _is_named_tuple(type_str):
match = re.search(r"(?<!:):(?!:)", type_str)
if match:
return match.start()
# If no match is found, return -1
return False

# Remove Outer Parentheses & Whitespace
stripped_tuple = abi_type[1:-1].replace(" ", "")

output_types = []
parenthesis_cache = [] # Tracks tuple opens and closes
Expand All @@ -196,7 +205,14 @@ def _parse_tuple(
if parenthesis_cache: # Currently Parsing Types inside Nested Tuple
type_cache.append(type_string)
else: # Append Types To Root Tuple
output_types.append(_parse_type(type_string, custom_types))
if _is_named_tuple(type_string):
output_types.append(
_parse_type(
type_string[_is_named_tuple(type_string) + 1 :], custom_types
)
)
else:
output_types.append(_parse_type(type_string, custom_types))

if tuple_close:
for _ in range(tuple_close):
Expand Down
Loading

0 comments on commit 51bc4c4

Please sign in to comment.