/ test / functional / rpc_signrawtransactionwithkey.py
rpc_signrawtransactionwithkey.py
  1  #!/usr/bin/env python3
  2  # Copyright (c) 2015-2022 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.blocktools import (
  8      COINBASE_MATURITY,
  9  )
 10  from test_framework.address import (
 11      address_to_scriptpubkey,
 12      script_to_p2sh,
 13  )
 14  from test_framework.test_framework import BitcoinTestFramework
 15  from test_framework.util import (
 16      assert_equal,
 17      assert_raises_rpc_error,
 18      find_vout_for_address,
 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  )
 29  from test_framework.wallet_util import (
 30      generate_keypair,
 31  )
 32  
 33  from decimal import (
 34      Decimal,
 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}
 45  
 46  class SignRawTransactionWithKeyTest(BitcoinTestFramework):
 47      def set_test_params(self):
 48          self.setup_clean_chain = True
 49          self.num_nodes = 2
 50  
 51      def send_to_address(self, addr, amount):
 52          input = {"txid": self.nodes[0].getblock(self.block_hash[self.blk_idx])["tx"][0], "vout": 0}
 53          output = {addr: amount}
 54          self.blk_idx += 1
 55          rawtx = self.nodes[0].createrawtransaction([input], output)
 56          txid = self.nodes[0].sendrawtransaction(self.nodes[0].signrawtransactionwithkey(rawtx, [self.nodes[0].get_deterministic_priv_key().key])["hex"], 0)
 57          return txid
 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  
 64      def successful_signing_test(self):
 65          """Create and sign a valid raw transaction with one input.
 66  
 67          Expected results:
 68  
 69          1) The transaction has a complete set of signatures
 70          2) No script verification error occurred"""
 71          self.log.info("Test valid raw transaction with one input")
 72          privKeys = ['cUeKHd5orzT3mz8P9pxyREHfsWtVfgsfDjiZZBcjUBAaGk1BTj7N', 'cVKpPfVKSJxKqVpE9awvXNWuLHCa5j5tiE7K6zbUSptFpTEtiFrA']
 73          rawTx = self.nodes[0].createrawtransaction(INPUTS, OUTPUTS)
 74          rawTxSigned = self.nodes[0].signrawtransactionwithkey(rawTx, privKeys, INPUTS)
 75  
 76          self.assert_signing_completed_successfully(rawTxSigned)
 77  
 78      def witness_script_test(self):
 79          self.log.info("Test signing transaction to P2SH-P2WSH addresses without wallet")
 80          # Create a new P2SH-P2WSH 1-of-1 multisig address:
 81          embedded_privkey, embedded_pubkey = generate_keypair(wif=True)
 82          p2sh_p2wsh_address = self.nodes[1].createmultisig(1, [embedded_pubkey.hex()], "p2sh-segwit")
 83          # send transaction to P2SH-P2WSH 1-of-1 multisig address
 84          self.block_hash = self.generate(self.nodes[0], COINBASE_MATURITY + 1)
 85          self.blk_idx = 0
 86          self.send_to_address(p2sh_p2wsh_address["address"], 49.999)
 87          self.generate(self.nodes[0], 1)
 88          # Get the UTXO info from scantxoutset
 89          unspent_output = self.nodes[1].scantxoutset('start', [p2sh_p2wsh_address['descriptor']])['unspents'][0]
 90          spk = script_to_p2sh_p2wsh_script(p2sh_p2wsh_address['redeemScript']).hex()
 91          unspent_output['witnessScript'] = p2sh_p2wsh_address['redeemScript']
 92          unspent_output['redeemScript'] = script_to_p2wsh_script(unspent_output['witnessScript']).hex()
 93          assert_equal(spk, unspent_output['scriptPubKey'])
 94          # Now create and sign a transaction spending that output on node[0], which doesn't know the scripts or keys
 95          spending_tx = self.nodes[0].createrawtransaction([unspent_output], {getnewdestination()[2]: Decimal("49.998")})
 96          spending_tx_signed = self.nodes[0].signrawtransactionwithkey(spending_tx, [embedded_privkey], [unspent_output])
 97          self.assert_signing_completed_successfully(spending_tx_signed)
 98  
 99          # Now test with P2PKH and P2PK scripts as the witnessScript
100          for tx_type in ['P2PKH', 'P2PK']:  # these tests are order-independent
101              self.verify_txn_with_witness_script(tx_type)
102  
103      def verify_txn_with_witness_script(self, tx_type):
104          self.log.info("Test with a {} script as the witnessScript".format(tx_type))
105          embedded_privkey, embedded_pubkey = generate_keypair(wif=True)
106          witness_script = {
107              'P2PKH': key_to_p2pkh_script(embedded_pubkey).hex(),
108              'P2PK': key_to_p2pk_script(embedded_pubkey).hex()
109          }.get(tx_type, "Invalid tx_type")
110          redeem_script = script_to_p2wsh_script(witness_script).hex()
111          addr = script_to_p2sh(redeem_script)
112          script_pub_key = address_to_scriptpubkey(addr).hex()
113          # Fund that address
114          txid = self.send_to_address(addr, 10)
115          vout = find_vout_for_address(self.nodes[0], txid, addr)
116          self.generate(self.nodes[0], 1)
117          # Now create and sign a transaction spending that output on node[0], which doesn't know the scripts or keys
118          spending_tx = self.nodes[0].createrawtransaction([{'txid': txid, 'vout': vout}], {getnewdestination()[2]: Decimal("9.999")})
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}])
120          self.assert_signing_completed_successfully(spending_tx_signed)
121          self.nodes[0].sendrawtransaction(spending_tx_signed['hex'])
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  
129      def run_test(self):
130          self.successful_signing_test()
131          self.witness_script_test()
132          self.invalid_sighashtype_test()
133  
134  
135  if __name__ == '__main__':
136      SignRawTransactionWithKeyTest().main()