From 7aadef4de336654310d8982e95d35d603d285359 Mon Sep 17 00:00:00 2001 From: The MMGen Project Date: Fri, 30 Jan 2026 09:20:21 +0000 Subject: [PATCH] mmgen-txsend: cleanups; Signable: new `get_last_sent()` method --- mmgen/autosign.py | 16 +++++++---- mmgen/main_txsend.py | 47 ++++++++++++++++++++------------- mmgen/proto/xmr/tx/completed.py | 4 +++ 3 files changed, 43 insertions(+), 24 deletions(-) diff --git a/mmgen/autosign.py b/mmgen/autosign.py index a93b3df4..589b9a5d 100755 --- a/mmgen/autosign.py +++ b/mmgen/autosign.py @@ -334,13 +334,19 @@ class Signable: shred_file(self.cfg, fn, iterations=15) 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 - 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) - 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 automount = True diff --git a/mmgen/main_txsend.py b/mmgen/main_txsend.py index 5a07b714..eb1463da 100755 --- a/mmgen/main_txsend.py +++ b/mmgen/main_txsend.py @@ -32,7 +32,10 @@ opts_data = { ], 'text': { 'desc': f'Send a signed {gc.proj_name} cryptocoin transaction', - 'usage': '[opts] [signed transaction file]', + 'usage2': [ + '[opts] ', + '[opts] --autosign', + ], 'options': """ -h, --help Print this help message --, --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 check_outfile_dir(cfg.dump_hex) +post_send_op = cfg.status or cfg.receipt + 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: + case [] if cfg.autosign: + init_autosign() case [infile]: from .fileutil import check_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 _: cfg._usage() @@ -122,8 +131,8 @@ async def main(): 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: tx = await OnlineSignedTX( cfg = cfg, @@ -149,7 +158,7 @@ async def main(): await tx.post_send(asi) 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(): die(1, 'Swap quote has expired. Please re-create the transaction') diff --git a/mmgen/proto/xmr/tx/completed.py b/mmgen/proto/xmr/tx/completed.py index 77315a7b..ddcbefb4 100755 --- a/mmgen/proto/xmr/tx/completed.py +++ b/mmgen/proto/xmr/tx/completed.py @@ -36,3 +36,7 @@ class Completed(Base): @cached_property def timestamp(self): return make_timestamp(self.compat_tx.data.create_time) + + @cached_property + def sent_timestamp(self): + return make_timestamp(self.compat_tx.data.submit_time)