From f8109a941270b9780c7cb860105f2ccbeb781e18 Mon Sep 17 00:00:00 2001 From: The MMGen Project Date: Sun, 29 Nov 2026 17:04:40 +0000 Subject: [PATCH] Unsigned.sign(): have caller handle exceptions --- mmgen/autosign/signable.py | 7 ++----- mmgen/main_txbump.py | 12 +++++------- mmgen/main_txdo.py | 12 ++++-------- mmgen/main_txsign.py | 12 +++++++----- mmgen/proto/btc/tx/unsigned.py | 27 +++++++++++---------------- mmgen/proto/vm/tx/unsigned.py | 19 +++++++------------ mmgen/tx/unsigned.py | 10 +--------- test/cmdtest_d/swap.py | 2 +- 8 files changed, 38 insertions(+), 63 deletions(-) diff --git a/mmgen/autosign/signable.py b/mmgen/autosign/signable.py index 1574f9ea..ce591111 100644 --- a/mmgen/autosign/signable.py +++ b/mmgen/autosign/signable.py @@ -88,11 +88,8 @@ class Signable: keylist = self.parent.keylist, passwdfile = str(self.parent.keyfile), autosign = True).keys) - if tx2: - tx2.file.write(ask_write=False, outdir=self.dir) - return tx2 - else: - return False + tx2.file.write(ask_write=False, outdir=self.dir) + return tx2 def print_summary(self, signables): diff --git a/mmgen/main_txbump.py b/mmgen/main_txbump.py index 2b2ce177..75950e65 100644 --- a/mmgen/main_txbump.py +++ b/mmgen/main_txbump.py @@ -207,13 +207,11 @@ async def main(): if sign_and_send: tx2 = UnsignedTX(cfg=cfg, data=tx.__dict__) - if tx3 := await tx2.sign( - TxKeys(cfg, tx2, seedfiles=seedfiles, keylist=kl, keyaddrlist=kal).keys): - tx4 = await OnlineSignedTX(cfg=cfg, data=tx3.__dict__) - tx4.file.write(ask_write=False) - sys.exit(await tx4.send(cfg, 'rpc_send', None, asi if cfg.autosign else None)) - else: - die(2, 'Transaction could not be signed') + tx3 = await tx2.sign( + TxKeys(cfg, tx2, seedfiles=seedfiles, keylist=kl, keyaddrlist=kal).keys) + tx4 = await OnlineSignedTX(cfg=cfg, data=tx3.__dict__) + tx4.file.write(ask_write=False) + sys.exit(await tx4.send(cfg, 'rpc_send', None, asi if cfg.autosign else None)) else: tx.file.write( outdir = asi.txauto_dir if cfg.autosign else None, diff --git a/mmgen/main_txdo.py b/mmgen/main_txdo.py index 75a48951..f088f40e 100644 --- a/mmgen/main_txdo.py +++ b/mmgen/main_txdo.py @@ -21,7 +21,7 @@ mmgen-txdo: Create, sign and send an online MMGen transaction import sys from .cfg import gc, Config -from .util import die, fmt_list, async_run +from .util import fmt_list, async_run from .subseed import SubSeedIdxRange target = gc.prog_name.split('-')[1].removesuffix('do') @@ -180,12 +180,8 @@ async def main(): caller = 'txdo') tx3 = await tx2.sign(TxKeys(cfg, tx2, seedfiles=seedfiles).keys) - - if tx3: - tx3.file.write(ask_write=False) - tx4 = await SentTX(cfg=cfg, data=tx3.__dict__) - return await tx4.send(cfg, 'rpc_send', None, asi=None) - else: - die(2, 'Transaction could not be signed') + tx3.file.write(ask_write=False) + tx4 = await SentTX(cfg=cfg, data=tx3.__dict__) + return await tx4.send(cfg, 'rpc_send', None, asi=None) sys.exit(async_run(cfg, main)) diff --git a/mmgen/main_txsign.py b/mmgen/main_txsign.py index 5b273f65..ea96a4c2 100644 --- a/mmgen/main_txsign.py +++ b/mmgen/main_txsign.py @@ -141,15 +141,17 @@ async def main(): if not cfg.yes: tx1.info.view_with_prompt(f'View data for transaction{tx_num_disp}?') - if tx2 := await tx1.sign( + try: + tx2 = await tx1.sign( TxKeys(cfg, tx1, seedfiles=seedfiles).keys, - tx_num_str = tx_num_disp): + tx_num_str = tx_num_disp) + except Exception as e: + ymsg(f'Transaction could not be signed:\n{e}') + bad_tx_count += 1 + else: if not cfg.yes: tx2.add_comment() # edits an existing comment tx2.file.write(ask_write=not cfg.yes, ask_write_default_yes=True, add_desc=tx_num_disp) - else: - ymsg('Transaction could not be signed') - bad_tx_count += 1 if bad_tx_count: die(2, f'{bad_tx_count} transaction{suf(bad_tx_count)} could not be signed') diff --git a/mmgen/proto/btc/tx/unsigned.py b/mmgen/proto/btc/tx/unsigned.py index e361bae6..87280fa2 100644 --- a/mmgen/proto/btc/tx/unsigned.py +++ b/mmgen/proto/btc/tx/unsigned.py @@ -18,7 +18,6 @@ from .completed import Completed class Unsigned(Completed, TxBase.Unsigned): desc = 'unsigned transaction' - # Return signed object or False. Don’t exit or raise exception: async def proto_sign(self, keys, *, tx_num_str): if (self.has_segwit_inputs() or self.has_segwit_outputs()) and not self.proto.cap('segwit'): @@ -55,22 +54,18 @@ class Unsigned(Completed, TxBase.Unsigned): ret = await self.rpc.call(*args) except Exception as e: ymsg(self.rpc.daemon.sigfail_errmsg(e)) - return False + raise - try: - self.update_serialized(ret['hex']) - from ....tx import SignedTX - new = SignedTX(cfg=self.cfg, data=self.__dict__, automount=self.automount) - tx_decoded = await self.rpc.call('decoderawtransaction', ret['hex']) - new.compare_size_and_estimated_size(tx_decoded) - new.coin_txid = CoinTxID(self.deserialized.txid) - if new.coin_txid != tx_decoded['txid']: - die('BadMMGenTxID', 'txid mismatch (after signing)') - msg('OK') - return new - except Exception as e: - ymsg(f'\n{e.args[0]}') - return False + self.update_serialized(ret['hex']) + from ....tx import SignedTX + new = SignedTX(cfg=self.cfg, data=self.__dict__, automount=self.automount) + tx_decoded = await self.rpc.call('decoderawtransaction', ret['hex']) + new.compare_size_and_estimated_size(tx_decoded) + new.coin_txid = CoinTxID(self.deserialized.txid) + if new.coin_txid != tx_decoded['txid']: + die('BadMMGenTxID', 'txid mismatch (after signing)') + msg('OK') + return new class AutomountUnsigned(TxBase.AutomountUnsigned, Unsigned): pass diff --git a/mmgen/proto/vm/tx/unsigned.py b/mmgen/proto/vm/tx/unsigned.py index f4332f5e..10ebe83b 100644 --- a/mmgen/proto/vm/tx/unsigned.py +++ b/mmgen/proto/vm/tx/unsigned.py @@ -15,7 +15,6 @@ from ....util import msg, msg_r, die class Unsigned: desc = 'unsigned transaction' - # Return signed object or False. Don’t exit or raise exception: async def proto_sign(self, keys, *, tx_num_str): o = self.txobj @@ -23,7 +22,7 @@ class Unsigned: def do_mismatch_err(io, j, k, desc): m = 'A compromised online installation may have altered your serialized data!' fs = '\n{} mismatch!\n{}\n orig: {}\n serialized: {}' - die(3, fs.format(desc.upper(), m, getattr(io[0], k), o[j])) + die('TxIntegrityCheckError', fs.format(desc.upper(), m, getattr(io[0], k), o[j])) if o['from'] != self.inputs[0].addr: do_mismatch_err(self.inputs, 'from', 'addr', 'from-address') @@ -34,14 +33,10 @@ class Unsigned: do_mismatch_err(self.outputs, 'amt', 'amt', 'amount') msg_r(f'Signing transaction{tx_num_str}...') + await self.do_sign(o, keys[0].sec.wif) + msg('OK') - try: - await self.do_sign(o, keys[0].sec.wif) - msg('OK') - from ....tx import SignedTX - tx = SignedTX(cfg=self.cfg, data=self.__dict__, automount=self.automount) - tx.check_serialized_integrity() - return tx - except Exception as e: - msg(f'{e}: transaction signing failed!') - return False + from ....tx import SignedTX + tx = SignedTX(cfg=self.cfg, data=self.__dict__, automount=self.automount) + tx.check_serialized_integrity() + return tx diff --git a/mmgen/tx/unsigned.py b/mmgen/tx/unsigned.py index 0d1f45b8..681d1d9d 100644 --- a/mmgen/tx/unsigned.py +++ b/mmgen/tx/unsigned.py @@ -18,16 +18,8 @@ class Unsigned(Completed): ext = 'rawtx' automount = False - # Return signed object or False. Don’t exit or raise exception, unless fatal: async def sign(self, keys, *, tx_num_str=''): - - try: - self.check_correct_chain() - except Exception as e: - if type(e).__name__ == 'TransactionChainMismatch': - return False - raise # fatal - + self.check_correct_chain() return await self.proto_sign(keys, tx_num_str=tx_num_str) def delete_attrs(self, desc, attr): diff --git a/test/cmdtest_d/swap.py b/test/cmdtest_d/swap.py index e77346f9..906cf2c1 100644 --- a/test/cmdtest_d/swap.py +++ b/test/cmdtest_d/swap.py @@ -747,7 +747,7 @@ class CmdTestSwap( return self.swaptxcreate6() def swaptxsign_bad1(self): - return self._swaptxsign_bad('non-wallet address forbidden') + return self._swaptxsign_bad('non-wallet address forbidden', exit_val=2) def swaptxsign_bad2_create(self): return self.swaptxcreate1(idx=4)