From 3d8fbca67eeebbb371caf3d0b290c51bac2af01e Mon Sep 17 00:00:00 2001 From: The MMGen Project Date: Mon, 5 Oct 2026 14:30:05 +0000 Subject: [PATCH] TwAddresses.get_change_address(): support full-list matching --- mmgen/tw/addresses.py | 37 +++++++++++++++++++++---------------- mmgen/tx/new.py | 18 +++++++----------- mmgen/tx/new_swap.py | 7 +------ 3 files changed, 29 insertions(+), 33 deletions(-) diff --git a/mmgen/tw/addresses.py b/mmgen/tw/addresses.py index 8404f461..b3ccd875 100755 --- a/mmgen/tw/addresses.py +++ b/mmgen/tw/addresses.py @@ -35,6 +35,7 @@ class TwAddresses(TwView): showused = 1 # tristate: 0: no, 1: yes, 2: only all_labels = False autochg_ignore_labels = False # if set, this overrides the global cfg var of the same name + autochg_match_full_list = False mod_subpath = 'tw.addresses' prompt_fs_in = [ @@ -354,29 +355,33 @@ class TwAddresses(TwView): desc = None): """ - Get lowest-indexed unused address in tracking wallet for requested AddrListID. + Get lowest-indexed unused address in tracking wallet matching requested key, if key is + given, else entire tracking wallet. + Return values on failure: - None: no addresses in wallet with requested AddrListID - False: no unused addresses in wallet with requested AddrListID + None: no addresses in wallet matching requested key + False: no unused addresses in wallet matching requested key, if key is given, else + no unused addresses in entire wallet """ assert self.sort_key == 'twmmid' assert self.reverse is False - from ..algo import get_start - target = self.get_target(key) - start = get_start( - data = self.data, - bot = 0 if bot is None else bot, - top = len(self.data) - 1 if top is None else top, - target = target, - bisect_key_func = self.get_bisect_key, - match_key_func = self.get_match_key) - if start is None: - return None + if key: + from ..algo import get_start + target = self.get_target(key) + start = get_start( + data = self.data, + bot = 0 if bot is None else bot, + top = len(self.data) - 1 if top is None else top, + target = target, + bisect_key_func = self.get_bisect_key, + match_key_func = self.get_match_key) + if start is None: + return None - for d in self.data[start:]: - if self.get_match_key(d) == target: + for d in (self.data[start:] if key else self.data): + if (not key) or self.get_match_key(d) == target: if ( not d.is_used and not d.twmmid in exclude diff --git a/mmgen/tx/new.py b/mmgen/tx/new.py index 206d0509..513e4fcd 100755 --- a/mmgen/tx/new.py +++ b/mmgen/tx/new.py @@ -206,19 +206,15 @@ class New(Base): return self._cl_output(arg, mmid or None, coin_addr, amt, None, is_vault) - async def get_autochg_addr( - self, - proto, - arg, - *, - exclude, - desc, - match_all_addrtypes = False): + async def get_autochg_addr(self, proto, arg, *, exclude, desc): from ..tw.addresses import TwAddresses al = await TwAddresses(self.cfg, proto, get_data=True) - if match_all_addrtypes: + if arg is None and al.autochg_match_full_list: + res = al.get_change_address(None, exclude=exclude, desc=desc) + req_desc = None + elif arg is None: res = al.get_change_address_by_addrtype(None, exclude=exclude, desc=desc) req_desc = 'of any allowed address type' elif obj := get_obj(MMGenAddrType, proto=proto, id_str=arg, silent=True): @@ -231,9 +227,9 @@ class New(Base): if res: return res - die(2, 'Tracking wallet contains no {t}addresses {d}'.format( + die(2, 'Tracking wallet contains no {t}addresses{d}'.format( t = '' if res is None else 'unused ', - d = req_desc)) + d = ' ' + req_desc if req_desc else '')) async def process_cmdline_args(self, cmd_args, ad_f, ad_w): diff --git a/mmgen/tx/new_swap.py b/mmgen/tx/new_swap.py index 3e1be722..50d86db2 100755 --- a/mmgen/tx/new_swap.py +++ b/mmgen/tx/new_swap.py @@ -53,12 +53,7 @@ class NewSwap(New): return ret(proto.coin, proto.network, pa.addr, pa.mmid) full_desc = f'{desc} address on the {proto.coin} {proto.network} network' - res = await self.get_autochg_addr( - proto, - arg, - exclude = [], - desc = full_desc, - match_all_addrtypes = not arg) + res = await self.get_autochg_addr(proto, arg, exclude=[], desc=full_desc) self.confirm_autoselected_addr(res.twmmid, res.comment, full_desc) return ret(proto.coin, proto.network, res.addr, res.twmmid)