How to use the scalecodec.base.ScaleType function in scalecodec

To help you get started, we’ve selected a few scalecodec examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github polkascan / py-scale-codec / scalecodec / metadata.py View on Github external
call_module_index += 1

        for event_module_index, event_module in enumerate(self.events_modules):
            for event_index, event in enumerate(event_module.events):
                event.lookup = "{:02x}{:02x}".format(event_module_index, event_index)
                self.event_index[event.lookup] = (event_module, event)

        result_data["metadata"]["MetadataV0"]["outerEvent"]["events"] = [e.value for e in self.events_modules]
        result_data["metadata"]["MetadataV0"]["modules"] = [m.value for m in self.modules]
        result_data["metadata"]["MetadataV0"]["sections"] = [s.value for s in self.sections]

        return result_data


class MetadataV0EventModule(ScaleType):

    def __init__(self, data, sub_type=None, **kwargs):
        self.name = None
        self.events = None
        super().__init__(data, sub_type, **kwargs)

    def process(self):
        self.name = self.process_type('Bytes').value
        self.events = self.process_type('Vec').elements

        return {
            'name': self.name,
            'events': [s.value for s in self.events]
        }
github polkascan / py-scale-codec / scalecodec / metadata.py View on Github external
call.lookup = "{:02x}{:02x}".format(call_module_index, call_index)
                    self.call_index[call.lookup] = (module, call)
                call_module_index += 1

            if module.events is not None:
                for event_index, event in enumerate(module.events):
                    event.lookup = "{:02x}{:02x}".format(event_module_index, event_index)
                    self.event_index[event.lookup] = (module, event)
                event_module_index += 1

        result_data["metadata"]["MetadataV6"]["modules"] = [m.value for m in self.modules]

        return result_data


class MetadataV6Module(ScaleType):

    def __init__(self, data, sub_type=None, **kwargs):
        self.name = None
        self.prefix = None
        self.call_index = None
        self.has_storage = False
        self.storage = None
        self.has_calls = False
        self.calls = None
        self.has_events = False
        self.events = None
        self.constants = []
        self.errors = []
        super().__init__(data, sub_type, **kwargs)

    def get_identifier(self):
github polkascan / py-scale-codec / scalecodec / types.py View on Github external
if type(value) == str and value[0:2] == '0x' and len(value) == 66:
            # value is AccountId
            return ScaleBytes('0x{}'.format(value[2:]))
        elif type(value) == int:
            # value is AccountIndex
            raise NotImplementedError('Encoding of AccountIndex Adresses not supported yet')
        else:
            raise ValueError('Value is in unsupported format, expected 32 bytes hex-string for AccountIds or int for AccountIndex')


class RawAddress(GenericAddress):
    pass


class Enum(ScaleType):

    value_list = []
    type_mapping = None

    def __init__(self, data, value_list=None, type_mapping=None, **kwargs):

        self.index = None

        if type_mapping:
            self.type_mapping = type_mapping

        if value_list:
            self.value_list = value_list

        super().__init__(data, **kwargs)
github polkascan / py-scale-codec / scalecodec / types.py View on Github external
class U8(ScaleType):

    def process(self):
        return self.get_next_u8()

    def process_encode(self, value):

        if 0 <= int(value) <= 2**8 - 1:
            return ScaleBytes(bytearray(int(value).to_bytes(1, 'little')))
        else:
            raise ValueError('{} out of range for u8'.format(value))


class U16(ScaleType):

    def process(self):
        return int.from_bytes(self.get_next_bytes(2), byteorder='little')

    def process_encode(self, value):

        if 0 <= int(value) <= 2**16 - 1:
            return ScaleBytes(bytearray(int(value).to_bytes(2, 'little')))
        else:
            raise ValueError('{} out of range for u16'.format(value))


class U32(ScaleType):

    def process(self):
        return int.from_bytes(self.get_next_bytes(4), byteorder='little')
github polkascan / py-scale-codec / scalecodec / types.py View on Github external
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)
            result.append(element.value)

        return result
github polkascan / py-scale-codec / scalecodec / types.py View on Github external
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 __init__(self, data, **kwargs):
        self.call_index = None
        self.call_function = None
        self.call_module = None
        self.call_args = []
        super().__init__(data, **kwargs)

    def process(self):

        self.call_index = self.get_next_bytes(2).hex()

        self.call_module, self.call_function = self.metadata.call_index[self.call_index]

        for arg in self.call_function.args:
github polkascan / py-scale-codec / scalecodec / metadata.py View on Github external
def process(self):
        self.name = self.process_type('Bytes').value

        self.args = self.process_type('Vec').elements

        self.docs = self.process_type('Vec').value

        return {
            "name": self.name,
            "args": [a.value for a in self.args],
            "docs": self.docs
        }


class MetadataModuleCallArgument(ScaleType):

    def __init__(self, data, sub_type=None, **kwargs):
        self.name = None
        self.type = None

        super().__init__(data, sub_type, **kwargs)

    def process(self):
        self.name = self.process_type('Bytes').value
        self.type = self.convert_type(self.process_type('Bytes').value)

        return {
            "name": self.name,
            "type": self.type
        }
github polkascan / py-scale-codec / scalecodec / metadata.py View on Github external
}

        self.fallback = self.process_type('HexBytes').value

        self.docs = self.process_type('Vec').value

        return {
            "name": self.name,
            "modifier": self.modifier,
            "type": self.type,
            "default": self.fallback,
            "docs": self.docs
        }


class MetadataV0Section(ScaleType):

    def __init__(self, data, sub_type=None, **kwargs):
        self.name = None
        self.code = None
        self.id = None

        super().__init__(data, sub_type, **kwargs)

    def process(self):
        self.name = self.process_type('Bytes').value
        self.code = self.process_type('Bytes').value
        self.id = self.get_next_bytes(2).hex()

        return {
            "name": self.name,
            "code": self.code,
github polkascan / py-scale-codec / scalecodec / metadata.py View on Github external
}

        self.fallback = self.process_type('HexBytes').value

        self.docs = self.process_type('Vec').value

        return {
            "name": self.name,
            "modifier": self.modifier,
            "type": self.type,
            "fallback": self.fallback,
            "docs": self.docs
        }


class MetadataModuleCall(ScaleType):

    def __init__(self, data, sub_type=None, **kwargs):
        self.name = None
        self.args = []
        self.docs = []
        super().__init__(data, sub_type, **kwargs)

    def get_identifier(self):
        return self.name

    def process(self):
        self.name = self.process_type('Bytes').value

        self.args = self.process_type('Vec').elements

        self.docs = self.process_type('Vec').value
github polkascan / py-scale-codec / scalecodec / types.py View on Github external
class I128(ScaleType):

    def process(self):
        return int.from_bytes(self.get_next_bytes(16), byteorder='little', signed=True)

    def process_encode(self, value):

        if -2**128 <= int(value) <= 2**128-1:
            return ScaleBytes(bytearray(int(value).to_bytes(16, 'little', signed=True)))
        else:
            raise ValueError('{} out of range for i128'.format(value))


class I256(ScaleType):

    def process(self):
        return int.from_bytes(self.get_next_bytes(32), byteorder='little', signed=True)

    def process_encode(self, value):

        if -2**256 <= int(value) <= 2**256-1:
            return ScaleBytes(bytearray(int(value).to_bytes(32, 'little', signed=True)))
        else:
            raise ValueError('{} out of range for i256'.format(value))


class H160(ScaleType):

    def process(self):
        return '0x{}'.format(self.get_next_bytes(20).hex())