1
0

Merge pull request #9906 from spesmilo/maybe_complete_wallet_path

CLI: complete wallet_path
This commit is contained in:
ThomasV
2025-06-05 09:18:02 +02:00
committed by GitHub
5 changed files with 36 additions and 35 deletions

View File

@@ -97,6 +97,9 @@
* CLI: * CLI:
- The command line help has been improved; parameters are - The command line help has been improved; parameters are
documented in the same docstring as the command they belong to. documented in the same docstring as the command they belong to.
- If the --wallet parameter passed to a command is a simple filename,
it is now interpreted as relative to the users wallets directory,
rather than to the current working directory
- Plugins may add extra commands to the CLI. Plugin commands must - Plugins may add extra commands to the CLI. Plugin commands must
be prefixed with the plugin's internal name be prefixed with the plugin's internal name
- support for hold invoices - support for hold invoices

View File

@@ -163,14 +163,13 @@ def command(s):
password = kwargs.get('password') password = kwargs.get('password')
daemon = cmd_runner.daemon daemon = cmd_runner.daemon
if daemon: if daemon:
if 'wallet_path' in cmd.options and kwargs.get('wallet_path') is None: if 'wallet_path' in cmd.options or cmd.requires_wallet:
kwargs['wallet_path'] = daemon.config.get_wallet_path() kwargs['wallet_path'] = daemon.config.maybe_complete_wallet_path(kwargs.get('wallet_path'))
if cmd.requires_wallet and kwargs.get('wallet') is None:
kwargs['wallet'] = daemon.config.get_wallet_path()
if 'wallet' in cmd.options: if 'wallet' in cmd.options:
wallet = kwargs.get('wallet', None) wallet_path = kwargs.pop('wallet_path', None) # unit tests may set wallet and not wallet_path
if isinstance(wallet, str): wallet = kwargs.get('wallet', None) # run_offline_command sets both
wallet = daemon.get_wallet(wallet) if wallet is None:
wallet = daemon.get_wallet(wallet_path)
if wallet is None: if wallet is None:
raise UserFacingException('wallet not loaded') raise UserFacingException('wallet not loaded')
kwargs['wallet'] = wallet kwargs['wallet'] = wallet

View File

@@ -367,10 +367,7 @@ class CommandsServer(AuthenticatedServer):
wallet_path = config_options.get('wallet_path') wallet_path = config_options.get('wallet_path')
if len(self.daemon._wallets) > 1 and wallet_path is None: if len(self.daemon._wallets) > 1 and wallet_path is None:
raise UserFacingException("error: wallet not specified") raise UserFacingException("error: wallet not specified")
if 'wallet_path' in cmd.options: kwargs['wallet_path'] = wallet_path
kwargs['wallet_path'] = wallet_path
else:
kwargs['wallet'] = wallet_path
func = getattr(self.cmd_runner, cmd.name) func = getattr(self.cmd_runner, cmd.name)
# execute requested command now. note: cmd can raise, the caller (self.handle) will wrap it. # execute requested command now. note: cmd can raise, the caller (self.handle) will wrap it.
result = await func(*args, **kwargs) result = await func(*args, **kwargs)

View File

@@ -458,11 +458,20 @@ class SimpleConfig(Logger):
else: else:
return self.WALLET_BACKUP_DIRECTORY return self.WALLET_BACKUP_DIRECTORY
def maybe_complete_wallet_path(self, path: Optional[str]) -> str:
return self._complete_wallet_path(path) if path is not None else self.get_wallet_path()
def _complete_wallet_path(self, path: str) -> str:
""" add user wallets directory if needed """
if os.path.split(path)[0] == '':
path = os.path.join(self.get_datadir_wallet_path(), path)
return path
def get_wallet_path(self) -> str: def get_wallet_path(self) -> str:
"""Returns the wallet path.""" """Returns the wallet path."""
# command line -w option # command line -w option
if self.get('wallet_path'): if path:= self.get('wallet_path'):
return os.path.join(self.get('cwd', ''), self.get('wallet_path')) return self._complete_wallet_path(path)
# current wallet # current wallet
path = self.CURRENT_WALLET path = self.CURRENT_WALLET
if path and os.path.exists(path): if path and os.path.exists(path):

View File

@@ -189,50 +189,43 @@ class TestUnifiedPassword(DaemonTestCase):
class TestCommandsWithDaemon(DaemonTestCase): class TestCommandsWithDaemon(DaemonTestCase):
TESTNET = True TESTNET = True
SEED = "bitter grass shiver impose acquire brush forget axis eager alone wine silver"
async def test_wp_command_with_inmemory_wallet_has_password(self): async def test_wp_command_with_inmemory_wallet_has_password(self):
cmds = Commands(config=self.config, daemon=self.daemon) cmds = Commands(config=self.config, daemon=self.daemon)
wallet = restore_wallet_from_text('bitter grass shiver impose acquire brush forget axis eager alone wine silver', wallet = restore_wallet_from_text(self.SEED,
gap_limit=2, gap_limit=2,
path=None, path=None,
password="123456", password="123456",
config=self.config)['wallet'] config=self.config)['wallet']
self.assertEqual("bitter grass shiver impose acquire brush forget axis eager alone wine silver", self.assertEqual(self.SEED, await cmds.getseed(wallet=wallet, password="123456"))
await cmds.getseed(wallet=wallet, password="123456"))
async def test_wp_command_with_inmemory_wallet_no_password(self): async def test_wp_command_with_inmemory_wallet_no_password(self):
cmds = Commands(config=self.config, daemon=self.daemon) cmds = Commands(config=self.config, daemon=self.daemon)
wallet = restore_wallet_from_text('bitter grass shiver impose acquire brush forget axis eager alone wine silver', wallet = restore_wallet_from_text(self.SEED,
gap_limit=2, gap_limit=2,
path=None, path=None,
config=self.config)['wallet'] config=self.config)['wallet']
self.assertEqual("bitter grass shiver impose acquire brush forget axis eager alone wine silver", self.assertEqual(self.SEED, await cmds.getseed(wallet=wallet))
await cmds.getseed(wallet=wallet))
async def test_wp_command_with_diskfile_wallet_has_password(self): async def test_wp_command_with_diskfile_wallet_has_password(self):
cmds = Commands(config=self.config, daemon=self.daemon) cmds = Commands(config=self.config, daemon=self.daemon)
wpath = self._restore_wallet_from_text("bitter grass shiver impose acquire brush forget axis eager alone wine silver", password="123456", encrypt_file=True) wpath = self._restore_wallet_from_text(self.SEED, password="123456", encrypt_file=True)
basename = os.path.basename(wpath)
await cmds.load_wallet(wallet_path=wpath, password="123456") await cmds.load_wallet(wallet_path=wpath, password="123456")
wallet = self.daemon.get_wallet(wpath) wallet = self.daemon.get_wallet(wpath)
self.assertIsInstance(wallet, Abstract_Wallet) self.assertIsInstance(wallet, Abstract_Wallet)
self.assertEqual(self.SEED, await cmds.getseed(wallet_path=wpath, password="123456"))
# when using the CLI/RPC to run commands, the "wallet" param is a path: self.assertEqual(self.SEED, await cmds.getseed(wallet_path=basename, password='123456'))
self.assertEqual("bitter grass shiver impose acquire brush forget axis eager alone wine silver", self.assertEqual(self.SEED, await cmds.getseed(wallet=wallet, password="123456"))
await cmds.getseed(wallet=wpath, password="123456"))
# in unit tests or custom code, the "wallet" param is often an Abstract_Wallet:
self.assertEqual("bitter grass shiver impose acquire brush forget axis eager alone wine silver",
await cmds.getseed(wallet=wallet, password="123456"))
async def test_wp_command_with_diskfile_wallet_no_password(self): async def test_wp_command_with_diskfile_wallet_no_password(self):
cmds = Commands(config=self.config, daemon=self.daemon) cmds = Commands(config=self.config, daemon=self.daemon)
wpath = self._restore_wallet_from_text("bitter grass shiver impose acquire brush forget axis eager alone wine silver", password=None) wpath = self._restore_wallet_from_text(self.SEED, password=None)
basename = os.path.basename(wpath)
await cmds.load_wallet(wallet_path=wpath, password=None) await cmds.load_wallet(wallet_path=wpath, password=None)
wallet = self.daemon.get_wallet(wpath) wallet = self.daemon.get_wallet(wpath)
self.assertIsInstance(wallet, Abstract_Wallet) self.assertIsInstance(wallet, Abstract_Wallet)
self.assertEqual(self.SEED, await cmds.getseed(wallet_path=wpath))
# when using the CLI/RPC to run commands, the "wallet" param is a path: self.assertEqual(self.SEED, await cmds.getseed(wallet_path=basename))
self.assertEqual("bitter grass shiver impose acquire brush forget axis eager alone wine silver", self.assertEqual(self.SEED, await cmds.getseed(wallet=wallet))
await cmds.getseed(wallet=wpath))
# in unit tests or custom code, the "wallet" param is often an Abstract_Wallet:
self.assertEqual("bitter grass shiver impose acquire brush forget axis eager alone wine silver",
await cmds.getseed(wallet=wallet))