Skip to content

Commit

Permalink
Added WrapperKeepOpaque type
Browse files Browse the repository at this point in the history
  • Loading branch information
arjanz committed Jan 30, 2022
1 parent 964c33c commit bfd5539
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 0 deletions.
1 change: 1 addition & 0 deletions scalecodec/type_registry/metadata_types.json
Original file line number Diff line number Diff line change
Expand Up @@ -1570,6 +1570,7 @@
"pallet_identity::types::Data": "Data",
"frame_support::storage::bounded_vec::BoundedVec": "BoundedVec",
"frame_support::storage::weak_bounded_vec::WeakBoundedVec": "BoundedVec",
"frame_support::traits::misc::WrapperKeepOpaque": "WrapperKeepOpaque",
"*::Call": "GenericCall",
"*::Event": {
"type": "struct",
Expand Down
38 changes: 38 additions & 0 deletions scalecodec/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -1529,6 +1529,44 @@ def process(self):
return value


class WrapperKeepOpaque(Struct):

def process_encode(self, value):
# Check requirements
if not self.type_mapping or len(self.type_mapping) != 2:
raise ValueError("type_mapping not set correctly for this WrapperKeepOpaque")

wrapped_obj = self.runtime_config.create_scale_object(
type_string=self.type_mapping[1], metadata=self.metadata
)

wrapped_obj.encode(value)

bytes_obj = self.runtime_config.create_scale_object("Bytes")
return bytes_obj.encode(wrapped_obj.get_used_bytes())

def process(self):
# Check requirements
if not self.type_mapping or len(self.type_mapping) != 2:
raise ValueError("type_mapping not set correctly for this WrapperKeepOpaque")

# Get bytes
bytes_obj = self.process_type("Bytes")
try:
# Try to decode bytes with wrapped SCALE type
wrapped_obj = self.runtime_config.create_scale_object(
type_string=self.type_mapping[1],
data=ScaleBytes(bytes_obj.value_object),
metadata=self.metadata
)

return wrapped_obj.process()
except:
# Decoding failed; return Opaque type
self.value_object = bytes_obj.value_object
return f'0x{bytes_obj.value_object.hex()}'


class MultiAccountId(GenericAccountId):

@classmethod
Expand Down
50 changes: 50 additions & 0 deletions test/test_scale_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,56 @@ def test_opaque_call(self):
self.assertEqual(value['call_args'][0]['value'], '0x0123456789')
self.assertEqual(value['call_args'][0]['name'], '_remark')

def test_wrapped_opaque_decode_success(self):
opaque_hex = '0x1805000022db73'
wrapped_obj = self.runtime_config_v14.create_scale_object(
type_string="WrapperKeepOpaque",
metadata=self.metadata_v14_obj
)
wrapped_obj.type_mapping = ("Compact<u32>", "Call")
wrapped_obj.decode(ScaleBytes(opaque_hex))
self.assertEqual("Indices", wrapped_obj.value["call_module"])

def test_wrapped_opaque_decode_fail(self):
opaque_hex = '0x180a000022db73'
wrapped_obj = self.runtime_config_v14.create_scale_object(
type_string="WrapperKeepOpaque",
metadata=self.metadata_v14_obj
)
wrapped_obj.type_mapping = ("Compact<u32>", "Call")
wrapped_obj.decode(ScaleBytes(opaque_hex))
self.assertEqual(
"0x0a000022db73",
wrapped_obj.value
)

def test_wrapped_opaque_decode_incorrect(self):
opaque_hex = '0xa405000022db73'
wrapped_obj = self.runtime_config_v14.create_scale_object(
type_string="WrapperKeepOpaque",
metadata=self.metadata_v14_obj
)
with self.assertRaises(ValueError):
wrapped_obj.decode(ScaleBytes(opaque_hex))

def test_wrapped_opaque_encode(self):
wrapped_obj = self.runtime_config_v14.create_scale_object(
type_string="WrapperKeepOpaque",
metadata=self.metadata_v14_obj
)
wrapped_obj.type_mapping = ("Compact<u32>", "Call")

wrapped_obj.encode({
'call_function': 'claim',
'call_module': 'Indices',
'call_args': {'index': 1943740928}
})

self.assertEqual(
"0x1805000022db73",
wrapped_obj.data.to_hex()
)

def test_era_immortal(self):
obj = RuntimeConfiguration().create_scale_object('Era', ScaleBytes('0x00'))
obj.decode()
Expand Down

0 comments on commit bfd5539

Please sign in to comment.