Browse Source

Fix CLI. Some commands require wallet_path. Return error on exception.

dependabot/pip/contrib/deterministic-build/ecdsa-0.13.3
ThomasV 5 years ago
parent
commit
35b0b3a43c
  1. 57
      electrum/commands.py
  2. 14
      electrum/daemon.py
  3. 4
      run_electrum

57
electrum/commands.py

@ -95,21 +95,30 @@ def command(s):
name = func.__name__ name = func.__name__
known_commands[name] = Command(func, s) known_commands[name] = Command(func, s)
@wraps(func) @wraps(func)
def func_wrapper(*args, **kwargs): async def func_wrapper(*args, **kwargs):
c = known_commands[func.__name__] cmd_runner = args[0]
cmd = known_commands[func.__name__]
password = kwargs.get('password') password = kwargs.get('password')
wallet = kwargs.get('wallet') # wallet is passed here if we are offline daemon = cmd_runner.daemon
if c.requires_wallet and wallet is None: if daemon:
cmd_runner = args[0] if (cmd.requires_wallet or 'wallet_path' in cmd.options) and kwargs.get('wallet_path') is None:
path = cmd_runner.config.get_wallet_path() kwargs['wallet_path'] = daemon.config.get_wallet_path()
path = standardize_path(path) if cmd.requires_wallet:
wallet = cmd_runner.daemon.wallets.get(path) wallet_path = kwargs.pop('wallet_path')
if wallet is None: wallet = daemon.wallets.get(wallet_path)
raise Exception("wallet not loaded. Use 'electrum load_wallet'") if wallet is None:
kwargs['wallet'] = wallet return {'error': "Wallet not loaded. try 'electrum load_wallet'" }
if c.requires_password and password is None and wallet.has_password(): kwargs['wallet'] = wallet
else:
# we are offline. the wallet must have been passed
pass
if cmd.requires_password and password is None and wallet.has_password():
return {'error': 'Password required' } return {'error': 'Password required' }
return func(*args, **kwargs) try:
return await func(*args, **kwargs)
except Exception as e:
#traceback.print_exc(sys.stderr)
return {'error':str(e)}
return func_wrapper return func_wrapper
return decorator return decorator
@ -182,27 +191,25 @@ class Commands:
return [{'path':k, 'synchronized':w.is_up_to_date()} for k, w in self.daemon.wallets.items()] return [{'path':k, 'synchronized':w.is_up_to_date()} for k, w in self.daemon.wallets.items()]
@command('n') @command('n')
async def load_wallet(self): async def load_wallet(self, wallet_path=None):
"""Open wallet in daemon""" """Open wallet in daemon"""
path = self.config.get_wallet_path() wallet = self.daemon.load_wallet(wallet_path, self.config.get('password'))
wallet = self.daemon.load_wallet(path, self.config.get('password'))
if wallet is not None: if wallet is not None:
run_hook('load_wallet', wallet, None) run_hook('load_wallet', wallet, None)
response = wallet is not None response = wallet is not None
return response return response
@command('n') @command('n')
async def close_wallet(self): async def close_wallet(self, wallet_path=None):
"""Close wallet""" """Close wallet"""
path = self.config.get_wallet_path() return self.daemon.stop_wallet(wallet_path)
return self.daemon.stop_wallet(path)
@command('') @command('')
async def create(self, passphrase=None, password=None, encrypt_file=True, seed_type=None): async def create(self, passphrase=None, password=None, encrypt_file=True, seed_type=None, wallet_path=None):
"""Create a new wallet. """Create a new wallet.
If you want to be prompted for an argument, type '?' or ':' (concealed) If you want to be prompted for an argument, type '?' or ':' (concealed)
""" """
d = create_new_wallet(path=self.config.get_wallet_path(), d = create_new_wallet(path=wallet_path,
passphrase=passphrase, passphrase=passphrase,
password=password, password=password,
encrypt_file=encrypt_file, encrypt_file=encrypt_file,
@ -214,7 +221,7 @@ class Commands:
} }
@command('') @command('')
async def restore(self, text, passphrase=None, password=None, encrypt_file=True): async def restore(self, text, passphrase=None, password=None, encrypt_file=True, wallet_path=None):
"""Restore a wallet from text. Text can be a seed phrase, a master """Restore a wallet from text. Text can be a seed phrase, a master
public key, a master private key, a list of bitcoin addresses public key, a master private key, a list of bitcoin addresses
or bitcoin private keys. or bitcoin private keys.
@ -222,7 +229,7 @@ class Commands:
""" """
# TODO create a separate command that blocks until wallet is synced # TODO create a separate command that blocks until wallet is synced
d = restore_wallet_from_text(text, d = restore_wallet_from_text(text,
path=self.config.get_wallet_path(), path=wallet_path,
passphrase=passphrase, passphrase=passphrase,
password=password, password=password,
encrypt_file=encrypt_file) encrypt_file=encrypt_file)
@ -1148,7 +1155,7 @@ def get_parser():
p = subparsers.add_parser(cmdname, help=cmd.help, description=cmd.description) p = subparsers.add_parser(cmdname, help=cmd.help, description=cmd.description)
add_global_options(p) add_global_options(p)
for optname, default in zip(cmd.options, cmd.defaults): for optname, default in zip(cmd.options, cmd.defaults):
if optname == 'wallet': if optname in ['wallet_path', 'wallet']:
continue continue
a, help = command_options[optname] a, help = command_options[optname]
b = '--' + optname b = '--' + optname
@ -1161,7 +1168,7 @@ def get_parser():
p.add_argument(*args, dest=optname, action=action, default=default, help=help) p.add_argument(*args, dest=optname, action=action, default=default, help=help)
for param in cmd.params: for param in cmd.params:
if param == 'wallet': if param in ['wallet_path', 'wallet']:
continue continue
h = param_descriptions.get(param, '') h = param_descriptions.get(param, '')
_type = arg_types.get(param, str) _type = arg_types.get(param, str)

14
electrum/daemon.py

@ -432,13 +432,6 @@ class Daemon(Logger):
config.mempool_fees = self.network.config.mempool_fees.copy() config.mempool_fees = self.network.config.mempool_fees.copy()
cmdname = config.get('cmd') cmdname = config.get('cmd')
cmd = known_commands[cmdname] cmd = known_commands[cmdname]
if cmd.requires_wallet:
path = config.get_wallet_path()
path = standardize_path(path)
wallet = self.wallets.get(path)
if wallet is None:
return {'error': 'Wallet "%s" is not loaded. Use "electrum load_wallet"'%os.path.basename(path) }
config_options['wallet'] = wallet
# arguments passed to function # arguments passed to function
args = map(lambda x: config.get(x), cmd.params) args = map(lambda x: config.get(x), cmd.params)
# decode json arguments # decode json arguments
@ -446,12 +439,9 @@ class Daemon(Logger):
# options # options
kwargs = {} kwargs = {}
for x in cmd.options: for x in cmd.options:
kwargs[x] = (config_options.get(x) if x in ['wallet', 'password', 'new_password'] else config.get(x)) kwargs[x] = (config_options.get(x) if x in ['password', 'new_password'] else config.get(x))
func = getattr(self.cmd_runner, cmd.name) func = getattr(self.cmd_runner, cmd.name)
try: result = await func(*args, **kwargs)
result = await func(*args, **kwargs)
except TypeError as e:
raise Exception("Wrapping TypeError to prevent JSONRPC-Pelix from hiding traceback") from e
return result return result
def run_daemon(self): def run_daemon(self):

4
run_electrum

@ -203,6 +203,8 @@ async def run_offline_command(config, config_options, plugins):
cmdname = config.get('cmd') cmdname = config.get('cmd')
cmd = known_commands[cmdname] cmd = known_commands[cmdname]
password = config_options.get('password') password = config_options.get('password')
if 'wallet_path' in cmd.options and config_options.get('wallet_path') is None:
config_options['wallet_path'] = config.get_wallet_path()
if cmd.requires_wallet: if cmd.requires_wallet:
storage = WalletStorage(config.get_wallet_path()) storage = WalletStorage(config.get_wallet_path())
if storage.is_encrypted(): if storage.is_encrypted():
@ -231,7 +233,7 @@ async def run_offline_command(config, config_options, plugins):
# options # options
kwargs = {} kwargs = {}
for x in cmd.options: for x in cmd.options:
kwargs[x] = (config_options.get(x) if x in ['wallet', 'password', 'new_password'] else config.get(x)) kwargs[x] = (config_options.get(x) if x in ['wallet_path', 'wallet', 'password', 'new_password'] else config.get(x))
cmd_runner = Commands(config=config) cmd_runner = Commands(config=config)
func = getattr(cmd_runner, cmd.name) func = getattr(cmd_runner, cmd.name)
result = await func(*args, **kwargs) result = await func(*args, **kwargs)

Loading…
Cancel
Save