Enable unencrypted wallet import and export

This commit is contained in:
zeppi 2022-10-10 18:09:24 -04:00 committed by Lex Berezhny
parent d0aad8ccaf
commit e4e1600f51
3 changed files with 29 additions and 5 deletions

View file

@ -1345,8 +1345,10 @@ class Daemon(metaclass=JSONRPCServerType):
(str) data (str) data
""" """
assert password is not None, "passwordless use is not implemented yet" # assert password is not None, "passwordless use is not implemented yet"
wallet = self.wallet_manager.get_wallet_or_default(wallet_id) wallet = self.wallet_manager.get_wallet_or_default(wallet_id)
if (password is None):
return wallet.pack()
encrypted = wallet.pack(password) encrypted = wallet.pack(password)
return encrypted.decode() return encrypted.decode()
@ -1370,7 +1372,7 @@ class Daemon(metaclass=JSONRPCServerType):
Returns: Returns:
(str) data (str) data
""" """
assert not data.strip().startswith("{"), "unencrypted wallet import is not implemented yet" # assert not data.strip().startswith("{"), "unencrypted wallet import is not implemented yet"
wallet = self.wallet_manager.get_wallet_or_default(wallet_id) wallet = self.wallet_manager.get_wallet_or_default(wallet_id)
added_accounts, merged_accounts = wallet.merge(self.wallet_manager, password, data) added_accounts, merged_accounts = wallet.merge(self.wallet_manager, password, data)
for new_account in itertools.chain(added_accounts, merged_accounts): for new_account in itertools.chain(added_accounts, merged_accounts):

View file

@ -163,15 +163,19 @@ class Wallet:
h.update(account.hash) h.update(account.hash)
return h.digest() return h.digest()
def pack(self, password): def pack(self, password=None):
assert not self.is_locked, "Cannot pack a wallet with locked/encrypted accounts." assert not self.is_locked, "Cannot pack a wallet with locked/encrypted accounts."
new_data = json.dumps(self.to_dict()) new_data = json.dumps(self.to_dict())
if password is None:
return new_data.encode()
new_data_compressed = zlib.compress(new_data.encode()) new_data_compressed = zlib.compress(new_data.encode())
return better_aes_encrypt(password, new_data_compressed) return better_aes_encrypt(password, new_data_compressed)
@classmethod @classmethod
def unpack(cls, password, encrypted): def unpack(cls, password, data):
decrypted = better_aes_decrypt(password, encrypted) if password is None:
return json.loads(data)
decrypted = better_aes_decrypt(password, data)
try: try:
decompressed = zlib.decompress(decrypted) decompressed = zlib.decompress(decrypted)
except zlib.error as e: except zlib.error as e:

View file

@ -491,3 +491,21 @@ class WalletEncryptionAndSynchronization(CommandTestCase):
daemon2.wallet_manager.default_account.channel_keys, daemon2.wallet_manager.default_account.channel_keys,
daemon.wallet_manager.default_wallet.accounts[1].channel_keys daemon.wallet_manager.default_wallet.accounts[1].channel_keys
) )
# test without passwords
daemon2.jsonrpc_preference_set("three", "3")
jsondata = await daemon2.jsonrpc_wallet_export()
await daemon.jsonrpc_wallet_import(data=jsondata, blocking=True)
self.assertDictEqual(
# "two" key added and "conflict" value changed to "2"
daemon.jsonrpc_preference_get(),
{
"one": "1",
"two": "2",
"three": "3",
"conflict": "2",
"another": "B",
"fruit": ["peach", "apricot"]
}
)