Skip to content

Commit 647d95a

Browse files
committed
test: add coverage for passing an invalid sighashtype
in RPCs descriptorprocesspbst, walletprocesspbst, signrawtransactionwithkey, and signrawtransactionwithwallet.
1 parent f57e724 commit 647d95a

File tree

3 files changed

+35
-16
lines changed

3 files changed

+35
-16
lines changed

test/functional/rpc_psbt.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -883,6 +883,9 @@ def test_psbt_input_keys(psbt_input, keys):
883883
comb_psbt = self.nodes[0].combinepsbt([psbt, parsed_psbt.to_base64()])
884884
assert_equal(comb_psbt, psbt)
885885

886+
self.log.info("Test walletprocesspsbt raises if an invalid sighashtype is passed")
887+
assert_raises_rpc_error(-8, "all is not a valid sighash parameter.", self.nodes[0].walletprocesspsbt, psbt, sighashtype="all")
888+
886889
self.log.info("Test decoding PSBT with per-input preimage types")
887890
# note that the decodepsbt RPC doesn't check whether preimages and hashes match
888891
hash_ripemd160, preimage_ripemd160 = random_bytes(20), random_bytes(50)
@@ -982,5 +985,9 @@ def test_psbt_input_keys(psbt_input, keys):
982985
rawtx = self.nodes[2].finalizepsbt(psbt)["hex"]
983986
self.nodes[2].sendrawtransaction(rawtx)
984987

988+
self.log.info("Test descriptorprocesspsbt raises if an invalid sighashtype is passed")
989+
assert_raises_rpc_error(-8, "all is not a valid sighash parameter.", self.nodes[2].descriptorprocesspsbt, psbt, [descriptor], sighashtype="all")
990+
991+
985992
if __name__ == '__main__':
986993
PSBTTest().main()

test/functional/rpc_signrawtransactionwithkey.py

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from test_framework.test_framework import BitcoinTestFramework
1515
from test_framework.util import (
1616
assert_equal,
17+
assert_raises_rpc_error,
1718
find_vout_for_address,
1819
)
1920
from test_framework.script_util import (
@@ -33,6 +34,14 @@
3334
Decimal,
3435
)
3536

37+
INPUTS = [
38+
# Valid pay-to-pubkey scripts
39+
{'txid': '9b907ef1e3c26fc71fe4a4b3580bc75264112f95050014157059c736f0202e71', 'vout': 0,
40+
'scriptPubKey': '76a91460baa0f494b38ce3c940dea67f3804dc52d1fb9488ac'},
41+
{'txid': '83a4f6a6b73660e13ee6cb3c6063fa3759c50c9b7521d0536022961898f4fb02', 'vout': 0,
42+
'scriptPubKey': '76a914669b857c03a5ed269d5d85a1ffac9ed5d663072788ac'},
43+
]
44+
OUTPUTS = {'mpLQjfK79b7CCV4VMJWEWAj5Mpx8Up5zxB': 0.1}
3645

3746
class SignRawTransactionWithKeyTest(BitcoinTestFramework):
3847
def set_test_params(self):
@@ -56,19 +65,8 @@ def successful_signing_test(self):
5665
2) No script verification error occurred"""
5766
self.log.info("Test valid raw transaction with one input")
5867
privKeys = ['cUeKHd5orzT3mz8P9pxyREHfsWtVfgsfDjiZZBcjUBAaGk1BTj7N', 'cVKpPfVKSJxKqVpE9awvXNWuLHCa5j5tiE7K6zbUSptFpTEtiFrA']
59-
60-
inputs = [
61-
# Valid pay-to-pubkey scripts
62-
{'txid': '9b907ef1e3c26fc71fe4a4b3580bc75264112f95050014157059c736f0202e71', 'vout': 0,
63-
'scriptPubKey': '76a91460baa0f494b38ce3c940dea67f3804dc52d1fb9488ac'},
64-
{'txid': '83a4f6a6b73660e13ee6cb3c6063fa3759c50c9b7521d0536022961898f4fb02', 'vout': 0,
65-
'scriptPubKey': '76a914669b857c03a5ed269d5d85a1ffac9ed5d663072788ac'},
66-
]
67-
68-
outputs = {'mpLQjfK79b7CCV4VMJWEWAj5Mpx8Up5zxB': 0.1}
69-
70-
rawTx = self.nodes[0].createrawtransaction(inputs, outputs)
71-
rawTxSigned = self.nodes[0].signrawtransactionwithkey(rawTx, privKeys, inputs)
68+
rawTx = self.nodes[0].createrawtransaction(INPUTS, OUTPUTS)
69+
rawTxSigned = self.nodes[0].signrawtransactionwithkey(rawTx, privKeys, INPUTS)
7270

7371
# 1) The transaction has a complete set of signatures
7472
assert rawTxSigned['complete']
@@ -125,9 +123,16 @@ def verify_txn_with_witness_script(self, tx_type):
125123
assert_equal(spending_tx_signed['complete'], True)
126124
self.nodes[0].sendrawtransaction(spending_tx_signed['hex'])
127125

126+
def invalid_sighashtype_test(self):
127+
self.log.info("Test signing transaction with invalid sighashtype")
128+
tx = self.nodes[0].createrawtransaction(INPUTS, OUTPUTS)
129+
privkeys = [self.nodes[0].get_deterministic_priv_key().key]
130+
assert_raises_rpc_error(-8, "all is not a valid sighash parameter.", self.nodes[0].signrawtransactionwithkey, tx, privkeys, sighashtype="all")
131+
128132
def run_test(self):
129133
self.successful_signing_test()
130134
self.witness_script_test()
135+
self.invalid_sighashtype_test()
131136

132137

133138
if __name__ == '__main__':

test/functional/wallet_signrawtransactionwithwallet.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@
3333
getcontext,
3434
)
3535

36+
37+
RAW_TX = '020000000156b958f78e3f24e0b2f4e4db1255426b0902027cb37e3ddadb52e37c3557dddb0000000000ffffffff01c0a6b929010000001600149a2ee8c77140a053f36018ac8124a6ececc1668a00000000'
38+
39+
3640
class SignRawTransactionWithWalletTest(BitcoinTestFramework):
3741
def add_options(self, parser):
3842
self.add_wallet_options(parser)
@@ -47,10 +51,12 @@ def skip_test_if_missing_module(self):
4751
def test_with_lock_outputs(self):
4852
self.log.info("Test correct error reporting when trying to sign a locked output")
4953
self.nodes[0].encryptwallet("password")
54+
assert_raises_rpc_error(-13, "Please enter the wallet passphrase with walletpassphrase first", self.nodes[0].signrawtransactionwithwallet, RAW_TX)
55+
self.nodes[0].walletpassphrase("password", 9999)
5056

51-
rawTx = '020000000156b958f78e3f24e0b2f4e4db1255426b0902027cb37e3ddadb52e37c3557dddb0000000000ffffffff01c0a6b929010000001600149a2ee8c77140a053f36018ac8124a6ececc1668a00000000'
52-
53-
assert_raises_rpc_error(-13, "Please enter the wallet passphrase with walletpassphrase first", self.nodes[0].signrawtransactionwithwallet, rawTx)
57+
def test_with_invalid_sighashtype(self):
58+
self.log.info("Test signrawtransactionwithwallet raises if an invalid sighashtype is passed")
59+
assert_raises_rpc_error(-8, "all is not a valid sighash parameter.", self.nodes[0].signrawtransactionwithwallet, hexstring=RAW_TX, sighashtype="all")
5460

5561
def script_verification_error_test(self):
5662
"""Create and sign a raw transaction with valid (vin 0), invalid (vin 1) and one missing (vin 2) input script.
@@ -299,6 +305,7 @@ def run_test(self):
299305
self.script_verification_error_test()
300306
self.OP_1NEGATE_test()
301307
self.test_with_lock_outputs()
308+
self.test_with_invalid_sighashtype()
302309
self.test_fully_signed_tx()
303310
self.test_signing_with_csv()
304311
self.test_signing_with_cltv()

0 commit comments

Comments
 (0)