def push_bytes(data): ms = StreamManager.get_stream() 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.write_byte(num) elif len(data) < 0x100: writer.write_byte(PUSHDATA1) writer.write_uint8(len(data)) elif len(data) < 0x10000: writer.write_byte(PUSHDATA2) writer.write_uint16(len(data)) else: writer.write_byte(PUSHDATA4) writer.write_uint32(len(data)) writer.write_bytes(data) ms.flush() res = ms.hexlify() StreamManager.release_stream(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, is_hex: bool = False) -> bytes or str: ms = StreamManager.get_stream() writer = BinaryWriter(ms) writer.write_bytes(self.serialize_unsigned()) 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 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) return hex_bytes
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.hexlify() res = bytes_reader(res) StreamManager.release_stream(ms) return res
def setUp(self): self.stream = StreamManager.get_stream() self.writer = BinaryWriter(self.stream)