From e4e1600f51ec28ba65eb06bedf0fb035d040d1c1 Mon Sep 17 00:00:00 2001 From: zeppi Date: Mon, 10 Oct 2022 18:09:24 -0400 Subject: [PATCH] Enable unencrypted wallet import and export --- lbry/extras/daemon/daemon.py | 6 ++++-- lbry/wallet/wallet.py | 10 +++++++--- .../blockchain/test_wallet_commands.py | 18 ++++++++++++++++++ 3 files changed, 29 insertions(+), 5 deletions(-) diff --git a/lbry/extras/daemon/daemon.py b/lbry/extras/daemon/daemon.py index d3b34fe3d..79de2d203 100644 --- a/lbry/extras/daemon/daemon.py +++ b/lbry/extras/daemon/daemon.py @@ -1345,8 +1345,10 @@ class Daemon(metaclass=JSONRPCServerType): (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) + if (password is None): + return wallet.pack() encrypted = wallet.pack(password) return encrypted.decode() @@ -1370,7 +1372,7 @@ class Daemon(metaclass=JSONRPCServerType): Returns: (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) added_accounts, merged_accounts = wallet.merge(self.wallet_manager, password, data) for new_account in itertools.chain(added_accounts, merged_accounts): diff --git a/lbry/wallet/wallet.py b/lbry/wallet/wallet.py index c3c2e8098..6c9ecb7b1 100644 --- a/lbry/wallet/wallet.py +++ b/lbry/wallet/wallet.py @@ -163,15 +163,19 @@ class Wallet: h.update(account.hash) 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." new_data = json.dumps(self.to_dict()) + if password is None: + return new_data.encode() new_data_compressed = zlib.compress(new_data.encode()) return better_aes_encrypt(password, new_data_compressed) @classmethod - def unpack(cls, password, encrypted): - decrypted = better_aes_decrypt(password, encrypted) + def unpack(cls, password, data): + if password is None: + return json.loads(data) + decrypted = better_aes_decrypt(password, data) try: decompressed = zlib.decompress(decrypted) except zlib.error as e: diff --git a/tests/integration/blockchain/test_wallet_commands.py b/tests/integration/blockchain/test_wallet_commands.py index 9bf8e9827..192fa9200 100644 --- a/tests/integration/blockchain/test_wallet_commands.py +++ b/tests/integration/blockchain/test_wallet_commands.py @@ -491,3 +491,21 @@ class WalletEncryptionAndSynchronization(CommandTestCase): daemon2.wallet_manager.default_account.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"] + } + ) +