Merge pull request #3692 from lbryio/fix-import/export-backwards-compat

fix backwards compatibility in wallet import/export
This commit is contained in:
jessopb 2022-10-18 11:16:34 -04:00 committed by GitHub
commit 20ae51b949
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 14 additions and 6 deletions

View file

@ -1346,9 +1346,9 @@ class Daemon(metaclass=JSONRPCServerType):
""" """
wallet = self.wallet_manager.get_wallet_or_default(wallet_id) wallet = self.wallet_manager.get_wallet_or_default(wallet_id)
if password: if password is None:
return wallet.pack(password).decode() return wallet.to_json()
return wallet.to_json() return wallet.pack(password).decode()
@requires("wallet") @requires("wallet")
async def jsonrpc_wallet_import(self, data, password=None, wallet_id=None, blocking=False): async def jsonrpc_wallet_import(self, data, password=None, wallet_id=None, blocking=False):

View file

@ -189,10 +189,10 @@ class Wallet:
password: str, data: str) -> (List['Account'], List['Account']): password: str, data: str) -> (List['Account'], List['Account']):
assert not self.is_locked, "Cannot sync apply on a locked wallet." assert not self.is_locked, "Cannot sync apply on a locked wallet."
added_accounts, merged_accounts = [], [] added_accounts, merged_accounts = [], []
if password: if password is None:
decrypted_data = self.unpack(password, data)
else:
decrypted_data = json.loads(data) decrypted_data = json.loads(data)
else:
decrypted_data = self.unpack(password, data)
self.preferences.merge(decrypted_data.get('preferences', {})) self.preferences.merge(decrypted_data.get('preferences', {}))
for account_dict in decrypted_data['accounts']: for account_dict in decrypted_data['accounts']:
ledger = manager.get_or_create_ledger(account_dict['ledger']) ledger = manager.get_or_create_ledger(account_dict['ledger'])

View file

@ -501,3 +501,11 @@ class WalletEncryptionAndSynchronization(CommandTestCase):
json_data["preferences"]["four"] = {"value": 4, "ts": 0} json_data["preferences"]["four"] = {"value": 4, "ts": 0}
await daemon.jsonrpc_wallet_import(data=json.dumps(json_data), blocking=True) await daemon.jsonrpc_wallet_import(data=json.dumps(json_data), blocking=True)
self.assertEqual(daemon.jsonrpc_preference_get("four"), {"four": 4}) self.assertEqual(daemon.jsonrpc_preference_get("four"), {"four": 4})
# if password is empty string, export is encrypted
data = await daemon2.jsonrpc_wallet_export(password="")
self.assertNotEqual(data[0], "{")
# if password is empty string, import is decrypted
await daemon.jsonrpc_wallet_import(data, password="")