From fa5f3e7e5539998e087552ea5c999999d2ad8b2b Mon Sep 17 00:00:00 2001 From: zeppi Date: Fri, 22 Jul 2022 15:37:15 -0400 Subject: [PATCH] change api for data first, password optional, return (str) --- lbry/extras/daemon/daemon.py | 61 +++++++++---------- .../blockchain/test_wallet_commands.py | 8 +-- 2 files changed, 33 insertions(+), 36 deletions(-) diff --git a/lbry/extras/daemon/daemon.py b/lbry/extras/daemon/daemon.py index ee621920e..c0760627c 100644 --- a/lbry/extras/daemon/daemon.py +++ b/lbry/extras/daemon/daemon.py @@ -1384,69 +1384,66 @@ class Daemon(metaclass=JSONRPCServerType): return wallet @requires("wallet") - async def jsonrpc_wallet_export(self, password, wallet_id=None): + async def jsonrpc_wallet_export(self, password=None, wallet_id=None): """ Export wallet data Wallet must be unlocked to perform this operation. Usage: - wallet_export [--wallet_id=] + wallet_export [--password=] [--wallet_id=] Options: --password= : (str) password to decrypt incoming and encrypt outgoing data - --wallet_id= : (str) wallet being sync'ed + --wallet_id= : (str) wallet being exported Returns: - (map) sync hash and data + (str) data """ + assert password is not None, "passwordless use is not implemented yet" wallet = self.wallet_manager.get_wallet_or_default(wallet_id) encrypted = wallet.pack(password) - return { - 'hash': self.jsonrpc_sync_hash(wallet_id), - 'data': encrypted.decode() - } - + return encrypted.decode() @requires("wallet") - async def jsonrpc_wallet_import(self, password, data, wallet_id=None, blocking=False): + async def jsonrpc_wallet_import(self, data, password=None, wallet_id=None, blocking=False): """ Import wallet data and merge accounts, preferences. Wallet must be unlocked to perform this operation. Usage: - wallet_import ( | --data=) [--wallet_id=] [--blocking] + wallet_import ( | --data=) [ | --password=] [--wallet_id=] [--blocking] Options: - --password= : (str) password to decrypt incoming and encrypt outgoing data --data= : (str) incoming wallet data + --password= : (str) password to decrypt incoming and encrypt outgoing data --wallet_id= : (str) wallet being merged into --blocking : (bool) wait until any new accounts have merged Returns: - (map) sync hash and data - + (str) data """ - wallet = self.wallet_manager.get_wallet_or_default(wallet_id) - added_accounts = wallet.merge(self.wallet_manager, password, data) - for new_account in added_accounts: - await new_account.maybe_migrate_certificates() - if added_accounts and self.ledger.network.is_connected: - if blocking: - await asyncio.wait([ - a.ledger.subscribe_account(a) for a in added_accounts - ]) - else: - for new_account in added_accounts: - asyncio.create_task(self.ledger.subscribe_account(new_account)) - wallet.save() - encrypted = wallet.pack(password) - return { - 'hash': self.jsonrpc_sync_hash(wallet_id), - 'data': encrypted.decode() - } + + if data.strip().startswith("{"): + print("passwordless use is not implemented yet") + else: + wallet = self.wallet_manager.get_wallet_or_default(wallet_id) + added_accounts = wallet.merge(self.wallet_manager, password, data) + for new_account in added_accounts: + await new_account.maybe_migrate_certificates() + if added_accounts and self.ledger.network.is_connected: + if blocking: + await asyncio.wait([ + a.ledger.subscribe_account(a) for a in added_accounts + ]) + else: + for new_account in added_accounts: + asyncio.create_task(self.ledger.subscribe_account(new_account)) + wallet.save() + encrypted = wallet.pack(password) + return encrypted.decode() @requires("wallet") async def jsonrpc_wallet_add(self, wallet_id): diff --git a/tests/integration/blockchain/test_wallet_commands.py b/tests/integration/blockchain/test_wallet_commands.py index 7130a705a..49de37a0c 100644 --- a/tests/integration/blockchain/test_wallet_commands.py +++ b/tests/integration/blockchain/test_wallet_commands.py @@ -442,8 +442,8 @@ class WalletEncryptionAndSynchronization(CommandTestCase): self.assertItemCount(await daemon.jsonrpc_account_list(), 1) - data = await daemon2.jsonrpc_wallet_export('password') - await daemon.jsonrpc_wallet_import('password', data=data['data'], blocking=True) + data = await daemon2.jsonrpc_wallet_export(password='password') + await daemon.jsonrpc_wallet_import(data=data, password='password', blocking=True) self.assertItemCount(await daemon.jsonrpc_account_list(), 2) self.assertDictEqual( @@ -473,8 +473,8 @@ class WalletEncryptionAndSynchronization(CommandTestCase): self.assertItemCount(await daemon2.jsonrpc_channel_list(), 1) self.assertEqual(len(daemon2.wallet_manager.default_account.channel_keys), 1) - data = await daemon2.jsonrpc_wallet_export('password') - await daemon.jsonrpc_wallet_import('password', data=data['data'], blocking=True) + data = await daemon2.jsonrpc_wallet_export(password='password') + await daemon.jsonrpc_wallet_import(data=data, password='password', blocking=True) # both daemons have the cert after sync'ing self.assertEqual(