@@ -622,20 +622,23 @@ def decode_hex_identity_dict(info_dictionary) -> dict[str, Any]:
622
622
623
623
Examples:
624
624
input_dict = {
625
- ... "name": {"value": "0x6a6f686e"},
626
- ... "additional": [
627
- ... [{"data": "0x64617461"}]
628
- ... ]
629
- ... }
625
+ "name": {"value": "0x6a6f686e"},
626
+ "additional": [
627
+ {"data1": "0x64617461"},
628
+ ("data2", "0x64617461")
629
+ ]
630
+ }
630
631
decode_hex_identity_dict(input_dict)
631
- {'name': 'john', 'additional': [('data', 'data')]}
632
+ {'name': 'john', 'additional': [('data1', ' data'), ('data2 ', 'data')]}
632
633
"""
633
634
634
- def get_decoded (data : str ) -> str :
635
+ def get_decoded (data : Optional [ str ] ) -> str :
635
636
"""Decodes a hex-encoded string."""
637
+ if data is None :
638
+ return ""
636
639
try :
637
640
return hex_to_bytes (data ).decode ()
638
- except UnicodeDecodeError :
641
+ except ( UnicodeDecodeError , ValueError ) :
639
642
print (f"Could not decode: { key } : { item } " )
640
643
641
644
for key , value in info_dictionary .items ():
@@ -651,12 +654,14 @@ def get_decoded(data: str) -> str:
651
654
if key == "additional" :
652
655
additional = []
653
656
for item in value :
654
- additional .append (
655
- tuple (
656
- get_decoded (data = next (iter (sub_item .values ())))
657
- for sub_item in item
658
- )
659
- )
657
+ if isinstance (item , dict ):
658
+ for k , v in item .items ():
659
+ additional .append ((k , get_decoded (v )))
660
+ else :
661
+ if isinstance (item , (tuple , list )) and len (item ) == 2 :
662
+ k_ , v = item
663
+ k = k_ if k_ is not None else ""
664
+ additional .append ((k , get_decoded (v )))
660
665
info_dictionary [key ] = additional
661
666
662
667
return info_dictionary
0 commit comments