mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-11-26 14:00:29 +01:00
test: refactor: deduplicate legacy ECDSA signing for tx inputs
There are several instances in functional tests and the framework
(MiniWallet, feature_block.py, p2p_segwit.py) where we create a legacy
ECDSA signature for a certain transaction's input by doing the following
steps:
1) calculate the `LegacySignatureHash` with the desired sighash type
2) create the actual digital signature by calling `ECKey.sign_ecdsa`
on the signature message hash calculated above
3) put the DER-encoded result as CScript data push into
tx input's scriptSig
Create a new helper `sign_input_legacy` which hides those details and
takes only the necessary parameters (tx, input index, relevant
scriptPubKey, private key, sighash type [SIGHASH_ALL by default]). For
further convenience, the signature is prepended to already existing
data-pushes in scriptSig, in order to avoid rehashing the transaction
after calling the new signing function.
This commit is contained in:
@@ -689,6 +689,16 @@ def LegacySignatureHash(*args, **kwargs):
|
||||
else:
|
||||
return (hash256(msg), err)
|
||||
|
||||
def sign_input_legacy(tx, input_index, input_scriptpubkey, privkey, sighash_type=SIGHASH_ALL):
|
||||
"""Add legacy ECDSA signature for a given transaction input. Note that the signature
|
||||
is prepended to the scriptSig field, i.e. additional data pushes necessary for more
|
||||
complex spends than P2PK (e.g. pubkey for P2PKH) can be already set before."""
|
||||
(sighash, err) = LegacySignatureHash(input_scriptpubkey, tx, input_index, sighash_type)
|
||||
assert err is None
|
||||
der_sig = privkey.sign_ecdsa(sighash)
|
||||
tx.vin[input_index].scriptSig = bytes(CScript([der_sig + bytes([sighash_type])])) + tx.vin[input_index].scriptSig
|
||||
tx.rehash()
|
||||
|
||||
# TODO: Allow cached hashPrevouts/hashSequence/hashOutputs to be provided.
|
||||
# Performance optimization probably not necessary for python tests, however.
|
||||
# Note that this corresponds to sigversion == 1 in EvalScript, which is used
|
||||
|
||||
Reference in New Issue
Block a user