[CoinControl] Allow non-wallet owned change addresses
[bitcoinplatinum.git] / qa / rpc-tests / importmulti.py
blob5c536f2f4952860259ea72642dd6a3870ad32eb4
1 #!/usr/bin/env python3
2 # Copyright (c) 2014-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.
6 from test_framework.test_framework import BitcoinTestFramework
7 from test_framework.util import *
9 class ImportMultiTest (BitcoinTestFramework):
10 def __init__(self):
11 super().__init__()
12 self.num_nodes = 2
13 self.setup_clean_chain = True
15 def setup_network(self, split=False):
16 self.nodes = start_nodes(2, self.options.tmpdir)
17 self.is_network_split=False
19 def run_test (self):
20 print ("Mining blocks...")
21 self.nodes[0].generate(1)
22 self.nodes[1].generate(1)
24 # keyword definition
25 PRIV_KEY = 'privkey'
26 PUB_KEY = 'pubkey'
27 ADDRESS_KEY = 'address'
28 SCRIPT_KEY = 'script'
31 node0_address1 = self.nodes[0].validateaddress(self.nodes[0].getnewaddress())
32 node0_address2 = self.nodes[0].validateaddress(self.nodes[0].getnewaddress())
33 node0_address3 = self.nodes[0].validateaddress(self.nodes[0].getnewaddress())
35 #Check only one address
36 assert_equal(node0_address1['ismine'], True)
38 #Node 1 sync test
39 assert_equal(self.nodes[1].getblockcount(),1)
41 #Address Test - before import
42 address_info = self.nodes[1].validateaddress(node0_address1['address'])
43 assert_equal(address_info['iswatchonly'], False)
44 assert_equal(address_info['ismine'], False)
47 # RPC importmulti -----------------------------------------------
49 # Bitcoin Address
50 print("Should import an address")
51 address = self.nodes[0].validateaddress(self.nodes[0].getnewaddress())
52 result = self.nodes[1].importmulti([{
53 "scriptPubKey": {
54 "address": address['address']
56 }])
57 assert_equal(result[0]['success'], True)
58 address_assert = self.nodes[1].validateaddress(address['address'])
59 assert_equal(address_assert['iswatchonly'], True)
60 assert_equal(address_assert['ismine'], False)
63 # ScriptPubKey + internal
64 print("Should import a scriptPubKey with internal flag")
65 address = self.nodes[0].validateaddress(self.nodes[0].getnewaddress())
66 result = self.nodes[1].importmulti([{
67 "scriptPubKey": address['scriptPubKey'],
68 "internal": True
69 }])
70 assert_equal(result[0]['success'], True)
71 address_assert = self.nodes[1].validateaddress(address['address'])
72 assert_equal(address_assert['iswatchonly'], True)
73 assert_equal(address_assert['ismine'], False)
75 # ScriptPubKey + !internal
76 print("Should not import a scriptPubKey without internal flag")
77 address = self.nodes[0].validateaddress(self.nodes[0].getnewaddress())
78 result = self.nodes[1].importmulti([{
79 "scriptPubKey": address['scriptPubKey']
80 }])
81 assert_equal(result[0]['success'], False)
82 assert_equal(result[0]['error']['code'], -8)
83 assert_equal(result[0]['error']['message'], 'Internal must be set for hex scriptPubKey')
84 address_assert = self.nodes[1].validateaddress(address['address'])
85 assert_equal(address_assert['iswatchonly'], False)
86 assert_equal(address_assert['ismine'], False)
89 # Address + Public key + !Internal
90 print("Should import an address with public key")
91 address = self.nodes[0].validateaddress(self.nodes[0].getnewaddress())
92 result = self.nodes[1].importmulti([{
93 "scriptPubKey": {
94 "address": address['address']
96 "pubkeys": [ address['pubkey'] ]
97 }])
98 assert_equal(result[0]['success'], True)
99 address_assert = self.nodes[1].validateaddress(address['address'])
100 assert_equal(address_assert['iswatchonly'], True)
101 assert_equal(address_assert['ismine'], False)
104 # ScriptPubKey + Public key + internal
105 print("Should import a scriptPubKey with internal and with public key")
106 address = self.nodes[0].validateaddress(self.nodes[0].getnewaddress())
107 request = [{
108 "scriptPubKey": address['scriptPubKey'],
109 "pubkeys": [ address['pubkey'] ],
110 "internal": True
112 result = self.nodes[1].importmulti(request)
113 assert_equal(result[0]['success'], True)
114 address_assert = self.nodes[1].validateaddress(address['address'])
115 assert_equal(address_assert['iswatchonly'], True)
116 assert_equal(address_assert['ismine'], False)
118 # ScriptPubKey + Public key + !internal
119 print("Should not import a scriptPubKey without internal and with public key")
120 address = self.nodes[0].validateaddress(self.nodes[0].getnewaddress())
121 request = [{
122 "scriptPubKey": address['scriptPubKey'],
123 "pubkeys": [ address['pubkey'] ]
125 result = self.nodes[1].importmulti(request)
126 assert_equal(result[0]['success'], False)
127 assert_equal(result[0]['error']['code'], -8)
128 assert_equal(result[0]['error']['message'], 'Internal must be set for hex scriptPubKey')
129 address_assert = self.nodes[1].validateaddress(address['address'])
130 assert_equal(address_assert['iswatchonly'], False)
131 assert_equal(address_assert['ismine'], False)
133 # Address + Private key + !watchonly
134 print("Should import an address with private key")
135 address = self.nodes[0].validateaddress(self.nodes[0].getnewaddress())
136 result = self.nodes[1].importmulti([{
137 "scriptPubKey": {
138 "address": address['address']
140 "keys": [ self.nodes[0].dumpprivkey(address['address']) ]
142 assert_equal(result[0]['success'], True)
143 address_assert = self.nodes[1].validateaddress(address['address'])
144 assert_equal(address_assert['iswatchonly'], False)
145 assert_equal(address_assert['ismine'], True)
147 # Address + Private key + watchonly
148 print("Should not import an address with private key and with watchonly")
149 address = self.nodes[0].validateaddress(self.nodes[0].getnewaddress())
150 result = self.nodes[1].importmulti([{
151 "scriptPubKey": {
152 "address": address['address']
154 "keys": [ self.nodes[0].dumpprivkey(address['address']) ],
155 "watchonly": True
157 assert_equal(result[0]['success'], False)
158 assert_equal(result[0]['error']['code'], -8)
159 assert_equal(result[0]['error']['message'], 'Incompatibility found between watchonly and keys')
160 address_assert = self.nodes[1].validateaddress(address['address'])
161 assert_equal(address_assert['iswatchonly'], False)
162 assert_equal(address_assert['ismine'], False)
164 # ScriptPubKey + Private key + internal
165 print("Should import a scriptPubKey with internal and with private key")
166 address = self.nodes[0].validateaddress(self.nodes[0].getnewaddress())
167 result = self.nodes[1].importmulti([{
168 "scriptPubKey": address['scriptPubKey'],
169 "keys": [ self.nodes[0].dumpprivkey(address['address']) ],
170 "internal": True
172 assert_equal(result[0]['success'], True)
173 address_assert = self.nodes[1].validateaddress(address['address'])
174 assert_equal(address_assert['iswatchonly'], False)
175 assert_equal(address_assert['ismine'], True)
177 # ScriptPubKey + Private key + !internal
178 print("Should not import a scriptPubKey without internal and with private key")
179 address = self.nodes[0].validateaddress(self.nodes[0].getnewaddress())
180 result = self.nodes[1].importmulti([{
181 "scriptPubKey": address['scriptPubKey'],
182 "keys": [ self.nodes[0].dumpprivkey(address['address']) ]
184 assert_equal(result[0]['success'], False)
185 assert_equal(result[0]['error']['code'], -8)
186 assert_equal(result[0]['error']['message'], 'Internal must be set for hex scriptPubKey')
187 address_assert = self.nodes[1].validateaddress(address['address'])
188 assert_equal(address_assert['iswatchonly'], False)
189 assert_equal(address_assert['ismine'], False)
192 # P2SH address
193 sig_address_1 = self.nodes[0].validateaddress(self.nodes[0].getnewaddress())
194 sig_address_2 = self.nodes[0].validateaddress(self.nodes[0].getnewaddress())
195 sig_address_3 = self.nodes[0].validateaddress(self.nodes[0].getnewaddress())
196 multi_sig_script = self.nodes[0].createmultisig(2, [sig_address_1['address'], sig_address_2['address'], sig_address_3['pubkey']])
197 self.nodes[1].generate(100)
198 transactionid = self.nodes[1].sendtoaddress(multi_sig_script['address'], 10.00)
199 self.nodes[1].generate(1)
200 transaction = self.nodes[1].gettransaction(transactionid);
202 print("Should import a p2sh")
203 result = self.nodes[1].importmulti([{
204 "scriptPubKey": {
205 "address": multi_sig_script['address']
208 assert_equal(result[0]['success'], True)
209 address_assert = self.nodes[1].validateaddress(multi_sig_script['address'])
210 assert_equal(address_assert['isscript'], True)
211 assert_equal(address_assert['iswatchonly'], True)
212 p2shunspent = self.nodes[1].listunspent(0,999999, [multi_sig_script['address']])[0]
213 assert_equal(p2shunspent['spendable'], False)
214 assert_equal(p2shunspent['solvable'], False)
217 # P2SH + Redeem script
218 sig_address_1 = self.nodes[0].validateaddress(self.nodes[0].getnewaddress())
219 sig_address_2 = self.nodes[0].validateaddress(self.nodes[0].getnewaddress())
220 sig_address_3 = self.nodes[0].validateaddress(self.nodes[0].getnewaddress())
221 multi_sig_script = self.nodes[0].createmultisig(2, [sig_address_1['address'], sig_address_2['address'], sig_address_3['pubkey']])
222 self.nodes[1].generate(100)
223 transactionid = self.nodes[1].sendtoaddress(multi_sig_script['address'], 10.00)
224 self.nodes[1].generate(1)
225 transaction = self.nodes[1].gettransaction(transactionid);
227 print("Should import a p2sh with respective redeem script")
228 result = self.nodes[1].importmulti([{
229 "scriptPubKey": {
230 "address": multi_sig_script['address']
232 "redeemscript": multi_sig_script['redeemScript']
234 assert_equal(result[0]['success'], True)
236 p2shunspent = self.nodes[1].listunspent(0,999999, [multi_sig_script['address']])[0]
237 assert_equal(p2shunspent['spendable'], False)
238 assert_equal(p2shunspent['solvable'], True)
241 # P2SH + Redeem script + Private Keys + !Watchonly
242 sig_address_1 = self.nodes[0].validateaddress(self.nodes[0].getnewaddress())
243 sig_address_2 = self.nodes[0].validateaddress(self.nodes[0].getnewaddress())
244 sig_address_3 = self.nodes[0].validateaddress(self.nodes[0].getnewaddress())
245 multi_sig_script = self.nodes[0].createmultisig(2, [sig_address_1['address'], sig_address_2['address'], sig_address_3['pubkey']])
246 self.nodes[1].generate(100)
247 transactionid = self.nodes[1].sendtoaddress(multi_sig_script['address'], 10.00)
248 self.nodes[1].generate(1)
249 transaction = self.nodes[1].gettransaction(transactionid);
251 print("Should import a p2sh with respective redeem script and private keys")
252 result = self.nodes[1].importmulti([{
253 "scriptPubKey": {
254 "address": multi_sig_script['address']
256 "redeemscript": multi_sig_script['redeemScript'],
257 "keys": [ self.nodes[0].dumpprivkey(sig_address_1['address']), self.nodes[0].dumpprivkey(sig_address_2['address'])]
259 assert_equal(result[0]['success'], True)
261 p2shunspent = self.nodes[1].listunspent(0,999999, [multi_sig_script['address']])[0]
262 assert_equal(p2shunspent['spendable'], False)
263 assert_equal(p2shunspent['solvable'], True)
265 # P2SH + Redeem script + Private Keys + Watchonly
266 sig_address_1 = self.nodes[0].validateaddress(self.nodes[0].getnewaddress())
267 sig_address_2 = self.nodes[0].validateaddress(self.nodes[0].getnewaddress())
268 sig_address_3 = self.nodes[0].validateaddress(self.nodes[0].getnewaddress())
269 multi_sig_script = self.nodes[0].createmultisig(2, [sig_address_1['address'], sig_address_2['address'], sig_address_3['pubkey']])
270 self.nodes[1].generate(100)
271 transactionid = self.nodes[1].sendtoaddress(multi_sig_script['address'], 10.00)
272 self.nodes[1].generate(1)
273 transaction = self.nodes[1].gettransaction(transactionid);
275 print("Should import a p2sh with respective redeem script and private keys")
276 result = self.nodes[1].importmulti([{
277 "scriptPubKey": {
278 "address": multi_sig_script['address']
280 "redeemscript": multi_sig_script['redeemScript'],
281 "keys": [ self.nodes[0].dumpprivkey(sig_address_1['address']), self.nodes[0].dumpprivkey(sig_address_2['address'])],
282 "watchonly": True
284 assert_equal(result[0]['success'], False)
285 assert_equal(result[0]['error']['code'], -8)
286 assert_equal(result[0]['error']['message'], 'Incompatibility found between watchonly and keys')
289 # Address + Public key + !Internal + Wrong pubkey
290 print("Should not import an address with a wrong public key")
291 address = self.nodes[0].validateaddress(self.nodes[0].getnewaddress())
292 address2 = self.nodes[0].validateaddress(self.nodes[0].getnewaddress())
293 result = self.nodes[1].importmulti([{
294 "scriptPubKey": {
295 "address": address['address']
297 "pubkeys": [ address2['pubkey'] ]
299 assert_equal(result[0]['success'], False)
300 assert_equal(result[0]['error']['code'], -5)
301 assert_equal(result[0]['error']['message'], 'Consistency check failed')
302 address_assert = self.nodes[1].validateaddress(address['address'])
303 assert_equal(address_assert['iswatchonly'], False)
304 assert_equal(address_assert['ismine'], False)
307 # ScriptPubKey + Public key + internal + Wrong pubkey
308 print("Should not import a scriptPubKey with internal and with a wrong public key")
309 address = self.nodes[0].validateaddress(self.nodes[0].getnewaddress())
310 address2 = self.nodes[0].validateaddress(self.nodes[0].getnewaddress())
311 request = [{
312 "scriptPubKey": address['scriptPubKey'],
313 "pubkeys": [ address2['pubkey'] ],
314 "internal": True
316 result = self.nodes[1].importmulti(request)
317 assert_equal(result[0]['success'], False)
318 assert_equal(result[0]['error']['code'], -5)
319 assert_equal(result[0]['error']['message'], 'Consistency check failed')
320 address_assert = self.nodes[1].validateaddress(address['address'])
321 assert_equal(address_assert['iswatchonly'], False)
322 assert_equal(address_assert['ismine'], False)
325 # Address + Private key + !watchonly + Wrong private key
326 print("Should not import an address with a wrong private key")
327 address = self.nodes[0].validateaddress(self.nodes[0].getnewaddress())
328 address2 = self.nodes[0].validateaddress(self.nodes[0].getnewaddress())
329 result = self.nodes[1].importmulti([{
330 "scriptPubKey": {
331 "address": address['address']
333 "keys": [ self.nodes[0].dumpprivkey(address2['address']) ]
335 assert_equal(result[0]['success'], False)
336 assert_equal(result[0]['error']['code'], -5)
337 assert_equal(result[0]['error']['message'], 'Consistency check failed')
338 address_assert = self.nodes[1].validateaddress(address['address'])
339 assert_equal(address_assert['iswatchonly'], False)
340 assert_equal(address_assert['ismine'], False)
343 # ScriptPubKey + Private key + internal + Wrong private key
344 print("Should not import a scriptPubKey with internal and with a wrong private key")
345 address = self.nodes[0].validateaddress(self.nodes[0].getnewaddress())
346 address2 = self.nodes[0].validateaddress(self.nodes[0].getnewaddress())
347 result = self.nodes[1].importmulti([{
348 "scriptPubKey": address['scriptPubKey'],
349 "keys": [ self.nodes[0].dumpprivkey(address2['address']) ],
350 "internal": True
352 assert_equal(result[0]['success'], False)
353 assert_equal(result[0]['error']['code'], -5)
354 assert_equal(result[0]['error']['message'], 'Consistency check failed')
355 address_assert = self.nodes[1].validateaddress(address['address'])
356 assert_equal(address_assert['iswatchonly'], False)
357 assert_equal(address_assert['ismine'], False)
359 if __name__ == '__main__':
360 ImportMultiTest ().main ()