async def load_keys_data(data, password): """ Loads wallet keys file passed as byte string :param file: :param password: :return: """ reader = xmrserialize.MemoryReaderWriter(bytearray(data)) ar = xmrserialize.Archive(reader, False) msg = xmrtypes.KeysFileData() await ar.message(msg) key = chacha.generate_key(password) buff = bytes(msg.iv + msg.account_data) dec = chacha.decrypt(key, buff) m = re.search(b'(.*)"key_data":"(.+?)",(.*)', dec) key_data = m.group(2) dat = xmrjson.unescape_json_str(key_data) reader = xmrserialize.MemoryReaderWriter(bytearray(dat)) ar = xmrrpc.Archive(reader, False) key_data = {} await ar.root() await ar.section(key_data) rest_json = m.group(1) + m.group(3) wallet_key = json.loads(rest_json) wallet_key["key_data"] = key_data return wallet_key
async def gen_keys_file(password, wkeyfile): """ Generates wallet keys file as bytestring :param password: :param wkeyfile: :return: """ key_data = wkeyfile.key_data # type: WalletKeyData js = wkeyfile.to_json() del js["key_data"] # encode wallet key file wth classical json encoder, key data added later with monero encoding. enc = json.dumps(js, cls=xmrjson.AutoJSONEncoder) # key_data KV serialization. Message -> Model. modeler = xmrrpc.Modeler(writing=True, modelize=True) mdl = await modeler.message(msg=key_data) # Model -> binary writer = xmrserialize.MemoryReaderWriter() ar = xmrrpc.Archive(writer, True) await ar.root() await ar.section(mdl) ser = bytes(writer.get_buffer()) ser2 = xmrjson.escape_string_json(ser) enc2 = b'{"key_data":"' + ser2 + b'",' + enc[1:].encode("utf8") key = chacha.generate_key(password) enc_enc = chacha.encrypt(key, enc2) writer = xmrserialize.MemoryReaderWriter() ar = xmrserialize.Archive(writer, True) msg = xmrtypes.KeysFileData() msg.iv = enc_enc[0:8] msg.account_data = enc_enc[8:] await ar.message(msg) return bytes(writer.get_buffer())