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_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)
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.to_bytes() StreamManager.release_stream(ms) res = bytes_reader(res) return res