Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_compact_u32_encode_decode_large(self):
value = 2**30
obj = CompactU32(ScaleBytes(bytearray()))
data = obj.encode(value)
obj = CompactU32(data)
self.assertEqual(obj.decode(), value)
def test_compact_u32_encode_decode_large(self):
value = 2**30
obj = CompactU32(ScaleBytes(bytearray()))
data = obj.encode(value)
obj = CompactU32(data)
self.assertEqual(obj.decode(), value)
def test_compact_u32_encode_decode(self):
value = 2000001
obj = ScaleDecoder.get_decoder_class('Compact')
data = obj.encode(value)
obj = CompactU32(data)
self.assertEqual(obj.decode(), value)
def process_encode(self, value):
string_length_compact = CompactU32()
if value[0:2] == '0x':
# TODO implicit HexBytes conversion can have unexpected result if string is actually starting with '0x'
value = bytes.fromhex(value[2:])
data = string_length_compact.encode(len(value))
data += value
else:
data = string_length_compact.encode(len(value))
data += value.encode()
return data
from scalecodec.utils.ss58 import ss58_decode
self.ss58_address = value
value = '0x{}'.format(ss58_decode(value))
return super().process_encode(value)
class GenericAccountIndex(U32):
pass
class KeyValue(Struct):
type_string = '(Vec, Vec)'
type_mapping = (('key', 'Vec'), ('value', 'Vec'))
class NewAccountOutcome(CompactU32):
type_string = 'NewAccountOutcome'
class Vec(ScaleType):
def __init__(self, data=None, **kwargs):
self.elements = []
super().__init__(data, **kwargs)
def process(self):
element_count = self.process_type('Compact').value
result = []
for _ in range(0, element_count):
element = self.process_type(self.sub_type, metadata=self.metadata)
self.elements.append(element)
class Bool(ScaleType):
def process(self):
return self.get_next_bool()
def process_encode(self, value):
if value is True:
return ScaleBytes('0x01')
elif value is False:
return ScaleBytes('0x00')
else:
raise ValueError("Value must be boolean")
class CompactMoment(CompactU32):
type_string = 'Compact'
def process(self):
int_value = super().process()
if int_value > 10000000000:
int_value = int_value / 1000
return datetime.utcfromtimestamp(int_value)
def serialize(self):
return self.value.isoformat()
class BoxProposal(ScaleType):
type_string = 'Box'
def process_encode(self, value):
if type(value) is not list:
raise ValueError("Provided value is not a list")
# encode element count to Compact
element_count_compact = CompactU32()
element_count_compact.encode(len(value))
data = element_count_compact.data
for element in value:
element_obj = self.get_decoder_class(self.sub_type, metadata=self.metadata)
data += element_obj.encode(element)
return data