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