1212from starknet_py .net .client_utils import _to_rpc_felt
1313from starknet_py .net .models .typed_data import DomainDict , Revision , TypedDataDict
1414from starknet_py .net .schemas .common import RevisionField
15+ from starknet_py .serialization .data_serializers import ByteArraySerializer
1516from starknet_py .utils .merkle_tree import MerkleTree
1617
1718
@@ -91,40 +92,6 @@ class BasicType(Enum):
9192 TIMESTAMP = "timestamp"
9293
9394
94- def _encode_value_v1 (basic_type : BasicType , value : Union [int , str ]) -> Optional [int ]:
95- if basic_type in (
96- BasicType .FELT ,
97- BasicType .SHORT_STRING ,
98- BasicType .CONTRACT_ADDRESS ,
99- BasicType .CLASS_HASH ,
100- ) and isinstance (value , (int , str )):
101- return parse_felt (value )
102-
103- if basic_type in (
104- BasicType .U128 ,
105- BasicType .TIMESTAMP ,
106- ) and isinstance (value , (int , str )):
107- return encode_u128 (value )
108-
109- if basic_type == BasicType .I128 and isinstance (value , (int , str )):
110- return encode_i128 (value )
111-
112- return None
113-
114-
115- def _encode_value_v0 (
116- basic_type : BasicType ,
117- value : Union [int , str ],
118- ) -> Optional [int ]:
119- if basic_type in (
120- BasicType .FELT ,
121- BasicType .STRING ,
122- ) and isinstance (value , (int , str )):
123- return parse_felt (value )
124-
125- return None
126-
127-
12895@dataclass (frozen = True )
12996class TypedData :
13097 """
@@ -167,6 +134,45 @@ def to_dict(self) -> dict:
167134 def _is_struct (self , type_name : str ) -> bool :
168135 return type_name in self .types
169136
137+ def _encode_value_v1 (
138+ self , basic_type : BasicType , value : Union [int , str , dict , list ]
139+ ) -> Optional [int ]:
140+ if basic_type in (
141+ BasicType .FELT ,
142+ BasicType .SHORT_STRING ,
143+ BasicType .CONTRACT_ADDRESS ,
144+ BasicType .CLASS_HASH ,
145+ ) and isinstance (value , (int , str )):
146+ return parse_felt (value )
147+
148+ if basic_type in (
149+ BasicType .U128 ,
150+ BasicType .TIMESTAMP ,
151+ ) and isinstance (value , (int , str )):
152+ return encode_u128 (value )
153+
154+ if basic_type == BasicType .I128 and isinstance (value , (int , str )):
155+ return encode_i128 (value )
156+
157+ if basic_type == BasicType .STRING and isinstance (value , str ):
158+ return self ._encode_long_string (value )
159+
160+ return None
161+
162+ # pylint: disable=no-self-use
163+ def _encode_value_v0 (
164+ self ,
165+ basic_type : BasicType ,
166+ value : Union [int , str , dict , list ],
167+ ) -> Optional [int ]:
168+ if basic_type in (
169+ BasicType .FELT ,
170+ BasicType .STRING ,
171+ ) and isinstance (value , (int , str )):
172+ return parse_felt (value )
173+
174+ return None
175+
170176 def _encode_value (
171177 self ,
172178 type_name : str ,
@@ -190,11 +196,11 @@ def _encode_value(
190196 if self .domain .resolved_revision == Revision .V0 and isinstance (
191197 value , (str , int )
192198 ):
193- encoded_value = _encode_value_v0 (basic_type , value )
199+ encoded_value = self . _encode_value_v0 (basic_type , value )
194200 elif self .domain .resolved_revision == Revision .V1 and isinstance (
195201 value , (str , int )
196202 ):
197- encoded_value = _encode_value_v1 (basic_type , value )
203+ encoded_value = self . _encode_value_v1 (basic_type , value )
198204
199205 if encoded_value is not None :
200206 return encoded_value
@@ -353,6 +359,11 @@ def _get_merkle_tree_leaves_type(self, context: TypeContext) -> str:
353359
354360 return target_type .contains
355361
362+ def _encode_long_string (self , value : str ) -> int :
363+ byte_array_serializer = ByteArraySerializer ()
364+ serialized_values = byte_array_serializer .serialize (value )
365+ return self ._hash_method .hash_many (serialized_values )
366+
356367
357368def parse_felt (value : Union [int , str ]) -> int :
358369 if isinstance (value , int ):
0 commit comments