feature_segwit.py raw

   1  #!/usr/bin/env python3
   2  # Copyright (c) 2016-2022 The Limenka 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 the SegWit changeover logic."""
   6  
   7  from decimal import Decimal
   8  
   9  from test_framework.address import (
  10      key_to_p2pkh,
  11      program_to_witness,
  12      script_to_p2sh,
  13      script_to_p2sh_p2wsh,
  14      script_to_p2wsh,
  15  )
  16  from test_framework.blocktools import (
  17      send_to_witness,
  18      witness_script,
  19  )
  20  from test_framework.descriptors import descsum_create
  21  from test_framework.messages import (
  22      COIN,
  23      COutPoint,
  24      CTransaction,
  25      CTxIn,
  26      CTxOut,
  27      tx_from_hex,
  28  )
  29  from test_framework.script import (
  30      CScript,
  31      OP_0,
  32      OP_1,
  33      OP_DROP,
  34      OP_TRUE,
  35  )
  36  from test_framework.script_util import (
  37      key_to_p2pk_script,
  38      key_to_p2pkh_script,
  39      key_to_p2wpkh_script,
  40      keys_to_multisig_script,
  41      script_to_p2sh_script,
  42      script_to_p2wsh_script,
  43  )
  44  from test_framework.test_framework import LimenkaTestFramework
  45  from test_framework.util import (
  46      assert_approx,
  47      assert_equal,
  48      assert_greater_than_or_equal,
  49      assert_is_hex_string,
  50      assert_raises_rpc_error,
  51      try_rpc,
  52  )
  53  from test_framework.wallet_util import (
  54      get_generate_key,
  55  )
  56  
  57  NODE_0 = 0
  58  NODE_2 = 2
  59  P2WPKH = 0
  60  P2WSH = 1
  61  
  62  
  63  def getutxo(txid):
  64      utxo = {}
  65      utxo["vout"] = 0
  66      utxo["txid"] = txid
  67      return utxo
  68  
  69  
  70  def find_spendable_utxo(node, min_value):
  71      for utxo in node.listunspent(query_options={'minimumAmount': min_value}):
  72          if utxo['spendable']:
  73              return utxo
  74  
  75      raise AssertionError(f"Unspent output equal or higher than {min_value} not found")
  76  
  77  
  78  txs_mined = {}  # txindex from txid to blockhash
  79  
  80  
  81  class SegWitTest(LimenkaTestFramework):
  82      def add_options(self, parser):
  83          self.add_wallet_options(parser)
  84  
  85      def set_test_params(self):
  86          self.setup_clean_chain = True
  87          self.num_nodes = 3
  88          # This test tests SegWit both pre and post-activation, so use the normal BIP9 activation.
  89          self.extra_args = [
  90              [
  91                  "-acceptnonstdtxn=1",
  92                  "-testactivationheight=segwit@165",
  93                  "-addresstype=legacy",
  94              ],
  95              [
  96                  "-acceptnonstdtxn=1",
  97                  "-testactivationheight=segwit@165",
  98                  "-addresstype=legacy",
  99              ],
 100              [
 101                  "-acceptnonstdtxn=1",
 102                  "-testactivationheight=segwit@165",
 103                  "-addresstype=legacy",
 104              ],
 105          ]
 106          self.rpc_timeout = 120
 107  
 108      def skip_test_if_missing_module(self):
 109          self.skip_if_no_wallet()
 110  
 111      def setup_network(self):
 112          super().setup_network()
 113          self.connect_nodes(0, 2)
 114          self.sync_all()
 115  
 116      def success_mine(self, node, txid, sign, redeem_script=""):
 117          send_to_witness(1, node, getutxo(txid), self.pubkey[0], False, Decimal("49.998"), sign, redeem_script)
 118          block = self.generate(node, 1)
 119          assert_equal(len(node.getblock(block[0])["tx"]), 2)
 120          self.sync_blocks()
 121  
 122      def fail_accept(self, node, error_msg, txid, sign, redeem_script=""):
 123          assert_raises_rpc_error(-26, error_msg, send_to_witness, use_p2wsh=1, node=node, utxo=getutxo(txid), pubkey=self.pubkey[0], encode_p2sh=False, amount=Decimal("49.998"), sign=sign, insert_redeem_script=redeem_script)
 124  
 125      def run_test(self):
 126          self.generate(self.nodes[0], 161)  # block 161
 127  
 128          self.log.info("Verify sigops are counted in GBT with pre-BIP141 rules before the fork")
 129          txid = self.nodes[0].sendtoaddress(self.nodes[0].getnewaddress(), 1)
 130          tmpl = self.nodes[0].getblocktemplate({'rules': ['segwit']})
 131          assert_equal(tmpl['sizelimit'], 1000000)
 132          assert 'weightlimit' not in tmpl
 133          assert_equal(tmpl['sigoplimit'], 20000)
 134          assert_equal(tmpl['transactions'][0]['hash'], txid)
 135          assert_equal(tmpl['transactions'][0]['sigops'], 2)
 136          assert '!segwit' not in tmpl['rules']
 137          self.generate(self.nodes[0], 1)  # block 162
 138  
 139          balance_presetup = self.nodes[0].getbalance()
 140          self.pubkey = []
 141          p2sh_ids = []  # p2sh_ids[NODE][TYPE] is an array of txids that spend to P2WPKH (TYPE=0) or P2WSH (TYPE=1) scripts to an address for NODE embedded in p2sh
 142          wit_ids = []  # wit_ids[NODE][TYPE] is an array of txids that spend to P2WPKH (TYPE=0) or P2WSH (TYPE=1) scripts to an address for NODE via bare witness
 143          for i in range(3):
 144              key = get_generate_key()
 145              self.pubkey.append(key.pubkey)
 146  
 147              multiscript = keys_to_multisig_script([self.pubkey[-1]])
 148              p2sh_ms_addr = self.nodes[i].createmultisig(1, [self.pubkey[-1]], 'p2sh-segwit')['address']
 149              bip173_ms_addr = self.nodes[i].createmultisig(1, [self.pubkey[-1]], 'bech32')['address']
 150              assert_equal(p2sh_ms_addr, script_to_p2sh_p2wsh(multiscript))
 151              assert_equal(bip173_ms_addr, script_to_p2wsh(multiscript))
 152  
 153              p2sh_ms_desc = descsum_create(f"sh(wsh(multi(1,{key.privkey})))")
 154              bip173_ms_desc = descsum_create(f"wsh(multi(1,{key.privkey}))")
 155              assert_equal(self.nodes[i].deriveaddresses(p2sh_ms_desc)[0], p2sh_ms_addr)
 156              assert_equal(self.nodes[i].deriveaddresses(bip173_ms_desc)[0], bip173_ms_addr)
 157  
 158              sh_wpkh_desc = descsum_create(f"sh(wpkh({key.privkey}))")
 159              wpkh_desc = descsum_create(f"wpkh({key.privkey})")
 160              assert_equal(self.nodes[i].deriveaddresses(sh_wpkh_desc)[0], key.p2sh_p2wpkh_addr)
 161              assert_equal(self.nodes[i].deriveaddresses(wpkh_desc)[0], key.p2wpkh_addr)
 162  
 163              if self.options.descriptors:
 164                  res = self.nodes[i].importdescriptors([
 165                  {"desc": p2sh_ms_desc, "timestamp": "now"},
 166                  {"desc": bip173_ms_desc, "timestamp": "now"},
 167                  {"desc": sh_wpkh_desc, "timestamp": "now"},
 168                  {"desc": wpkh_desc, "timestamp": "now"},
 169              ])
 170              else:
 171                  # The nature of the legacy wallet is that this import results in also adding all of the necessary scripts
 172                  res = self.nodes[i].importmulti([
 173                      {"desc": p2sh_ms_desc, "timestamp": "now"},
 174                  ])
 175              assert all([r["success"] for r in res])
 176  
 177              p2sh_ids.append([])
 178              wit_ids.append([])
 179              for _ in range(2):
 180                  p2sh_ids[i].append([])
 181                  wit_ids[i].append([])
 182  
 183          for _ in range(5):
 184              for n in range(3):
 185                  for v in range(2):
 186                      wit_ids[n][v].append(send_to_witness(v, self.nodes[0], find_spendable_utxo(self.nodes[0], 50), self.pubkey[n], False, Decimal("49.999")))
 187                      p2sh_ids[n][v].append(send_to_witness(v, self.nodes[0], find_spendable_utxo(self.nodes[0], 50), self.pubkey[n], True, Decimal("49.999")))
 188  
 189          self.generate(self.nodes[0], 1)  # block 163
 190  
 191          # Make sure all nodes recognize the transactions as theirs
 192          assert_equal(self.nodes[0].getbalance(), balance_presetup - 60 * 50 + 20 * Decimal("49.999") + 50)
 193          assert_equal(self.nodes[1].getbalance(), 20 * Decimal("49.999"))
 194          assert_equal(self.nodes[2].getbalance(), 20 * Decimal("49.999"))
 195  
 196          self.log.info("Verify unsigned p2sh witness txs without a redeem script are invalid")
 197          self.fail_accept(self.nodes[2], "mempool-script-verify-flag-failed (Operation not valid with the current stack size)", p2sh_ids[NODE_2][P2WPKH][1], sign=False)
 198          self.fail_accept(self.nodes[2], "mempool-script-verify-flag-failed (Operation not valid with the current stack size)", p2sh_ids[NODE_2][P2WSH][1], sign=False)
 199  
 200          self.generate(self.nodes[0], 1)  # block 164
 201  
 202          self.log.info("Verify witness txs are mined as soon as segwit activates")
 203  
 204          send_to_witness(1, self.nodes[2], getutxo(wit_ids[NODE_2][P2WPKH][0]), self.pubkey[0], encode_p2sh=False, amount=Decimal("49.998"), sign=True)
 205          send_to_witness(1, self.nodes[2], getutxo(wit_ids[NODE_2][P2WSH][0]), self.pubkey[0], encode_p2sh=False, amount=Decimal("49.998"), sign=True)
 206          send_to_witness(1, self.nodes[2], getutxo(p2sh_ids[NODE_2][P2WPKH][0]), self.pubkey[0], encode_p2sh=False, amount=Decimal("49.998"), sign=True)
 207          send_to_witness(1, self.nodes[2], getutxo(p2sh_ids[NODE_2][P2WSH][0]), self.pubkey[0], encode_p2sh=False, amount=Decimal("49.998"), sign=True)
 208  
 209          assert_equal(len(self.nodes[2].getrawmempool()), 4)
 210          blockhash = self.generate(self.nodes[2], 1)[0]  # block 165 (first block with new rules)
 211          assert_equal(len(self.nodes[2].getrawmempool()), 0)
 212          segwit_tx_list = self.nodes[2].getblock(blockhash)["tx"]
 213          assert_equal(len(segwit_tx_list), 5)
 214  
 215          self.log.info("Verify default node can't accept txs with missing witness")
 216          # unsigned, no scriptsig
 217          self.fail_accept(self.nodes[0], "mempool-script-verify-flag-failed (Witness program hash mismatch)", wit_ids[NODE_0][P2WPKH][0], sign=False)
 218          self.fail_accept(self.nodes[0], "mempool-script-verify-flag-failed (Witness program was passed an empty witness)", wit_ids[NODE_0][P2WSH][0], sign=False)
 219          self.fail_accept(self.nodes[0], "mempool-script-verify-flag-failed (Operation not valid with the current stack size)", p2sh_ids[NODE_0][P2WPKH][0], sign=False)
 220          self.fail_accept(self.nodes[0], "mempool-script-verify-flag-failed (Operation not valid with the current stack size)", p2sh_ids[NODE_0][P2WSH][0], sign=False)
 221          # unsigned with redeem script
 222          self.fail_accept(self.nodes[0], "mempool-script-verify-flag-failed (Witness program hash mismatch)", p2sh_ids[NODE_0][P2WPKH][0], sign=False, redeem_script=witness_script(False, self.pubkey[0]))
 223          self.fail_accept(self.nodes[0], "mempool-script-verify-flag-failed (Witness program was passed an empty witness)", p2sh_ids[NODE_0][P2WSH][0], sign=False, redeem_script=witness_script(True, self.pubkey[0]))
 224  
 225          # Coinbase contains the witness commitment nonce, check that RPC shows us
 226          coinbase_txid = self.nodes[2].getblock(blockhash)['tx'][0]
 227          coinbase_tx = self.nodes[2].gettransaction(txid=coinbase_txid, verbose=True)
 228          witnesses = coinbase_tx["decoded"]["vin"][0]["txinwitness"]
 229          assert_equal(len(witnesses), 1)
 230          assert_is_hex_string(witnesses[0])
 231          assert_equal(witnesses[0], '00' * 32)
 232  
 233          self.log.info("Verify witness txs without witness data are invalid after the fork")
 234          self.fail_accept(self.nodes[2], 'mempool-script-verify-flag-failed (Witness program hash mismatch)', wit_ids[NODE_2][P2WPKH][2], sign=False)
 235          self.fail_accept(self.nodes[2], 'mempool-script-verify-flag-failed (Witness program was passed an empty witness)', wit_ids[NODE_2][P2WSH][2], sign=False)
 236          self.fail_accept(self.nodes[2], 'mempool-script-verify-flag-failed (Witness program hash mismatch)', p2sh_ids[NODE_2][P2WPKH][2], sign=False, redeem_script=witness_script(False, self.pubkey[2]))
 237          self.fail_accept(self.nodes[2], 'mempool-script-verify-flag-failed (Witness program was passed an empty witness)', p2sh_ids[NODE_2][P2WSH][2], sign=False, redeem_script=witness_script(True, self.pubkey[2]))
 238  
 239          self.log.info("Verify default node can now use witness txs")
 240          self.success_mine(self.nodes[0], wit_ids[NODE_0][P2WPKH][0], True)
 241          self.success_mine(self.nodes[0], wit_ids[NODE_0][P2WSH][0], True)
 242          self.success_mine(self.nodes[0], p2sh_ids[NODE_0][P2WPKH][0], True)
 243          self.success_mine(self.nodes[0], p2sh_ids[NODE_0][P2WSH][0], True)
 244  
 245          self.log.info("Verify sigops are counted in GBT with BIP141 rules after the fork")
 246          txid = self.nodes[0].sendtoaddress(self.nodes[0].getnewaddress(), 1)
 247          raw_tx = self.nodes[0].getrawtransaction(txid, True)
 248          tmpl = self.nodes[0].getblocktemplate({'rules': ['segwit']})
 249          assert_greater_than_or_equal(tmpl['sizelimit'], 3999577)  # actual maximum size is lower due to minimum mandatory non-witness data
 250          assert_equal(tmpl['weightlimit'], 4000000)
 251          assert_equal(tmpl['sigoplimit'], 80000)
 252          assert_equal(tmpl['transactions'][0]['txid'], txid)
 253          expected_sigops = 9 if 'txinwitness' in raw_tx["vin"][0] else 8
 254          assert_equal(tmpl['transactions'][0]['sigops'], expected_sigops)
 255          assert '!segwit' in tmpl['rules']
 256  
 257          self.generate(self.nodes[0], 1)  # Mine a block to clear the gbt cache
 258  
 259          self.log.info("Non-segwit miners are able to use GBT response after activation.")
 260          # Create a 3-tx chain: tx1 (non-segwit input, paying to a segwit output) ->
 261          #                      tx2 (segwit input, paying to a non-segwit output) ->
 262          #                      tx3 (non-segwit input, paying to a non-segwit output).
 263          # tx1 is allowed to appear in the block, but no others.
 264          txid1 = send_to_witness(1, self.nodes[0], find_spendable_utxo(self.nodes[0], 50), self.pubkey[0], False, Decimal("49.996"))
 265          assert txid1 in self.nodes[0].getrawmempool()
 266  
 267          tx1_hex = self.nodes[0].gettransaction(txid1)['hex']
 268          tx1 = tx_from_hex(tx1_hex)
 269  
 270          # Check that hash and wtxid are properly reported in mempool entry (txid1)
 271          assert_equal(int(self.nodes[0].getmempoolentry(txid1)["hash"], 16), tx1.calc_sha256(True))
 272          assert_equal(int(self.nodes[0].getmempoolentry(txid1)["wtxid"], 16), tx1.calc_sha256(True))
 273  
 274          # Check that weight and vsize are properly reported in mempool entry (txid1)
 275          assert_equal(self.nodes[0].getmempoolentry(txid1)["vsize"], tx1.get_vsize())
 276          assert_equal(self.nodes[0].getmempoolentry(txid1)["weight"], tx1.get_weight())
 277  
 278          # Now create tx2, which will spend from txid1.
 279          tx = CTransaction()
 280          tx.vin.append(CTxIn(COutPoint(int(txid1, 16), 0), b''))
 281          tx.vout.append(CTxOut(int(49.99 * COIN), CScript([OP_TRUE, OP_DROP] * 15 + [OP_TRUE])))
 282          tx2_hex = self.nodes[0].signrawtransactionwithwallet(tx.serialize().hex())['hex']
 283          txid2 = self.nodes[0].sendrawtransaction(tx2_hex)
 284          tx = tx_from_hex(tx2_hex)
 285          assert not tx.wit.is_null()
 286  
 287          # Check that hash and wtxid are properly reported in mempool entry (txid2)
 288          assert_equal(int(self.nodes[0].getmempoolentry(txid2)["hash"], 16), tx.calc_sha256(True))
 289          assert_equal(int(self.nodes[0].getmempoolentry(txid2)["wtxid"], 16), tx.calc_sha256(True))
 290  
 291          # Check that weight and vsize are properly reported in mempool entry (txid2)
 292          assert_equal(self.nodes[0].getmempoolentry(txid2)["vsize"], tx.get_vsize())
 293          assert_equal(self.nodes[0].getmempoolentry(txid2)["weight"], tx.get_weight())
 294  
 295          # Now create tx3, which will spend from txid2
 296          tx = CTransaction()
 297          tx.vin.append(CTxIn(COutPoint(int(txid2, 16), 0), b""))
 298          tx.vout.append(CTxOut(int(49.95 * COIN), CScript([OP_TRUE, OP_DROP] * 15 + [OP_TRUE])))  # Huge fee
 299          tx.calc_sha256()
 300          txid3 = self.nodes[0].sendrawtransaction(hexstring=tx.serialize().hex(), maxfeerate=0)
 301          assert tx.wit.is_null()
 302          assert txid3 in self.nodes[0].getrawmempool()
 303  
 304          # Check that getblocktemplate includes all transactions.
 305          template = self.nodes[0].getblocktemplate({"rules": ["segwit"]})
 306          template_txids = [t['txid'] for t in template['transactions']]
 307          assert txid1 in template_txids
 308          assert txid2 in template_txids
 309          assert txid3 in template_txids
 310  
 311          # Check that hash and wtxid are properly reported in mempool entry (txid3)
 312          assert_equal(int(self.nodes[0].getmempoolentry(txid3)["hash"], 16), tx.calc_sha256(True))
 313          assert_equal(int(self.nodes[0].getmempoolentry(txid3)["wtxid"], 16), tx.calc_sha256(True))
 314  
 315          # Check that weight and vsize are properly reported in mempool entry (txid3)
 316          assert_equal(self.nodes[0].getmempoolentry(txid3)["vsize"], tx.get_vsize())
 317          assert_equal(self.nodes[0].getmempoolentry(txid3)["weight"], tx.get_weight())
 318  
 319          # Mine a block to clear the gbt cache again.
 320          self.generate(self.nodes[0], 1)
 321  
 322          self.log.info("Signing with all-segwit inputs reveals fee rate")
 323          addr = self.nodes[0].getnewaddress(address_type='p2sh-segwit')
 324          txid = self.nodes[0].sendtoaddress(addr, 1)
 325          tx = self.nodes[0].getrawtransaction(txid, True)
 326          n = -1
 327          value = -1
 328          for o in tx["vout"]:
 329              if o["scriptPubKey"]["address"] == addr:
 330                  n = o["n"]
 331                  value = Decimal(o["value"])
 332                  break
 333          assert n > -1 # failure means we could not find the address in the outputs despite sending to it
 334          assert_equal(value, 1) # failure means we got an unexpected amount of coins, despite trying to send 1
 335          fee = Decimal("0.00010000")
 336          value_out = value - fee
 337          self.generatetoaddress(self.nodes[0], 1, self.nodes[0].getnewaddress())
 338          raw = self.nodes[0].createrawtransaction([{"txid" : txid, "vout" : n}], [{self.nodes[0].getnewaddress() : value_out}])
 339          signed = self.nodes[0].signrawtransactionwithwallet(raw)
 340          assert_equal(signed["complete"], True)
 341          txsize = self.nodes[0].decoderawtransaction(signed['hex'])['vsize']
 342          exp_feerate = 1000 * fee / Decimal(txsize)
 343          assert_approx(signed["feerate"], exp_feerate, Decimal("0.00000010"))
 344          # discrepancy = 100000000 * (exp_feerate - signed["feerate"])
 345          # assert -10 < discrepancy < 10
 346          assert_equal(Decimal(signed["fee"]), fee)
 347  
 348          if not self.options.descriptors:
 349              self.log.info("Verify behaviour of importaddress and listunspent")
 350  
 351              # Some public keys to be used later
 352              pubkeys = [
 353                  "0363D44AABD0F1699138239DF2F042C3282C0671CC7A76826A55C8203D90E39242",  # cPiM8Ub4heR9NBYmgVzJQiUH1if44GSBGiqaeJySuL2BKxubvgwb
 354                  "02D3E626B3E616FC8662B489C123349FECBFC611E778E5BE739B257EAE4721E5BF",  # cPpAdHaD6VoYbW78kveN2bsvb45Q7G5PhaPApVUGwvF8VQ9brD97
 355                  "04A47F2CBCEFFA7B9BCDA184E7D5668D3DA6F9079AD41E422FA5FD7B2D458F2538A62F5BD8EC85C2477F39650BD391EA6250207065B2A81DA8B009FC891E898F0E",  # 91zqCU5B9sdWxzMt1ca3VzbtVm2YM6Hi5Rxn4UDtxEaN9C9nzXV
 356                  "02A47F2CBCEFFA7B9BCDA184E7D5668D3DA6F9079AD41E422FA5FD7B2D458F2538",  # cPQFjcVRpAUBG8BA9hzr2yEzHwKoMgLkJZBBtK9vJnvGJgMjzTbd
 357                  "036722F784214129FEB9E8129D626324F3F6716555B603FFE8300BBCB882151228",  # cQGtcm34xiLjB1v7bkRa4V3aAc9tS2UTuBZ1UnZGeSeNy627fN66
 358                  "0266A8396EE936BF6D99D17920DB21C6C7B1AB14C639D5CD72B300297E416FD2EC",  # cTW5mR5M45vHxXkeChZdtSPozrFwFgmEvTNnanCW6wrqwaCZ1X7K
 359                  "0450A38BD7F0AC212FEBA77354A9B036A32E0F7C81FC4E0C5ADCA7C549C4505D2522458C2D9AE3CEFD684E039194B72C8A10F9CB9D4764AB26FCC2718D421D3B84",  # 92h2XPssjBpsJN5CqSP7v9a7cf2kgDunBC6PDFwJHMACM1rrVBJ
 360              ]
 361  
 362              # Import a compressed key and an uncompressed key, generate some multisig addresses
 363              self.nodes[0].importprivkey("92e6XLo5jVAVwrQKPNTs93oQco8f8sDNBcpv73Dsrs397fQtFQn")
 364              uncompressed_spendable_address = ["mvozP4UwyGD2mGZU4D2eMvMLPB9WkMmMQu"]
 365              self.nodes[0].importprivkey("cNC8eQ5dg3mFAVePDX4ddmPYpPbw41r9bm2jd1nLJT77e6RrzTRR")
 366              compressed_spendable_address = ["mmWQubrDomqpgSYekvsU7HWEVjLFHAakLe"]
 367              assert not self.nodes[0].getaddressinfo(uncompressed_spendable_address[0])['iscompressed']
 368              assert self.nodes[0].getaddressinfo(compressed_spendable_address[0])['iscompressed']
 369  
 370              self.nodes[0].importpubkey(pubkeys[0])
 371              compressed_solvable_address = [key_to_p2pkh(pubkeys[0])]
 372              self.nodes[0].importpubkey(pubkeys[1])
 373              compressed_solvable_address.append(key_to_p2pkh(pubkeys[1]))
 374              self.nodes[0].importpubkey(pubkeys[2])
 375              uncompressed_solvable_address = [key_to_p2pkh(pubkeys[2])]
 376  
 377              spendable_anytime = []                      # These outputs should be seen anytime after importprivkey and addmultisigaddress
 378              spendable_after_importaddress = []          # These outputs should be seen after importaddress
 379              solvable_after_importaddress = []           # These outputs should be seen after importaddress but not spendable
 380              unsolvable_after_importaddress = []         # These outputs should be unsolvable after importaddress
 381              solvable_anytime = []                       # These outputs should be solvable after importpubkey
 382              unseen_anytime = []                         # These outputs should never be seen
 383  
 384              uncompressed_spendable_address.append(self.nodes[0].addmultisigaddress(2, [uncompressed_spendable_address[0], compressed_spendable_address[0]])['address'])
 385              uncompressed_spendable_address.append(self.nodes[0].addmultisigaddress(2, [uncompressed_spendable_address[0], uncompressed_spendable_address[0]])['address'])
 386              compressed_spendable_address.append(self.nodes[0].addmultisigaddress(2, [compressed_spendable_address[0], compressed_spendable_address[0]])['address'])
 387              uncompressed_solvable_address.append(self.nodes[0].addmultisigaddress(2, [compressed_spendable_address[0], uncompressed_solvable_address[0]])['address'])
 388              compressed_solvable_address.append(self.nodes[0].addmultisigaddress(2, [compressed_spendable_address[0], compressed_solvable_address[0]])['address'])
 389              compressed_solvable_address.append(self.nodes[0].addmultisigaddress(2, [compressed_solvable_address[0], compressed_solvable_address[1]])['address'])
 390  
 391              # Test multisig_without_privkey
 392              # We have 2 public keys without private keys, use addmultisigaddress to add to wallet.
 393              # Money sent to P2SH of multisig of this should only be seen after importaddress with the BASE58 P2SH address.
 394  
 395              multisig_without_privkey_address = self.nodes[0].addmultisigaddress(2, [pubkeys[3], pubkeys[4]])['address']
 396              script = keys_to_multisig_script([pubkeys[3], pubkeys[4]])
 397              solvable_after_importaddress.append(script_to_p2sh_script(script))
 398  
 399              for i in compressed_spendable_address:
 400                  v = self.nodes[0].getaddressinfo(i)
 401                  if v['isscript']:
 402                      [bare, p2sh, p2wsh, p2sh_p2wsh] = self.p2sh_address_to_script(v)
 403                      # p2sh multisig with compressed keys should always be spendable
 404                      spendable_anytime.extend([p2sh])
 405                      # P2WSH and P2SH(P2WSH) multisig with compressed keys are spendable after direct importaddress
 406                      spendable_after_importaddress.extend([p2wsh, p2sh_p2wsh])
 407                  else:
 408                      [p2wpkh, p2sh_p2wpkh, p2pk, p2pkh, p2sh_p2pk, p2sh_p2pkh, p2wsh_p2pk, p2wsh_p2pkh, p2sh_p2wsh_p2pk, p2sh_p2wsh_p2pkh] = self.p2pkh_address_to_script(v)
 409                      # normal P2PKH and P2PK with compressed keys should always be spendable
 410                      spendable_anytime.extend([p2pkh])
 411                      # P2SH_P2PK, P2SH_P2PKH with compressed keys are spendable after direct importaddress
 412                      spendable_after_importaddress.extend([p2sh_p2pk, p2sh_p2pkh, p2wsh_p2pk, p2wsh_p2pkh, p2sh_p2wsh_p2pk, p2sh_p2wsh_p2pkh])
 413                      # P2WPKH and P2SH_P2WPKH with compressed keys should always be spendable
 414                      spendable_anytime.extend([p2wpkh, p2sh_p2wpkh])
 415  
 416              for i in uncompressed_spendable_address:
 417                  v = self.nodes[0].getaddressinfo(i)
 418                  if v['isscript']:
 419                      [bare, p2sh, p2wsh, p2sh_p2wsh] = self.p2sh_address_to_script(v)
 420                      # p2sh multisig with uncompressed keys should always be spendable
 421                      spendable_anytime.extend([p2sh])
 422                      # P2WSH and P2SH(P2WSH) multisig with uncompressed keys are never seen
 423                      unseen_anytime.extend([p2wsh, p2sh_p2wsh])
 424                  else:
 425                      [p2wpkh, p2sh_p2wpkh, p2pk, p2pkh, p2sh_p2pk, p2sh_p2pkh, p2wsh_p2pk, p2wsh_p2pkh, p2sh_p2wsh_p2pk, p2sh_p2wsh_p2pkh] = self.p2pkh_address_to_script(v)
 426                      # normal P2PKH and P2PK with uncompressed keys should always be spendable
 427                      spendable_anytime.extend([p2pkh])
 428                      # P2SH_P2PK and P2SH_P2PKH are spendable after direct importaddress
 429                      spendable_after_importaddress.extend([p2sh_p2pk, p2sh_p2pkh])
 430                      # Witness output types with uncompressed keys are never seen
 431                      unseen_anytime.extend([p2wpkh, p2sh_p2wpkh, p2wsh_p2pk, p2wsh_p2pkh, p2sh_p2wsh_p2pk, p2sh_p2wsh_p2pkh])
 432  
 433              for i in compressed_solvable_address:
 434                  v = self.nodes[0].getaddressinfo(i)
 435                  if v['isscript']:
 436                      # Multisig without private is not seen after addmultisigaddress, but seen after importaddress
 437                      [bare, p2sh, p2wsh, p2sh_p2wsh] = self.p2sh_address_to_script(v)
 438                      solvable_after_importaddress.extend([p2sh, p2wsh, p2sh_p2wsh])
 439                  else:
 440                      [p2wpkh, p2sh_p2wpkh, p2pk, p2pkh, p2sh_p2pk, p2sh_p2pkh, p2wsh_p2pk, p2wsh_p2pkh, p2sh_p2wsh_p2pk, p2sh_p2wsh_p2pkh] = self.p2pkh_address_to_script(v)
 441                      # normal P2PKH, P2PK, P2WPKH and P2SH_P2WPKH with compressed keys should always be seen
 442                      solvable_anytime.extend([p2pkh, p2wpkh, p2sh_p2wpkh])
 443                      # P2SH_P2PK, P2SH_P2PKH with compressed keys are seen after direct importaddress
 444                      solvable_after_importaddress.extend([p2sh_p2pk, p2sh_p2pkh, p2wsh_p2pk, p2wsh_p2pkh, p2sh_p2wsh_p2pk, p2sh_p2wsh_p2pkh])
 445  
 446              for i in uncompressed_solvable_address:
 447                  v = self.nodes[0].getaddressinfo(i)
 448                  if v['isscript']:
 449                      [bare, p2sh, p2wsh, p2sh_p2wsh] = self.p2sh_address_to_script(v)
 450                      # Base uncompressed multisig without private is not seen after addmultisigaddress, but seen after importaddress
 451                      solvable_after_importaddress.extend([p2sh])
 452                      # P2WSH and P2SH(P2WSH) multisig with uncompressed keys are never seen
 453                      unseen_anytime.extend([p2wsh, p2sh_p2wsh])
 454                  else:
 455                      [p2wpkh, p2sh_p2wpkh, p2pk, p2pkh, p2sh_p2pk, p2sh_p2pkh, p2wsh_p2pk, p2wsh_p2pkh, p2sh_p2wsh_p2pk, p2sh_p2wsh_p2pkh] = self.p2pkh_address_to_script(v)
 456                      # normal P2PKH and P2PK with uncompressed keys should always be seen
 457                      solvable_anytime.extend([p2pkh])
 458                      # P2SH_P2PK, P2SH_P2PKH with uncompressed keys are seen after direct importaddress
 459                      solvable_after_importaddress.extend([p2sh_p2pk, p2sh_p2pkh])
 460                      # Witness output types with uncompressed keys are never seen
 461                      unseen_anytime.extend([p2wpkh, p2sh_p2wpkh, p2wsh_p2pk, p2wsh_p2pkh, p2sh_p2wsh_p2pk, p2sh_p2wsh_p2pkh])
 462  
 463              op1 = CScript([OP_1])
 464              op0 = CScript([OP_0])
 465              # 2N7MGY19ti4KDMSzRfPAssP6Pxyuxoi6jLe is the P2SH(P2PKH) version of mjoE3sSrb8ByYEvgnC3Aox86u1CHnfJA4V
 466              unsolvable_address_key = bytes.fromhex("02341AEC7587A51CDE5279E0630A531AEA2615A9F80B17E8D9376327BAEAA59E3D")
 467              unsolvablep2pkh = key_to_p2pkh_script(unsolvable_address_key)
 468              unsolvablep2wshp2pkh = script_to_p2wsh_script(unsolvablep2pkh)
 469              p2shop0 = script_to_p2sh_script(op0)
 470              p2wshop1 = script_to_p2wsh_script(op1)
 471              unsolvable_after_importaddress.append(unsolvablep2pkh)
 472              unsolvable_after_importaddress.append(unsolvablep2wshp2pkh)
 473              unsolvable_after_importaddress.append(op1)  # OP_1 will be imported as script
 474              unsolvable_after_importaddress.append(p2wshop1)
 475              unseen_anytime.append(op0)  # OP_0 will be imported as P2SH address with no script provided
 476              unsolvable_after_importaddress.append(p2shop0)
 477  
 478              spendable_txid = []
 479              solvable_txid = []
 480              spendable_txid.append(self.mine_and_test_listunspent(spendable_anytime, 2))
 481              solvable_txid.append(self.mine_and_test_listunspent(solvable_anytime, 1))
 482              self.mine_and_test_listunspent(spendable_after_importaddress + solvable_after_importaddress + unseen_anytime + unsolvable_after_importaddress, 0)
 483  
 484              importlist = []
 485              for i in compressed_spendable_address + uncompressed_spendable_address + compressed_solvable_address + uncompressed_solvable_address:
 486                  v = self.nodes[0].getaddressinfo(i)
 487                  if v['isscript']:
 488                      bare = bytes.fromhex(v['hex'])
 489                      importlist.append(bare.hex())
 490                      importlist.append(script_to_p2wsh_script(bare).hex())
 491                  else:
 492                      pubkey = bytes.fromhex(v['pubkey'])
 493                      p2pk = key_to_p2pk_script(pubkey)
 494                      p2pkh = key_to_p2pkh_script(pubkey)
 495                      importlist.append(p2pk.hex())
 496                      importlist.append(p2pkh.hex())
 497                      importlist.append(key_to_p2wpkh_script(pubkey).hex())
 498                      importlist.append(script_to_p2wsh_script(p2pk).hex())
 499                      importlist.append(script_to_p2wsh_script(p2pkh).hex())
 500  
 501              importlist.append(unsolvablep2pkh.hex())
 502              importlist.append(unsolvablep2wshp2pkh.hex())
 503              importlist.append(op1.hex())
 504              importlist.append(p2wshop1.hex())
 505  
 506              for i in importlist:
 507                  # import all generated addresses. The wallet already has the private keys for some of these, so catch JSON RPC
 508                  # exceptions and continue.
 509                  try_rpc(-4, "The wallet already contains the private key for this address or script", self.nodes[0].importaddress, i, "", False, True)
 510  
 511              self.nodes[0].importaddress(script_to_p2sh(op0))  # import OP_0 as address only
 512              self.nodes[0].importaddress(multisig_without_privkey_address)  # Test multisig_without_privkey
 513  
 514              spendable_txid.append(self.mine_and_test_listunspent(spendable_anytime + spendable_after_importaddress, 2))
 515              solvable_txid.append(self.mine_and_test_listunspent(solvable_anytime + solvable_after_importaddress, 1))
 516              self.mine_and_test_listunspent(unsolvable_after_importaddress, 1)
 517              self.mine_and_test_listunspent(unseen_anytime, 0)
 518  
 519              spendable_txid.append(self.mine_and_test_listunspent(spendable_anytime + spendable_after_importaddress, 2))
 520              solvable_txid.append(self.mine_and_test_listunspent(solvable_anytime + solvable_after_importaddress, 1))
 521              self.mine_and_test_listunspent(unsolvable_after_importaddress, 1)
 522              self.mine_and_test_listunspent(unseen_anytime, 0)
 523  
 524              # Repeat some tests. This time we don't add witness scripts with importaddress
 525              # Import a compressed key and an uncompressed key, generate some multisig addresses
 526              self.nodes[0].importprivkey("927pw6RW8ZekycnXqBQ2JS5nPyo1yRfGNN8oq74HeddWSpafDJH")
 527              uncompressed_spendable_address = ["mguN2vNSCEUh6rJaXoAVwY3YZwZvEmf5xi"]
 528              self.nodes[0].importprivkey("cMcrXaaUC48ZKpcyydfFo8PxHAjpsYLhdsp6nmtB3E2ER9UUHWnw")
 529              compressed_spendable_address = ["n1UNmpmbVUJ9ytXYXiurmGPQ3TRrXqPWKL"]
 530  
 531              self.nodes[0].importpubkey(pubkeys[5])
 532              compressed_solvable_address = [key_to_p2pkh(pubkeys[5])]
 533              self.nodes[0].importpubkey(pubkeys[6])
 534              uncompressed_solvable_address = [key_to_p2pkh(pubkeys[6])]
 535  
 536              unseen_anytime = []                         # These outputs should never be seen
 537              solvable_anytime = []                       # These outputs should be solvable after importpubkey
 538              unseen_anytime = []                         # These outputs should never be seen
 539  
 540              uncompressed_spendable_address.append(self.nodes[0].addmultisigaddress(2, [uncompressed_spendable_address[0], compressed_spendable_address[0]])['address'])
 541              uncompressed_spendable_address.append(self.nodes[0].addmultisigaddress(2, [uncompressed_spendable_address[0], uncompressed_spendable_address[0]])['address'])
 542              compressed_spendable_address.append(self.nodes[0].addmultisigaddress(2, [compressed_spendable_address[0], compressed_spendable_address[0]])['address'])
 543              uncompressed_solvable_address.append(self.nodes[0].addmultisigaddress(2, [compressed_solvable_address[0], uncompressed_solvable_address[0]])['address'])
 544              compressed_solvable_address.append(self.nodes[0].addmultisigaddress(2, [compressed_spendable_address[0], compressed_solvable_address[0]])['address'])
 545  
 546              premature_witaddress = []
 547  
 548              for i in compressed_spendable_address:
 549                  v = self.nodes[0].getaddressinfo(i)
 550                  if v['isscript']:
 551                      [bare, p2sh, p2wsh, p2sh_p2wsh] = self.p2sh_address_to_script(v)
 552                      premature_witaddress.append(script_to_p2sh(p2wsh))
 553                  else:
 554                      [p2wpkh, p2sh_p2wpkh, p2pk, p2pkh, p2sh_p2pk, p2sh_p2pkh, p2wsh_p2pk, p2wsh_p2pkh, p2sh_p2wsh_p2pk, p2sh_p2wsh_p2pkh] = self.p2pkh_address_to_script(v)
 555                      # P2WPKH, P2SH_P2WPKH are always spendable
 556                      spendable_anytime.extend([p2wpkh, p2sh_p2wpkh])
 557  
 558              for i in uncompressed_spendable_address + uncompressed_solvable_address:
 559                  v = self.nodes[0].getaddressinfo(i)
 560                  if v['isscript']:
 561                      [bare, p2sh, p2wsh, p2sh_p2wsh] = self.p2sh_address_to_script(v)
 562                      # P2WSH and P2SH(P2WSH) multisig with uncompressed keys are never seen
 563                      unseen_anytime.extend([p2wsh, p2sh_p2wsh])
 564                  else:
 565                      [p2wpkh, p2sh_p2wpkh, p2pk, p2pkh, p2sh_p2pk, p2sh_p2pkh, p2wsh_p2pk, p2wsh_p2pkh, p2sh_p2wsh_p2pk, p2sh_p2wsh_p2pkh] = self.p2pkh_address_to_script(v)
 566                      # P2WPKH, P2SH_P2WPKH with uncompressed keys are never seen
 567                      unseen_anytime.extend([p2wpkh, p2sh_p2wpkh])
 568  
 569              for i in compressed_solvable_address:
 570                  v = self.nodes[0].getaddressinfo(i)
 571                  if v['isscript']:
 572                      [bare, p2sh, p2wsh, p2sh_p2wsh] = self.p2sh_address_to_script(v)
 573                      premature_witaddress.append(script_to_p2sh(p2wsh))
 574                  else:
 575                      [p2wpkh, p2sh_p2wpkh, p2pk, p2pkh, p2sh_p2pk, p2sh_p2pkh, p2wsh_p2pk, p2wsh_p2pkh, p2sh_p2wsh_p2pk, p2sh_p2wsh_p2pkh] = self.p2pkh_address_to_script(v)
 576                      # P2SH_P2PK, P2SH_P2PKH with compressed keys are always solvable
 577                      solvable_anytime.extend([p2wpkh, p2sh_p2wpkh])
 578  
 579              self.mine_and_test_listunspent(spendable_anytime, 2)
 580              self.mine_and_test_listunspent(solvable_anytime, 1)
 581              self.mine_and_test_listunspent(unseen_anytime, 0)
 582  
 583              # Check that createrawtransaction/decoderawtransaction with non-v0 Bech32 works
 584              v1_addr = program_to_witness(1, [3, 5])
 585              v1_tx = self.nodes[0].createrawtransaction([getutxo(spendable_txid[0])], {v1_addr: 1})
 586              v1_decoded = self.nodes[1].decoderawtransaction(v1_tx)
 587              assert_equal(v1_decoded['vout'][0]['scriptPubKey']['address'], v1_addr)
 588              assert_equal(v1_decoded['vout'][0]['scriptPubKey']['hex'], "51020305")
 589  
 590              # Check that spendable outputs are really spendable
 591              self.create_and_mine_tx_from_txids(spendable_txid)
 592  
 593              # import all the private keys so solvable addresses become spendable
 594              self.nodes[0].importprivkey("cPiM8Ub4heR9NBYmgVzJQiUH1if44GSBGiqaeJySuL2BKxubvgwb")
 595              self.nodes[0].importprivkey("cPpAdHaD6VoYbW78kveN2bsvb45Q7G5PhaPApVUGwvF8VQ9brD97")
 596              self.nodes[0].importprivkey("91zqCU5B9sdWxzMt1ca3VzbtVm2YM6Hi5Rxn4UDtxEaN9C9nzXV")
 597              self.nodes[0].importprivkey("cPQFjcVRpAUBG8BA9hzr2yEzHwKoMgLkJZBBtK9vJnvGJgMjzTbd")
 598              self.nodes[0].importprivkey("cQGtcm34xiLjB1v7bkRa4V3aAc9tS2UTuBZ1UnZGeSeNy627fN66")
 599              self.nodes[0].importprivkey("cTW5mR5M45vHxXkeChZdtSPozrFwFgmEvTNnanCW6wrqwaCZ1X7K")
 600              self.create_and_mine_tx_from_txids(solvable_txid)
 601  
 602              # Test that importing native P2WPKH/P2WSH scripts works
 603              for use_p2wsh in [False, True]:
 604                  if use_p2wsh:
 605                      scriptPubKey = "00203a59f3f56b713fdcf5d1a57357f02c44342cbf306ffe0c4741046837bf90561a"
 606                      transaction = "01000000000100e1f505000000002200203a59f3f56b713fdcf5d1a57357f02c44342cbf306ffe0c4741046837bf90561a00000000"
 607                  else:
 608                      scriptPubKey = "a9142f8c469c2f0084c48e11f998ffbe7efa7549f26d87"
 609                      transaction = "01000000000100e1f5050000000017a9142f8c469c2f0084c48e11f998ffbe7efa7549f26d8700000000"
 610  
 611                  self.nodes[1].importaddress(scriptPubKey, "", False)
 612                  rawtxfund = self.nodes[1].fundrawtransaction(transaction)['hex']
 613                  rawtxfund = self.nodes[1].signrawtransactionwithwallet(rawtxfund)["hex"]
 614                  txid = self.nodes[1].sendrawtransaction(rawtxfund)
 615  
 616                  assert_equal(self.nodes[1].gettransaction(txid, True)["txid"], txid)
 617                  assert_equal(self.nodes[1].listtransactions("*", 1, 0, True)[0]["txid"], txid)
 618  
 619                  # Assert it is properly saved
 620                  self.restart_node(1)
 621                  assert_equal(self.nodes[1].gettransaction(txid, True)["txid"], txid)
 622                  assert_equal(self.nodes[1].listtransactions("*", 1, 0, True)[0]["txid"], txid)
 623  
 624      def mine_and_test_listunspent(self, script_list, ismine):
 625          utxo = find_spendable_utxo(self.nodes[0], 50)
 626          tx = CTransaction()
 627          tx.vin.append(CTxIn(COutPoint(int('0x' + utxo['txid'], 0), utxo['vout'])))
 628          for i in script_list:
 629              tx.vout.append(CTxOut(10000000, i))
 630          tx.rehash()
 631          signresults = self.nodes[0].signrawtransactionwithwallet(tx.serialize_without_witness().hex())['hex']
 632          txid = self.nodes[0].sendrawtransaction(hexstring=signresults, maxfeerate=0)
 633          txs_mined[txid] = self.generate(self.nodes[0], 1)[0]
 634          watchcount = 0
 635          spendcount = 0
 636          for i in self.nodes[0].listunspent():
 637              if i['txid'] == txid:
 638                  watchcount += 1
 639                  if i['spendable']:
 640                      spendcount += 1
 641          if ismine == 2:
 642              assert_equal(spendcount, len(script_list))
 643          elif ismine == 1:
 644              assert_equal(watchcount, len(script_list))
 645              assert_equal(spendcount, 0)
 646          else:
 647              assert_equal(watchcount, 0)
 648          return txid
 649  
 650      def p2sh_address_to_script(self, v):
 651          bare = CScript(bytes.fromhex(v['hex']))
 652          p2sh = CScript(bytes.fromhex(v['scriptPubKey']))
 653          p2wsh = script_to_p2wsh_script(bare)
 654          p2sh_p2wsh = script_to_p2sh_script(p2wsh)
 655          return [bare, p2sh, p2wsh, p2sh_p2wsh]
 656  
 657      def p2pkh_address_to_script(self, v):
 658          pubkey = bytes.fromhex(v['pubkey'])
 659          p2wpkh = key_to_p2wpkh_script(pubkey)
 660          p2sh_p2wpkh = script_to_p2sh_script(p2wpkh)
 661          p2pk = key_to_p2pk_script(pubkey)
 662          p2pkh = CScript(bytes.fromhex(v['scriptPubKey']))
 663          p2sh_p2pk = script_to_p2sh_script(p2pk)
 664          p2sh_p2pkh = script_to_p2sh_script(p2pkh)
 665          p2wsh_p2pk = script_to_p2wsh_script(p2pk)
 666          p2wsh_p2pkh = script_to_p2wsh_script(p2pkh)
 667          p2sh_p2wsh_p2pk = script_to_p2sh_script(p2wsh_p2pk)
 668          p2sh_p2wsh_p2pkh = script_to_p2sh_script(p2wsh_p2pkh)
 669          return [p2wpkh, p2sh_p2wpkh, p2pk, p2pkh, p2sh_p2pk, p2sh_p2pkh, p2wsh_p2pk, p2wsh_p2pkh, p2sh_p2wsh_p2pk, p2sh_p2wsh_p2pkh]
 670  
 671      def create_and_mine_tx_from_txids(self, txids, success=True):
 672          tx = CTransaction()
 673          for i in txids:
 674              txraw = self.nodes[0].getrawtransaction(i, 0, txs_mined[i])
 675              txtmp = tx_from_hex(txraw)
 676              for j in range(len(txtmp.vout)):
 677                  tx.vin.append(CTxIn(COutPoint(int('0x' + i, 0), j)))
 678          tx.vout.append(CTxOut(0, CScript()))
 679          tx.rehash()
 680          signresults = self.nodes[0].signrawtransactionwithwallet(tx.serialize_without_witness().hex())['hex']
 681          self.nodes[0].sendrawtransaction(hexstring=signresults, maxfeerate=0)
 682          self.generate(self.nodes[0], 1)
 683  
 684  
 685  if __name__ == '__main__':
 686      SegWitTest(__file__).main()
 687