Skip to content

Commit 83d7cfd

Browse files
committed
test: refactor: deduplicate segwitv0 ECDSA signing for tx inputs
Follow-up for bitcoin#28025.
1 parent e35fb7b commit 83d7cfd

File tree

2 files changed

+18
-11
lines changed

2 files changed

+18
-11
lines changed

test/functional/p2p_segwit.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,9 @@
7070
SIGHASH_ANYONECANPAY,
7171
SIGHASH_NONE,
7272
SIGHASH_SINGLE,
73-
SegwitV0SignatureHash,
7473
hash160,
7574
sign_input_legacy,
75+
sign_input_segwitv0,
7676
)
7777
from test_framework.script_util import (
7878
key_to_p2pk_script,
@@ -121,10 +121,8 @@ def func_wrapper(self, *args, **kwargs):
121121

122122
def sign_p2pk_witness_input(script, tx_to, in_idx, hashtype, value, key):
123123
"""Add signature for a P2PK witness script."""
124-
tx_hash = SegwitV0SignatureHash(script, tx_to, in_idx, hashtype, value)
125-
signature = key.sign_ecdsa(tx_hash) + chr(hashtype).encode('latin-1')
126-
tx_to.wit.vtxinwit[in_idx].scriptWitness.stack = [signature, script]
127-
tx_to.rehash()
124+
tx_to.wit.vtxinwit[in_idx].scriptWitness.stack = [script]
125+
sign_input_segwitv0(tx_to, in_idx, script, value, key, hashtype)
128126

129127
def test_transaction_acceptance(node, p2p, tx, with_witness, accepted, reason=None):
130128
"""Send a transaction to the node and check that it's accepted to the mempool
@@ -1476,11 +1474,9 @@ def test_uncompressed_pubkey(self):
14761474
tx2.vin.append(CTxIn(COutPoint(tx.sha256, 0), b""))
14771475
tx2.vout.append(CTxOut(tx.vout[0].nValue - 1000, script_wsh))
14781476
script = keyhash_to_p2pkh_script(pubkeyhash)
1479-
sig_hash = SegwitV0SignatureHash(script, tx2, 0, SIGHASH_ALL, tx.vout[0].nValue)
1480-
signature = key.sign_ecdsa(sig_hash) + b'\x01' # 0x1 is SIGHASH_ALL
14811477
tx2.wit.vtxinwit.append(CTxInWitness())
1482-
tx2.wit.vtxinwit[0].scriptWitness.stack = [signature, pubkey]
1483-
tx2.rehash()
1478+
tx2.wit.vtxinwit[0].scriptWitness.stack = [pubkey]
1479+
sign_input_segwitv0(tx2, 0, script, tx.vout[0].nValue, key)
14841480

14851481
# Should fail policy test.
14861482
test_transaction_acceptance(self.nodes[0], self.test_node, tx2, True, False, 'non-mandatory-script-verify-flag (Using non-compressed keys in segwit)')
@@ -1676,11 +1672,13 @@ def test_signature_version_1(self):
16761672
tx2.vout.append(CTxOut(tx.vout[0].nValue, CScript([OP_TRUE])))
16771673

16781674
script = keyhash_to_p2pkh_script(pubkeyhash)
1679-
sig_hash = SegwitV0SignatureHash(script, tx2, 0, SIGHASH_ALL, tx.vout[0].nValue)
1680-
signature = key.sign_ecdsa(sig_hash) + b'\x01' # 0x1 is SIGHASH_ALL
1675+
tx2.wit.vtxinwit.append(CTxInWitness())
1676+
sign_input_segwitv0(tx2, 0, script, tx.vout[0].nValue, key)
1677+
signature = tx2.wit.vtxinwit[0].scriptWitness.stack.pop()
16811678

16821679
# Check that we can't have a scriptSig
16831680
tx2.vin[0].scriptSig = CScript([signature, pubkey])
1681+
tx2.rehash()
16841682
block = self.build_next_block()
16851683
self.update_witness_block_with_transactions(block, [tx, tx2])
16861684
test_witness_block(self.nodes[0], self.test_node, block, accepted=False,

test/functional/test_framework/script.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -699,6 +699,15 @@ def sign_input_legacy(tx, input_index, input_scriptpubkey, privkey, sighash_type
699699
tx.vin[input_index].scriptSig = bytes(CScript([der_sig + bytes([sighash_type])])) + tx.vin[input_index].scriptSig
700700
tx.rehash()
701701

702+
def sign_input_segwitv0(tx, input_index, input_scriptpubkey, input_amount, privkey, sighash_type=SIGHASH_ALL):
703+
"""Add segwitv0 ECDSA signature for a given transaction input. Note that the signature
704+
is inserted at the bottom of the witness stack, i.e. additional witness data
705+
needed (e.g. pubkey for P2WPKH) can already be set before."""
706+
sighash = SegwitV0SignatureHash(input_scriptpubkey, tx, input_index, sighash_type, input_amount)
707+
der_sig = privkey.sign_ecdsa(sighash)
708+
tx.wit.vtxinwit[input_index].scriptWitness.stack.insert(0, der_sig + bytes([sighash_type]))
709+
tx.rehash()
710+
702711
# TODO: Allow cached hashPrevouts/hashSequence/hashOutputs to be provided.
703712
# Performance optimization probably not necessary for python tests, however.
704713
# Note that this corresponds to sigversion == 1 in EvalScript, which is used

0 commit comments

Comments
 (0)