def encode(self, value): if value is not None and self.sub_type: sub_type_obj = self.get_decoder_class(self.sub_type) return ScaleBytes('0x01') + sub_type_obj.encode(value) return ScaleBytes('0x00')
def encode(self, value): if 0 <= value <= 2 ** 64 - 1: self.data = ScaleBytes(bytearray(int(value).to_bytes(8, 'little'))) else: raise ValueError('{} out of range for u64'.format(value)) return self.data
def encode(self, value): # Check requirements if 'call_index' in value: self.call_index = value['call_index'] elif 'call_module' in value and 'call_function' in value: # Look up call module from metadata for call_index, (call_module, call_function) in self.metadata.call_index.items(): if call_module.name == value['call_module'] and call_function.name == value['call_function']: self.call_index = call_index self.call_module = call_module self.call_function = call_function break if not self.call_index: raise ValueError('Specified call module and function not found in metadata') elif not self.call_module or not self.call_function: raise ValueError('No call module and function specified') data = ScaleBytes(bytearray.fromhex(self.call_index)) # Encode call params if len(self.call_function.args) > 0: for arg in self.call_function.args: if arg.name not in value['call_args']: raise ValueError('Parameter \'{}\' not specified'.format(arg.name)) else: param_value = value['call_args'][arg.name] arg_obj = self.get_decoder_class(arg.type, metadata=self.metadata) data += arg_obj.encode(param_value) return data
def process(self): result = {} for key, data_type in self.type_mapping: result[key] = self.process_type(data_type, metadata=self.metadata).value # Replace HexBytes with actual proposal result['proposal'] = Proposal(ScaleBytes(result['proposal']), metadata=self.metadata).decode() return result
def generate_hash(self): if self.contains_transaction: if self.extrinsic_length: extrinsic_data = self.data.data else: # Fallback for legacy version, prefix additional Compact<u32> with length extrinsic_length_type = CompactU32(ScaleBytes(bytearray())) extrinsic_length_type.encode(self.data.length) extrinsic_data = extrinsic_length_type.data.data + self.data.data return blake2b(extrinsic_data, digest_size=32).digest().hex() else: return None
def encode(self, value: int): if value <= 0b00111111: self.data = ScaleBytes(bytearray(int(value << 2).to_bytes(1, 'little'))) elif value <= 0b0011111111111111: self.data = ScaleBytes(bytearray(int((value << 2) | 0b01).to_bytes(2, 'little'))) elif value <= 0b00111111111111111111111111111111: self.data = ScaleBytes(bytearray(int((value << 2) | 0b10).to_bytes(4, 'little'))) else: for bytes_length in range(4, 68): if 2 ** (8 * (bytes_length - 1)) <= value < 2 ** (8 * bytes_length): self.data = ScaleBytes(bytearray( ((bytes_length - 4) << 2 | 0b11).to_bytes(1, 'little') + value.to_bytes(bytes_length, 'little'))) break else: raise ValueError('{} out of range'.format(value)) return self.data
def process(self): self.process_compact_bytes() if self.sub_type: byte_data = self.get_decoder_class(self.sub_type, ScaleBytes(self.compact_bytes)).process() # TODO Assumptions if (type(byte_data) is str and byte_data.isdigit() or type(byte_data) is int) and self.compact_length <= 4: return int(int(byte_data) / 4) else: # TODO raise exception? return byte_data else: return self.compact_bytes