44from typing import Any
55
66import rlp # type: ignore[import-untyped]
7+ from eth_typing import HexStr
78from hexbytes import HexBytes
89from web3 import Web3
910from web3 .contract import Contract
1213
1314from . import contract
1415from .contract import STORAGE_ADDRESS
16+ from .exceptions import EntityKeyException
1517from .types import (
1618 Annotation ,
1719 AnnotationValue ,
2830logger = logging .getLogger (__name__ )
2931
3032
33+ def to_entity_key (entity_key_int : int ) -> EntityKey :
34+ hex_value = Web3 .to_hex (entity_key_int )
35+ # ensure lenth is 66 (0x + 64 hex)
36+ if len (hex_value ) < 66 :
37+ hex_value = HexStr ("0x" + hex_value [2 :].zfill (64 ))
38+ return EntityKey (hex_value )
39+
40+
41+ def entity_key_to_bytes (entity_key : EntityKey ) -> bytes :
42+ return bytes .fromhex (entity_key [2 :]) # Strip '0x' prefix and convert to bytes
43+
44+
45+ def check_entity_key (entity_key : Any | None , label : str | None = None ) -> None :
46+ """Validates entity key."""
47+ prefix = ""
48+ if label :
49+ prefix = f"{ label } : "
50+
51+ logger .info (f"{ prefix } Checking entity key { entity_key } " )
52+
53+ if entity_key is None :
54+ raise EntityKeyException ("Entity key should not be None" )
55+ if not isinstance (entity_key , str ):
56+ raise EntityKeyException (
57+ f"Entity key type should be str but is: { type (entity_key )} "
58+ )
59+ if len (entity_key ) != 66 :
60+ raise EntityKeyException (
61+ f"Entity key should be 66 characters long (0x + 64 hex) but is: { len (entity_key )} "
62+ )
63+ if not is_hex_str (entity_key ):
64+ raise EntityKeyException ("Entity key should be a valid hex string" )
65+
66+
67+ def is_hex_str (value : str ) -> bool :
68+ if not isinstance (value , str ):
69+ return False
70+ if value .startswith ("0x" ):
71+ value = value [2 :]
72+ try :
73+ int (value , 16 )
74+ return True
75+ except ValueError :
76+ return False
77+
78+
3179def to_create_operation (
3280 payload : bytes | None = None ,
3381 annotations : dict [str , AnnotationValue ] | None = None ,
@@ -116,33 +164,36 @@ def to_receipt(
116164 event_args : dict [str , Any ] = event_data ["args" ]
117165 event_name = event_data ["event" ]
118166
167+ entity_key : EntityKey = to_entity_key (event_args ["entityKey" ])
168+ expiration_block : int = event_args ["expirationBlock" ]
169+
119170 match event_name :
120171 case contract .CREATED_EVENT :
121172 creates .append (
122173 CreateReceipt (
123- entity_key = EntityKey ( event_args [ "entityKey" ]) ,
124- expiration_block = int ( event_args [ "expirationBlock" ]) ,
174+ entity_key = entity_key ,
175+ expiration_block = expiration_block ,
125176 )
126177 )
127178 case contract .UPDATED_EVENT :
128179 updates .append (
129180 UpdateReceipt (
130- entity_key = EntityKey ( event_args [ "entityKey" ]) ,
131- expiration_block = int ( event_args [ "expirationBlock" ]) ,
181+ entity_key = entity_key ,
182+ expiration_block = expiration_block ,
132183 )
133184 )
134185 case contract .DELETED_EVENT :
135186 deletes .append (
136187 DeleteReceipt (
137- entity_key = EntityKey ( event_args [ "entityKey" ]) ,
188+ entity_key = entity_key ,
138189 )
139190 )
140191 case contract .EXTENDED_EVENT :
141192 extensions .append (
142193 ExtendReceipt (
143- entity_key = EntityKey ( event_args [ "entityKey" ]) ,
144- old_expiration_block = int ( event_args ["oldExpirationBlock" ]) ,
145- new_expiration_block = int ( event_args ["newExpirationBlock" ]) ,
194+ entity_key = entity_key ,
195+ old_expiration_block = event_args ["oldExpirationBlock" ],
196+ new_expiration_block = event_args ["newExpirationBlock" ],
146197 )
147198 )
148199 case _:
@@ -195,7 +246,7 @@ def format_annotation(annotation: Annotation) -> tuple[str, AnnotationValue]:
195246 # Update
196247 [
197248 [
198- element .entity_key . to_bytes ( ),
249+ entity_key_to_bytes ( element .entity_key ),
199250 element .btl ,
200251 element .data ,
201252 list (map (format_annotation , element .string_annotations )),
@@ -206,14 +257,14 @@ def format_annotation(annotation: Annotation) -> tuple[str, AnnotationValue]:
206257 # Delete
207258 [
208259 [
209- element .entity_key . to_bytes ( ),
260+ entity_key_to_bytes ( element .entity_key ),
210261 ]
211262 for element in tx .deletes
212263 ],
213264 # Extend
214265 [
215266 [
216- element .entity_key . to_bytes ( ),
267+ entity_key_to_bytes ( element .entity_key ),
217268 element .number_of_blocks ,
218269 ]
219270 for element in tx .extensions
0 commit comments