Merge #12079: Improve prioritisetransaction test coverage
[bitcoinplatinum.git] / test / functional / segwit.py
blobdbf4a86e3812fe79675c12142071e5b568a2872f
1 #!/usr/bin/env python3
2 # Copyright (c) 2016-2017 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."""
7 from test_framework.test_framework import BitcoinTestFramework
8 from test_framework.util import *
9 from test_framework.mininode import sha256, CTransaction, CTxIn, COutPoint, CTxOut, COIN, ToHex, FromHex
10 from test_framework.address import script_to_p2sh, key_to_p2pkh, key_to_p2sh_p2wpkh, key_to_p2wpkh, script_to_p2sh_p2wsh, script_to_p2wsh, program_to_witness
11 from test_framework.script import CScript, OP_HASH160, OP_CHECKSIG, OP_0, hash160, OP_EQUAL, OP_DUP, OP_EQUALVERIFY, OP_1, OP_2, OP_CHECKMULTISIG, OP_TRUE
12 from io import BytesIO
14 NODE_0 = 0
15 NODE_2 = 2
16 WIT_V0 = 0
17 WIT_V1 = 1
19 # Create a scriptPubKey corresponding to either a P2WPKH output for the
20 # given pubkey, or a P2WSH output of a 1-of-1 multisig for the given
21 # pubkey. Returns the hex encoding of the scriptPubKey.
22 def witness_script(use_p2wsh, pubkey):
23 if (use_p2wsh == False):
24 # P2WPKH instead
25 pubkeyhash = hash160(hex_str_to_bytes(pubkey))
26 pkscript = CScript([OP_0, pubkeyhash])
27 else:
28 # 1-of-1 multisig
29 witness_program = CScript([OP_1, hex_str_to_bytes(pubkey), OP_1, OP_CHECKMULTISIG])
30 scripthash = sha256(witness_program)
31 pkscript = CScript([OP_0, scripthash])
32 return bytes_to_hex_str(pkscript)
34 # Return a transaction (in hex) that spends the given utxo to a segwit output,
35 # optionally wrapping the segwit output using P2SH.
36 def create_witness_tx(node, use_p2wsh, utxo, pubkey, encode_p2sh, amount):
37 if use_p2wsh:
38 program = CScript([OP_1, hex_str_to_bytes(pubkey), OP_1, OP_CHECKMULTISIG])
39 addr = script_to_p2sh_p2wsh(program) if encode_p2sh else script_to_p2wsh(program)
40 else:
41 addr = key_to_p2sh_p2wpkh(pubkey) if encode_p2sh else key_to_p2wpkh(pubkey)
42 if not encode_p2sh:
43 assert_equal(node.validateaddress(addr)['scriptPubKey'], witness_script(use_p2wsh, pubkey))
44 return node.createrawtransaction([utxo], {addr: amount})
46 # Create a transaction spending a given utxo to a segwit output corresponding
47 # to the given pubkey: use_p2wsh determines whether to use P2WPKH or P2WSH;
48 # encode_p2sh determines whether to wrap in P2SH.
49 # sign=True will have the given node sign the transaction.
50 # insert_redeem_script will be added to the scriptSig, if given.
51 def send_to_witness(use_p2wsh, node, utxo, pubkey, encode_p2sh, amount, sign=True, insert_redeem_script=""):
52 tx_to_witness = create_witness_tx(node, use_p2wsh, utxo, pubkey, encode_p2sh, amount)
53 if (sign):
54 signed = node.signrawtransaction(tx_to_witness)
55 assert("errors" not in signed or len(["errors"]) == 0)
56 return node.sendrawtransaction(signed["hex"])
57 else:
58 if (insert_redeem_script):
59 tx = FromHex(CTransaction(), tx_to_witness)
60 tx.vin[0].scriptSig += CScript([hex_str_to_bytes(insert_redeem_script)])
61 tx_to_witness = ToHex(tx)
63 return node.sendrawtransaction(tx_to_witness)
65 def getutxo(txid):
66 utxo = {}
67 utxo["vout"] = 0
68 utxo["txid"] = txid
69 return utxo
71 def find_unspent(node, min_value):
72 for utxo in node.listunspent():
73 if utxo['amount'] >= min_value:
74 return utxo
76 class SegWitTest(BitcoinTestFramework):
77 def set_test_params(self):
78 self.setup_clean_chain = True
79 self.num_nodes = 3
80 # This test tests SegWit both pre and post-activation, so use the normal BIP9 activation.
81 self.extra_args = [["-walletprematurewitness", "-rpcserialversion=0", "-vbparams=segwit:0:999999999999"],
82 ["-blockversion=4", "-promiscuousmempoolflags=517", "-prematurewitness", "-walletprematurewitness", "-rpcserialversion=1", "-vbparams=segwit:0:999999999999"],
83 ["-blockversion=536870915", "-promiscuousmempoolflags=517", "-prematurewitness", "-walletprematurewitness", "-vbparams=segwit:0:999999999999"]]
85 def setup_network(self):
86 super().setup_network()
87 connect_nodes(self.nodes[0], 2)
88 self.sync_all()
90 def success_mine(self, node, txid, sign, redeem_script=""):
91 send_to_witness(1, node, getutxo(txid), self.pubkey[0], False, Decimal("49.998"), sign, redeem_script)
92 block = node.generate(1)
93 assert_equal(len(node.getblock(block[0])["tx"]), 2)
94 sync_blocks(self.nodes)
96 def skip_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"]), 1)
100 sync_blocks(self.nodes)
102 def fail_accept(self, node, error_msg, txid, sign, redeem_script=""):
103 assert_raises_rpc_error(-26, error_msg, send_to_witness, 1, node, getutxo(txid), self.pubkey[0], False, Decimal("49.998"), sign, redeem_script)
105 def fail_mine(self, node, txid, sign, redeem_script=""):
106 send_to_witness(1, node, getutxo(txid), self.pubkey[0], False, Decimal("49.998"), sign, redeem_script)
107 assert_raises_rpc_error(-1, "CreateNewBlock: TestBlockValidity failed", node.generate, 1)
108 sync_blocks(self.nodes)
110 def run_test(self):
111 self.nodes[0].generate(161) #block 161
113 self.log.info("Verify sigops are counted in GBT with pre-BIP141 rules before the fork")
114 txid = self.nodes[0].sendtoaddress(self.nodes[0].getnewaddress(), 1)
115 tmpl = self.nodes[0].getblocktemplate({})
116 assert(tmpl['sizelimit'] == 1000000)
117 assert('weightlimit' not in tmpl)
118 assert(tmpl['sigoplimit'] == 20000)
119 assert(tmpl['transactions'][0]['hash'] == txid)
120 assert(tmpl['transactions'][0]['sigops'] == 2)
121 tmpl = self.nodes[0].getblocktemplate({'rules':['segwit']})
122 assert(tmpl['sizelimit'] == 1000000)
123 assert('weightlimit' not in tmpl)
124 assert(tmpl['sigoplimit'] == 20000)
125 assert(tmpl['transactions'][0]['hash'] == txid)
126 assert(tmpl['transactions'][0]['sigops'] == 2)
127 self.nodes[0].generate(1) #block 162
129 balance_presetup = self.nodes[0].getbalance()
130 self.pubkey = []
131 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
132 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
133 for i in range(3):
134 newaddress = self.nodes[i].getnewaddress()
135 self.pubkey.append(self.nodes[i].validateaddress(newaddress)["pubkey"])
136 multiaddress = self.nodes[i].addmultisigaddress(1, [self.pubkey[-1]])
137 multiscript = CScript([OP_1, hex_str_to_bytes(self.pubkey[-1]), OP_1, OP_CHECKMULTISIG])
138 p2sh_addr = self.nodes[i].addwitnessaddress(newaddress, True)
139 bip173_addr = self.nodes[i].addwitnessaddress(newaddress, False)
140 p2sh_ms_addr = self.nodes[i].addwitnessaddress(multiaddress, True)
141 bip173_ms_addr = self.nodes[i].addwitnessaddress(multiaddress, False)
142 assert_equal(p2sh_addr, key_to_p2sh_p2wpkh(self.pubkey[-1]))
143 assert_equal(bip173_addr, key_to_p2wpkh(self.pubkey[-1]))
144 assert_equal(p2sh_ms_addr, script_to_p2sh_p2wsh(multiscript))
145 assert_equal(bip173_ms_addr, script_to_p2wsh(multiscript))
146 p2sh_ids.append([])
147 wit_ids.append([])
148 for v in range(2):
149 p2sh_ids[i].append([])
150 wit_ids[i].append([])
152 for i in range(5):
153 for n in range(3):
154 for v in range(2):
155 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")))
156 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")))
158 self.nodes[0].generate(1) #block 163
159 sync_blocks(self.nodes)
161 # Make sure all nodes recognize the transactions as theirs
162 assert_equal(self.nodes[0].getbalance(), balance_presetup - 60*50 + 20*Decimal("49.999") + 50)
163 assert_equal(self.nodes[1].getbalance(), 20*Decimal("49.999"))
164 assert_equal(self.nodes[2].getbalance(), 20*Decimal("49.999"))
166 self.nodes[0].generate(260) #block 423
167 sync_blocks(self.nodes)
169 self.log.info("Verify default node can't accept any witness format txs before fork")
170 # unsigned, no scriptsig
171 self.fail_accept(self.nodes[0], "mandatory-script-verify-flag", wit_ids[NODE_0][WIT_V0][0], False)
172 self.fail_accept(self.nodes[0], "mandatory-script-verify-flag", wit_ids[NODE_0][WIT_V1][0], False)
173 self.fail_accept(self.nodes[0], "mandatory-script-verify-flag", p2sh_ids[NODE_0][WIT_V0][0], False)
174 self.fail_accept(self.nodes[0], "mandatory-script-verify-flag", p2sh_ids[NODE_0][WIT_V1][0], False)
175 # unsigned with redeem script
176 self.fail_accept(self.nodes[0], "mandatory-script-verify-flag", p2sh_ids[NODE_0][WIT_V0][0], False, witness_script(False, self.pubkey[0]))
177 self.fail_accept(self.nodes[0], "mandatory-script-verify-flag", p2sh_ids[NODE_0][WIT_V1][0], False, witness_script(True, self.pubkey[0]))
178 # signed
179 self.fail_accept(self.nodes[0], "no-witness-yet", wit_ids[NODE_0][WIT_V0][0], True)
180 self.fail_accept(self.nodes[0], "no-witness-yet", wit_ids[NODE_0][WIT_V1][0], True)
181 self.fail_accept(self.nodes[0], "no-witness-yet", p2sh_ids[NODE_0][WIT_V0][0], True)
182 self.fail_accept(self.nodes[0], "no-witness-yet", p2sh_ids[NODE_0][WIT_V1][0], True)
184 self.log.info("Verify witness txs are skipped for mining before the fork")
185 self.skip_mine(self.nodes[2], wit_ids[NODE_2][WIT_V0][0], True) #block 424
186 self.skip_mine(self.nodes[2], wit_ids[NODE_2][WIT_V1][0], True) #block 425
187 self.skip_mine(self.nodes[2], p2sh_ids[NODE_2][WIT_V0][0], True) #block 426
188 self.skip_mine(self.nodes[2], p2sh_ids[NODE_2][WIT_V1][0], True) #block 427
190 # TODO: An old node would see these txs without witnesses and be able to mine them
192 self.log.info("Verify unsigned bare witness txs in versionbits-setting blocks are valid before the fork")
193 self.success_mine(self.nodes[2], wit_ids[NODE_2][WIT_V0][1], False) #block 428
194 self.success_mine(self.nodes[2], wit_ids[NODE_2][WIT_V1][1], False) #block 429
196 self.log.info("Verify unsigned p2sh witness txs without a redeem script are invalid")
197 self.fail_accept(self.nodes[2], "mandatory-script-verify-flag", p2sh_ids[NODE_2][WIT_V0][1], False)
198 self.fail_accept(self.nodes[2], "mandatory-script-verify-flag", p2sh_ids[NODE_2][WIT_V1][1], False)
200 self.log.info("Verify unsigned p2sh witness txs with a redeem script in versionbits-settings blocks are valid before the fork")
201 self.success_mine(self.nodes[2], p2sh_ids[NODE_2][WIT_V0][1], False, witness_script(False, self.pubkey[2])) #block 430
202 self.success_mine(self.nodes[2], p2sh_ids[NODE_2][WIT_V1][1], False, witness_script(True, self.pubkey[2])) #block 431
204 self.log.info("Verify previous witness txs skipped for mining can now be mined")
205 assert_equal(len(self.nodes[2].getrawmempool()), 4)
206 block = self.nodes[2].generate(1) #block 432 (first block with new rules; 432 = 144 * 3)
207 sync_blocks(self.nodes)
208 assert_equal(len(self.nodes[2].getrawmempool()), 0)
209 segwit_tx_list = self.nodes[2].getblock(block[0])["tx"]
210 assert_equal(len(segwit_tx_list), 5)
212 self.log.info("Verify block and transaction serialization rpcs return differing serializations depending on rpc serialization flag")
213 assert(self.nodes[2].getblock(block[0], False) != self.nodes[0].getblock(block[0], False))
214 assert(self.nodes[1].getblock(block[0], False) == self.nodes[2].getblock(block[0], False))
215 for i in range(len(segwit_tx_list)):
216 tx = FromHex(CTransaction(), self.nodes[2].gettransaction(segwit_tx_list[i])["hex"])
217 assert(self.nodes[2].getrawtransaction(segwit_tx_list[i]) != self.nodes[0].getrawtransaction(segwit_tx_list[i]))
218 assert(self.nodes[1].getrawtransaction(segwit_tx_list[i], 0) == self.nodes[2].getrawtransaction(segwit_tx_list[i]))
219 assert(self.nodes[0].getrawtransaction(segwit_tx_list[i]) != self.nodes[2].gettransaction(segwit_tx_list[i])["hex"])
220 assert(self.nodes[1].getrawtransaction(segwit_tx_list[i]) == self.nodes[2].gettransaction(segwit_tx_list[i])["hex"])
221 assert(self.nodes[0].getrawtransaction(segwit_tx_list[i]) == bytes_to_hex_str(tx.serialize_without_witness()))
223 self.log.info("Verify witness txs without witness data are invalid after the fork")
224 self.fail_mine(self.nodes[2], wit_ids[NODE_2][WIT_V0][2], False)
225 self.fail_mine(self.nodes[2], wit_ids[NODE_2][WIT_V1][2], False)
226 self.fail_mine(self.nodes[2], p2sh_ids[NODE_2][WIT_V0][2], False, witness_script(False, self.pubkey[2]))
227 self.fail_mine(self.nodes[2], p2sh_ids[NODE_2][WIT_V1][2], False, witness_script(True, self.pubkey[2]))
229 self.log.info("Verify default node can now use witness txs")
230 self.success_mine(self.nodes[0], wit_ids[NODE_0][WIT_V0][0], True) #block 432
231 self.success_mine(self.nodes[0], wit_ids[NODE_0][WIT_V1][0], True) #block 433
232 self.success_mine(self.nodes[0], p2sh_ids[NODE_0][WIT_V0][0], True) #block 434
233 self.success_mine(self.nodes[0], p2sh_ids[NODE_0][WIT_V1][0], True) #block 435
235 self.log.info("Verify sigops are counted in GBT with BIP141 rules after the fork")
236 txid = self.nodes[0].sendtoaddress(self.nodes[0].getnewaddress(), 1)
237 tmpl = self.nodes[0].getblocktemplate({'rules':['segwit']})
238 assert(tmpl['sizelimit'] >= 3999577) # actual maximum size is lower due to minimum mandatory non-witness data
239 assert(tmpl['weightlimit'] == 4000000)
240 assert(tmpl['sigoplimit'] == 80000)
241 assert(tmpl['transactions'][0]['txid'] == txid)
242 assert(tmpl['transactions'][0]['sigops'] == 8)
244 self.nodes[0].generate(1) # Mine a block to clear the gbt cache
246 self.log.info("Non-segwit miners are able to use GBT response after activation.")
247 # Create a 3-tx chain: tx1 (non-segwit input, paying to a segwit output) ->
248 # tx2 (segwit input, paying to a non-segwit output) ->
249 # tx3 (non-segwit input, paying to a non-segwit output).
250 # tx1 is allowed to appear in the block, but no others.
251 txid1 = send_to_witness(1, self.nodes[0], find_unspent(self.nodes[0], 50), self.pubkey[0], False, Decimal("49.996"))
252 hex_tx = self.nodes[0].gettransaction(txid)['hex']
253 tx = FromHex(CTransaction(), hex_tx)
254 assert(tx.wit.is_null()) # This should not be a segwit input
255 assert(txid1 in self.nodes[0].getrawmempool())
257 # Now create tx2, which will spend from txid1.
258 tx = CTransaction()
259 tx.vin.append(CTxIn(COutPoint(int(txid1, 16), 0), b''))
260 tx.vout.append(CTxOut(int(49.99*COIN), CScript([OP_TRUE])))
261 tx2_hex = self.nodes[0].signrawtransaction(ToHex(tx))['hex']
262 txid2 = self.nodes[0].sendrawtransaction(tx2_hex)
263 tx = FromHex(CTransaction(), tx2_hex)
264 assert(not tx.wit.is_null())
266 # Now create tx3, which will spend from txid2
267 tx = CTransaction()
268 tx.vin.append(CTxIn(COutPoint(int(txid2, 16), 0), b""))
269 tx.vout.append(CTxOut(int(49.95*COIN), CScript([OP_TRUE]))) # Huge fee
270 tx.calc_sha256()
271 txid3 = self.nodes[0].sendrawtransaction(ToHex(tx))
272 assert(tx.wit.is_null())
273 assert(txid3 in self.nodes[0].getrawmempool())
275 # Now try calling getblocktemplate() without segwit support.
276 template = self.nodes[0].getblocktemplate()
278 # Check that tx1 is the only transaction of the 3 in the template.
279 template_txids = [ t['txid'] for t in template['transactions'] ]
280 assert(txid2 not in template_txids and txid3 not in template_txids)
281 assert(txid1 in template_txids)
283 # Check that running with segwit support results in all 3 being included.
284 template = self.nodes[0].getblocktemplate({"rules": ["segwit"]})
285 template_txids = [ t['txid'] for t in template['transactions'] ]
286 assert(txid1 in template_txids)
287 assert(txid2 in template_txids)
288 assert(txid3 in template_txids)
290 # Check that wtxid is properly reported in mempool entry
291 assert_equal(int(self.nodes[0].getmempoolentry(txid3)["wtxid"], 16), tx.calc_sha256(True))
293 # Mine a block to clear the gbt cache again.
294 self.nodes[0].generate(1)
296 self.log.info("Verify behaviour of importaddress, addwitnessaddress and listunspent")
298 # Some public keys to be used later
299 pubkeys = [
300 "0363D44AABD0F1699138239DF2F042C3282C0671CC7A76826A55C8203D90E39242", # cPiM8Ub4heR9NBYmgVzJQiUH1if44GSBGiqaeJySuL2BKxubvgwb
301 "02D3E626B3E616FC8662B489C123349FECBFC611E778E5BE739B257EAE4721E5BF", # cPpAdHaD6VoYbW78kveN2bsvb45Q7G5PhaPApVUGwvF8VQ9brD97
302 "04A47F2CBCEFFA7B9BCDA184E7D5668D3DA6F9079AD41E422FA5FD7B2D458F2538A62F5BD8EC85C2477F39650BD391EA6250207065B2A81DA8B009FC891E898F0E", # 91zqCU5B9sdWxzMt1ca3VzbtVm2YM6Hi5Rxn4UDtxEaN9C9nzXV
303 "02A47F2CBCEFFA7B9BCDA184E7D5668D3DA6F9079AD41E422FA5FD7B2D458F2538", # cPQFjcVRpAUBG8BA9hzr2yEzHwKoMgLkJZBBtK9vJnvGJgMjzTbd
304 "036722F784214129FEB9E8129D626324F3F6716555B603FFE8300BBCB882151228", # cQGtcm34xiLjB1v7bkRa4V3aAc9tS2UTuBZ1UnZGeSeNy627fN66
305 "0266A8396EE936BF6D99D17920DB21C6C7B1AB14C639D5CD72B300297E416FD2EC", # cTW5mR5M45vHxXkeChZdtSPozrFwFgmEvTNnanCW6wrqwaCZ1X7K
306 "0450A38BD7F0AC212FEBA77354A9B036A32E0F7C81FC4E0C5ADCA7C549C4505D2522458C2D9AE3CEFD684E039194B72C8A10F9CB9D4764AB26FCC2718D421D3B84", # 92h2XPssjBpsJN5CqSP7v9a7cf2kgDunBC6PDFwJHMACM1rrVBJ
309 # Import a compressed key and an uncompressed key, generate some multisig addresses
310 self.nodes[0].importprivkey("92e6XLo5jVAVwrQKPNTs93oQco8f8sDNBcpv73Dsrs397fQtFQn")
311 uncompressed_spendable_address = ["mvozP4UwyGD2mGZU4D2eMvMLPB9WkMmMQu"]
312 self.nodes[0].importprivkey("cNC8eQ5dg3mFAVePDX4ddmPYpPbw41r9bm2jd1nLJT77e6RrzTRR")
313 compressed_spendable_address = ["mmWQubrDomqpgSYekvsU7HWEVjLFHAakLe"]
314 assert ((self.nodes[0].validateaddress(uncompressed_spendable_address[0])['iscompressed'] == False))
315 assert ((self.nodes[0].validateaddress(compressed_spendable_address[0])['iscompressed'] == True))
317 self.nodes[0].importpubkey(pubkeys[0])
318 compressed_solvable_address = [key_to_p2pkh(pubkeys[0])]
319 self.nodes[0].importpubkey(pubkeys[1])
320 compressed_solvable_address.append(key_to_p2pkh(pubkeys[1]))
321 self.nodes[0].importpubkey(pubkeys[2])
322 uncompressed_solvable_address = [key_to_p2pkh(pubkeys[2])]
324 spendable_anytime = [] # These outputs should be seen anytime after importprivkey and addmultisigaddress
325 spendable_after_importaddress = [] # These outputs should be seen after importaddress
326 solvable_after_importaddress = [] # These outputs should be seen after importaddress but not spendable
327 unsolvable_after_importaddress = [] # These outputs should be unsolvable after importaddress
328 solvable_anytime = [] # These outputs should be solvable after importpubkey
329 unseen_anytime = [] # These outputs should never be seen
331 uncompressed_spendable_address.append(self.nodes[0].addmultisigaddress(2, [uncompressed_spendable_address[0], compressed_spendable_address[0]]))
332 uncompressed_spendable_address.append(self.nodes[0].addmultisigaddress(2, [uncompressed_spendable_address[0], uncompressed_spendable_address[0]]))
333 compressed_spendable_address.append(self.nodes[0].addmultisigaddress(2, [compressed_spendable_address[0], compressed_spendable_address[0]]))
334 uncompressed_solvable_address.append(self.nodes[0].addmultisigaddress(2, [compressed_spendable_address[0], uncompressed_solvable_address[0]]))
335 compressed_solvable_address.append(self.nodes[0].addmultisigaddress(2, [compressed_spendable_address[0], compressed_solvable_address[0]]))
336 compressed_solvable_address.append(self.nodes[0].addmultisigaddress(2, [compressed_solvable_address[0], compressed_solvable_address[1]]))
337 unknown_address = ["mtKKyoHabkk6e4ppT7NaM7THqPUt7AzPrT", "2NDP3jLWAFT8NDAiUa9qiE6oBt2awmMq7Dx"]
339 # Test multisig_without_privkey
340 # We have 2 public keys without private keys, use addmultisigaddress to add to wallet.
341 # Money sent to P2SH of multisig of this should only be seen after importaddress with the BASE58 P2SH address.
343 multisig_without_privkey_address = self.nodes[0].addmultisigaddress(2, [pubkeys[3], pubkeys[4]])
344 script = CScript([OP_2, hex_str_to_bytes(pubkeys[3]), hex_str_to_bytes(pubkeys[4]), OP_2, OP_CHECKMULTISIG])
345 solvable_after_importaddress.append(CScript([OP_HASH160, hash160(script), OP_EQUAL]))
347 for i in compressed_spendable_address:
348 v = self.nodes[0].validateaddress(i)
349 if (v['isscript']):
350 [bare, p2sh, p2wsh, p2sh_p2wsh] = self.p2sh_address_to_script(v)
351 # bare and p2sh multisig with compressed keys should always be spendable
352 spendable_anytime.extend([bare, p2sh])
353 # P2WSH and P2SH(P2WSH) multisig with compressed keys are spendable after direct importaddress
354 spendable_after_importaddress.extend([p2wsh, p2sh_p2wsh])
355 else:
356 [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)
357 # normal P2PKH and P2PK with compressed keys should always be spendable
358 spendable_anytime.extend([p2pkh, p2pk])
359 # P2SH_P2PK, P2SH_P2PKH, and witness with compressed keys are spendable after direct importaddress
360 spendable_after_importaddress.extend([p2wpkh, p2sh_p2wpkh, p2sh_p2pk, p2sh_p2pkh, p2wsh_p2pk, p2wsh_p2pkh, p2sh_p2wsh_p2pk, p2sh_p2wsh_p2pkh])
362 for i in uncompressed_spendable_address:
363 v = self.nodes[0].validateaddress(i)
364 if (v['isscript']):
365 [bare, p2sh, p2wsh, p2sh_p2wsh] = self.p2sh_address_to_script(v)
366 # bare and p2sh multisig with uncompressed keys should always be spendable
367 spendable_anytime.extend([bare, p2sh])
368 # P2WSH and P2SH(P2WSH) multisig with uncompressed keys are never seen
369 unseen_anytime.extend([p2wsh, p2sh_p2wsh])
370 else:
371 [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)
372 # normal P2PKH and P2PK with uncompressed keys should always be spendable
373 spendable_anytime.extend([p2pkh, p2pk])
374 # P2SH_P2PK and P2SH_P2PKH are spendable after direct importaddress
375 spendable_after_importaddress.extend([p2sh_p2pk, p2sh_p2pkh])
376 # witness with uncompressed keys are never seen
377 unseen_anytime.extend([p2wpkh, p2sh_p2wpkh, p2wsh_p2pk, p2wsh_p2pkh, p2sh_p2wsh_p2pk, p2sh_p2wsh_p2pkh])
379 for i in compressed_solvable_address:
380 v = self.nodes[0].validateaddress(i)
381 if (v['isscript']):
382 # Multisig without private is not seen after addmultisigaddress, but seen after importaddress
383 [bare, p2sh, p2wsh, p2sh_p2wsh] = self.p2sh_address_to_script(v)
384 solvable_after_importaddress.extend([bare, p2sh, p2wsh, p2sh_p2wsh])
385 else:
386 [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)
387 # normal P2PKH and P2PK with compressed keys should always be seen
388 solvable_anytime.extend([p2pkh, p2pk])
389 # P2SH_P2PK, P2SH_P2PKH, and witness with compressed keys are seen after direct importaddress
390 solvable_after_importaddress.extend([p2wpkh, p2sh_p2wpkh, p2sh_p2pk, p2sh_p2pkh, p2wsh_p2pk, p2wsh_p2pkh, p2sh_p2wsh_p2pk, p2sh_p2wsh_p2pkh])
392 for i in uncompressed_solvable_address:
393 v = self.nodes[0].validateaddress(i)
394 if (v['isscript']):
395 [bare, p2sh, p2wsh, p2sh_p2wsh] = self.p2sh_address_to_script(v)
396 # Base uncompressed multisig without private is not seen after addmultisigaddress, but seen after importaddress
397 solvable_after_importaddress.extend([bare, p2sh])
398 # P2WSH and P2SH(P2WSH) multisig with uncompressed keys are never seen
399 unseen_anytime.extend([p2wsh, p2sh_p2wsh])
400 else:
401 [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)
402 # normal P2PKH and P2PK with uncompressed keys should always be seen
403 solvable_anytime.extend([p2pkh, p2pk])
404 # P2SH_P2PK, P2SH_P2PKH with uncompressed keys are seen after direct importaddress
405 solvable_after_importaddress.extend([p2sh_p2pk, p2sh_p2pkh])
406 # witness with uncompressed keys are never seen
407 unseen_anytime.extend([p2wpkh, p2sh_p2wpkh, p2wsh_p2pk, p2wsh_p2pkh, p2sh_p2wsh_p2pk, p2sh_p2wsh_p2pkh])
409 op1 = CScript([OP_1])
410 op0 = CScript([OP_0])
411 # 2N7MGY19ti4KDMSzRfPAssP6Pxyuxoi6jLe is the P2SH(P2PKH) version of mjoE3sSrb8ByYEvgnC3Aox86u1CHnfJA4V
412 unsolvable_address = ["mjoE3sSrb8ByYEvgnC3Aox86u1CHnfJA4V", "2N7MGY19ti4KDMSzRfPAssP6Pxyuxoi6jLe", script_to_p2sh(op1), script_to_p2sh(op0)]
413 unsolvable_address_key = hex_str_to_bytes("02341AEC7587A51CDE5279E0630A531AEA2615A9F80B17E8D9376327BAEAA59E3D")
414 unsolvablep2pkh = CScript([OP_DUP, OP_HASH160, hash160(unsolvable_address_key), OP_EQUALVERIFY, OP_CHECKSIG])
415 unsolvablep2wshp2pkh = CScript([OP_0, sha256(unsolvablep2pkh)])
416 p2shop0 = CScript([OP_HASH160, hash160(op0), OP_EQUAL])
417 p2wshop1 = CScript([OP_0, sha256(op1)])
418 unsolvable_after_importaddress.append(unsolvablep2pkh)
419 unsolvable_after_importaddress.append(unsolvablep2wshp2pkh)
420 unsolvable_after_importaddress.append(op1) # OP_1 will be imported as script
421 unsolvable_after_importaddress.append(p2wshop1)
422 unseen_anytime.append(op0) # OP_0 will be imported as P2SH address with no script provided
423 unsolvable_after_importaddress.append(p2shop0)
425 spendable_txid = []
426 solvable_txid = []
427 spendable_txid.append(self.mine_and_test_listunspent(spendable_anytime, 2))
428 solvable_txid.append(self.mine_and_test_listunspent(solvable_anytime, 1))
429 self.mine_and_test_listunspent(spendable_after_importaddress + solvable_after_importaddress + unseen_anytime + unsolvable_after_importaddress, 0)
431 importlist = []
432 for i in compressed_spendable_address + uncompressed_spendable_address + compressed_solvable_address + uncompressed_solvable_address:
433 v = self.nodes[0].validateaddress(i)
434 if (v['isscript']):
435 bare = hex_str_to_bytes(v['hex'])
436 importlist.append(bytes_to_hex_str(bare))
437 importlist.append(bytes_to_hex_str(CScript([OP_0, sha256(bare)])))
438 else:
439 pubkey = hex_str_to_bytes(v['pubkey'])
440 p2pk = CScript([pubkey, OP_CHECKSIG])
441 p2pkh = CScript([OP_DUP, OP_HASH160, hash160(pubkey), OP_EQUALVERIFY, OP_CHECKSIG])
442 importlist.append(bytes_to_hex_str(p2pk))
443 importlist.append(bytes_to_hex_str(p2pkh))
444 importlist.append(bytes_to_hex_str(CScript([OP_0, hash160(pubkey)])))
445 importlist.append(bytes_to_hex_str(CScript([OP_0, sha256(p2pk)])))
446 importlist.append(bytes_to_hex_str(CScript([OP_0, sha256(p2pkh)])))
448 importlist.append(bytes_to_hex_str(unsolvablep2pkh))
449 importlist.append(bytes_to_hex_str(unsolvablep2wshp2pkh))
450 importlist.append(bytes_to_hex_str(op1))
451 importlist.append(bytes_to_hex_str(p2wshop1))
453 for i in importlist:
454 # import all generated addresses. The wallet already has the private keys for some of these, so catch JSON RPC
455 # exceptions and continue.
456 try_rpc(-4, "The wallet already contains the private key for this address or script", self.nodes[0].importaddress, i, "", False, True)
458 self.nodes[0].importaddress(script_to_p2sh(op0)) # import OP_0 as address only
459 self.nodes[0].importaddress(multisig_without_privkey_address) # Test multisig_without_privkey
461 spendable_txid.append(self.mine_and_test_listunspent(spendable_anytime + spendable_after_importaddress, 2))
462 solvable_txid.append(self.mine_and_test_listunspent(solvable_anytime + solvable_after_importaddress, 1))
463 self.mine_and_test_listunspent(unsolvable_after_importaddress, 1)
464 self.mine_and_test_listunspent(unseen_anytime, 0)
466 # addwitnessaddress should refuse to return a witness address if an uncompressed key is used
467 # note that no witness address should be returned by unsolvable addresses
468 for i in uncompressed_spendable_address + uncompressed_solvable_address + unknown_address + unsolvable_address:
469 assert_raises_rpc_error(-4, "Public key or redeemscript not known to wallet, or the key is uncompressed", self.nodes[0].addwitnessaddress, i)
471 # addwitnessaddress should return a witness addresses even if keys are not in the wallet
472 self.nodes[0].addwitnessaddress(multisig_without_privkey_address)
474 for i in compressed_spendable_address + compressed_solvable_address:
475 witaddress = self.nodes[0].addwitnessaddress(i)
476 # addwitnessaddress should return the same address if it is a known P2SH-witness address
477 assert_equal(witaddress, self.nodes[0].addwitnessaddress(witaddress))
479 spendable_txid.append(self.mine_and_test_listunspent(spendable_anytime + spendable_after_importaddress, 2))
480 solvable_txid.append(self.mine_and_test_listunspent(solvable_anytime + solvable_after_importaddress, 1))
481 self.mine_and_test_listunspent(unsolvable_after_importaddress, 1)
482 self.mine_and_test_listunspent(unseen_anytime, 0)
484 # Repeat some tests. This time we don't add witness scripts with importaddress
485 # Import a compressed key and an uncompressed key, generate some multisig addresses
486 self.nodes[0].importprivkey("927pw6RW8ZekycnXqBQ2JS5nPyo1yRfGNN8oq74HeddWSpafDJH")
487 uncompressed_spendable_address = ["mguN2vNSCEUh6rJaXoAVwY3YZwZvEmf5xi"]
488 self.nodes[0].importprivkey("cMcrXaaUC48ZKpcyydfFo8PxHAjpsYLhdsp6nmtB3E2ER9UUHWnw")
489 compressed_spendable_address = ["n1UNmpmbVUJ9ytXYXiurmGPQ3TRrXqPWKL"]
491 self.nodes[0].importpubkey(pubkeys[5])
492 compressed_solvable_address = [key_to_p2pkh(pubkeys[5])]
493 self.nodes[0].importpubkey(pubkeys[6])
494 uncompressed_solvable_address = [key_to_p2pkh(pubkeys[6])]
496 spendable_after_addwitnessaddress = [] # These outputs should be seen after importaddress
497 solvable_after_addwitnessaddress=[] # These outputs should be seen after importaddress but not spendable
498 unseen_anytime = [] # These outputs should never be seen
500 uncompressed_spendable_address.append(self.nodes[0].addmultisigaddress(2, [uncompressed_spendable_address[0], compressed_spendable_address[0]]))
501 uncompressed_spendable_address.append(self.nodes[0].addmultisigaddress(2, [uncompressed_spendable_address[0], uncompressed_spendable_address[0]]))
502 compressed_spendable_address.append(self.nodes[0].addmultisigaddress(2, [compressed_spendable_address[0], compressed_spendable_address[0]]))
503 uncompressed_solvable_address.append(self.nodes[0].addmultisigaddress(2, [compressed_solvable_address[0], uncompressed_solvable_address[0]]))
504 compressed_solvable_address.append(self.nodes[0].addmultisigaddress(2, [compressed_spendable_address[0], compressed_solvable_address[0]]))
506 premature_witaddress = []
508 for i in compressed_spendable_address:
509 v = self.nodes[0].validateaddress(i)
510 if (v['isscript']):
511 [bare, p2sh, p2wsh, p2sh_p2wsh] = self.p2sh_address_to_script(v)
512 # P2WSH and P2SH(P2WSH) multisig with compressed keys are spendable after addwitnessaddress
513 spendable_after_addwitnessaddress.extend([p2wsh, p2sh_p2wsh])
514 premature_witaddress.append(script_to_p2sh(p2wsh))
515 else:
516 [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)
517 # P2WPKH, P2SH_P2WPKH are spendable after addwitnessaddress
518 spendable_after_addwitnessaddress.extend([p2wpkh, p2sh_p2wpkh])
519 premature_witaddress.append(script_to_p2sh(p2wpkh))
521 for i in uncompressed_spendable_address + uncompressed_solvable_address:
522 v = self.nodes[0].validateaddress(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])
532 for i in compressed_solvable_address:
533 v = self.nodes[0].validateaddress(i)
534 if (v['isscript']):
535 # P2WSH multisig without private key are seen after addwitnessaddress
536 [bare, p2sh, p2wsh, p2sh_p2wsh] = self.p2sh_address_to_script(v)
537 solvable_after_addwitnessaddress.extend([p2wsh, p2sh_p2wsh])
538 premature_witaddress.append(script_to_p2sh(p2wsh))
539 else:
540 [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)
541 # P2SH_P2PK, P2SH_P2PKH with compressed keys are seen after addwitnessaddress
542 solvable_after_addwitnessaddress.extend([p2wpkh, p2sh_p2wpkh])
543 premature_witaddress.append(script_to_p2sh(p2wpkh))
545 self.mine_and_test_listunspent(spendable_after_addwitnessaddress + solvable_after_addwitnessaddress + unseen_anytime, 0)
547 # addwitnessaddress should refuse to return a witness address if an uncompressed key is used
548 # note that a multisig address returned by addmultisigaddress is not solvable until it is added with importaddress
549 # premature_witaddress are not accepted until the script is added with addwitnessaddress first
550 for i in uncompressed_spendable_address + uncompressed_solvable_address + premature_witaddress:
551 # This will raise an exception
552 assert_raises_rpc_error(-4, "Public key or redeemscript not known to wallet, or the key is uncompressed", self.nodes[0].addwitnessaddress, i)
554 # after importaddress it should pass addwitnessaddress
555 v = self.nodes[0].validateaddress(compressed_solvable_address[1])
556 self.nodes[0].importaddress(v['hex'],"",False,True)
557 for i in compressed_spendable_address + compressed_solvable_address + premature_witaddress:
558 witaddress = self.nodes[0].addwitnessaddress(i)
559 assert_equal(witaddress, self.nodes[0].addwitnessaddress(witaddress))
561 spendable_txid.append(self.mine_and_test_listunspent(spendable_after_addwitnessaddress, 2))
562 solvable_txid.append(self.mine_and_test_listunspent(solvable_after_addwitnessaddress, 1))
563 self.mine_and_test_listunspent(unseen_anytime, 0)
565 # Check that createrawtransaction/decoderawtransaction with non-v0 Bech32 works
566 v1_addr = program_to_witness(1, [3,5])
567 v1_tx = self.nodes[0].createrawtransaction([getutxo(spendable_txid[0])],{v1_addr: 1})
568 v1_decoded = self.nodes[1].decoderawtransaction(v1_tx)
569 assert_equal(v1_decoded['vout'][0]['scriptPubKey']['addresses'][0], v1_addr)
570 assert_equal(v1_decoded['vout'][0]['scriptPubKey']['hex'], "51020305")
572 # Check that spendable outputs are really spendable
573 self.create_and_mine_tx_from_txids(spendable_txid)
575 # import all the private keys so solvable addresses become spendable
576 self.nodes[0].importprivkey("cPiM8Ub4heR9NBYmgVzJQiUH1if44GSBGiqaeJySuL2BKxubvgwb")
577 self.nodes[0].importprivkey("cPpAdHaD6VoYbW78kveN2bsvb45Q7G5PhaPApVUGwvF8VQ9brD97")
578 self.nodes[0].importprivkey("91zqCU5B9sdWxzMt1ca3VzbtVm2YM6Hi5Rxn4UDtxEaN9C9nzXV")
579 self.nodes[0].importprivkey("cPQFjcVRpAUBG8BA9hzr2yEzHwKoMgLkJZBBtK9vJnvGJgMjzTbd")
580 self.nodes[0].importprivkey("cQGtcm34xiLjB1v7bkRa4V3aAc9tS2UTuBZ1UnZGeSeNy627fN66")
581 self.nodes[0].importprivkey("cTW5mR5M45vHxXkeChZdtSPozrFwFgmEvTNnanCW6wrqwaCZ1X7K")
582 self.create_and_mine_tx_from_txids(solvable_txid)
584 # Test that importing native P2WPKH/P2WSH scripts works
585 for use_p2wsh in [False, True]:
586 if use_p2wsh:
587 scriptPubKey = "00203a59f3f56b713fdcf5d1a57357f02c44342cbf306ffe0c4741046837bf90561a"
588 transaction = "01000000000100e1f505000000002200203a59f3f56b713fdcf5d1a57357f02c44342cbf306ffe0c4741046837bf90561a00000000"
589 else:
590 scriptPubKey = "a9142f8c469c2f0084c48e11f998ffbe7efa7549f26d87"
591 transaction = "01000000000100e1f5050000000017a9142f8c469c2f0084c48e11f998ffbe7efa7549f26d8700000000"
593 self.nodes[1].importaddress(scriptPubKey, "", False)
594 rawtxfund = self.nodes[1].fundrawtransaction(transaction)['hex']
595 rawtxfund = self.nodes[1].signrawtransaction(rawtxfund)["hex"]
596 txid = self.nodes[1].sendrawtransaction(rawtxfund)
598 assert_equal(self.nodes[1].gettransaction(txid, True)["txid"], txid)
599 assert_equal(self.nodes[1].listtransactions("*", 1, 0, True)[0]["txid"], txid)
601 # Assert it is properly saved
602 self.stop_node(1)
603 self.start_node(1)
604 assert_equal(self.nodes[1].gettransaction(txid, True)["txid"], txid)
605 assert_equal(self.nodes[1].listtransactions("*", 1, 0, True)[0]["txid"], txid)
607 def mine_and_test_listunspent(self, script_list, ismine):
608 utxo = find_unspent(self.nodes[0], 50)
609 tx = CTransaction()
610 tx.vin.append(CTxIn(COutPoint(int('0x'+utxo['txid'],0), utxo['vout'])))
611 for i in script_list:
612 tx.vout.append(CTxOut(10000000, i))
613 tx.rehash()
614 signresults = self.nodes[0].signrawtransaction(bytes_to_hex_str(tx.serialize_without_witness()))['hex']
615 txid = self.nodes[0].sendrawtransaction(signresults, True)
616 self.nodes[0].generate(1)
617 sync_blocks(self.nodes)
618 watchcount = 0
619 spendcount = 0
620 for i in self.nodes[0].listunspent():
621 if (i['txid'] == txid):
622 watchcount += 1
623 if (i['spendable'] == True):
624 spendcount += 1
625 if (ismine == 2):
626 assert_equal(spendcount, len(script_list))
627 elif (ismine == 1):
628 assert_equal(watchcount, len(script_list))
629 assert_equal(spendcount, 0)
630 else:
631 assert_equal(watchcount, 0)
632 return txid
634 def p2sh_address_to_script(self,v):
635 bare = CScript(hex_str_to_bytes(v['hex']))
636 p2sh = CScript(hex_str_to_bytes(v['scriptPubKey']))
637 p2wsh = CScript([OP_0, sha256(bare)])
638 p2sh_p2wsh = CScript([OP_HASH160, hash160(p2wsh), OP_EQUAL])
639 return([bare, p2sh, p2wsh, p2sh_p2wsh])
641 def p2pkh_address_to_script(self,v):
642 pubkey = hex_str_to_bytes(v['pubkey'])
643 p2wpkh = CScript([OP_0, hash160(pubkey)])
644 p2sh_p2wpkh = CScript([OP_HASH160, hash160(p2wpkh), OP_EQUAL])
645 p2pk = CScript([pubkey, OP_CHECKSIG])
646 p2pkh = CScript(hex_str_to_bytes(v['scriptPubKey']))
647 p2sh_p2pk = CScript([OP_HASH160, hash160(p2pk), OP_EQUAL])
648 p2sh_p2pkh = CScript([OP_HASH160, hash160(p2pkh), OP_EQUAL])
649 p2wsh_p2pk = CScript([OP_0, sha256(p2pk)])
650 p2wsh_p2pkh = CScript([OP_0, sha256(p2pkh)])
651 p2sh_p2wsh_p2pk = CScript([OP_HASH160, hash160(p2wsh_p2pk), OP_EQUAL])
652 p2sh_p2wsh_p2pkh = CScript([OP_HASH160, hash160(p2wsh_p2pkh), OP_EQUAL])
653 return [p2wpkh, p2sh_p2wpkh, p2pk, p2pkh, p2sh_p2pk, p2sh_p2pkh, p2wsh_p2pk, p2wsh_p2pkh, p2sh_p2wsh_p2pk, p2sh_p2wsh_p2pkh]
655 def create_and_mine_tx_from_txids(self, txids, success = True):
656 tx = CTransaction()
657 for i in txids:
658 txtmp = CTransaction()
659 txraw = self.nodes[0].getrawtransaction(i)
660 f = BytesIO(hex_str_to_bytes(txraw))
661 txtmp.deserialize(f)
662 for j in range(len(txtmp.vout)):
663 tx.vin.append(CTxIn(COutPoint(int('0x'+i,0), j)))
664 tx.vout.append(CTxOut(0, CScript()))
665 tx.rehash()
666 signresults = self.nodes[0].signrawtransaction(bytes_to_hex_str(tx.serialize_without_witness()))['hex']
667 self.nodes[0].sendrawtransaction(signresults, True)
668 self.nodes[0].generate(1)
669 sync_blocks(self.nodes)
672 if __name__ == '__main__':
673 SegWitTest().main()