4
4
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
5
5
"""Test transaction signing using the signrawtransactionwithkey RPC."""
6
6
7
- from test_framework .blocktools import (
8
- COINBASE_MATURITY ,
7
+ from test_framework .messages import (
8
+ COIN ,
9
9
)
10
10
from test_framework .address import (
11
11
address_to_scriptpubkey ,
16
16
from test_framework .util import (
17
17
assert_equal ,
18
18
assert_raises_rpc_error ,
19
- find_vout_for_address ,
20
19
)
21
20
from test_framework .script_util import (
22
21
key_to_p2pk_script ,
26
25
)
27
26
from test_framework .wallet import (
28
27
getnewdestination ,
28
+ MiniWallet ,
29
29
)
30
30
from test_framework .wallet_util import (
31
31
generate_keypair ,
46
46
47
47
class SignRawTransactionWithKeyTest (BitcoinTestFramework ):
48
48
def set_test_params (self ):
49
- self .setup_clean_chain = True
50
49
self .num_nodes = 2
51
50
52
51
def send_to_address (self , addr , amount ):
53
- input = {"txid" : self .nodes [0 ].getblock (self .block_hash [self .blk_idx ])["tx" ][0 ], "vout" : 0 }
54
- output = {addr : amount }
55
- self .blk_idx += 1
56
- rawtx = self .nodes [0 ].createrawtransaction ([input ], output )
57
- txid = self .nodes [0 ].sendrawtransaction (self .nodes [0 ].signrawtransactionwithkey (rawtx , [self .nodes [0 ].get_deterministic_priv_key ().key ])["hex" ], 0 )
58
- return txid
52
+ script_pub_key = address_to_scriptpubkey (addr )
53
+ tx = self .wallet .send_to (from_node = self .nodes [0 ], scriptPubKey = script_pub_key , amount = int (amount * COIN ))
54
+ return tx ["txid" ], tx ["sent_vout" ]
59
55
60
56
def assert_signing_completed_successfully (self , signed_tx ):
61
57
assert 'errors' not in signed_tx
@@ -82,8 +78,6 @@ def witness_script_test(self):
82
78
embedded_privkey , embedded_pubkey = generate_keypair (wif = True )
83
79
p2sh_p2wsh_address = self .nodes [1 ].createmultisig (1 , [embedded_pubkey .hex ()], "p2sh-segwit" )
84
80
# send transaction to P2SH-P2WSH 1-of-1 multisig address
85
- self .block_hash = self .generate (self .nodes [0 ], COINBASE_MATURITY + 1 )
86
- self .blk_idx = 0
87
81
self .send_to_address (p2sh_p2wsh_address ["address" ], 49.999 )
88
82
self .generate (self .nodes [0 ], 1 )
89
83
# Get the UTXO info from scantxoutset
@@ -103,9 +97,9 @@ def witness_script_test(self):
103
97
104
98
def keyless_signing_test (self ):
105
99
self .log .info ("Test that keyless 'signing' of pay-to-anchor input succeeds" )
106
- funding_txid = self .send_to_address (p2a (), 49.999 )
100
+ [ txid , vout ] = self .send_to_address (p2a (), 49.999 )
107
101
spending_tx = self .nodes [0 ].createrawtransaction (
108
- [{"txid" : funding_txid , "vout" : 0 }],
102
+ [{"txid" : txid , "vout" : vout }],
109
103
[{getnewdestination ()[2 ]: Decimal ("49.998" )}])
110
104
spending_tx_signed = self .nodes [0 ].signrawtransactionwithkey (spending_tx , [], [])
111
105
self .assert_signing_completed_successfully (spending_tx_signed )
@@ -124,9 +118,7 @@ def verify_txn_with_witness_script(self, tx_type):
124
118
addr = script_to_p2sh (redeem_script )
125
119
script_pub_key = address_to_scriptpubkey (addr ).hex ()
126
120
# Fund that address
127
- txid = self .send_to_address (addr , 10 )
128
- vout = find_vout_for_address (self .nodes [0 ], txid , addr )
129
- self .generate (self .nodes [0 ], 1 )
121
+ [txid , vout ] = self .send_to_address (addr , 10 )
130
122
# Now create and sign a transaction spending that output on node[0], which doesn't know the scripts or keys
131
123
spending_tx = self .nodes [0 ].createrawtransaction ([{'txid' : txid , 'vout' : vout }], {getnewdestination ()[2 ]: Decimal ("9.999" )})
132
124
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 }])
@@ -149,6 +141,7 @@ def invalid_private_key_and_tx(self):
149
141
assert_raises_rpc_error (- 22 , "TX decode failed. Make sure the tx has at least one input." , self .nodes [0 ].signrawtransactionwithkey , tx + "00" , privkeys )
150
142
151
143
def run_test (self ):
144
+ self .wallet = MiniWallet (self .nodes [0 ])
152
145
self .successful_signing_test ()
153
146
self .witness_script_test ()
154
147
self .keyless_signing_test ()
0 commit comments