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