Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Fixed length array's U16/U32/U128/U256 #110

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions scalecodec/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -1957,6 +1957,22 @@ def process_encode(self, value):

else:

if type(value) is str:
if value[0:2] != '0x':
raise ValueError('Give the value is not from 0x')
elif self.runtime_config.get_decoder_class(self.sub_type) is U16 and len(value[2:]) != self.element_count * 4:
raise ValueError('Value should be {} bytes long'.format(self.element_count))
elif self.runtime_config.get_decoder_class(self.sub_type) is U32 and len(value[2:]) != self.element_count * 8:
raise ValueError('Value should be {} bytes long'.format(self.element_count))
elif self.runtime_config.get_decoder_class(self.sub_type) is U64 and len(value[2:]) != self.element_count * 16:
raise ValueError('Value should be {} bytes long'.format(self.element_count))
elif self.runtime_config.get_decoder_class(self.sub_type) is U128 and len(value[2:]) != self.element_count * 32:
raise ValueError('Value should be {} bytes long'.format(self.element_count))
elif self.runtime_config.get_decoder_class(self.sub_type) is U256 and len(value[2:]) != self.element_count * 64:
raise ValueError('Value should be {} bytes long'.format(self.element_count))
else:
return ScaleBytes(value)

if not type(value) is list:
raise ValueError('Given value is not a list')

Expand Down
15 changes: 15 additions & 0 deletions test/test_scale_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,9 @@ def test_dynamic_fixed_array_type_decode(self):
obj = RuntimeConfiguration().create_scale_object('[u32; 0]', data=ScaleBytes(bytes()))
self.assertEqual([], obj.decode())

obj = RuntimeConfiguration().create_scale_object('[u64; 3]', data=ScaleBytes("0x010000000000000002000000000000000300000000000000"))
self.assertEqual([1, 2, 3], obj.decode())

def test_dynamic_fixed_array_type_decode_u8(self):
obj = RuntimeConfiguration().create_scale_object('[u8; 65]', data=ScaleBytes("0xc42b82d02bce3202f6a05d4b06d1ad46963d3be36fd0528bbe90e7f7a4e5fcd38d14234b1c9fcee920d76cfcf43b4ed5dd718e357c2bc1aae3a642975207e67f01"))
self.assertEqual('0xc42b82d02bce3202f6a05d4b06d1ad46963d3be36fd0528bbe90e7f7a4e5fcd38d14234b1c9fcee920d76cfcf43b4ed5dd718e357c2bc1aae3a642975207e67f01', obj.decode())
Expand All @@ -381,13 +384,25 @@ def test_dynamic_fixed_array_type_encode(self):
obj = RuntimeConfiguration().create_scale_object('[u8; 3]')
self.assertEqual('0x010203', str(obj.encode('0x010203')))

obj = RuntimeConfiguration().create_scale_object('[u64; 1]')
self.assertEqual('0x01000000000000000200000000000000', str(obj.encode([1, 2])))

obj = RuntimeConfiguration().create_scale_object('[u64; 3]')
self.assertEqual('0x010000000000000002000000000000000300000000000000', str(obj.encode([1, 2, 3])))

obj = RuntimeConfiguration().create_scale_object('[u64; 3]')
self.assertEqual('0x010000000000000002000000000000000300000000000000', str(obj.encode('0x010000000000000002000000000000000300000000000000')))

def test_invalid_fixed_array_type_encode(self):
obj = RuntimeConfiguration().create_scale_object('[u8; 3]')
self.assertRaises(ValueError, obj.encode, '0x0102')

obj = RuntimeConfiguration().create_scale_object('[u32; 3]')
self.assertRaises(ValueError, obj.encode, '0x0102')

obj = RuntimeConfiguration().create_scale_object('[u64; 3]')
self.assertRaises(ValueError, obj.encode, '0x0102')

def test_custom_tuple(self):
obj = RuntimeConfiguration().create_scale_object('(u8,u8)', ScaleBytes("0x0102"))
self.assertEqual((1, 2), obj.decode())
Expand Down