44# file COPYING or http://www.opensource.org/licenses/mit-license.php.
55"""Test transaction signing using the signrawtransactionwithkey RPC."""
66
7- from test_framework .blocktools import (
8- COINBASE_MATURITY ,
7+ from test_framework .messages import (
8+ COIN ,
99)
1010from test_framework .address import (
1111 address_to_scriptpubkey ,
1616from test_framework .util import (
1717 assert_equal ,
1818 assert_raises_rpc_error ,
19- find_vout_for_address ,
2019)
2120from test_framework .script_util import (
2221 key_to_p2pk_script ,
2625)
2726from test_framework .wallet import (
2827 getnewdestination ,
28+ MiniWallet ,
2929)
3030from test_framework .wallet_util import (
3131 generate_keypair ,
4646
4747class SignRawTransactionWithKeyTest (BitcoinTestFramework ):
4848 def set_test_params (self ):
49- self .setup_clean_chain = True
5049 self .num_nodes = 2
5150
5251 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" ]
5955
6056 def assert_signing_completed_successfully (self , signed_tx ):
6157 assert 'errors' not in signed_tx
@@ -82,8 +78,6 @@ def witness_script_test(self):
8278 embedded_privkey , embedded_pubkey = generate_keypair (wif = True )
8379 p2sh_p2wsh_address = self .nodes [1 ].createmultisig (1 , [embedded_pubkey .hex ()], "p2sh-segwit" )
8480 # 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
8781 self .send_to_address (p2sh_p2wsh_address ["address" ], 49.999 )
8882 self .generate (self .nodes [0 ], 1 )
8983 # Get the UTXO info from scantxoutset
@@ -103,9 +97,9 @@ def witness_script_test(self):
10397
10498 def keyless_signing_test (self ):
10599 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 )
107101 spending_tx = self .nodes [0 ].createrawtransaction (
108- [{"txid" : funding_txid , "vout" : 0 }],
102+ [{"txid" : txid , "vout" : vout }],
109103 [{getnewdestination ()[2 ]: Decimal ("49.998" )}])
110104 spending_tx_signed = self .nodes [0 ].signrawtransactionwithkey (spending_tx , [], [])
111105 self .assert_signing_completed_successfully (spending_tx_signed )
@@ -124,9 +118,7 @@ def verify_txn_with_witness_script(self, tx_type):
124118 addr = script_to_p2sh (redeem_script )
125119 script_pub_key = address_to_scriptpubkey (addr ).hex ()
126120 # 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 )
130122 # Now create and sign a transaction spending that output on node[0], which doesn't know the scripts or keys
131123 spending_tx = self .nodes [0 ].createrawtransaction ([{'txid' : txid , 'vout' : vout }], {getnewdestination ()[2 ]: Decimal ("9.999" )})
132124 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):
149141 assert_raises_rpc_error (- 22 , "TX decode failed. Make sure the tx has at least one input." , self .nodes [0 ].signrawtransactionwithkey , tx + "00" , privkeys )
150142
151143 def run_test (self ):
144+ self .wallet = MiniWallet (self .nodes [0 ])
152145 self .successful_signing_test ()
153146 self .witness_script_test ()
154147 self .keyless_signing_test ()
0 commit comments