Browse Source

tx.new: get_autochg_addr(): add `desc` param

The MMGen Project 3 weeks ago
parent
commit
9631433072
2 changed files with 18 additions and 17 deletions
  1. 6 6
      mmgen/tw/addresses.py
  2. 12 11
      mmgen/tx/new.py

+ 6 - 6
mmgen/tw/addresses.py

@@ -274,7 +274,7 @@ class TwAddresses(TwView):
 				return bool(e.recvd)
 				return bool(e.recvd)
 		return None # addr not in tracking wallet
 		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.
 		Get lowest-indexed unused address in tracking wallet for requested AddrListID.
 		Return values on failure:
 		Return values on failure:
@@ -326,14 +326,14 @@ class TwAddresses(TwView):
 								d.twmmid.hl(),
 								d.twmmid.hl(),
 								yellow('has a label,'),
 								yellow('has a label,'),
 								d.comment.hl2(encl='‘’'),
 								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
 						return d
 				else:
 				else:
 					break
 					break
 			return False
 			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,
 		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.
 		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 ''
 					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
 			from ..ui import line_input
 			while True:
 			while True:

+ 12 - 11
mmgen/tx/new.py

@@ -194,25 +194,23 @@ class New(Base):
 
 
 		return _pa(arg, mmid, coin_addr, amt, None)
 		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
 		from ..tw.addresses import TwAddresses
 		al = await TwAddresses(self.cfg, proto, get_data=True)
 		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:
 		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:
 		if res:
 			return 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 ',
 			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):
 	async def process_cmdline_args(self, cmd_args, ad_f, ad_w):
 
 
@@ -228,8 +226,11 @@ class New(Base):
 			if a.data:
 			if a.data:
 				self.add_output(None, self.proto.coin_amt('0'), data=a.data)
 				self.add_output(None, self.proto.coin_amt('0'), data=a.data)
 			else:
 			else:
+				exclude = [a.mmid for a in parsed_args if a.mmid]
 				self.add_output(
 				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'),
 					amt      = self.proto.coin_amt(a.amt or '0'),
 					is_chg   = not a.amt)
 					is_chg   = not a.amt)