Browse Source

mmgen-txsend: cleanups; Signable: new `get_last_sent()` method

The MMGen Project 1 month ago
parent
commit
7aadef4de3
3 changed files with 43 additions and 24 deletions
  1. 11 5
      mmgen/autosign.py
  2. 28 19
      mmgen/main_txsend.py
  3. 4 0
      mmgen/proto/xmr/tx/completed.py

+ 11 - 5
mmgen/autosign.py

@@ -334,13 +334,19 @@ class Signable:
 				shred_file(self.cfg, fn, iterations=15)
 				shred_file(self.cfg, fn, iterations=15)
 			sys.exit(0)
 			sys.exit(0)
 
 
-		async def get_last_created(self):
+		async def get_last_sent(self):
+			return await self.get_last_created(
+				# compat fallback - ‘sent_timestamp’ attr is missing in some old TX files:
+				sort_key = lambda x: x.sent_timestamp or x.timestamp)
+
+		async def get_last_created(self, *, sort_key=lambda x: x.timestamp):
 			from .tx import CompletedTX
 			from .tx import CompletedTX
-			files = [f for f in self.dir.iterdir() if f.name.endswith(self.subext)]
-			return sorted(
+			fns = [f for f in self.dir.iterdir() if f.name.endswith(self.subext)]
+			files = sorted(
 				[await CompletedTX(cfg=self.cfg, filename=str(txfile), quiet_open=True)
 				[await CompletedTX(cfg=self.cfg, filename=str(txfile), quiet_open=True)
-					for txfile in files],
-				key = lambda x: x.timestamp)[-1]
+					for txfile in fns],
+				key = sort_key)
+			return files[-1]
 
 
 	class xmr_signable: # mixin class
 	class xmr_signable: # mixin class
 		automount = True
 		automount = True

+ 28 - 19
mmgen/main_txsend.py

@@ -32,7 +32,10 @@ opts_data = {
 	],
 	],
 	'text': {
 	'text': {
 		'desc':    f'Send a signed {gc.proj_name} cryptocoin transaction',
 		'desc':    f'Send a signed {gc.proj_name} cryptocoin transaction',
-		'usage':   '[opts] [signed transaction file]',
+		'usage2': [
+			'[opts] <signed transaction file>',
+			'[opts] --autosign',
+		],
 		'options': """
 		'options': """
 -h, --help       Print this help message
 -h, --help       Print this help message
 --, --longhelp   Print help message for long (global) options
 --, --longhelp   Print help message for long (global) options
@@ -88,27 +91,33 @@ if cfg.dump_hex and cfg.dump_hex != '-':
 	from .fileutil import check_outfile_dir
 	from .fileutil import check_outfile_dir
 	check_outfile_dir(cfg.dump_hex)
 	check_outfile_dir(cfg.dump_hex)
 
 
+post_send_op = cfg.status or cfg.receipt
+
 asi = None
 asi = None
 
 
+def init_autosign():
+	global asi, si, infile
+	from .tx.util import mount_removable_device
+	from .autosign import Signable
+	asi = mount_removable_device(cfg)
+	si = Signable.automount_transaction(asi)
+	if cfg.abort:
+		si.shred_abortable() # prompts user, then raises exception or exits
+	elif post_send_op:
+		if si.unsent:
+			die(1, 'Transaction is unsent')
+		if si.unsigned:
+			die(1, 'Transaction is unsigned')
+	else:
+		infile = si.get_unsent()
+		cfg._util.qmsg(f'Got signed transaction file ‘{infile}’')
+
 match cfg._args:
 match cfg._args:
+	case [] if cfg.autosign:
+		init_autosign()
 	case [infile]:
 	case [infile]:
 		from .fileutil import check_infile
 		from .fileutil import check_infile
 		check_infile(infile)
 		check_infile(infile)
-	case [] if cfg.autosign:
-		from .tx.util import mount_removable_device
-		from .autosign import Signable
-		asi = mount_removable_device(cfg)
-		si = Signable.automount_transaction(asi)
-		if cfg.abort:
-			si.shred_abortable() # prompts user, then raises exception or exits
-		elif cfg.status or cfg.receipt:
-			if si.unsent:
-				die(1, 'Transaction is unsent')
-			if si.unsigned:
-				die(1, 'Transaction is unsigned')
-		else:
-			infile = si.get_unsent()
-			cfg._util.qmsg(f'Got signed transaction file ‘{infile}’')
 	case _:
 	case _:
 		cfg._usage()
 		cfg._usage()
 
 
@@ -122,8 +131,8 @@ async def main():
 
 
 	global cfg
 	global cfg
 
 
-	if (cfg.status or cfg.receipt) and cfg.autosign:
-		tx = await si.get_last_created()
+	if cfg.autosign and post_send_op:
+		tx = await si.get_last_sent()
 	else:
 	else:
 		tx = await OnlineSignedTX(
 		tx = await OnlineSignedTX(
 			cfg        = cfg,
 			cfg        = cfg,
@@ -149,7 +158,7 @@ async def main():
 		await tx.post_send(asi)
 		await tx.post_send(asi)
 		sys.exit(0)
 		sys.exit(0)
 
 
-	if not (cfg.status or cfg.receipt):
+	if not post_send_op:
 		if tx.is_swap and not tx.check_swap_expiry():
 		if tx.is_swap and not tx.check_swap_expiry():
 			die(1, 'Swap quote has expired. Please re-create the transaction')
 			die(1, 'Swap quote has expired. Please re-create the transaction')
 
 

+ 4 - 0
mmgen/proto/xmr/tx/completed.py

@@ -36,3 +36,7 @@ class Completed(Base):
 	@cached_property
 	@cached_property
 	def timestamp(self):
 	def timestamp(self):
 		return make_timestamp(self.compat_tx.data.create_time)
 		return make_timestamp(self.compat_tx.data.create_time)
+
+	@cached_property
+	def sent_timestamp(self):
+		return make_timestamp(self.compat_tx.data.submit_time)