rpc_signrawtransactionwithkey.py
1 #!/usr/bin/env python3 2 # Copyright (c) 2015-present The Bitcoin Core developers 3 # Distributed under the MIT software license, see the accompanying 4 # file COPYING or http://www.opensource.org/licenses/mit-license.php. 5 """Test transaction signing using the signrawtransactionwithkey RPC.""" 6 7 from test_framework.messages import ( 8 COIN, 9 ) 10 from test_framework.address import ( 11 address_to_scriptpubkey, 12 p2a, 13 script_to_p2sh, 14 ) 15 from test_framework.test_framework import BitcoinTestFramework 16 from test_framework.util import ( 17 assert_equal, 18 assert_raises_rpc_error, 19 ) 20 from test_framework.script_util import ( 21 key_to_p2pk_script, 22 key_to_p2pkh_script, 23 script_to_p2sh_p2wsh_script, 24 script_to_p2wsh_script, 25 ) 26 from test_framework.wallet import ( 27 getnewdestination, 28 MiniWallet, 29 ) 30 from test_framework.wallet_util import ( 31 generate_keypair, 32 ) 33 34 from decimal import ( 35 Decimal, 36 ) 37 38 INPUTS = [ 39 # Valid pay-to-pubkey scripts 40 {'txid': '9b907ef1e3c26fc71fe4a4b3580bc75264112f95050014157059c736f0202e71', 'vout': 0, 41 'scriptPubKey': '76a91460baa0f494b38ce3c940dea67f3804dc52d1fb9488ac'}, 42 {'txid': '83a4f6a6b73660e13ee6cb3c6063fa3759c50c9b7521d0536022961898f4fb02', 'vout': 0, 43 'scriptPubKey': '76a914669b857c03a5ed269d5d85a1ffac9ed5d663072788ac'}, 44 ] 45 OUTPUTS = {'mpLQjfK79b7CCV4VMJWEWAj5Mpx8Up5zxB': 0.1} 46 47 class SignRawTransactionWithKeyTest(BitcoinTestFramework): 48 def set_test_params(self): 49 self.num_nodes = 1 50 51 def send_to_address(self, addr, amount): 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"] 55 56 def assert_signing_completed_successfully(self, signed_tx): 57 assert 'errors' not in signed_tx 58 assert 'complete' in signed_tx 59 assert_equal(signed_tx['complete'], True) 60 61 def successful_signing_test(self): 62 """Create and sign a valid raw transaction with one input. 63 64 Expected results: 65 66 1) The transaction has a complete set of signatures 67 2) No script verification error occurred""" 68 self.log.info("Test valid raw transaction with one input") 69 privKeys = ['cUeKHd5orzT3mz8P9pxyREHfsWtVfgsfDjiZZBcjUBAaGk1BTj7N', 'cVKpPfVKSJxKqVpE9awvXNWuLHCa5j5tiE7K6zbUSptFpTEtiFrA'] 70 rawTx = self.nodes[0].createrawtransaction(INPUTS, OUTPUTS) 71 rawTxSigned = self.nodes[0].signrawtransactionwithkey(rawTx, privKeys, INPUTS) 72 73 self.assert_signing_completed_successfully(rawTxSigned) 74 75 def witness_script_test(self): 76 self.log.info("Test signing transaction to P2SH-P2WSH addresses without wallet") 77 # Create a new P2SH-P2WSH 1-of-1 multisig address: 78 embedded_privkey, embedded_pubkey = generate_keypair(wif=True) 79 p2sh_p2wsh_address = self.nodes[0].createmultisig(1, [embedded_pubkey.hex()], "p2sh-segwit") 80 # send transaction to P2SH-P2WSH 1-of-1 multisig address 81 self.send_to_address(p2sh_p2wsh_address["address"], 49.999) 82 self.generate(self.nodes[0], 1) 83 # Get the UTXO info from scantxoutset 84 unspent_output = self.nodes[0].scantxoutset('start', [p2sh_p2wsh_address['descriptor']])['unspents'][0] 85 spk = script_to_p2sh_p2wsh_script(p2sh_p2wsh_address['redeemScript']).hex() 86 unspent_output['witnessScript'] = p2sh_p2wsh_address['redeemScript'] 87 unspent_output['redeemScript'] = script_to_p2wsh_script(unspent_output['witnessScript']).hex() 88 assert_equal(spk, unspent_output['scriptPubKey']) 89 # Now create and sign a transaction spending that output on node[0], which doesn't know the scripts or keys 90 spending_tx = self.nodes[0].createrawtransaction([unspent_output], {getnewdestination()[2]: Decimal("49.998")}) 91 spending_tx_signed = self.nodes[0].signrawtransactionwithkey(spending_tx, [embedded_privkey], [unspent_output]) 92 self.assert_signing_completed_successfully(spending_tx_signed) 93 94 # Now test with P2PKH and P2PK scripts as the witnessScript 95 for tx_type in ['P2PKH', 'P2PK']: # these tests are order-independent 96 self.verify_txn_with_witness_script(tx_type) 97 98 def keyless_signing_test(self): 99 self.log.info("Test that keyless 'signing' of pay-to-anchor input succeeds") 100 [txid, vout] = self.send_to_address(p2a(), 49.999) 101 spending_tx = self.nodes[0].createrawtransaction( 102 [{"txid": txid, "vout": vout}], 103 [{getnewdestination()[2]: Decimal("49.998")}]) 104 spending_tx_signed = self.nodes[0].signrawtransactionwithkey(spending_tx, [], []) 105 self.assert_signing_completed_successfully(spending_tx_signed) 106 assert self.nodes[0].testmempoolaccept([spending_tx_signed["hex"]])[0]["allowed"] 107 # 'signing' a P2A prevout is a no-op, so signed and unsigned txs shouldn't differ 108 assert_equal(spending_tx, spending_tx_signed["hex"]) 109 110 def verify_txn_with_witness_script(self, tx_type): 111 self.log.info("Test with a {} script as the witnessScript".format(tx_type)) 112 embedded_privkey, embedded_pubkey = generate_keypair(wif=True) 113 witness_script = { 114 'P2PKH': key_to_p2pkh_script(embedded_pubkey).hex(), 115 'P2PK': key_to_p2pk_script(embedded_pubkey).hex() 116 }.get(tx_type, "Invalid tx_type") 117 redeem_script = script_to_p2wsh_script(witness_script).hex() 118 addr = script_to_p2sh(redeem_script) 119 script_pub_key = address_to_scriptpubkey(addr).hex() 120 # Fund that address 121 [txid, vout] = self.send_to_address(addr, 10) 122 # Now create and sign a transaction spending that output on node[0], which doesn't know the scripts or keys 123 spending_tx = self.nodes[0].createrawtransaction([{'txid': txid, 'vout': vout}], {getnewdestination()[2]: Decimal("9.999")}) 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}]) 125 self.assert_signing_completed_successfully(spending_tx_signed) 126 self.nodes[0].sendrawtransaction(spending_tx_signed['hex']) 127 128 def invalid_sighashtype_test(self): 129 self.log.info("Test signing transaction with invalid sighashtype") 130 tx = self.nodes[0].createrawtransaction(INPUTS, OUTPUTS) 131 privkeys = [self.nodes[0].get_deterministic_priv_key().key] 132 assert_raises_rpc_error(-8, "'all' is not a valid sighash parameter.", self.nodes[0].signrawtransactionwithkey, tx, privkeys, sighashtype="all") 133 134 def invalid_private_key_and_tx(self): 135 self.log.info("Test signing transaction with an invalid private key") 136 tx = self.nodes[0].createrawtransaction(INPUTS, OUTPUTS) 137 privkeys = ["123"] 138 assert_raises_rpc_error(-5, "Invalid private key", self.nodes[0].signrawtransactionwithkey, tx, privkeys) 139 self.log.info("Test signing transaction with an invalid tx hex") 140 privkeys = [self.nodes[0].get_deterministic_priv_key().key] 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) 142 143 def run_test(self): 144 self.wallet = MiniWallet(self.nodes[0]) 145 self.successful_signing_test() 146 self.witness_script_test() 147 self.keyless_signing_test() 148 self.invalid_sighashtype_test() 149 self.invalid_private_key_and_tx() 150 151 152 if __name__ == '__main__': 153 SignRawTransactionWithKeyTest(__file__).main()