14
14
from test_framework .test_framework import BitcoinTestFramework
15
15
from test_framework .util import (
16
16
assert_equal ,
17
+ assert_raises_rpc_error ,
17
18
find_vout_for_address ,
18
19
)
19
20
from test_framework .script_util import (
33
34
Decimal ,
34
35
)
35
36
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 }
36
45
37
46
class SignRawTransactionWithKeyTest (BitcoinTestFramework ):
38
47
def set_test_params (self ):
@@ -47,6 +56,11 @@ def send_to_address(self, addr, amount):
47
56
txid = self .nodes [0 ].sendrawtransaction (self .nodes [0 ].signrawtransactionwithkey (rawtx , [self .nodes [0 ].get_deterministic_priv_key ().key ])["hex" ], 0 )
48
57
return txid
49
58
59
+ def assert_signing_completed_successfully (self , signed_tx ):
60
+ assert 'errors' not in signed_tx
61
+ assert 'complete' in signed_tx
62
+ assert_equal (signed_tx ['complete' ], True )
63
+
50
64
def successful_signing_test (self ):
51
65
"""Create and sign a valid raw transaction with one input.
52
66
@@ -56,25 +70,10 @@ def successful_signing_test(self):
56
70
2) No script verification error occurred"""
57
71
self .log .info ("Test valid raw transaction with one input" )
58
72
privKeys = ['cUeKHd5orzT3mz8P9pxyREHfsWtVfgsfDjiZZBcjUBAaGk1BTj7N' , 'cVKpPfVKSJxKqVpE9awvXNWuLHCa5j5tiE7K6zbUSptFpTEtiFrA' ]
73
+ rawTx = self .nodes [0 ].createrawtransaction (INPUTS , OUTPUTS )
74
+ rawTxSigned = self .nodes [0 ].signrawtransactionwithkey (rawTx , privKeys , INPUTS )
59
75
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 )
72
-
73
- # 1) The transaction has a complete set of signatures
74
- assert rawTxSigned ['complete' ]
75
-
76
- # 2) No script verification error occurred
77
- assert 'errors' not in rawTxSigned
76
+ self .assert_signing_completed_successfully (rawTxSigned )
78
77
79
78
def witness_script_test (self ):
80
79
self .log .info ("Test signing transaction to P2SH-P2WSH addresses without wallet" )
@@ -95,9 +94,7 @@ def witness_script_test(self):
95
94
# Now create and sign a transaction spending that output on node[0], which doesn't know the scripts or keys
96
95
spending_tx = self .nodes [0 ].createrawtransaction ([unspent_output ], {getnewdestination ()[2 ]: Decimal ("49.998" )})
97
96
spending_tx_signed = self .nodes [0 ].signrawtransactionwithkey (spending_tx , [embedded_privkey ], [unspent_output ])
98
- # Check the signing completed successfully
99
- assert 'complete' in spending_tx_signed
100
- assert_equal (spending_tx_signed ['complete' ], True )
97
+ self .assert_signing_completed_successfully (spending_tx_signed )
101
98
102
99
# Now test with P2PKH and P2PK scripts as the witnessScript
103
100
for tx_type in ['P2PKH' , 'P2PK' ]: # these tests are order-independent
@@ -120,14 +117,19 @@ def verify_txn_with_witness_script(self, tx_type):
120
117
# Now create and sign a transaction spending that output on node[0], which doesn't know the scripts or keys
121
118
spending_tx = self .nodes [0 ].createrawtransaction ([{'txid' : txid , 'vout' : vout }], {getnewdestination ()[2 ]: Decimal ("9.999" )})
122
119
spending_tx_signed = self .nodes [0 ].signrawtransactionwithkey (spending_tx , [embedded_privkey ], [{'txid' : txid , 'vout' : vout , 'scriptPubKey' : script_pub_key , 'redeemScript' : redeem_script , 'witnessScript' : witness_script , 'amount' : 10 }])
123
- # Check the signing completed successfully
124
- assert 'complete' in spending_tx_signed
125
- assert_equal (spending_tx_signed ['complete' ], True )
120
+ self .assert_signing_completed_successfully (spending_tx_signed )
126
121
self .nodes [0 ].sendrawtransaction (spending_tx_signed ['hex' ])
127
122
123
+ def invalid_sighashtype_test (self ):
124
+ self .log .info ("Test signing transaction with invalid sighashtype" )
125
+ tx = self .nodes [0 ].createrawtransaction (INPUTS , OUTPUTS )
126
+ privkeys = [self .nodes [0 ].get_deterministic_priv_key ().key ]
127
+ assert_raises_rpc_error (- 8 , "all is not a valid sighash parameter." , self .nodes [0 ].signrawtransactionwithkey , tx , privkeys , sighashtype = "all" )
128
+
128
129
def run_test (self ):
129
130
self .successful_signing_test ()
130
131
self .witness_script_test ()
132
+ self .invalid_sighashtype_test ()
131
133
132
134
133
135
if __name__ == '__main__' :
0 commit comments