1#!/usr/bin/env python3
2# Copyright (c) 2016 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
6#
7# Test the SegWit changeover logic
8#
9
10from test_framework.test_framework import BitcoinTestFramework
11from test_framework.util import *
12from test_framework.mininode import sha256, ripemd160, CTransaction, CTxIn, COutPoint, CTxOut
13from test_framework.address import script_to_p2sh, key_to_p2pkh
14from test_framework.script import CScript, OP_HASH160, OP_CHECKSIG, OP_0, hash160, OP_EQUAL, OP_DUP, OP_EQUALVERIFY, OP_1, OP_2, OP_CHECKMULTISIG
15from io import BytesIO
16from test_framework.mininode import FromHex
17
18NODE_0 = 0
19NODE_1 = 1
20NODE_2 = 2
21WIT_V0 = 0
22WIT_V1 = 1
23
24def witness_script(version, pubkey):
25    if (version == 0):
26        pubkeyhash = bytes_to_hex_str(ripemd160(sha256(hex_str_to_bytes(pubkey))))
27        pkscript = "0014" + pubkeyhash
28    elif (version == 1):
29        # 1-of-1 multisig
30        scripthash = bytes_to_hex_str(sha256(hex_str_to_bytes("5121" + pubkey + "51ae")))
31        pkscript = "0020" + scripthash
32    else:
33        assert("Wrong version" == "0 or 1")
34    return pkscript
35
36def addlength(script):
37    scriptlen = format(len(script)//2, 'x')
38    assert(len(scriptlen) == 2)
39    return scriptlen + script
40
41def create_witnessprogram(version, node, utxo, pubkey, encode_p2sh, amount):
42    pkscript = witness_script(version, pubkey);
43    if (encode_p2sh):
44        p2sh_hash = bytes_to_hex_str(ripemd160(sha256(hex_str_to_bytes(pkscript))))
45        pkscript = "a914"+p2sh_hash+"87"
46    inputs = []
47    outputs = {}
48    inputs.append({ "txid" : utxo["txid"], "vout" : utxo["vout"]} )
49    DUMMY_P2SH = "2MySexEGVzZpRgNQ1JdjdP5bRETznm3roQ2" # P2SH of "OP_1 OP_DROP"
50    outputs[DUMMY_P2SH] = amount
51    tx_to_witness = node.createrawtransaction(inputs,outputs)
52    #replace dummy output with our own
53    tx_to_witness = tx_to_witness[0:110] + addlength(pkscript) + tx_to_witness[-8:]
54    return tx_to_witness
55
56def send_to_witness(version, node, utxo, pubkey, encode_p2sh, amount, sign=True, insert_redeem_script=""):
57    tx_to_witness = create_witnessprogram(version, node, utxo, pubkey, encode_p2sh, amount)
58    if (sign):
59        signed = node.signrawtransaction(tx_to_witness)
60        assert("errors" not in signed or len(["errors"]) == 0)
61        return node.sendrawtransaction(signed["hex"])
62    else:
63        if (insert_redeem_script):
64            tx_to_witness = tx_to_witness[0:82] + addlength(insert_redeem_script) + tx_to_witness[84:]
65
66    return node.sendrawtransaction(tx_to_witness)
67
68def getutxo(txid):
69    utxo = {}
70    utxo["vout"] = 0
71    utxo["txid"] = txid
72    return utxo
73
74def find_unspent(node, min_value):
75    for utxo in node.listunspent():
76        if utxo['amount'] >= min_value:
77            return utxo
78
79class SegWitTest(BitcoinTestFramework):
80
81    def setup_chain(self):
82        print("Initializing test directory "+self.options.tmpdir)
83        initialize_chain_clean(self.options.tmpdir, 3)
84
85    def setup_network(self):
86        self.nodes = []
87        self.nodes.append(start_node(0, self.options.tmpdir, ["-logtimemicros", "-debug", "-walletprematurewitness", "-rpcserialversion=0"]))
88        self.nodes.append(start_node(1, self.options.tmpdir, ["-logtimemicros", "-debug", "-blockversion=4", "-promiscuousmempoolflags=517", "-prematurewitness", "-walletprematurewitness", "-rpcserialversion=1"]))
89        self.nodes.append(start_node(2, self.options.tmpdir, ["-logtimemicros", "-debug", "-blockversion=536870915", "-promiscuousmempoolflags=517", "-prematurewitness", "-walletprematurewitness"]))
90        connect_nodes(self.nodes[1], 0)
91        connect_nodes(self.nodes[2], 1)
92        connect_nodes(self.nodes[0], 2)
93        self.is_network_split = False
94        self.sync_all()
95
96    def success_mine(self, node, txid, sign, redeem_script=""):
97        send_to_witness(1, node, getutxo(txid), self.pubkey[0], False, Decimal("49.998"), sign, redeem_script)
98        block = node.generate(1)
99        assert_equal(len(node.getblock(block[0])["tx"]), 2)
100        sync_blocks(self.nodes)
101
102    def skip_mine(self, node, txid, sign, redeem_script=""):
103        send_to_witness(1, node, getutxo(txid), self.pubkey[0], False, Decimal("49.998"), sign, redeem_script)
104        block = node.generate(1)
105        assert_equal(len(node.getblock(block[0])["tx"]), 1)
106        sync_blocks(self.nodes)
107
108    def fail_accept(self, node, txid, sign, redeem_script=""):
109        try:
110            send_to_witness(1, node, getutxo(txid), self.pubkey[0], False, Decimal("49.998"), sign, redeem_script)
111        except JSONRPCException as exp:
112            assert(exp.error["code"] == -26)
113        else:
114            raise AssertionError("Tx should not have been accepted")
115
116    def fail_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        try:
119            node.generate(1)
120        except JSONRPCException as exp:
121            assert(exp.error["code"] == -1)
122        else:
123            raise AssertionError("Created valid block when TestBlockValidity should have failed")
124        sync_blocks(self.nodes)
125
126    def run_test(self):
127        self.nodes[0].generate(161) #block 161
128
129        print("Verify sigops are counted in GBT with pre-BIP141 rules before the fork")
130        txid = self.nodes[0].sendtoaddress(self.nodes[0].getnewaddress(), 1)
131        tmpl = self.nodes[0].getblocktemplate({})
132        assert(tmpl['sigoplimit'] == 20000)
133        assert(tmpl['transactions'][0]['hash'] == txid)
134        assert(tmpl['transactions'][0]['sigops'] == 2)
135        tmpl = self.nodes[0].getblocktemplate({'rules':['segwit']})
136        assert(tmpl['sigoplimit'] == 20000)
137        assert(tmpl['transactions'][0]['hash'] == txid)
138        assert(tmpl['transactions'][0]['sigops'] == 2)
139        self.nodes[0].generate(1) #block 162
140
141        balance_presetup = self.nodes[0].getbalance()
142        self.pubkey = []
143        p2sh_ids = [] # p2sh_ids[NODE][VER] is an array of txids that spend to a witness version VER pkscript to an address for NODE embedded in p2sh
144        wit_ids = [] # wit_ids[NODE][VER] is an array of txids that spend to a witness version VER pkscript to an address for NODE via bare witness
145        for i in range(3):
146            newaddress = self.nodes[i].getnewaddress()
147            self.pubkey.append(self.nodes[i].validateaddress(newaddress)["pubkey"])
148            multiaddress = self.nodes[i].addmultisigaddress(1, [self.pubkey[-1]])
149            self.nodes[i].addwitnessaddress(newaddress)
150            self.nodes[i].addwitnessaddress(multiaddress)
151            p2sh_ids.append([])
152            wit_ids.append([])
153            for v in range(2):
154                p2sh_ids[i].append([])
155                wit_ids[i].append([])
156
157        for i in range(5):
158            for n in range(3):
159                for v in range(2):
160                    wit_ids[n][v].append(send_to_witness(v, self.nodes[0], find_unspent(self.nodes[0], 50), self.pubkey[n], False, Decimal("49.999")))
161                    p2sh_ids[n][v].append(send_to_witness(v, self.nodes[0], find_unspent(self.nodes[0], 50), self.pubkey[n], True, Decimal("49.999")))
162
163        self.nodes[0].generate(1) #block 163
164        sync_blocks(self.nodes)
165
166        # Make sure all nodes recognize the transactions as theirs
167        assert_equal(self.nodes[0].getbalance(), balance_presetup - 60*50 + 20*Decimal("49.999") + 50)
168        assert_equal(self.nodes[1].getbalance(), 20*Decimal("49.999"))
169        assert_equal(self.nodes[2].getbalance(), 20*Decimal("49.999"))
170
171        self.nodes[0].generate(260) #block 423
172        sync_blocks(self.nodes)
173
174        print("Verify default node can't accept any witness format txs before fork")
175        # unsigned, no scriptsig
176        self.fail_accept(self.nodes[0], wit_ids[NODE_0][WIT_V0][0], False)
177        self.fail_accept(self.nodes[0], wit_ids[NODE_0][WIT_V1][0], False)
178        self.fail_accept(self.nodes[0], p2sh_ids[NODE_0][WIT_V0][0], False)
179        self.fail_accept(self.nodes[0], p2sh_ids[NODE_0][WIT_V1][0], False)
180        # unsigned with redeem script
181        self.fail_accept(self.nodes[0], p2sh_ids[NODE_0][WIT_V0][0], False, addlength(witness_script(0, self.pubkey[0])))
182        self.fail_accept(self.nodes[0], p2sh_ids[NODE_0][WIT_V1][0], False, addlength(witness_script(1, self.pubkey[0])))
183        # signed
184        self.fail_accept(self.nodes[0], wit_ids[NODE_0][WIT_V0][0], True)
185        self.fail_accept(self.nodes[0], wit_ids[NODE_0][WIT_V1][0], True)
186        self.fail_accept(self.nodes[0], p2sh_ids[NODE_0][WIT_V0][0], True)
187        self.fail_accept(self.nodes[0], p2sh_ids[NODE_0][WIT_V1][0], True)
188
189        print("Verify witness txs are skipped for mining before the fork")
190        self.skip_mine(self.nodes[2], wit_ids[NODE_2][WIT_V0][0], True) #block 424
191        self.skip_mine(self.nodes[2], wit_ids[NODE_2][WIT_V1][0], True) #block 425
192        self.skip_mine(self.nodes[2], p2sh_ids[NODE_2][WIT_V0][0], True) #block 426
193        self.skip_mine(self.nodes[2], p2sh_ids[NODE_2][WIT_V1][0], True) #block 427
194
195        # TODO: An old node would see these txs without witnesses and be able to mine them
196
197        print("Verify unsigned bare witness txs in versionbits-setting blocks are valid before the fork")
198        self.success_mine(self.nodes[2], wit_ids[NODE_2][WIT_V0][1], False) #block 428
199        self.success_mine(self.nodes[2], wit_ids[NODE_2][WIT_V1][1], False) #block 429
200
201        print("Verify unsigned p2sh witness txs without a redeem script are invalid")
202        self.fail_accept(self.nodes[2], p2sh_ids[NODE_2][WIT_V0][1], False)
203        self.fail_accept(self.nodes[2], p2sh_ids[NODE_2][WIT_V1][1], False)
204
205        print("Verify unsigned p2sh witness txs with a redeem script in versionbits-settings blocks are valid before the fork")
206        self.success_mine(self.nodes[2], p2sh_ids[NODE_2][WIT_V0][1], False, addlength(witness_script(0, self.pubkey[2]))) #block 430
207        self.success_mine(self.nodes[2], p2sh_ids[NODE_2][WIT_V1][1], False, addlength(witness_script(1, self.pubkey[2]))) #block 431
208
209        print("Verify previous witness txs skipped for mining can now be mined")
210        assert_equal(len(self.nodes[2].getrawmempool()), 4)
211        block = self.nodes[2].generate(1) #block 432 (first block with new rules; 432 = 144 * 3)
212        sync_blocks(self.nodes)
213        assert_equal(len(self.nodes[2].getrawmempool()), 0)
214        segwit_tx_list = self.nodes[2].getblock(block[0])["tx"]
215        assert_equal(len(segwit_tx_list), 5)
216
217        print("Verify block and transaction serialization rpcs return differing serializations depending on rpc serialization flag")
218        assert(self.nodes[2].getblock(block[0], False) !=  self.nodes[0].getblock(block[0], False))
219        assert(self.nodes[1].getblock(block[0], False) ==  self.nodes[2].getblock(block[0], False))
220        for i in range(len(segwit_tx_list)):
221            tx = FromHex(CTransaction(), self.nodes[2].gettransaction(segwit_tx_list[i])["hex"])
222            assert(self.nodes[2].getrawtransaction(segwit_tx_list[i]) != self.nodes[0].getrawtransaction(segwit_tx_list[i]))
223            assert(self.nodes[1].getrawtransaction(segwit_tx_list[i], 0) == self.nodes[2].getrawtransaction(segwit_tx_list[i]))
224            assert(self.nodes[0].getrawtransaction(segwit_tx_list[i]) != self.nodes[2].gettransaction(segwit_tx_list[i])["hex"])
225            assert(self.nodes[1].getrawtransaction(segwit_tx_list[i]) == self.nodes[2].gettransaction(segwit_tx_list[i])["hex"])
226            assert(self.nodes[0].getrawtransaction(segwit_tx_list[i]) == bytes_to_hex_str(tx.serialize_without_witness()))
227
228        print("Verify witness txs without witness data are invalid after the fork")
229        self.fail_mine(self.nodes[2], wit_ids[NODE_2][WIT_V0][2], False)
230        self.fail_mine(self.nodes[2], wit_ids[NODE_2][WIT_V1][2], False)
231        self.fail_mine(self.nodes[2], p2sh_ids[NODE_2][WIT_V0][2], False, addlength(witness_script(0, self.pubkey[2])))
232        self.fail_mine(self.nodes[2], p2sh_ids[NODE_2][WIT_V1][2], False, addlength(witness_script(1, self.pubkey[2])))
233
234        print("Verify default node can now use witness txs")
235        self.success_mine(self.nodes[0], wit_ids[NODE_0][WIT_V0][0], True) #block 432
236        self.success_mine(self.nodes[0], wit_ids[NODE_0][WIT_V1][0], True) #block 433
237        self.success_mine(self.nodes[0], p2sh_ids[NODE_0][WIT_V0][0], True) #block 434
238        self.success_mine(self.nodes[0], p2sh_ids[NODE_0][WIT_V1][0], True) #block 435
239
240        print("Verify sigops are counted in GBT with BIP141 rules after the fork")
241        txid = self.nodes[0].sendtoaddress(self.nodes[0].getnewaddress(), 1)
242        tmpl = self.nodes[0].getblocktemplate({'rules':['segwit']})
243        assert(tmpl['sigoplimit'] == 80000)
244        assert(tmpl['transactions'][0]['txid'] == txid)
245        assert(tmpl['transactions'][0]['sigops'] == 8)
246
247        print("Verify non-segwit miners get a valid GBT response after the fork")
248        send_to_witness(1, self.nodes[0], find_unspent(self.nodes[0], 50), self.pubkey[0], False, Decimal("49.998"))
249        try:
250            tmpl = self.nodes[0].getblocktemplate({})
251            assert(len(tmpl['transactions']) == 1)  # Doesn't include witness tx
252            assert(tmpl['sigoplimit'] == 20000)
253            assert(tmpl['transactions'][0]['hash'] == txid)
254            assert(tmpl['transactions'][0]['sigops'] == 2)
255            assert(('!segwit' in tmpl['rules']) or ('segwit' not in tmpl['rules']))
256        except JSONRPCException:
257            # This is an acceptable outcome
258            pass
259
260        print("Verify behaviour of importaddress, addwitnessaddress and listunspent")
261
262        # Some public keys to be used later
263        pubkeys = [
264            "0363D44AABD0F1699138239DF2F042C3282C0671CC7A76826A55C8203D90E39242", # cPiM8Ub4heR9NBYmgVzJQiUH1if44GSBGiqaeJySuL2BKxubvgwb
265            "02D3E626B3E616FC8662B489C123349FECBFC611E778E5BE739B257EAE4721E5BF", # cPpAdHaD6VoYbW78kveN2bsvb45Q7G5PhaPApVUGwvF8VQ9brD97
266            "04A47F2CBCEFFA7B9BCDA184E7D5668D3DA6F9079AD41E422FA5FD7B2D458F2538A62F5BD8EC85C2477F39650BD391EA6250207065B2A81DA8B009FC891E898F0E", # 91zqCU5B9sdWxzMt1ca3VzbtVm2YM6Hi5Rxn4UDtxEaN9C9nzXV
267            "02A47F2CBCEFFA7B9BCDA184E7D5668D3DA6F9079AD41E422FA5FD7B2D458F2538", # cPQFjcVRpAUBG8BA9hzr2yEzHwKoMgLkJZBBtK9vJnvGJgMjzTbd
268            "036722F784214129FEB9E8129D626324F3F6716555B603FFE8300BBCB882151228", # cQGtcm34xiLjB1v7bkRa4V3aAc9tS2UTuBZ1UnZGeSeNy627fN66
269            "0266A8396EE936BF6D99D17920DB21C6C7B1AB14C639D5CD72B300297E416FD2EC", # cTW5mR5M45vHxXkeChZdtSPozrFwFgmEvTNnanCW6wrqwaCZ1X7K
270            "0450A38BD7F0AC212FEBA77354A9B036A32E0F7C81FC4E0C5ADCA7C549C4505D2522458C2D9AE3CEFD684E039194B72C8A10F9CB9D4764AB26FCC2718D421D3B84", # 92h2XPssjBpsJN5CqSP7v9a7cf2kgDunBC6PDFwJHMACM1rrVBJ
271        ]
272
273        # Import a compressed key and an uncompressed key, generate some multisig addresses
274        self.nodes[0].importprivkey("92e6XLo5jVAVwrQKPNTs93oQco8f8sDNBcpv73Dsrs397fQtFQn")
275        uncompressed_spendable_address = ["mvozP4UwyGD2mGZU4D2eMvMLPB9WkMmMQu"]
276        self.nodes[0].importprivkey("cNC8eQ5dg3mFAVePDX4ddmPYpPbw41r9bm2jd1nLJT77e6RrzTRR")
277        compressed_spendable_address = ["mmWQubrDomqpgSYekvsU7HWEVjLFHAakLe"]
278        assert ((self.nodes[0].validateaddress(uncompressed_spendable_address[0])['iscompressed'] == False))
279        assert ((self.nodes[0].validateaddress(compressed_spendable_address[0])['iscompressed'] == True))
280
281        self.nodes[0].importpubkey(pubkeys[0])
282        compressed_solvable_address = [key_to_p2pkh(pubkeys[0])]
283        self.nodes[0].importpubkey(pubkeys[1])
284        compressed_solvable_address.append(key_to_p2pkh(pubkeys[1]))
285        self.nodes[0].importpubkey(pubkeys[2])
286        uncompressed_solvable_address = [key_to_p2pkh(pubkeys[2])]
287
288        spendable_anytime = []                      # These outputs should be seen anytime after importprivkey and addmultisigaddress
289        spendable_after_importaddress = []          # These outputs should be seen after importaddress
290        solvable_after_importaddress = []           # These outputs should be seen after importaddress but not spendable
291        unsolvable_after_importaddress = []         # These outputs should be unsolvable after importaddress
292        solvable_anytime = []                       # These outputs should be solvable after importpubkey
293        unseen_anytime = []                         # These outputs should never be seen
294
295        uncompressed_spendable_address.append(self.nodes[0].addmultisigaddress(2, [uncompressed_spendable_address[0], compressed_spendable_address[0]]))
296        uncompressed_spendable_address.append(self.nodes[0].addmultisigaddress(2, [uncompressed_spendable_address[0], uncompressed_spendable_address[0]]))
297        compressed_spendable_address.append(self.nodes[0].addmultisigaddress(2, [compressed_spendable_address[0], compressed_spendable_address[0]]))
298        uncompressed_solvable_address.append(self.nodes[0].addmultisigaddress(2, [compressed_spendable_address[0], uncompressed_solvable_address[0]]))
299        compressed_solvable_address.append(self.nodes[0].addmultisigaddress(2, [compressed_spendable_address[0], compressed_solvable_address[0]]))
300        compressed_solvable_address.append(self.nodes[0].addmultisigaddress(2, [compressed_solvable_address[0], compressed_solvable_address[1]]))
301        unknown_address = ["mtKKyoHabkk6e4ppT7NaM7THqPUt7AzPrT", "2NDP3jLWAFT8NDAiUa9qiE6oBt2awmMq7Dx"]
302
303        # Test multisig_without_privkey
304        # We have 2 public keys without private keys, use addmultisigaddress to add to wallet.
305        # Money sent to P2SH of multisig of this should only be seen after importaddress with the BASE58 P2SH address.
306
307        multisig_without_privkey_address = self.nodes[0].addmultisigaddress(2, [pubkeys[3], pubkeys[4]])
308        script = CScript([OP_2, hex_str_to_bytes(pubkeys[3]), hex_str_to_bytes(pubkeys[4]), OP_2, OP_CHECKMULTISIG])
309        solvable_after_importaddress.append(CScript([OP_HASH160, hash160(script), OP_EQUAL]))
310
311        for i in compressed_spendable_address:
312            v = self.nodes[0].validateaddress(i)
313            if (v['isscript']):
314                [bare, p2sh, p2wsh, p2sh_p2wsh] = self.p2sh_address_to_script(v)
315                # bare and p2sh multisig with compressed keys should always be spendable
316                spendable_anytime.extend([bare, p2sh])
317                # P2WSH and P2SH(P2WSH) multisig with compressed keys are spendable after direct importaddress
318                spendable_after_importaddress.extend([p2wsh, p2sh_p2wsh])
319            else:
320                [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)
321                # normal P2PKH and P2PK with compressed keys should always be spendable
322                spendable_anytime.extend([p2pkh, p2pk])
323                # P2SH_P2PK, P2SH_P2PKH, and witness with compressed keys are spendable after direct importaddress
324                spendable_after_importaddress.extend([p2wpkh, p2sh_p2wpkh, p2sh_p2pk, p2sh_p2pkh, p2wsh_p2pk, p2wsh_p2pkh, p2sh_p2wsh_p2pk, p2sh_p2wsh_p2pkh])
325
326        for i in uncompressed_spendable_address:
327            v = self.nodes[0].validateaddress(i)
328            if (v['isscript']):
329                [bare, p2sh, p2wsh, p2sh_p2wsh] = self.p2sh_address_to_script(v)
330                # bare and p2sh multisig with uncompressed keys should always be spendable
331                spendable_anytime.extend([bare, p2sh])
332                # P2WSH and P2SH(P2WSH) multisig with uncompressed keys are never seen
333                unseen_anytime.extend([p2wsh, p2sh_p2wsh])
334            else:
335                [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)
336                # normal P2PKH and P2PK with uncompressed keys should always be spendable
337                spendable_anytime.extend([p2pkh, p2pk])
338                # P2SH_P2PK and P2SH_P2PKH are spendable after direct importaddress
339                spendable_after_importaddress.extend([p2sh_p2pk, p2sh_p2pkh])
340                # witness with uncompressed keys are never seen
341                unseen_anytime.extend([p2wpkh, p2sh_p2wpkh, p2wsh_p2pk, p2wsh_p2pkh, p2sh_p2wsh_p2pk, p2sh_p2wsh_p2pkh])
342
343        for i in compressed_solvable_address:
344            v = self.nodes[0].validateaddress(i)
345            if (v['isscript']):
346                # Multisig without private is not seen after addmultisigaddress, but seen after importaddress
347                [bare, p2sh, p2wsh, p2sh_p2wsh] = self.p2sh_address_to_script(v)
348                solvable_after_importaddress.extend([bare, p2sh, p2wsh, p2sh_p2wsh])
349            else:
350                [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)
351                # normal P2PKH and P2PK with compressed keys should always be seen
352                solvable_anytime.extend([p2pkh, p2pk])
353                # P2SH_P2PK, P2SH_P2PKH, and witness with compressed keys are seen after direct importaddress
354                solvable_after_importaddress.extend([p2wpkh, p2sh_p2wpkh, p2sh_p2pk, p2sh_p2pkh, p2wsh_p2pk, p2wsh_p2pkh, p2sh_p2wsh_p2pk, p2sh_p2wsh_p2pkh])
355
356        for i in uncompressed_solvable_address:
357            v = self.nodes[0].validateaddress(i)
358            if (v['isscript']):
359                [bare, p2sh, p2wsh, p2sh_p2wsh] = self.p2sh_address_to_script(v)
360                # Base uncompressed multisig without private is not seen after addmultisigaddress, but seen after importaddress
361                solvable_after_importaddress.extend([bare, p2sh])
362                # P2WSH and P2SH(P2WSH) multisig with uncompressed keys are never seen
363                unseen_anytime.extend([p2wsh, p2sh_p2wsh])
364            else:
365                [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)
366                # normal P2PKH and P2PK with uncompressed keys should always be seen
367                solvable_anytime.extend([p2pkh, p2pk])
368                # P2SH_P2PK, P2SH_P2PKH with uncompressed keys are seen after direct importaddress
369                solvable_after_importaddress.extend([p2sh_p2pk, p2sh_p2pkh])
370                # witness with uncompressed keys are never seen
371                unseen_anytime.extend([p2wpkh, p2sh_p2wpkh, p2wsh_p2pk, p2wsh_p2pkh, p2sh_p2wsh_p2pk, p2sh_p2wsh_p2pkh])
372
373        op1 = CScript([OP_1])
374        op0 = CScript([OP_0])
375        # 2N7MGY19ti4KDMSzRfPAssP6Pxyuxoi6jLe is the P2SH(P2PKH) version of mjoE3sSrb8ByYEvgnC3Aox86u1CHnfJA4V
376        unsolvable_address = ["mjoE3sSrb8ByYEvgnC3Aox86u1CHnfJA4V", "2N7MGY19ti4KDMSzRfPAssP6Pxyuxoi6jLe", script_to_p2sh(op1), script_to_p2sh(op0)]
377        unsolvable_address_key = hex_str_to_bytes("02341AEC7587A51CDE5279E0630A531AEA2615A9F80B17E8D9376327BAEAA59E3D")
378        unsolvablep2pkh = CScript([OP_DUP, OP_HASH160, hash160(unsolvable_address_key), OP_EQUALVERIFY, OP_CHECKSIG])
379        unsolvablep2wshp2pkh = CScript([OP_0, sha256(unsolvablep2pkh)])
380        p2shop0 = CScript([OP_HASH160, hash160(op0), OP_EQUAL])
381        p2wshop1 = CScript([OP_0, sha256(op1)])
382        unsolvable_after_importaddress.append(unsolvablep2pkh)
383        unsolvable_after_importaddress.append(unsolvablep2wshp2pkh)
384        unsolvable_after_importaddress.append(op1) # OP_1 will be imported as script
385        unsolvable_after_importaddress.append(p2wshop1)
386        unseen_anytime.append(op0) # OP_0 will be imported as P2SH address with no script provided
387        unsolvable_after_importaddress.append(p2shop0)
388
389        spendable_txid = []
390        solvable_txid = []
391        spendable_txid.append(self.mine_and_test_listunspent(spendable_anytime, 2))
392        solvable_txid.append(self.mine_and_test_listunspent(solvable_anytime, 1))
393        self.mine_and_test_listunspent(spendable_after_importaddress + solvable_after_importaddress + unseen_anytime + unsolvable_after_importaddress, 0)
394
395        importlist = []
396        for i in compressed_spendable_address + uncompressed_spendable_address + compressed_solvable_address + uncompressed_solvable_address:
397            v = self.nodes[0].validateaddress(i)
398            if (v['isscript']):
399                bare = hex_str_to_bytes(v['hex'])
400                importlist.append(bytes_to_hex_str(bare))
401                importlist.append(bytes_to_hex_str(CScript([OP_0, sha256(bare)])))
402            else:
403                pubkey = hex_str_to_bytes(v['pubkey'])
404                p2pk = CScript([pubkey, OP_CHECKSIG])
405                p2pkh = CScript([OP_DUP, OP_HASH160, hash160(pubkey), OP_EQUALVERIFY, OP_CHECKSIG])
406                importlist.append(bytes_to_hex_str(p2pk))
407                importlist.append(bytes_to_hex_str(p2pkh))
408                importlist.append(bytes_to_hex_str(CScript([OP_0, hash160(pubkey)])))
409                importlist.append(bytes_to_hex_str(CScript([OP_0, sha256(p2pk)])))
410                importlist.append(bytes_to_hex_str(CScript([OP_0, sha256(p2pkh)])))
411
412        importlist.append(bytes_to_hex_str(unsolvablep2pkh))
413        importlist.append(bytes_to_hex_str(unsolvablep2wshp2pkh))
414        importlist.append(bytes_to_hex_str(op1))
415        importlist.append(bytes_to_hex_str(p2wshop1))
416
417        for i in importlist:
418            try:
419                self.nodes[0].importaddress(i,"",False,True)
420            except JSONRPCException as exp:
421                assert_equal(exp.error["message"], "The wallet already contains the private key for this address or script")
422
423        self.nodes[0].importaddress(script_to_p2sh(op0)) # import OP_0 as address only
424        self.nodes[0].importaddress(multisig_without_privkey_address) # Test multisig_without_privkey
425
426        spendable_txid.append(self.mine_and_test_listunspent(spendable_anytime + spendable_after_importaddress, 2))
427        solvable_txid.append(self.mine_and_test_listunspent(solvable_anytime + solvable_after_importaddress, 1))
428        self.mine_and_test_listunspent(unsolvable_after_importaddress, 1)
429        self.mine_and_test_listunspent(unseen_anytime, 0)
430
431        # addwitnessaddress should refuse to return a witness address if an uncompressed key is used or the address is
432        # not in the wallet
433        # note that no witness address should be returned by unsolvable addresses
434        # the multisig_without_privkey_address will fail because its keys were not added with importpubkey
435        for i in uncompressed_spendable_address + uncompressed_solvable_address + unknown_address + unsolvable_address + [multisig_without_privkey_address]:
436            try:
437                self.nodes[0].addwitnessaddress(i)
438            except JSONRPCException as exp:
439                assert_equal(exp.error["message"], "Public key or redeemscript not known to wallet, or the key is uncompressed")
440            else:
441                assert(False)
442
443        for i in compressed_spendable_address + compressed_solvable_address:
444            witaddress = self.nodes[0].addwitnessaddress(i)
445            # addwitnessaddress should return the same address if it is a known P2SH-witness address
446            assert_equal(witaddress, self.nodes[0].addwitnessaddress(witaddress))
447
448        spendable_txid.append(self.mine_and_test_listunspent(spendable_anytime + spendable_after_importaddress, 2))
449        solvable_txid.append(self.mine_and_test_listunspent(solvable_anytime + solvable_after_importaddress, 1))
450        self.mine_and_test_listunspent(unsolvable_after_importaddress, 1)
451        self.mine_and_test_listunspent(unseen_anytime, 0)
452
453        # Repeat some tests. This time we don't add witness scripts with importaddress
454        # Import a compressed key and an uncompressed key, generate some multisig addresses
455        self.nodes[0].importprivkey("927pw6RW8ZekycnXqBQ2JS5nPyo1yRfGNN8oq74HeddWSpafDJH")
456        uncompressed_spendable_address = ["mguN2vNSCEUh6rJaXoAVwY3YZwZvEmf5xi"]
457        self.nodes[0].importprivkey("cMcrXaaUC48ZKpcyydfFo8PxHAjpsYLhdsp6nmtB3E2ER9UUHWnw")
458        compressed_spendable_address = ["n1UNmpmbVUJ9ytXYXiurmGPQ3TRrXqPWKL"]
459
460        self.nodes[0].importpubkey(pubkeys[5])
461        compressed_solvable_address = [key_to_p2pkh(pubkeys[5])]
462        self.nodes[0].importpubkey(pubkeys[6])
463        uncompressed_solvable_address = [key_to_p2pkh(pubkeys[6])]
464
465        spendable_after_addwitnessaddress = []      # These outputs should be seen after importaddress
466        solvable_after_addwitnessaddress=[]         # These outputs should be seen after importaddress but not spendable
467        unseen_anytime = []                         # These outputs should never be seen
468
469        uncompressed_spendable_address.append(self.nodes[0].addmultisigaddress(2, [uncompressed_spendable_address[0], compressed_spendable_address[0]]))
470        uncompressed_spendable_address.append(self.nodes[0].addmultisigaddress(2, [uncompressed_spendable_address[0], uncompressed_spendable_address[0]]))
471        compressed_spendable_address.append(self.nodes[0].addmultisigaddress(2, [compressed_spendable_address[0], compressed_spendable_address[0]]))
472        uncompressed_solvable_address.append(self.nodes[0].addmultisigaddress(2, [compressed_solvable_address[0], uncompressed_solvable_address[0]]))
473        compressed_solvable_address.append(self.nodes[0].addmultisigaddress(2, [compressed_spendable_address[0], compressed_solvable_address[0]]))
474
475        premature_witaddress = []
476
477        for i in compressed_spendable_address:
478            v = self.nodes[0].validateaddress(i)
479            if (v['isscript']):
480                [bare, p2sh, p2wsh, p2sh_p2wsh] = self.p2sh_address_to_script(v)
481                # P2WSH and P2SH(P2WSH) multisig with compressed keys are spendable after addwitnessaddress
482                spendable_after_addwitnessaddress.extend([p2wsh, p2sh_p2wsh])
483                premature_witaddress.append(script_to_p2sh(p2wsh))
484            else:
485                [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)
486                # P2WPKH, P2SH_P2WPKH are spendable after addwitnessaddress
487                spendable_after_addwitnessaddress.extend([p2wpkh, p2sh_p2wpkh])
488                premature_witaddress.append(script_to_p2sh(p2wpkh))
489
490        for i in uncompressed_spendable_address + uncompressed_solvable_address:
491            v = self.nodes[0].validateaddress(i)
492            if (v['isscript']):
493                [bare, p2sh, p2wsh, p2sh_p2wsh] = self.p2sh_address_to_script(v)
494                # P2WSH and P2SH(P2WSH) multisig with uncompressed keys are never seen
495                unseen_anytime.extend([p2wsh, p2sh_p2wsh])
496            else:
497                [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)
498                # P2WPKH, P2SH_P2WPKH with uncompressed keys are never seen
499                unseen_anytime.extend([p2wpkh, p2sh_p2wpkh])
500
501        for i in compressed_solvable_address:
502            v = self.nodes[0].validateaddress(i)
503            if (v['isscript']):
504                # P2WSH multisig without private key are seen after addwitnessaddress
505                [bare, p2sh, p2wsh, p2sh_p2wsh] = self.p2sh_address_to_script(v)
506                solvable_after_addwitnessaddress.extend([p2wsh, p2sh_p2wsh])
507                premature_witaddress.append(script_to_p2sh(p2wsh))
508            else:
509                [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)
510                # P2SH_P2PK, P2SH_P2PKH with compressed keys are seen after addwitnessaddress
511                solvable_after_addwitnessaddress.extend([p2wpkh, p2sh_p2wpkh])
512                premature_witaddress.append(script_to_p2sh(p2wpkh))
513
514        self.mine_and_test_listunspent(spendable_after_addwitnessaddress + solvable_after_addwitnessaddress + unseen_anytime, 0)
515
516        # addwitnessaddress should refuse to return a witness address if an uncompressed key is used
517        # note that a multisig address returned by addmultisigaddress is not solvable until it is added with importaddress
518        # premature_witaddress are not accepted until the script is added with addwitnessaddress first
519        for i in uncompressed_spendable_address + uncompressed_solvable_address + premature_witaddress + [compressed_solvable_address[1]]:
520            try:
521                self.nodes[0].addwitnessaddress(i)
522            except JSONRPCException as exp:
523                assert_equal(exp.error["message"], "Public key or redeemscript not known to wallet, or the key is uncompressed")
524            else:
525                assert(False)
526
527        # after importaddress it should pass addwitnessaddress
528        v = self.nodes[0].validateaddress(compressed_solvable_address[1])
529        self.nodes[0].importaddress(v['hex'],"",False,True)
530        for i in compressed_spendable_address + compressed_solvable_address + premature_witaddress:
531            witaddress = self.nodes[0].addwitnessaddress(i)
532            assert_equal(witaddress, self.nodes[0].addwitnessaddress(witaddress))
533
534        spendable_txid.append(self.mine_and_test_listunspent(spendable_after_addwitnessaddress, 2))
535        solvable_txid.append(self.mine_and_test_listunspent(solvable_after_addwitnessaddress, 1))
536        self.mine_and_test_listunspent(unseen_anytime, 0)
537
538        # Check that spendable outputs are really spendable
539        self.create_and_mine_tx_from_txids(spendable_txid)
540
541        # import all the private keys so solvable addresses become spendable
542        self.nodes[0].importprivkey("cPiM8Ub4heR9NBYmgVzJQiUH1if44GSBGiqaeJySuL2BKxubvgwb")
543        self.nodes[0].importprivkey("cPpAdHaD6VoYbW78kveN2bsvb45Q7G5PhaPApVUGwvF8VQ9brD97")
544        self.nodes[0].importprivkey("91zqCU5B9sdWxzMt1ca3VzbtVm2YM6Hi5Rxn4UDtxEaN9C9nzXV")
545        self.nodes[0].importprivkey("cPQFjcVRpAUBG8BA9hzr2yEzHwKoMgLkJZBBtK9vJnvGJgMjzTbd")
546        self.nodes[0].importprivkey("cQGtcm34xiLjB1v7bkRa4V3aAc9tS2UTuBZ1UnZGeSeNy627fN66")
547        self.nodes[0].importprivkey("cTW5mR5M45vHxXkeChZdtSPozrFwFgmEvTNnanCW6wrqwaCZ1X7K")
548        self.create_and_mine_tx_from_txids(solvable_txid)
549
550    def mine_and_test_listunspent(self, script_list, ismine):
551        utxo = find_unspent(self.nodes[0], 50)
552        tx = CTransaction()
553        tx.vin.append(CTxIn(COutPoint(int('0x'+utxo['txid'],0), utxo['vout'])))
554        for i in script_list:
555            tx.vout.append(CTxOut(10000000, i))
556        tx.rehash()
557        signresults = self.nodes[0].signrawtransaction(bytes_to_hex_str(tx.serialize_without_witness()))['hex']
558        txid = self.nodes[0].sendrawtransaction(signresults, True)
559        self.nodes[0].generate(1)
560        sync_blocks(self.nodes)
561        watchcount = 0
562        spendcount = 0
563        for i in self.nodes[0].listunspent():
564            if (i['txid'] == txid):
565                watchcount += 1
566                if (i['spendable'] == True):
567                    spendcount += 1
568        if (ismine == 2):
569            assert_equal(spendcount, len(script_list))
570        elif (ismine == 1):
571            assert_equal(watchcount, len(script_list))
572            assert_equal(spendcount, 0)
573        else:
574            assert_equal(watchcount, 0)
575        return txid
576
577    def p2sh_address_to_script(self,v):
578        bare = CScript(hex_str_to_bytes(v['hex']))
579        p2sh = CScript(hex_str_to_bytes(v['scriptPubKey']))
580        p2wsh = CScript([OP_0, sha256(bare)])
581        p2sh_p2wsh = CScript([OP_HASH160, hash160(p2wsh), OP_EQUAL])
582        return([bare, p2sh, p2wsh, p2sh_p2wsh])
583
584    def p2pkh_address_to_script(self,v):
585        pubkey = hex_str_to_bytes(v['pubkey'])
586        p2wpkh = CScript([OP_0, hash160(pubkey)])
587        p2sh_p2wpkh = CScript([OP_HASH160, hash160(p2wpkh), OP_EQUAL])
588        p2pk = CScript([pubkey, OP_CHECKSIG])
589        p2pkh = CScript(hex_str_to_bytes(v['scriptPubKey']))
590        p2sh_p2pk = CScript([OP_HASH160, hash160(p2pk), OP_EQUAL])
591        p2sh_p2pkh = CScript([OP_HASH160, hash160(p2pkh), OP_EQUAL])
592        p2wsh_p2pk = CScript([OP_0, sha256(p2pk)])
593        p2wsh_p2pkh = CScript([OP_0, sha256(p2pkh)])
594        p2sh_p2wsh_p2pk = CScript([OP_HASH160, hash160(p2wsh_p2pk), OP_EQUAL])
595        p2sh_p2wsh_p2pkh = CScript([OP_HASH160, hash160(p2wsh_p2pkh), OP_EQUAL])
596        return [p2wpkh, p2sh_p2wpkh, p2pk, p2pkh, p2sh_p2pk, p2sh_p2pkh, p2wsh_p2pk, p2wsh_p2pkh, p2sh_p2wsh_p2pk, p2sh_p2wsh_p2pkh]
597
598    def create_and_mine_tx_from_txids(self, txids, success = True):
599        tx = CTransaction()
600        for i in txids:
601            txtmp = CTransaction()
602            txraw = self.nodes[0].getrawtransaction(i)
603            f = BytesIO(hex_str_to_bytes(txraw))
604            txtmp.deserialize(f)
605            for j in range(len(txtmp.vout)):
606                tx.vin.append(CTxIn(COutPoint(int('0x'+i,0), j)))
607        tx.vout.append(CTxOut(0, CScript()))
608        tx.rehash()
609        signresults = self.nodes[0].signrawtransaction(bytes_to_hex_str(tx.serialize_without_witness()))['hex']
610        self.nodes[0].sendrawtransaction(signresults, True)
611        self.nodes[0].generate(1)
612        sync_blocks(self.nodes)
613
614
615if __name__ == '__main__':
616    SegWitTest().main()
617