proto.btc.tx.unsigned: clean up proto_sign()

This commit is contained in:
The MMGen Project 2026-11-29 17:04:40 +00:00
commit 6cf721632c
Signed by: mmgen
GPG key ID: 3F8B1861E32B7DA2
2 changed files with 33 additions and 28 deletions

View file

@ -12,8 +12,7 @@ proto.btc.tx.completed: Bitcoin completed transaction class
from ....tx import completed as TxBase
from ....obj import HexStr
from ....util import msg, die
from .base import Base, decodeScriptPubKey
from .base import Base
class Completed(Base, TxBase.Completed):
fn_fee_unit = 'satoshi'
@ -45,20 +44,6 @@ class Completed(Base, TxBase.Completed):
assert (200 < len(ti['scriptSig']) < 300), 'malformed scriptSig' # VERY rough check
return True
def check_pubkey_scripts(self):
for n, i in enumerate(self.inputs, 1):
ds = decodeScriptPubKey(self.proto, i.scriptPubKey)
if ds.addr != i.addr:
if ds.addr_fmt != i.addr.addr_fmt:
m = 'Address format of scriptPubKey ({}) does not match that of address ({}) in input #{}'
msg(m.format(ds.addr_fmt, i.addr.addr_fmt, n))
m = 'ERROR: Address and scriptPubKey of transaction input #{} do not match!'
die(3, (m+'\n {:23}{}'*3).format(
n,
'address:', i.addr,
'scriptPubKey:', i.scriptPubKey,
'scriptPubKey->address:', ds.addr))
def is_replaceable(self):
return self.inputs[0].sequence == self.proto.max_int - 2

View file

@ -13,18 +13,29 @@ proto.btc.tx.unsigned: Bitcoin unsigned transaction class
from ....tx import unsigned as TxBase
from ....obj import CoinTxID, MMGenDict
from ....util import msg, msg_r, ymsg, suf, die
from .base import decodeScriptPubKey
from .completed import Completed
def do_addr_mismatch_err(d, ds, i):
if ds.addr_fmt != d.addr.addr_fmt:
msg(f'Address format of scriptPubKey ({ds.addr_fmt}) does not match that of address '
f'({d.addr.addr_fmt}) in input #{i + 1}')
die(3, ('ERROR: Address and scriptPubKey of transaction input #{} do not match!'
+ ('\n {:23}{}' * 3)).format(
i + 1,
'address:', d.addr,
'scriptPubKey:', d.scriptPubKey,
'scriptPubKey->address:', ds.addr))
class Unsigned(Completed, TxBase.Unsigned):
desc = 'unsigned transaction'
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'):
ymsg(f"TX has Segwit inputs or outputs, but {self.coin} doesn't support Segwit!")
return False
self.check_pubkey_scripts()
die('TxIntegrityCheckError',
f'TX has Segwit inputs or outputs, but {self.coin} doesn’t support Segwit!')
self.cfg._util.qmsg(f'Passing {len(keys)} key{suf(keys)} to {self.rpc.daemon.exec_fn}')
@ -34,14 +45,23 @@ class Unsigned(Completed, TxBase.Unsigned):
ag = AddrGenerator(self.cfg, self.proto, 'segwit')
keydict = MMGenDict([(d.addr, d.sec) for d in keys])
sig_data = []
for d in self.inputs:
e = {k: getattr(d, k) for k in ('txid', 'vout', 'scriptPubKey', 'amt')}
e['amount'] = e['amt']
del e['amt']
if d.mmtype == 'S':
e['redeemScript'] = ag.to_segwit_redeem_script(kg.gen_data(keydict[d.addr]))
sig_data.append(e)
sig_data = [{
'txid': d.txid,
'vout': d.vout,
'scriptPubKey': d.scriptPubKey,
'amount': d.amt,
} for d in self.inputs]
for i, d in enumerate(self.inputs):
ds = decodeScriptPubKey(self.proto, d.scriptPubKey)
if d.addr != ds.addr:
do_addr_mismatch_err(d, ds, i)
if 'segwit' in self.proto.caps:
match ds.type:
case 'scripthash':
sig_data[i]['redeemScript'] = ag.to_segwit_redeem_script(kg.gen_data(keydict[d.addr]))
case 'pubkeyhash':
del sig_data[i]['amount']
msg_r(f'Signing transaction{tx_num_str}...')
wifs = [d.sec.wif for d in keys]