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.
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
)
22 for (int i
= 0; i
< 3; i
++) {
23 keys
[i
].MakeNewKey(true);
24 pubkeys
[i
] = keys
[i
].GetPubKey();
29 std::vector
<std::vector
<unsigned char> > solutions
;
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]));
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()));
48 CScript
redeemScript(s
); // initialize with leftover P2PKH script
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
)));
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}));
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}));
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
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
105 CSHA256().Write(&redeemScript
[0], redeemScript
.size())
106 .Finalize(scriptHash
.begin());
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
));
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
)
126 key
.MakeNewKey(true);
127 pubkey
= key
.GetPubKey();
130 txnouttype whichType
;
131 std::vector
<std::vector
<unsigned char> > solutions
;
133 // TX_PUBKEY with incorrectly sized pubkey
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
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
145 s
<< OP_HASH160
<< std::vector
<unsigned char>(21, 0x01) << OP_EQUAL
;
146 BOOST_CHECK(!Solver(s
, whichType
, solutions
));
150 s
<< OP_0
<< ToByteVector(pubkey
) << OP_1
<< OP_CHECKMULTISIG
;
151 BOOST_CHECK(!Solver(s
, whichType
, solutions
));
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
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
165 s
<< OP_1
<< OP_1
<< OP_CHECKMULTISIG
;
166 BOOST_CHECK(!Solver(s
, whichType
, solutions
));
168 // TX_NULL_DATA with other opcodes
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
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
)
183 key
.MakeNewKey(true);
184 pubkey
= key
.GetPubKey();
187 CTxDestination address
;
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());
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());
204 CScript
redeemScript(s
); // initialize with leftover P2PKH script
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
));
213 s
<< OP_1
<< ToByteVector(pubkey
) << OP_1
<< OP_CHECKMULTISIG
;
214 BOOST_CHECK(!ExtractDestination(s
, address
));
218 s
<< OP_RETURN
<< std::vector
<unsigned char>({75});
219 BOOST_CHECK(!ExtractDestination(s
, address
));
221 // TX_WITNESS_V0_KEYHASH
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
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
239 s
<< OP_1
<< ToByteVector(pubkey
);
240 BOOST_CHECK(ExtractDestination(s
, address
));
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
)
252 for (int i
= 0; i
< 3; i
++) {
253 keys
[i
].MakeNewKey(true);
254 pubkeys
[i
] = keys
[i
].GetPubKey();
258 txnouttype whichType
;
259 std::vector
<CTxDestination
> addresses
;
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());
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());
283 CScript
redeemScript(s
); // initialize with leftover P2PKH script
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
));
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());
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_
)
318 for (int i
= 0; i
< 3; i
++) {
319 keys
[i
].MakeNewKey(true);
320 pubkeys
[i
] = keys
[i
].GetPubKey();
323 CScript expected
, result
;
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
);
332 CScript
redeemScript(result
);
334 expected
<< OP_HASH160
<< ToByteVector(CScriptID(redeemScript
)) << OP_EQUAL
;
335 result
= GetScriptForDestination(CScriptID(redeemScript
));
336 BOOST_CHECK(result
== expected
);
340 result
= GetScriptForDestination(CNoDestination());
341 BOOST_CHECK(result
== expected
);
343 // GetScriptForRawPubKey
345 expected
<< ToByteVector(pubkeys
[0]) << OP_CHECKSIG
;
346 result
= GetScriptForRawPubKey(pubkeys
[0]);
347 BOOST_CHECK(result
== expected
);
349 // GetScriptForMultisig
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
;
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
;
377 CSHA256().Write(&witnessScript
[0], witnessScript
.size())
378 .Finalize(scriptHash
.begin());
381 expected
<< OP_0
<< ToByteVector(scriptHash
);
382 result
= GetScriptForWitness(witnessScript
);
383 BOOST_CHECK(result
== expected
);
386 BOOST_AUTO_TEST_CASE(script_standard_IsMine
)
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
;
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
);
415 keystore
.AddKey(keys
[0]);
416 result
= IsMine(keystore
, scriptPubKey
, isInvalid
);
417 BOOST_CHECK_EQUAL(result
, ISMINE_SPENDABLE
);
418 BOOST_CHECK(!isInvalid
);
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
);
433 keystore
.AddKey(uncompressedKey
);
434 result
= IsMine(keystore
, scriptPubKey
, isInvalid
);
435 BOOST_CHECK_EQUAL(result
, ISMINE_SPENDABLE
);
436 BOOST_CHECK(!isInvalid
);
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
);
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
);
469 keystore
.AddKey(uncompressedKey
);
470 result
= IsMine(keystore
, scriptPubKey
, isInvalid
);
471 BOOST_CHECK_EQUAL(result
, ISMINE_SPENDABLE
);
472 BOOST_CHECK(!isInvalid
);
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
);
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
);
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
;
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
;
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
;
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
);
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
);
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()