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)
Example #2
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
Example #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()