diff --git a/scalecodec/types.py b/scalecodec/types.py index 368286b..0290256 100644 --- a/scalecodec/types.py +++ b/scalecodec/types.py @@ -147,12 +147,18 @@ class Option(ScaleType): def process(self): option_byte = self.get_next_bytes(1) - - if self.sub_type and option_byte != b'\x00': + if self.sub_type is None: + raise ValueError("Plain 'Option' without type parameter is not valid as a type") + if option_byte == b'\x01': self.value_object = self.process_type(self.sub_type) return self.value_object.value + elif option_byte == b'\x00': + return None + else: + raise InvalidScaleTypeValueException( + f"Invalid starting byte for 'Option': '0x{option_byte.hex()}'" + ) - return None def process_encode(self, value): diff --git a/test/test_type_encoding.py b/test/test_type_encoding.py index dedd2ac..03dfc00 100644 --- a/test/test_type_encoding.py +++ b/test/test_type_encoding.py @@ -17,6 +17,7 @@ import unittest from scalecodec.base import ScaleBytes, ScaleDecoder, RuntimeConfiguration +from scalecodec.exceptions import InvalidScaleTypeValueException from scalecodec.type_registry import load_type_registry_preset, load_type_registry_file from scalecodec.types import CompactU32, Struct @@ -335,6 +336,13 @@ def test_option_bytes_encode_decode(self): self.assertEqual(obj_check.decode(), value) + def test_option_decode_invalid(self): + obj = RuntimeConfiguration().create_scale_object('Option', ScaleBytes("0x0200")) + with self.assertRaisesRegex( + InvalidScaleTypeValueException, r"^Invalid starting byte for 'Option': '0x02'$" + ): + obj.decode() + def test_proposal_encode_decode(self): value = {