Unsigned.sign(): have caller handle exceptions

This commit is contained in:
The MMGen Project 2026-11-29 17:04:40 +00:00
commit f8109a9412
Signed by: mmgen
GPG key ID: 3F8B1861E32B7DA2
8 changed files with 38 additions and 63 deletions

View file

@ -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):

View file

@ -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,

View file

@ -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))

View file

@ -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')

View file

@ -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

View file

@ -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

View file

@ -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):

View file

@ -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)