tx.new: get_autochg_addr(): add desc param

This commit is contained in:
The MMGen Project 2025-02-15 09:54:19 +00:00
commit 9631433072
Signed by: mmgen
GPG key ID: 3F8B1861E32B7DA2
2 changed files with 18 additions and 17 deletions

View file

@ -274,7 +274,7 @@ class TwAddresses(TwView):
return bool(e.recvd)
return None # addr not in tracking wallet
def get_change_address(self, al_id, bot=None, top=None, exclude=None):
def get_change_address(self, al_id, bot=None, top=None, exclude=None, desc=None):
"""
Get lowest-indexed unused address in tracking wallet for requested AddrListID.
Return values on failure:
@ -326,14 +326,14 @@ class TwAddresses(TwView):
d.twmmid.hl(),
yellow('has a label,'),
d.comment.hl2(encl='‘’'),
yellow(',\n but allowing it for change anyway by user request')
yellow(f',\n but allowing it for {desc} anyway by user request')
))
return d
else:
break
return False
def get_change_address_by_addrtype(self, mmtype, exclude=None):
def get_change_address_by_addrtype(self, mmtype, exclude, desc):
"""
Find the lowest-indexed change addresses in tracking wallet of given address type,
present them in a menu and return a single change address chosen by the user.
@ -352,9 +352,9 @@ class TwAddresses(TwView):
c = yellow(' <== has a label!') if d.comment else ''
)
prompt = '\nChoose a change address:\n\n{}\n\nEnter a number> '.format(
'\n'.join(format_line(n, d) for n, d in enumerate(addrs, 1))
)
prompt = '\nChoose a {desc}:\n\n{items}\n\nEnter a number> '.format(
desc = desc,
items = '\n'.join(format_line(n, d) for n, d in enumerate(addrs, 1)))
from ..ui import line_input
while True:

View file

@ -194,25 +194,23 @@ class New(Base):
return _pa(arg, mmid, coin_addr, amt, None)
async def get_autochg_addr(self, proto, arg, parsed_args):
async def get_autochg_addr(self, proto, arg, exclude, desc):
from ..tw.addresses import TwAddresses
al = await TwAddresses(self.cfg, proto, get_data=True)
exclude = [a.mmid for a in parsed_args if a.mmid]
if is_mmgen_addrtype(proto, arg):
res = al.get_change_address_by_addrtype(MMGenAddrType(proto, arg), exclude=exclude)
desc = 'of address type'
if obj := get_obj(MMGenAddrType, proto=proto, id_str=arg, silent=True):
res = al.get_change_address_by_addrtype(obj, exclude=exclude, desc=desc)
req_desc = f'of address type {arg!r}'
else:
res = al.get_change_address(arg, exclude=exclude)
desc = 'from address list'
res = al.get_change_address(arg, exclude=exclude, desc=desc)
req_desc = f'from address list {arg!r}'
if res:
return res
die(2, 'Tracking wallet contains no {t}addresses {d} {a!r}'.format(
die(2, 'Tracking wallet contains no {t}addresses {d}'.format(
t = '' if res is None else 'unused ',
d = desc,
a = arg))
d = req_desc))
async def process_cmdline_args(self, cmd_args, ad_f, ad_w):
@ -228,8 +226,11 @@ class New(Base):
if a.data:
self.add_output(None, self.proto.coin_amt('0'), data=a.data)
else:
exclude = [a.mmid for a in parsed_args if a.mmid]
self.add_output(
coinaddr = a.addr or (await self.get_autochg_addr(self.proto, a.arg, parsed_args)).addr,
coinaddr = a.addr or (
await self.get_autochg_addr(self.proto, a.arg, exclude=exclude, desc='change address')
).addr,
amt = self.proto.coin_amt(a.amt or '0'),
is_chg = not a.amt)