Merge #11603: Move RPC registration out of AppInitParameterInteraction
[bitcoinplatinum.git] / src / test / script_standard_tests.cpp
blob19060eccc939572e4bd8794ca1254efc04455007
1 // Copyright (c) 2017 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 #include <key.h>
6 #include <keystore.h>
7 #include <script/ismine.h>
8 #include <script/script.h>
9 #include <script/script_error.h>
10 #include <script/standard.h>
11 #include <test/test_bitcoin.h>
13 #include <boost/test/unit_test.hpp>
16 BOOST_FIXTURE_TEST_SUITE(script_standard_tests, BasicTestingSetup)
18 BOOST_AUTO_TEST_CASE(script_standard_Solver_success)
20 CKey keys[3];
21 CPubKey pubkeys[3];
22 for (int i = 0; i < 3; i++) {
23 keys[i].MakeNewKey(true);
24 pubkeys[i] = keys[i].GetPubKey();
27 CScript s;
28 txnouttype whichType;
29 std::vector<std::vector<unsigned char> > solutions;
31 // TX_PUBKEY
32 s.clear();
33 s << ToByteVector(pubkeys[0]) << OP_CHECKSIG;
34 BOOST_CHECK(Solver(s, whichType, solutions));
35 BOOST_CHECK_EQUAL(whichType, TX_PUBKEY);
36 BOOST_CHECK_EQUAL(solutions.size(), 1);
37 BOOST_CHECK(solutions[0] == ToByteVector(pubkeys[0]));
39 // TX_PUBKEYHASH
40 s.clear();
41 s << OP_DUP << OP_HASH160 << ToByteVector(pubkeys[0].GetID()) << OP_EQUALVERIFY << OP_CHECKSIG;
42 BOOST_CHECK(Solver(s, whichType, solutions));
43 BOOST_CHECK_EQUAL(whichType, TX_PUBKEYHASH);
44 BOOST_CHECK_EQUAL(solutions.size(), 1);
45 BOOST_CHECK(solutions[0] == ToByteVector(pubkeys[0].GetID()));
47 // TX_SCRIPTHASH
48 CScript redeemScript(s); // initialize with leftover P2PKH script
49 s.clear();
50 s << OP_HASH160 << ToByteVector(CScriptID(redeemScript)) << OP_EQUAL;
51 BOOST_CHECK(Solver(s, whichType, solutions));
52 BOOST_CHECK_EQUAL(whichType, TX_SCRIPTHASH);
53 BOOST_CHECK_EQUAL(solutions.size(), 1);
54 BOOST_CHECK(solutions[0] == ToByteVector(CScriptID(redeemScript)));
56 // TX_MULTISIG
57 s.clear();
58 s << OP_1 <<
59 ToByteVector(pubkeys[0]) <<
60 ToByteVector(pubkeys[1]) <<
61 OP_2 << OP_CHECKMULTISIG;
62 BOOST_CHECK(Solver(s, whichType, solutions));
63 BOOST_CHECK_EQUAL(whichType, TX_MULTISIG);
64 BOOST_CHECK_EQUAL(solutions.size(), 4);
65 BOOST_CHECK(solutions[0] == std::vector<unsigned char>({1}));
66 BOOST_CHECK(solutions[1] == ToByteVector(pubkeys[0]));
67 BOOST_CHECK(solutions[2] == ToByteVector(pubkeys[1]));
68 BOOST_CHECK(solutions[3] == std::vector<unsigned char>({2}));
70 s.clear();
71 s << OP_2 <<
72 ToByteVector(pubkeys[0]) <<
73 ToByteVector(pubkeys[1]) <<
74 ToByteVector(pubkeys[2]) <<
75 OP_3 << OP_CHECKMULTISIG;
76 BOOST_CHECK(Solver(s, whichType, solutions));
77 BOOST_CHECK_EQUAL(whichType, TX_MULTISIG);
78 BOOST_CHECK_EQUAL(solutions.size(), 5);
79 BOOST_CHECK(solutions[0] == std::vector<unsigned char>({2}));
80 BOOST_CHECK(solutions[1] == ToByteVector(pubkeys[0]));
81 BOOST_CHECK(solutions[2] == ToByteVector(pubkeys[1]));
82 BOOST_CHECK(solutions[3] == ToByteVector(pubkeys[2]));
83 BOOST_CHECK(solutions[4] == std::vector<unsigned char>({3}));
85 // TX_NULL_DATA
86 s.clear();
87 s << OP_RETURN <<
88 std::vector<unsigned char>({0}) <<
89 std::vector<unsigned char>({75}) <<
90 std::vector<unsigned char>({255});
91 BOOST_CHECK(Solver(s, whichType, solutions));
92 BOOST_CHECK_EQUAL(whichType, TX_NULL_DATA);
93 BOOST_CHECK_EQUAL(solutions.size(), 0);
95 // TX_WITNESS_V0_KEYHASH
96 s.clear();
97 s << OP_0 << ToByteVector(pubkeys[0].GetID());
98 BOOST_CHECK(Solver(s, whichType, solutions));
99 BOOST_CHECK_EQUAL(whichType, TX_WITNESS_V0_KEYHASH);
100 BOOST_CHECK_EQUAL(solutions.size(), 1);
101 BOOST_CHECK(solutions[0] == ToByteVector(pubkeys[0].GetID()));
103 // TX_WITNESS_V0_SCRIPTHASH
104 uint256 scriptHash;
105 CSHA256().Write(&redeemScript[0], redeemScript.size())
106 .Finalize(scriptHash.begin());
108 s.clear();
109 s << OP_0 << ToByteVector(scriptHash);
110 BOOST_CHECK(Solver(s, whichType, solutions));
111 BOOST_CHECK_EQUAL(whichType, TX_WITNESS_V0_SCRIPTHASH);
112 BOOST_CHECK_EQUAL(solutions.size(), 1);
113 BOOST_CHECK(solutions[0] == ToByteVector(scriptHash));
115 // TX_NONSTANDARD
116 s.clear();
117 s << OP_9 << OP_ADD << OP_11 << OP_EQUAL;
118 BOOST_CHECK(!Solver(s, whichType, solutions));
119 BOOST_CHECK_EQUAL(whichType, TX_NONSTANDARD);
122 BOOST_AUTO_TEST_CASE(script_standard_Solver_failure)
124 CKey key;
125 CPubKey pubkey;
126 key.MakeNewKey(true);
127 pubkey = key.GetPubKey();
129 CScript s;
130 txnouttype whichType;
131 std::vector<std::vector<unsigned char> > solutions;
133 // TX_PUBKEY with incorrectly sized pubkey
134 s.clear();
135 s << std::vector<unsigned char>(30, 0x01) << OP_CHECKSIG;
136 BOOST_CHECK(!Solver(s, whichType, solutions));
138 // TX_PUBKEYHASH with incorrectly sized key hash
139 s.clear();
140 s << OP_DUP << OP_HASH160 << ToByteVector(pubkey) << OP_EQUALVERIFY << OP_CHECKSIG;
141 BOOST_CHECK(!Solver(s, whichType, solutions));
143 // TX_SCRIPTHASH with incorrectly sized script hash
144 s.clear();
145 s << OP_HASH160 << std::vector<unsigned char>(21, 0x01) << OP_EQUAL;
146 BOOST_CHECK(!Solver(s, whichType, solutions));
148 // TX_MULTISIG 0/2
149 s.clear();
150 s << OP_0 << ToByteVector(pubkey) << OP_1 << OP_CHECKMULTISIG;
151 BOOST_CHECK(!Solver(s, whichType, solutions));
153 // TX_MULTISIG 2/1
154 s.clear();
155 s << OP_2 << ToByteVector(pubkey) << OP_1 << OP_CHECKMULTISIG;
156 BOOST_CHECK(!Solver(s, whichType, solutions));
158 // TX_MULTISIG n = 2 with 1 pubkey
159 s.clear();
160 s << OP_1 << ToByteVector(pubkey) << OP_2 << OP_CHECKMULTISIG;
161 BOOST_CHECK(!Solver(s, whichType, solutions));
163 // TX_MULTISIG n = 1 with 0 pubkeys
164 s.clear();
165 s << OP_1 << OP_1 << OP_CHECKMULTISIG;
166 BOOST_CHECK(!Solver(s, whichType, solutions));
168 // TX_NULL_DATA with other opcodes
169 s.clear();
170 s << OP_RETURN << std::vector<unsigned char>({75}) << OP_ADD;
171 BOOST_CHECK(!Solver(s, whichType, solutions));
173 // TX_WITNESS with incorrect program size
174 s.clear();
175 s << OP_0 << std::vector<unsigned char>(19, 0x01);
176 BOOST_CHECK(!Solver(s, whichType, solutions));
179 BOOST_AUTO_TEST_CASE(script_standard_ExtractDestination)
181 CKey key;
182 CPubKey pubkey;
183 key.MakeNewKey(true);
184 pubkey = key.GetPubKey();
186 CScript s;
187 CTxDestination address;
189 // TX_PUBKEY
190 s.clear();
191 s << ToByteVector(pubkey) << OP_CHECKSIG;
192 BOOST_CHECK(ExtractDestination(s, address));
193 BOOST_CHECK(boost::get<CKeyID>(&address) &&
194 *boost::get<CKeyID>(&address) == pubkey.GetID());
196 // TX_PUBKEYHASH
197 s.clear();
198 s << OP_DUP << OP_HASH160 << ToByteVector(pubkey.GetID()) << OP_EQUALVERIFY << OP_CHECKSIG;
199 BOOST_CHECK(ExtractDestination(s, address));
200 BOOST_CHECK(boost::get<CKeyID>(&address) &&
201 *boost::get<CKeyID>(&address) == pubkey.GetID());
203 // TX_SCRIPTHASH
204 CScript redeemScript(s); // initialize with leftover P2PKH script
205 s.clear();
206 s << OP_HASH160 << ToByteVector(CScriptID(redeemScript)) << OP_EQUAL;
207 BOOST_CHECK(ExtractDestination(s, address));
208 BOOST_CHECK(boost::get<CScriptID>(&address) &&
209 *boost::get<CScriptID>(&address) == CScriptID(redeemScript));
211 // TX_MULTISIG
212 s.clear();
213 s << OP_1 << ToByteVector(pubkey) << OP_1 << OP_CHECKMULTISIG;
214 BOOST_CHECK(!ExtractDestination(s, address));
216 // TX_NULL_DATA
217 s.clear();
218 s << OP_RETURN << std::vector<unsigned char>({75});
219 BOOST_CHECK(!ExtractDestination(s, address));
221 // TX_WITNESS_V0_KEYHASH
222 s.clear();
223 s << OP_0 << ToByteVector(pubkey.GetID());
224 BOOST_CHECK(ExtractDestination(s, address));
225 WitnessV0KeyHash keyhash;
226 CHash160().Write(pubkey.begin(), pubkey.size()).Finalize(keyhash.begin());
227 BOOST_CHECK(boost::get<WitnessV0KeyHash>(&address) && *boost::get<WitnessV0KeyHash>(&address) == keyhash);
229 // TX_WITNESS_V0_SCRIPTHASH
230 s.clear();
231 WitnessV0ScriptHash scripthash;
232 CSHA256().Write(redeemScript.data(), redeemScript.size()).Finalize(scripthash.begin());
233 s << OP_0 << ToByteVector(scripthash);
234 BOOST_CHECK(ExtractDestination(s, address));
235 BOOST_CHECK(boost::get<WitnessV0ScriptHash>(&address) && *boost::get<WitnessV0ScriptHash>(&address) == scripthash);
237 // TX_WITNESS with unknown version
238 s.clear();
239 s << OP_1 << ToByteVector(pubkey);
240 BOOST_CHECK(ExtractDestination(s, address));
241 WitnessUnknown unk;
242 unk.length = 33;
243 unk.version = 1;
244 std::copy(pubkey.begin(), pubkey.end(), unk.program);
245 BOOST_CHECK(boost::get<WitnessUnknown>(&address) && *boost::get<WitnessUnknown>(&address) == unk);
248 BOOST_AUTO_TEST_CASE(script_standard_ExtractDestinations)
250 CKey keys[3];
251 CPubKey pubkeys[3];
252 for (int i = 0; i < 3; i++) {
253 keys[i].MakeNewKey(true);
254 pubkeys[i] = keys[i].GetPubKey();
257 CScript s;
258 txnouttype whichType;
259 std::vector<CTxDestination> addresses;
260 int nRequired;
262 // TX_PUBKEY
263 s.clear();
264 s << ToByteVector(pubkeys[0]) << OP_CHECKSIG;
265 BOOST_CHECK(ExtractDestinations(s, whichType, addresses, nRequired));
266 BOOST_CHECK_EQUAL(whichType, TX_PUBKEY);
267 BOOST_CHECK_EQUAL(addresses.size(), 1);
268 BOOST_CHECK_EQUAL(nRequired, 1);
269 BOOST_CHECK(boost::get<CKeyID>(&addresses[0]) &&
270 *boost::get<CKeyID>(&addresses[0]) == pubkeys[0].GetID());
272 // TX_PUBKEYHASH
273 s.clear();
274 s << OP_DUP << OP_HASH160 << ToByteVector(pubkeys[0].GetID()) << OP_EQUALVERIFY << OP_CHECKSIG;
275 BOOST_CHECK(ExtractDestinations(s, whichType, addresses, nRequired));
276 BOOST_CHECK_EQUAL(whichType, TX_PUBKEYHASH);
277 BOOST_CHECK_EQUAL(addresses.size(), 1);
278 BOOST_CHECK_EQUAL(nRequired, 1);
279 BOOST_CHECK(boost::get<CKeyID>(&addresses[0]) &&
280 *boost::get<CKeyID>(&addresses[0]) == pubkeys[0].GetID());
282 // TX_SCRIPTHASH
283 CScript redeemScript(s); // initialize with leftover P2PKH script
284 s.clear();
285 s << OP_HASH160 << ToByteVector(CScriptID(redeemScript)) << OP_EQUAL;
286 BOOST_CHECK(ExtractDestinations(s, whichType, addresses, nRequired));
287 BOOST_CHECK_EQUAL(whichType, TX_SCRIPTHASH);
288 BOOST_CHECK_EQUAL(addresses.size(), 1);
289 BOOST_CHECK_EQUAL(nRequired, 1);
290 BOOST_CHECK(boost::get<CScriptID>(&addresses[0]) &&
291 *boost::get<CScriptID>(&addresses[0]) == CScriptID(redeemScript));
293 // TX_MULTISIG
294 s.clear();
295 s << OP_2 <<
296 ToByteVector(pubkeys[0]) <<
297 ToByteVector(pubkeys[1]) <<
298 OP_2 << OP_CHECKMULTISIG;
299 BOOST_CHECK(ExtractDestinations(s, whichType, addresses, nRequired));
300 BOOST_CHECK_EQUAL(whichType, TX_MULTISIG);
301 BOOST_CHECK_EQUAL(addresses.size(), 2);
302 BOOST_CHECK_EQUAL(nRequired, 2);
303 BOOST_CHECK(boost::get<CKeyID>(&addresses[0]) &&
304 *boost::get<CKeyID>(&addresses[0]) == pubkeys[0].GetID());
305 BOOST_CHECK(boost::get<CKeyID>(&addresses[1]) &&
306 *boost::get<CKeyID>(&addresses[1]) == pubkeys[1].GetID());
308 // TX_NULL_DATA
309 s.clear();
310 s << OP_RETURN << std::vector<unsigned char>({75});
311 BOOST_CHECK(!ExtractDestinations(s, whichType, addresses, nRequired));
314 BOOST_AUTO_TEST_CASE(script_standard_GetScriptFor_)
316 CKey keys[3];
317 CPubKey pubkeys[3];
318 for (int i = 0; i < 3; i++) {
319 keys[i].MakeNewKey(true);
320 pubkeys[i] = keys[i].GetPubKey();
323 CScript expected, result;
325 // CKeyID
326 expected.clear();
327 expected << OP_DUP << OP_HASH160 << ToByteVector(pubkeys[0].GetID()) << OP_EQUALVERIFY << OP_CHECKSIG;
328 result = GetScriptForDestination(pubkeys[0].GetID());
329 BOOST_CHECK(result == expected);
331 // CScriptID
332 CScript redeemScript(result);
333 expected.clear();
334 expected << OP_HASH160 << ToByteVector(CScriptID(redeemScript)) << OP_EQUAL;
335 result = GetScriptForDestination(CScriptID(redeemScript));
336 BOOST_CHECK(result == expected);
338 // CNoDestination
339 expected.clear();
340 result = GetScriptForDestination(CNoDestination());
341 BOOST_CHECK(result == expected);
343 // GetScriptForRawPubKey
344 expected.clear();
345 expected << ToByteVector(pubkeys[0]) << OP_CHECKSIG;
346 result = GetScriptForRawPubKey(pubkeys[0]);
347 BOOST_CHECK(result == expected);
349 // GetScriptForMultisig
350 expected.clear();
351 expected << OP_2 <<
352 ToByteVector(pubkeys[0]) <<
353 ToByteVector(pubkeys[1]) <<
354 ToByteVector(pubkeys[2]) <<
355 OP_3 << OP_CHECKMULTISIG;
356 result = GetScriptForMultisig(2, std::vector<CPubKey>(pubkeys, pubkeys + 3));
357 BOOST_CHECK(result == expected);
359 // GetScriptForWitness
360 CScript witnessScript;
362 witnessScript << ToByteVector(pubkeys[0]) << OP_CHECKSIG;
363 expected.clear();
364 expected << OP_0 << ToByteVector(pubkeys[0].GetID());
365 result = GetScriptForWitness(witnessScript);
366 BOOST_CHECK(result == expected);
368 witnessScript.clear();
369 witnessScript << OP_DUP << OP_HASH160 << ToByteVector(pubkeys[0].GetID()) << OP_EQUALVERIFY << OP_CHECKSIG;
370 result = GetScriptForWitness(witnessScript);
371 BOOST_CHECK(result == expected);
373 witnessScript.clear();
374 witnessScript << OP_1 << ToByteVector(pubkeys[0]) << OP_1 << OP_CHECKMULTISIG;
376 uint256 scriptHash;
377 CSHA256().Write(&witnessScript[0], witnessScript.size())
378 .Finalize(scriptHash.begin());
380 expected.clear();
381 expected << OP_0 << ToByteVector(scriptHash);
382 result = GetScriptForWitness(witnessScript);
383 BOOST_CHECK(result == expected);
386 BOOST_AUTO_TEST_CASE(script_standard_IsMine)
388 CKey keys[2];
389 CPubKey pubkeys[2];
390 for (int i = 0; i < 2; i++) {
391 keys[i].MakeNewKey(true);
392 pubkeys[i] = keys[i].GetPubKey();
395 CKey uncompressedKey;
396 uncompressedKey.MakeNewKey(false);
397 CPubKey uncompressedPubkey = uncompressedKey.GetPubKey();
399 CScript scriptPubKey;
400 isminetype result;
401 bool isInvalid;
403 // P2PK compressed
405 CBasicKeyStore keystore;
406 scriptPubKey.clear();
407 scriptPubKey << ToByteVector(pubkeys[0]) << OP_CHECKSIG;
409 // Keystore does not have key
410 result = IsMine(keystore, scriptPubKey, isInvalid);
411 BOOST_CHECK_EQUAL(result, ISMINE_NO);
412 BOOST_CHECK(!isInvalid);
414 // Keystore has key
415 keystore.AddKey(keys[0]);
416 result = IsMine(keystore, scriptPubKey, isInvalid);
417 BOOST_CHECK_EQUAL(result, ISMINE_SPENDABLE);
418 BOOST_CHECK(!isInvalid);
421 // P2PK uncompressed
423 CBasicKeyStore keystore;
424 scriptPubKey.clear();
425 scriptPubKey << ToByteVector(uncompressedPubkey) << OP_CHECKSIG;
427 // Keystore does not have key
428 result = IsMine(keystore, scriptPubKey, isInvalid);
429 BOOST_CHECK_EQUAL(result, ISMINE_NO);
430 BOOST_CHECK(!isInvalid);
432 // Keystore has key
433 keystore.AddKey(uncompressedKey);
434 result = IsMine(keystore, scriptPubKey, isInvalid);
435 BOOST_CHECK_EQUAL(result, ISMINE_SPENDABLE);
436 BOOST_CHECK(!isInvalid);
439 // P2PKH compressed
441 CBasicKeyStore keystore;
442 scriptPubKey.clear();
443 scriptPubKey << OP_DUP << OP_HASH160 << ToByteVector(pubkeys[0].GetID()) << OP_EQUALVERIFY << OP_CHECKSIG;
445 // Keystore does not have key
446 result = IsMine(keystore, scriptPubKey, isInvalid);
447 BOOST_CHECK_EQUAL(result, ISMINE_NO);
448 BOOST_CHECK(!isInvalid);
450 // Keystore has key
451 keystore.AddKey(keys[0]);
452 result = IsMine(keystore, scriptPubKey, isInvalid);
453 BOOST_CHECK_EQUAL(result, ISMINE_SPENDABLE);
454 BOOST_CHECK(!isInvalid);
457 // P2PKH uncompressed
459 CBasicKeyStore keystore;
460 scriptPubKey.clear();
461 scriptPubKey << OP_DUP << OP_HASH160 << ToByteVector(uncompressedPubkey.GetID()) << OP_EQUALVERIFY << OP_CHECKSIG;
463 // Keystore does not have key
464 result = IsMine(keystore, scriptPubKey, isInvalid);
465 BOOST_CHECK_EQUAL(result, ISMINE_NO);
466 BOOST_CHECK(!isInvalid);
468 // Keystore has key
469 keystore.AddKey(uncompressedKey);
470 result = IsMine(keystore, scriptPubKey, isInvalid);
471 BOOST_CHECK_EQUAL(result, ISMINE_SPENDABLE);
472 BOOST_CHECK(!isInvalid);
475 // P2SH
477 CBasicKeyStore keystore;
479 CScript redeemScript;
480 redeemScript << OP_DUP << OP_HASH160 << ToByteVector(pubkeys[0].GetID()) << OP_EQUALVERIFY << OP_CHECKSIG;
482 scriptPubKey.clear();
483 scriptPubKey << OP_HASH160 << ToByteVector(CScriptID(redeemScript)) << OP_EQUAL;
485 // Keystore does not have redeemScript or key
486 result = IsMine(keystore, scriptPubKey, isInvalid);
487 BOOST_CHECK_EQUAL(result, ISMINE_NO);
488 BOOST_CHECK(!isInvalid);
490 // Keystore has redeemScript but no key
491 keystore.AddCScript(redeemScript);
492 result = IsMine(keystore, scriptPubKey, isInvalid);
493 BOOST_CHECK_EQUAL(result, ISMINE_NO);
494 BOOST_CHECK(!isInvalid);
496 // Keystore has redeemScript and key
497 keystore.AddKey(keys[0]);
498 result = IsMine(keystore, scriptPubKey, isInvalid);
499 BOOST_CHECK_EQUAL(result, ISMINE_SPENDABLE);
500 BOOST_CHECK(!isInvalid);
503 // P2WPKH compressed
505 CBasicKeyStore keystore;
506 keystore.AddKey(keys[0]);
508 scriptPubKey.clear();
509 scriptPubKey << OP_0 << ToByteVector(pubkeys[0].GetID());
511 // Keystore has key, but no P2SH redeemScript
512 result = IsMine(keystore, scriptPubKey, isInvalid);
513 BOOST_CHECK_EQUAL(result, ISMINE_NO);
514 BOOST_CHECK(!isInvalid);
516 // Keystore has key and P2SH redeemScript
517 keystore.AddCScript(scriptPubKey);
518 result = IsMine(keystore, scriptPubKey, isInvalid);
519 BOOST_CHECK_EQUAL(result, ISMINE_SPENDABLE);
520 BOOST_CHECK(!isInvalid);
523 // P2WPKH uncompressed
525 CBasicKeyStore keystore;
526 keystore.AddKey(uncompressedKey);
528 scriptPubKey.clear();
529 scriptPubKey << OP_0 << ToByteVector(uncompressedPubkey.GetID());
531 // Keystore has key, but no P2SH redeemScript
532 result = IsMine(keystore, scriptPubKey, isInvalid);
533 BOOST_CHECK_EQUAL(result, ISMINE_NO);
534 BOOST_CHECK(!isInvalid);
536 // Keystore has key and P2SH redeemScript
537 keystore.AddCScript(scriptPubKey);
538 result = IsMine(keystore, scriptPubKey, isInvalid);
539 BOOST_CHECK_EQUAL(result, ISMINE_NO);
540 BOOST_CHECK(isInvalid);
543 // scriptPubKey multisig
545 CBasicKeyStore keystore;
547 scriptPubKey.clear();
548 scriptPubKey << OP_2 <<
549 ToByteVector(uncompressedPubkey) <<
550 ToByteVector(pubkeys[1]) <<
551 OP_2 << OP_CHECKMULTISIG;
553 // Keystore does not have any keys
554 result = IsMine(keystore, scriptPubKey, isInvalid);
555 BOOST_CHECK_EQUAL(result, ISMINE_NO);
556 BOOST_CHECK(!isInvalid);
558 // Keystore has 1/2 keys
559 keystore.AddKey(uncompressedKey);
561 result = IsMine(keystore, scriptPubKey, isInvalid);
562 BOOST_CHECK_EQUAL(result, ISMINE_NO);
563 BOOST_CHECK(!isInvalid);
565 // Keystore has 2/2 keys
566 keystore.AddKey(keys[1]);
568 result = IsMine(keystore, scriptPubKey, isInvalid);
569 BOOST_CHECK_EQUAL(result, ISMINE_SPENDABLE);
570 BOOST_CHECK(!isInvalid);
573 // P2SH multisig
575 CBasicKeyStore keystore;
576 keystore.AddKey(uncompressedKey);
577 keystore.AddKey(keys[1]);
579 CScript redeemScript;
580 redeemScript << OP_2 <<
581 ToByteVector(uncompressedPubkey) <<
582 ToByteVector(pubkeys[1]) <<
583 OP_2 << OP_CHECKMULTISIG;
585 scriptPubKey.clear();
586 scriptPubKey << OP_HASH160 << ToByteVector(CScriptID(redeemScript)) << OP_EQUAL;
588 // Keystore has no redeemScript
589 result = IsMine(keystore, scriptPubKey, isInvalid);
590 BOOST_CHECK_EQUAL(result, ISMINE_NO);
591 BOOST_CHECK(!isInvalid);
593 // Keystore has redeemScript
594 keystore.AddCScript(redeemScript);
595 result = IsMine(keystore, scriptPubKey, isInvalid);
596 BOOST_CHECK_EQUAL(result, ISMINE_SPENDABLE);
597 BOOST_CHECK(!isInvalid);
600 // P2WSH multisig with compressed keys
602 CBasicKeyStore keystore;
603 keystore.AddKey(keys[0]);
604 keystore.AddKey(keys[1]);
606 CScript witnessScript;
607 witnessScript << OP_2 <<
608 ToByteVector(pubkeys[0]) <<
609 ToByteVector(pubkeys[1]) <<
610 OP_2 << OP_CHECKMULTISIG;
612 uint256 scriptHash;
613 CSHA256().Write(&witnessScript[0], witnessScript.size())
614 .Finalize(scriptHash.begin());
616 scriptPubKey.clear();
617 scriptPubKey << OP_0 << ToByteVector(scriptHash);
619 // Keystore has keys, but no witnessScript or P2SH redeemScript
620 result = IsMine(keystore, scriptPubKey, isInvalid);
621 BOOST_CHECK_EQUAL(result, ISMINE_NO);
622 BOOST_CHECK(!isInvalid);
624 // Keystore has keys and witnessScript, but no P2SH redeemScript
625 keystore.AddCScript(witnessScript);
626 result = IsMine(keystore, scriptPubKey, isInvalid);
627 BOOST_CHECK_EQUAL(result, ISMINE_NO);
628 BOOST_CHECK(!isInvalid);
630 // Keystore has keys, witnessScript, P2SH redeemScript
631 keystore.AddCScript(scriptPubKey);
632 result = IsMine(keystore, scriptPubKey, isInvalid);
633 BOOST_CHECK_EQUAL(result, ISMINE_SPENDABLE);
634 BOOST_CHECK(!isInvalid);
637 // P2WSH multisig with uncompressed key
639 CBasicKeyStore keystore;
640 keystore.AddKey(uncompressedKey);
641 keystore.AddKey(keys[1]);
643 CScript witnessScript;
644 witnessScript << OP_2 <<
645 ToByteVector(uncompressedPubkey) <<
646 ToByteVector(pubkeys[1]) <<
647 OP_2 << OP_CHECKMULTISIG;
649 uint256 scriptHash;
650 CSHA256().Write(&witnessScript[0], witnessScript.size())
651 .Finalize(scriptHash.begin());
653 scriptPubKey.clear();
654 scriptPubKey << OP_0 << ToByteVector(scriptHash);
656 // Keystore has keys, but no witnessScript or P2SH redeemScript
657 result = IsMine(keystore, scriptPubKey, isInvalid);
658 BOOST_CHECK_EQUAL(result, ISMINE_NO);
659 BOOST_CHECK(!isInvalid);
661 // Keystore has keys and witnessScript, but no P2SH redeemScript
662 keystore.AddCScript(witnessScript);
663 result = IsMine(keystore, scriptPubKey, isInvalid);
664 BOOST_CHECK_EQUAL(result, ISMINE_NO);
665 BOOST_CHECK(!isInvalid);
667 // Keystore has keys, witnessScript, P2SH redeemScript
668 keystore.AddCScript(scriptPubKey);
669 result = IsMine(keystore, scriptPubKey, isInvalid);
670 BOOST_CHECK_EQUAL(result, ISMINE_NO);
671 BOOST_CHECK(isInvalid);
674 // P2WSH multisig wrapped in P2SH
676 CBasicKeyStore keystore;
678 CScript witnessScript;
679 witnessScript << OP_2 <<
680 ToByteVector(pubkeys[0]) <<
681 ToByteVector(pubkeys[1]) <<
682 OP_2 << OP_CHECKMULTISIG;
684 uint256 scriptHash;
685 CSHA256().Write(&witnessScript[0], witnessScript.size())
686 .Finalize(scriptHash.begin());
688 CScript redeemScript;
689 redeemScript << OP_0 << ToByteVector(scriptHash);
691 scriptPubKey.clear();
692 scriptPubKey << OP_HASH160 << ToByteVector(CScriptID(redeemScript)) << OP_EQUAL;
694 // Keystore has no witnessScript, P2SH redeemScript, or keys
695 result = IsMine(keystore, scriptPubKey, isInvalid);
696 BOOST_CHECK_EQUAL(result, ISMINE_NO);
697 BOOST_CHECK(!isInvalid);
699 // Keystore has witnessScript and P2SH redeemScript, but no keys
700 keystore.AddCScript(redeemScript);
701 keystore.AddCScript(witnessScript);
702 result = IsMine(keystore, scriptPubKey, isInvalid);
703 BOOST_CHECK_EQUAL(result, ISMINE_NO);
704 BOOST_CHECK(!isInvalid);
706 // Keystore has keys, witnessScript, P2SH redeemScript
707 keystore.AddKey(keys[0]);
708 keystore.AddKey(keys[1]);
709 result = IsMine(keystore, scriptPubKey, isInvalid);
710 BOOST_CHECK_EQUAL(result, ISMINE_SPENDABLE);
711 BOOST_CHECK(!isInvalid);
714 // OP_RETURN
716 CBasicKeyStore keystore;
717 keystore.AddKey(keys[0]);
719 scriptPubKey.clear();
720 scriptPubKey << OP_RETURN << ToByteVector(pubkeys[0]);
722 result = IsMine(keystore, scriptPubKey, isInvalid);
723 BOOST_CHECK_EQUAL(result, ISMINE_NO);
724 BOOST_CHECK(!isInvalid);
727 // Nonstandard
729 CBasicKeyStore keystore;
730 keystore.AddKey(keys[0]);
732 scriptPubKey.clear();
733 scriptPubKey << OP_9 << OP_ADD << OP_11 << OP_EQUAL;
735 result = IsMine(keystore, scriptPubKey, isInvalid);
736 BOOST_CHECK_EQUAL(result, ISMINE_NO);
737 BOOST_CHECK(!isInvalid);
741 BOOST_AUTO_TEST_SUITE_END()