From a244674b8725309fa9b9320bb8cbfec1fab14990 Mon Sep 17 00:00:00 2001 From: The MMGen Project Date: Mon, 16 Nov 2026 10:45:27 +0000 Subject: [PATCH] OpWallet: open wallet in `main()` where feasible --- mmgen/xmrwallet/ops/create.py | 1 + mmgen/xmrwallet/ops/dump.py | 5 ++--- mmgen/xmrwallet/ops/export.py | 2 +- mmgen/xmrwallet/ops/import.py | 3 --- mmgen/xmrwallet/ops/new.py | 5 +++-- mmgen/xmrwallet/ops/restore.py | 3 ++- mmgen/xmrwallet/ops/sign.py | 2 -- mmgen/xmrwallet/ops/sync.py | 1 + mmgen/xmrwallet/ops/view.py | 2 +- mmgen/xmrwallet/ops/wallet.py | 11 +++++++---- 10 files changed, 18 insertions(+), 17 deletions(-) diff --git a/mmgen/xmrwallet/ops/create.py b/mmgen/xmrwallet/ops/create.py index e90e02f6..473c5c9c 100644 --- a/mmgen/xmrwallet/ops/create.py +++ b/mmgen/xmrwallet/ops/create.py @@ -20,6 +20,7 @@ from .wallet import OpWallet class OpCreate(OpWallet): stem = 'creat' opts = ('restore_height',) + main_opens_wallet = False def check_uopts(self): if self.cfg.restore_height != 'current' and int(self.cfg.restore_height or 0) < 0: diff --git a/mmgen/xmrwallet/ops/dump.py b/mmgen/xmrwallet/ops/dump.py index c8d42f1d..1bf057f1 100644 --- a/mmgen/xmrwallet/ops/dump.py +++ b/mmgen/xmrwallet/ops/dump.py @@ -18,9 +18,9 @@ from .wallet import OpWallet class OpDump(OpWallet): wallet_offline = True + wallet_desc = 'source' async def process_wallet(self, d, h, last): - h.open_wallet('source') wallet_data = h.get_wallet_data(print=False) msg('') MoneroWalletDumpFile.New( @@ -33,11 +33,10 @@ class OpDump(OpWallet): class OpDumpDataCommon(OpWallet): wallet_offline = True return_data = True + wallet_desc = 'source' async def process_wallet(self, d, h, last): - h.open_wallet('source', refresh=not self.wallet_offline) - if (height := self.c.call('get_height')['height']) > 1: from requests.exceptions import ConnectionError blockcount = height - 1 diff --git a/mmgen/xmrwallet/ops/export.py b/mmgen/xmrwallet/ops/export.py index a2e866e3..3b397117 100644 --- a/mmgen/xmrwallet/ops/export.py +++ b/mmgen/xmrwallet/ops/export.py @@ -19,10 +19,10 @@ from .wallet import OpWallet class OpExportOutputs(OpWallet): action = 'exporting outputs from' stem = 'process' + wallet_desc = 'source' sign = False async def process_wallet(self, d, h, last): - h.open_wallet('source') if self.cfg.rescan_blockchain: gmsg_r('\n Rescanning blockchain...') diff --git a/mmgen/xmrwallet/ops/import.py b/mmgen/xmrwallet/ops/import.py index 3d93bb15..1f80659e 100644 --- a/mmgen/xmrwallet/ops/import.py +++ b/mmgen/xmrwallet/ops/import.py @@ -24,8 +24,6 @@ class OpImportOutputs(OpWallet): main_starts_daemon = True async def process_wallet(self, d, h, last): - if self.need_daemon_start or len(self.kal.data) != 1: - h.open_wallet(refresh=False) self.head_msg(d.idx, h.fn) outputs_file = Path(self.uargs.spec) m = MoneroWalletOutputsFile.Unsigned(parent=self, fn=outputs_file) @@ -65,7 +63,6 @@ class OpImportKeyImages(OpWallet): if not keyimage_fn: msg(f'No signed key image file found for wallet #{d.idx}') return False - h.open_wallet() self.head_msg(d.idx, h.fn) m = MoneroWalletOutputsFile.Signed(parent=self, fn=keyimage_fn) data = m.data.signed_key_images or [] diff --git a/mmgen/xmrwallet/ops/new.py b/mmgen/xmrwallet/ops/new.py index 82cf02d7..3e9d05ef 100644 --- a/mmgen/xmrwallet/ops/new.py +++ b/mmgen/xmrwallet/ops/new.py @@ -24,10 +24,11 @@ class OpNew(OpMixinSpec, OpWallet): spec_id = 'newaddr_spec' spec_key = ((1, 'source'),) wallet_offline = True + wallet_desc = 'Monero' async def main(self, add_timestr=True): h = MoneroWalletRPC(self, self.source) - h.open_wallet('Monero', refresh=False) + h.open_wallet(self.wallet_desc, refresh=False) desc = 'account' if self.account is None else 'address' label = TwComment( @@ -65,7 +66,7 @@ class OpNew(OpMixinSpec, OpWallet): # wallet must be left open: otherwise the 'stop_wallet' RPC call used to stop the daemon will fail if self.cfg.no_stop_wallet_daemon: - h.close_wallet('Monero') + h.close_wallet(self.wallet_desc) msg('') return ret diff --git a/mmgen/xmrwallet/ops/restore.py b/mmgen/xmrwallet/ops/restore.py index 127fbe58..c707b948 100644 --- a/mmgen/xmrwallet/ops/restore.py +++ b/mmgen/xmrwallet/ops/restore.py @@ -18,6 +18,7 @@ from .create import OpCreate class OpRestore(OpCreate): wallet_offline = True + wallet_desc = 'newly created' def check_uopts(self): if self.cfg.restore_height is not None: @@ -86,7 +87,7 @@ class OpRestore(OpCreate): await super().process_wallet(d, h, last) - h.open_wallet('newly created') + h.open_wallet(self.wallet_desc) msg('') data = get_dump_data() diff --git a/mmgen/xmrwallet/ops/sign.py b/mmgen/xmrwallet/ops/sign.py index d290eac9..06d7bda3 100644 --- a/mmgen/xmrwallet/ops/sign.py +++ b/mmgen/xmrwallet/ops/sign.py @@ -130,8 +130,6 @@ class OpSign(OpWallet): do_err(f'sum of outputs ({osum}) does not match sum of inputs ({res["amount_in"]})') async def process_wallet(self, d, h, last): - if self.need_daemon_start or len(self.kal.data) != 1: - h.open_wallet(refresh=False) self.head_msg(d.idx, h.fn) tx = self.get_tx_cls('Unsigned')(self.cfg, Path(self.uargs.spec)) self.check_integrity( diff --git a/mmgen/xmrwallet/ops/sync.py b/mmgen/xmrwallet/ops/sync.py index fe0d338f..f220d5fe 100644 --- a/mmgen/xmrwallet/ops/sync.py +++ b/mmgen/xmrwallet/ops/sync.py @@ -20,6 +20,7 @@ from .wallet import OpWallet class OpSync(OpWallet): opts = ('rescan_blockchain', 'skip_empty_accounts', 'skip_empty_addresses') + main_opens_wallet = False def check_uopts(self): if self.cfg.rescan_blockchain and self.cfg.watch_only: diff --git a/mmgen/xmrwallet/ops/view.py b/mmgen/xmrwallet/ops/view.py index f67cf297..bfca8a28 100644 --- a/mmgen/xmrwallet/ops/view.py +++ b/mmgen/xmrwallet/ops/view.py @@ -40,13 +40,13 @@ class OpList(OpSync): class OpView(OpSync): stem = 'open' opts = () + main_opens_wallet = True wallet_offline = True def pre_init_action(self): ymsg('Running in offline mode. Balances may be out of date!') async def process_wallet(self, d, h, last): - h.open_wallet(refresh=False) wallet_height = self.c.call('get_height')['height'] msg(f' Wallet height: {wallet_height}') diff --git a/mmgen/xmrwallet/ops/wallet.py b/mmgen/xmrwallet/ops/wallet.py index 8486109e..a436c9be 100644 --- a/mmgen/xmrwallet/ops/wallet.py +++ b/mmgen/xmrwallet/ops/wallet.py @@ -33,11 +33,13 @@ class OpWallet(OpBase): 'no_stop_wallet_daemon', 'autosign', 'watch_only') + wallet_desc = None wallet_offline = False start_daemon = True skip_wallet_check = False # for debugging disable_authentication = False main_starts_daemon = False + main_opens_wallet = True def __init__(self, cfg, uarg_tuple): @@ -210,10 +212,11 @@ class OpWallet(OpBase): b = n + 1, c = len(self.addr_data), d = fn.name)) - data.append(await self.process_wallet( - d, - MoneroWalletRPC(self, d), - last = n == len(self.addr_data) - 1)) + + h = MoneroWalletRPC(self, d) + if self.main_opens_wallet: + h.open_wallet(self.wallet_desc, refresh=not self.wallet_offline) + data.append(await self.process_wallet(d, h, last=(n == len(self.addr_data) - 1))) if not self.compat_call: gmsg(f'\n{len(data)} wallet{suf(len(data))} {self.stem}ed\n') return data if self.return_data else sum(map(bool, data))