Exemplo n.º 1
0
 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)
Exemplo n.º 2
0
 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
Exemplo n.º 3
0
class TestBinaryWriter(unittest.TestCase):
    def setUp(self):
        self.stream = StreamManager.get_stream()
        self.writer = BinaryWriter(self.stream)

    def test_write_byte(self):
        values = [15, 255, 'a', 'z', b'a', 'byte']
        results = [b'0f', b'ff', b'61', b'7a', b'61', b'62']
        for i, v in enumerate(values):
            self.writer.write_byte(v)
            self.assertEqual(results[i], self.stream.hexlify())
            self.stream.clean_up()

    def test_write_uint8(self):
        values = [15, 255, 15, 255]
        results = [b'0f', b'ff', b'0f', b'ff']
        little_endian_lst = [True, True, False, False]
        for i, v in enumerate(values):
            self.writer.write_uint8(v, little_endian=little_endian_lst[i])
            self.assertEqual(results[i], self.stream.hexlify())
            self.stream.clean_up()
Exemplo n.º 4
0
 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
Exemplo n.º 5
0
    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 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)
Exemplo n.º 7
0
 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
Exemplo n.º 8
0
 def setUp(self):
     self.stream = StreamManager.get_stream()
     self.writer = BinaryWriter(self.stream)
Exemplo n.º 9
0
 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