@@ -655,7 +655,18 @@ def deserialize(self, value: str) -> str:
655
655
return value
656
656
657
657
658
+ class ArrayObject (ScaleType ):
659
+
660
+ def to_bytes (self ) -> bytes :
661
+ if self .type_def .type_def is not U8 :
662
+ raise ScaleDeserializeException ('Only an Array of U8 can be represented as bytes' )
663
+ return self .value_object
664
+
665
+
658
666
class Array (ScaleTypeDef ):
667
+
668
+ scale_type_cls = ArrayObject
669
+
659
670
def __init__ (self , type_def : ScaleTypeDef , length : int ):
660
671
self .type_def = type_def
661
672
self .length = length
@@ -715,12 +726,27 @@ def serialize(self, value: Union[list, bytes]) -> Union[list, str]:
715
726
return f'0x{ value .hex ()} '
716
727
717
728
def deserialize (self , value : Union [list , str , bytes ]) -> Union [list , bytes ]:
729
+
730
+ if type (value ) not in [list , str , bytes ]:
731
+ raise ScaleDeserializeException ('value should be of type list, str or bytes' )
732
+
718
733
if type (value ) is str :
719
734
if value [0 :2 ] == '0x' :
720
- return bytes .fromhex (value [2 :])
735
+ value = bytes .fromhex (value [2 :])
721
736
else :
722
- return value .encode ()
723
- else :
737
+ value = value .encode ()
738
+
739
+ if len (value ) != self .length :
740
+ raise ScaleDeserializeException ('Length of array does not match size of value' )
741
+
742
+ if type (value ) is bytes :
743
+ if self .type_def is not U8 :
744
+ raise ScaleDeserializeException ('Only an Array of U8 can be represented as (hex)bytes' )
745
+
746
+ return value
747
+
748
+ if type (value ) is list :
749
+
724
750
value_object = []
725
751
726
752
for item in value :
@@ -793,7 +819,13 @@ def decode(self, data: ScaleBytes) -> list:
793
819
return value
794
820
795
821
def serialize (self , value : list ) -> list :
796
- return [(k .value_serialized , v .value_serialized ) for k , v in value ]
822
+ output = []
823
+ for k , v in value :
824
+ if type (k ) is ScaleType and type (v ) is ScaleType :
825
+ output .append ((k .value_serialized , v .value_serialized ))
826
+ else :
827
+ output .append ((k , v ))
828
+ return output
797
829
798
830
def deserialize (self , value : list ) -> list :
799
831
return [(self .key_def .deserialize (k ), self .value_def .deserialize (v )) for k , v in value ]
@@ -833,12 +865,20 @@ def decode(self, data: ScaleBytes) -> bytearray:
833
865
def serialize (self , value : bytearray ) -> str :
834
866
return f'0x{ value .hex ()} '
835
867
836
- def deserialize (self , value : str ) -> bytearray :
837
- if type (value ) is str :
868
+ def deserialize (self , value : Union [bytes , str , list ]) -> bytes :
869
+
870
+ if type (value ) in (list , bytearray ):
871
+ value = bytes (value )
872
+
873
+ elif type (value ) is str :
838
874
if value [0 :2 ] == '0x' :
839
- value = bytearray .fromhex (value [2 :])
875
+ value = bytes .fromhex (value [2 :])
840
876
else :
841
877
value = value .encode ('utf-8' )
878
+
879
+ if type (value ) is not bytes :
880
+ raise ScaleDeserializeException (f'Cannot deserialize type "{ type (value )} "' )
881
+
842
882
return value
843
883
844
884
def example_value (self , _recursion_level : int = 0 , max_recursion : int = TYPE_DECOMP_MAX_RECURSIVE ):
@@ -860,13 +900,19 @@ def serialize(self, value: str) -> str:
860
900
def deserialize (self , value : str ) -> str :
861
901
return value
862
902
863
-
864
903
def create_example (self , _recursion_level : int = 0 ):
865
904
return 'String'
866
905
867
906
907
+ class HashDefObject (ScaleType ):
908
+ def to_bytes (self ) -> bytes :
909
+ return self .value_object
910
+
911
+
868
912
class HashDef (ScaleTypeDef ):
869
913
914
+ scale_type_cls = HashDefObject
915
+
870
916
def __init__ (self , bits : int ):
871
917
super ().__init__ ()
872
918
self .bits = bits
@@ -897,6 +943,10 @@ def serialize(self, value: bytes) -> str:
897
943
def deserialize (self , value : Union [str , bytes ]) -> bytes :
898
944
if type (value ) is str :
899
945
value = bytes .fromhex (value [2 :])
946
+
947
+ if type (value ) is not bytes :
948
+ raise ScaleDeserializeException ('value should be of type str or bytes' )
949
+
900
950
return value
901
951
902
952
def example_value (self , _recursion_level : int = 0 , max_recursion : int = TYPE_DECOMP_MAX_RECURSIVE ):
0 commit comments