def __get_peer_pool_map(self, peer_pubkey=None): contract_address = bytearray.fromhex(self.CONTRACT_ADDRESS) contract_address.reverse() view = self.__sdk.rpc.get_storage(contract_address.hex(), self.GOVERNANCE_VIEW.encode().hex()) stream = StreamManager.GetStream(bytearray.fromhex(view)) reader = BinaryReader(stream) governance_view = GovernanceView() governance_view.deserialize(reader) stream.close() stream2 = StreamManager.GetStream() writer = BinaryWriter(stream2) writer.write_int32(governance_view.view) view_bytes = stream2.ToArray() peer_pool_bytes = self.PEER_POOL.encode('utf-8') key_bytes = peer_pool_bytes + a2b_hex(view_bytes) value = self.__sdk.rpc.get_storage(contract_address.hex(), key_bytes.hex()) stream3 = StreamManager.GetStream(bytearray.fromhex(value)) reader2 = BinaryReader(stream3) length = reader2.read_int32() peer_pool_map = {} for i in range(length): item = PeerPoolItem() item.deserialize(reader2) peer_pool_map[item.peer_pubkey] = item.to_json() if peer_pubkey is not None: if peer_pubkey not in peer_pool_map: return None return peer_pool_map[peer_pubkey] return peer_pool_map
def push_bytes(data): ms = StreamManager.GetStream() writer = BinaryWriter(ms) if len(data) == 0: raise ValueError("push data error: data is null") if len(data) <= int.from_bytes(PUSHBYTES75, 'little') + 1 - int.from_bytes( PUSHBYTES1, 'little'): num = len(data) + int.from_bytes(PUSHBYTES1, 'little') - 1 writer.WriteByte(num) elif len(data) < 0x100: writer.WriteByte(PUSHDATA1) writer.WriteUInt8(len(data)) elif len(data) < 0x10000: writer.WriteByte(PUSHDATA2) writer.WriteUInt16(len(data)) else: writer.WriteByte(PUSHDATA4) writer.WriteUInt32(len(data)) writer.WriteBytes(data) ms.flush() res = ms.ToArray() StreamManager.ReleaseStream(ms) res = bytes_reader(res) return res
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 get_public_key_serialize(self): stream = StreamManager.get_stream() writer = BinaryWriter(stream) if self.__key_type == KeyType.ECDSA: writer.write_var_bytes(self.__public_key) else: raise SDKException(ErrorCode.unknown_asymmetric_key_type) stream.flush() bytes_stream = stream.hexlify() StreamManager.release_stream(stream) return bytes_stream
def serialize(self) -> bytes: ms = StreamManager.GetStream() writer = BinaryWriter(ms) writer.WriteBytes(self.serialize_unsigned()) writer.WriteVarInt(len(self.sigs)) for sig in self.sigs: writer.WriteBytes(sig.serialize()) ms.flush() temp = ms.ToArray() StreamManager.ReleaseStream(ms) return a2b_hex(temp)
def serialize(self) -> bytes: invoke_script = ProgramBuilder.program_from_params(self.sig_data) if len(self.public_keys) == 0: raise ValueError("no public key in sig") if len(self.public_keys) == 1: verification_script = ProgramBuilder.program_from_pubkey( self.public_keys[0]) else: verification_script = ProgramBuilder.program_from_multi_pubkey( self.M, self.public_keys) ms = StreamManager.GetStream() writer = BinaryWriter(ms) writer.WriteVarBytes(invoke_script) writer.WriteVarBytes(verification_script) ms.flush() res = ms.ToArray() res = bytes_reader(res) StreamManager.ReleaseStream(ms) return res
def serialize(self, is_hex: bool = False) -> bytes: ms = StreamManager.GetStream() writer = BinaryWriter(ms) writer.write_bytes(self.serialize_unsigned()) writer.write_var_int(len(self.sigs)) for sig in self.sigs: writer.write_bytes(sig.serialize()) ms.flush() temp = ms.ToArray() StreamManager.ReleaseStream(ms) if is_hex: return temp else: return a2b_hex(temp)
def serialize(self, is_hex: bool = False) -> bytes or str: ms = StreamManager.get_stream() writer = BinaryWriter(ms) writer.write_bytes(self.serialize_unsigned(is_str=False)) writer.write_var_int(len(self.sig_list)) for sig in self.sig_list: writer.write_bytes(sig.serialize()) ms.flush() bytes_tx = ms.to_bytes() StreamManager.release_stream(ms) if is_hex: return bytes_tx.hex() else: return bytes_tx
def serialize_unsigned(self) -> bytes: ms = StreamManager.GetStream() writer = BinaryWriter(ms) writer.WriteUInt8(self.version) writer.WriteUInt8(self.tx_type) writer.WriteUInt32(self.nonce) writer.WriteUInt64(self.gas_price) writer.WriteUInt64(self.gas_limit) writer.WriteBytes(bytes(self.payer)) writer.WriteVarBytes(bytes(self.payload)) writer.WriteVarInt(len(self.attributes)) ms.flush() res = ms.ToArray() StreamManager.ReleaseStream(ms) return res
def serialize(self) -> bytes: invoke_script = ProgramBuilder.program_from_params(self.__sig_data) if len(self.public_keys) == 0: raise SDKException(ErrorCode.other_error('Public key in sig is empty.')) if len(self.public_keys) == 1: verification_script = ProgramBuilder.program_from_pubkey(self.public_keys[0]) else: verification_script = ProgramBuilder.program_from_multi_pubkey(self.m, self.public_keys) ms = StreamManager.get_stream() writer = BinaryWriter(ms) writer.write_var_bytes(invoke_script) writer.write_var_bytes(verification_script) ms.flush() res = ms.to_bytes() res = bytes_reader(res) StreamManager.release_stream(ms) return res
def runtime_serialize(self, config: Config, engine: ExecutionEngine): t = PushData.pop_stack_item(engine) stream = StreamManager.GetStream() writer = BinaryWriter(stream) self.__serialize_stack_item(t, writer) PushData.push_data(engine, stream.ToArray())
def serialize_exclusive_data(self, writer: BinaryWriter): writer.write_var_bytes(self.code)
def serialize_unsigned(self) -> bytes: ms = StreamManager.GetStream() writer = BinaryWriter(ms) writer.write_uint8(self.version) writer.write_uint8(self.tx_type) writer.write_uint32(self.nonce) writer.write_uint64(self.gas_price) writer.write_uint64(self.gas_limit) writer.write_bytes(bytes(self.payer)) self.serialize_exclusive_data(writer) if hasattr(self, "payload"): writer.write_var_bytes(bytes(self.payload)) writer.write_var_int(len(self.attributes)) ms.flush() res = ms.ToArray() StreamManager.ReleaseStream(ms) return res
def serialize_unsigned(self, is_str: bool = False) -> bytes or str: ms = StreamManager.get_stream() writer = BinaryWriter(ms) writer.write_uint8(self.version) writer.write_uint8(self.tx_type) writer.write_uint32(self.nonce) writer.write_uint64(self.gas_price) writer.write_uint64(self.gas_limit) writer.write_bytes(self.payer) self.serialize_exclusive_data(writer) if self.payload is not None and len(self.payload) != 0: writer.write_var_bytes(bytes(self.payload)) writer.write_var_int(len(self.attributes)) ms.flush() hex_bytes = ms.to_bytes() StreamManager.release_stream(ms) if is_str: return bytes.hex(hex_bytes) else: return hex_bytes
def __serialize_stack_item(self, item: StackItems, writer: BinaryWriter): if type(item) == ByteArrayItem: writer.write_byte(BuildParams.Type.bytearraytype.value) bys = item.get_bytearray() writer.write_var_bytes(bys) elif type(item) == IntegerItem: writer.write_byte(BuildParams.Type.integertype.value) bys = item.get_bytearray() writer.write_var_bytes(bys) elif type(item) == BoolItem: writer.write_byte(BuildParams.Type.booltype.value) bys = item.get_bytearray() writer.write_var_bytes(bys) elif type(item) == ArrayItem: writer.write_byte(BuildParams.Type.arraytype.value) arr = item.get_array() writer.write_var_int(len(arr)) for i in range(len(arr)): self.__serialize_stack_item(item, writer) elif type(item) == StructItem: pass elif type(item) == MapItem: writer.write_byte(BuildParams.Type.maptype.value) map = item.get_map() writer.write_var_int(len(map)) for key, value in map.items(): self.__serialize_stack_item(key, writer) self.__serialize_stack_item(value, writer) else: pass
def serialize_exclusive_data(self, writer: BinaryWriter): writer.write_var_bytes(self.code) writer.write_bool(self.need_storage) writer.write_var_str(self.name) writer.write_var_str(self.code_version) writer.write_var_str(self.author) writer.write_var_str(self.email) writer.write_var_str(self.description)
def serialize_exclusive_data(self, writer: BinaryWriter): writer.write_var_bytes(self.__code) writer.write_byte(self.__vm_type.value) writer.write_var_str(self.__name) writer.write_var_str(self.__code_version) writer.write_var_str(self.__author) writer.write_var_str(self.__email) writer.write_var_str(self.__description)