def __deserialize_stack_item(reader: BinaryReader) -> dict or bytearray: param_type = reader.read_byte() if param_type == BuildParams.Type.bytearray_type.value: b = reader.read_var_bytes() return b elif param_type == BuildParams.Type.bool_type.value: return reader.read_bool() elif param_type == BuildParams.Type.int_type.value: b = reader.read_var_bytes() return ContractDataParser.__big_int_from_bytes(bytearray(b)) elif param_type == BuildParams.Type.struct_type.value or param_type == BuildParams.Type.array_type.value: count = reader.read_var_int() item_list = list() for _ in range(count): item = ContractDataParser.__deserialize_stack_item(reader) item_list.append(item) if param_type == BuildParams.Type.struct_type.value: return Struct(item_list) return item_list elif param_type == BuildParams.Type.dict_type.value: count = reader.read_var_int() item_dict = dict() for _ in range(count): key = ContractDataParser.__deserialize_stack_item(reader) value = ContractDataParser.__deserialize_stack_item(reader) item_dict[key] = value return item_dict else: raise SDKException(ErrorCode.other_error('type error'))
def deserialize_stack_item(reader: BinaryReader) -> dict: t = reader.read_byte() if t == BuildParams.Type.bytearray_type.value: b = reader.read_var_bytes() return b elif t == BuildParams.Type.bool_type.value: return reader.read_bool() elif t == BuildParams.Type.int_type.value: b = reader.read_var_bytes() return bigint_from_bytes(b) elif t == BuildParams.Type.struct_type.value or t == BuildParams.Type.array_type.value: count = reader.read_var_int() # TODO item_list = list() for _ in range(count): item = deserialize_stack_item(reader) item_list.append(item) if t == t == BuildParams.Type.struct_type.value: return Struct(item_list) return item_list elif t == BuildParams.Type.dict_type.value: count = reader.read_var_int() item_dict = dict() for _ in range(count): key = deserialize_stack_item(reader) value = deserialize_stack_item(reader) item_dict[key] = value return item_dict else: raise SDKException(ErrorCode.other_error('type error'))
def test_read_var_int(self): value = 123 writer_stream = StreamManager.get_stream() writer = BinaryWriter(writer_stream) writer.write_var_int(value) reader_stream = StreamManager.get_stream(writer_stream.getbuffer()) reader = BinaryReader(reader_stream) self.assertEqual(reader.read_var_int(), value)
def deserialize_from(txbytes: bytes): ms = StreamManager.GetStream(txbytes) reader = BinaryReader(ms) tx = Transaction() tx.version = reader.read_uint8() tx.tx_type = reader.read_uint8() tx.nonce = reader.read_uint32() tx.gas_price = reader.read_uint64() tx.gas_limit = reader.read_uint64() tx.payer = reader.read_bytes(20) tx.payload = reader.read_var_bytes() attri_len = reader.read_var_int() if attri_len is 0: tx.attributes = bytearray() sigs_len = reader.read_var_int() tx.sigs = [] for i in range(sigs_len): tx.sigs.append(Sig.deserialize(reader)) return tx
def deserialize_from(bytes_tx: bytes): ms = StreamManager.get_stream(bytes_tx) reader = BinaryReader(ms) tx = Transaction() tx.version = reader.read_uint8() tx.tx_type = reader.read_uint8() tx.nonce = reader.read_uint32() tx.gas_price = reader.read_uint64() tx.gas_limit = reader.read_uint64() tx.payer = reader.read_bytes(20) tx.payload = reader.read_var_bytes() attribute_len = reader.read_var_int() if attribute_len is 0: tx.attributes = bytearray() sig_len = reader.read_var_int() tx.sig_list = list() for _ in range(0, sig_len): tx.sig_list.append(Sig.deserialize(reader)) return tx
class VmReader(object): def __init__(self, bys: bytearray): self.ms = StreamManager.GetStream(bys) self.reader = BinaryReader(self.ms) self.code = bys def read_byte(self): return self.reader.read_byte() def read_bool(self): b = self.reader.read_byte() return b == 1 def read_bytes(self, count: int): return self.reader.read_bytes(count) def read_var_bytes(self): return self.reader.read_var_bytes() def read_uint16(self): return self.read_uint16() def read_uint32(self): return self.reader.read_int32() def read_uint64(self): return self.reader.read_int64() def position(self): return self.reader.stream.tell() def read_int16(self): try: res = bytearray(self.reader.read_bytes(2)) res.reverse() return int(res.hex(), 16) except Exception as e: return 0 def read_var_int(self): return self.reader.read_var_int() def seek(self, offset: int): return self.reader.stream.seek(offset)