-
+ 1672C86E35D81A41CA3043FEC45BCD3A6278D29FD6C7E040EC84E969839639AA40E4ABD5D7096F10AC37D46E41479E219BC4747872768DDEBF4173521CCBEC75bitcoin/src/test/script_tests.cpp(0 . 0)(1 . 173)
21387 #include <vector>
21388 #include <boost/test/unit_test.hpp>
21389 #include <boost/foreach.hpp>
21390
21391 #include "../main.h"
21392 #include "../wallet.h"
21393
21394 using namespace std;
21395 extern uint256 SignatureHash(CScript scriptCode, const CTransaction& txTo, unsigned int nIn, int nHashType);
21396 extern bool VerifyScript(const CScript& scriptSig, const CScript& scriptPubKey, const CTransaction& txTo, unsigned int nIn, int nHashType);
21397 extern bool VerifySignature(const CTransaction& txFrom, const CTransaction& txTo, unsigned int nIn, int nHashType);
21398
21399 BOOST_AUTO_TEST_SUITE(script_tests)
21400
21401 BOOST_AUTO_TEST_CASE(script_PushData)
21402 {
21403 // Check that PUSHDATA1, PUSHDATA2, and PUSHDATA4 create the same value on
21404 // the stack as the 1-75 opcodes do.
21405 static const unsigned char direct[] = { 1, 0x5a };
21406 static const unsigned char pushdata1[] = { OP_PUSHDATA1, 1, 0x5a };
21407 static const unsigned char pushdata2[] = { OP_PUSHDATA2, 1, 0, 0x5a };
21408 static const unsigned char pushdata4[] = { OP_PUSHDATA4, 1, 0, 0, 0, 0x5a };
21409
21410 vector<vector<unsigned char> > directStack;
21411 BOOST_CHECK(EvalScript(directStack, CScript(&direct[0], &direct[sizeof(direct)]), CTransaction(), 0, 0));
21412
21413 vector<vector<unsigned char> > pushdata1Stack;
21414 BOOST_CHECK(EvalScript(pushdata1Stack, CScript(&pushdata1[0], &pushdata1[sizeof(pushdata1)]), CTransaction(), 0, 0));
21415 BOOST_CHECK(pushdata1Stack == directStack);
21416
21417 vector<vector<unsigned char> > pushdata2Stack;
21418 BOOST_CHECK(EvalScript(pushdata2Stack, CScript(&pushdata2[0], &pushdata2[sizeof(pushdata2)]), CTransaction(), 0, 0));
21419 BOOST_CHECK(pushdata2Stack == directStack);
21420
21421 vector<vector<unsigned char> > pushdata4Stack;
21422 BOOST_CHECK(EvalScript(pushdata4Stack, CScript(&pushdata4[0], &pushdata4[sizeof(pushdata4)]), CTransaction(), 0, 0));
21423 BOOST_CHECK(pushdata4Stack == directStack);
21424 }
21425
21426 CScript
21427 sign_multisig(CScript scriptPubKey, std::vector<CKey> keys, CTransaction transaction)
21428 {
21429 uint256 hash = SignatureHash(scriptPubKey, transaction, 0, SIGHASH_ALL);
21430
21431 CScript result;
21432 //
21433 // NOTE: CHECKMULTISIG has an unfortunate bug; it requires
21434 // one extra item on the stack, before the signatures.
21435 // Putting OP_0 on the stack is the workaround;
21436 // fixing the bug would mean splitting the blockchain (old
21437 // clients would not accept new CHECKMULTISIG transactions,
21438 // and vice-versa)
21439 //
21440 result << OP_0;
21441 BOOST_FOREACH(CKey key, keys)
21442 {
21443 vector<unsigned char> vchSig;
21444 BOOST_CHECK(key.Sign(hash, vchSig));
21445 vchSig.push_back((unsigned char)SIGHASH_ALL);
21446 result << vchSig;
21447 }
21448 return result;
21449 }
21450 CScript
21451 sign_multisig(CScript scriptPubKey, CKey key, CTransaction transaction)
21452 {
21453 std::vector<CKey> keys;
21454 keys.push_back(key);
21455 return sign_multisig(scriptPubKey, keys, transaction);
21456 }
21457
21458 BOOST_AUTO_TEST_CASE(script_CHECKMULTISIG12)
21459 {
21460 CKey key1, key2, key3;
21461 key1.MakeNewKey();
21462 key2.MakeNewKey();
21463 key3.MakeNewKey();
21464
21465 CScript scriptPubKey12;
21466 scriptPubKey12 << OP_1 << key1.GetPubKey() << key2.GetPubKey() << OP_2 << OP_CHECKMULTISIG;
21467
21468 CTransaction txFrom12;
21469 txFrom12.vout.resize(1);
21470 txFrom12.vout[0].scriptPubKey = scriptPubKey12;
21471
21472 CTransaction txTo12;
21473 txTo12.vin.resize(1);
21474 txTo12.vout.resize(1);
21475 txTo12.vin[0].prevout.n = 0;
21476 txTo12.vin[0].prevout.hash = txFrom12.GetHash();
21477 txTo12.vout[0].nValue = 1;
21478
21479 CScript goodsig1 = sign_multisig(scriptPubKey12, key1, txTo12);
21480 BOOST_CHECK(VerifyScript(goodsig1, scriptPubKey12, txTo12, 0, 0));
21481 txTo12.vout[0].nValue = 2;
21482 BOOST_CHECK(!VerifyScript(goodsig1, scriptPubKey12, txTo12, 0, 0));
21483
21484 CScript goodsig2 = sign_multisig(scriptPubKey12, key2, txTo12);
21485 BOOST_CHECK(VerifyScript(goodsig2, scriptPubKey12, txTo12, 0, 0));
21486
21487 CScript badsig1 = sign_multisig(scriptPubKey12, key3, txTo12);
21488 BOOST_CHECK(!VerifyScript(badsig1, scriptPubKey12, txTo12, 0, 0));
21489 }
21490
21491 BOOST_AUTO_TEST_CASE(script_CHECKMULTISIG23)
21492 {
21493 CKey key1, key2, key3, key4;
21494 key1.MakeNewKey();
21495 key2.MakeNewKey();
21496 key3.MakeNewKey();
21497 key4.MakeNewKey();
21498
21499 CScript scriptPubKey23;
21500 scriptPubKey23 << OP_2 << key1.GetPubKey() << key2.GetPubKey() << key3.GetPubKey() << OP_3 << OP_CHECKMULTISIG;
21501
21502 CTransaction txFrom23;
21503 txFrom23.vout.resize(1);
21504 txFrom23.vout[0].scriptPubKey = scriptPubKey23;
21505
21506 CTransaction txTo23;
21507 txTo23.vin.resize(1);
21508 txTo23.vout.resize(1);
21509 txTo23.vin[0].prevout.n = 0;
21510 txTo23.vin[0].prevout.hash = txFrom23.GetHash();
21511 txTo23.vout[0].nValue = 1;
21512
21513 std::vector<CKey> keys;
21514 keys.push_back(key1); keys.push_back(key2);
21515 CScript goodsig1 = sign_multisig(scriptPubKey23, keys, txTo23);
21516 BOOST_CHECK(VerifyScript(goodsig1, scriptPubKey23, txTo23, 0, 0));
21517
21518 keys.clear();
21519 keys.push_back(key1); keys.push_back(key3);
21520 CScript goodsig2 = sign_multisig(scriptPubKey23, keys, txTo23);
21521 BOOST_CHECK(VerifyScript(goodsig2, scriptPubKey23, txTo23, 0, 0));
21522
21523 keys.clear();
21524 keys.push_back(key2); keys.push_back(key3);
21525 CScript goodsig3 = sign_multisig(scriptPubKey23, keys, txTo23);
21526 BOOST_CHECK(VerifyScript(goodsig3, scriptPubKey23, txTo23, 0, 0));
21527
21528 keys.clear();
21529 keys.push_back(key2); keys.push_back(key2); // Can't re-use sig
21530 CScript badsig1 = sign_multisig(scriptPubKey23, keys, txTo23);
21531 BOOST_CHECK(!VerifyScript(badsig1, scriptPubKey23, txTo23, 0, 0));
21532
21533 keys.clear();
21534 keys.push_back(key2); keys.push_back(key1); // sigs must be in correct order
21535 CScript badsig2 = sign_multisig(scriptPubKey23, keys, txTo23);
21536 BOOST_CHECK(!VerifyScript(badsig2, scriptPubKey23, txTo23, 0, 0));
21537
21538 keys.clear();
21539 keys.push_back(key3); keys.push_back(key2); // sigs must be in correct order
21540 CScript badsig3 = sign_multisig(scriptPubKey23, keys, txTo23);
21541 BOOST_CHECK(!VerifyScript(badsig3, scriptPubKey23, txTo23, 0, 0));
21542
21543 keys.clear();
21544 keys.push_back(key4); keys.push_back(key2); // sigs must match pubkeys
21545 CScript badsig4 = sign_multisig(scriptPubKey23, keys, txTo23);
21546 BOOST_CHECK(!VerifyScript(badsig4, scriptPubKey23, txTo23, 0, 0));
21547
21548 keys.clear();
21549 keys.push_back(key1); keys.push_back(key4); // sigs must match pubkeys
21550 CScript badsig5 = sign_multisig(scriptPubKey23, keys, txTo23);
21551 BOOST_CHECK(!VerifyScript(badsig5, scriptPubKey23, txTo23, 0, 0));
21552
21553 keys.clear(); // Must have signatures
21554 CScript badsig6 = sign_multisig(scriptPubKey23, keys, txTo23);
21555 BOOST_CHECK(!VerifyScript(badsig6, scriptPubKey23, txTo23, 0, 0));
21556 }
21557
21558
21559 BOOST_AUTO_TEST_SUITE_END()