Back to home page

Bitcoin sources

 
 

    


0001 #include <vector>
0002 #include <boost/test/unit_test.hpp>
0003 #include <boost/foreach.hpp>
0004 
0005 #include "../main.h"
0006 #include "../wallet.h"
0007 
0008 using namespace std;
0009 extern uint256 SignatureHash(CScript scriptCode, const CTransaction& txTo, unsigned int nIn, int nHashType);
0010 extern bool VerifyScript(const CScript& scriptSig, const CScript& scriptPubKey, const CTransaction& txTo, unsigned int nIn, int nHashType);
0011 extern bool VerifySignature(const CTransaction& txFrom, const CTransaction& txTo, unsigned int nIn, int nHashType);
0012 
0013 BOOST_AUTO_TEST_SUITE(script_tests)
0014 
0015 BOOST_AUTO_TEST_CASE(script_PushData)
0016 {
0017     // Check that PUSHDATA1, PUSHDATA2, and PUSHDATA4 create the same value on
0018     // the stack as the 1-75 opcodes do.
0019     static const unsigned char direct[] = { 1, 0x5a };
0020     static const unsigned char pushdata1[] = { OP_PUSHDATA1, 1, 0x5a };
0021     static const unsigned char pushdata2[] = { OP_PUSHDATA2, 1, 0, 0x5a };
0022     static const unsigned char pushdata4[] = { OP_PUSHDATA4, 1, 0, 0, 0, 0x5a };
0023 
0024     vector<vector<unsigned char> > directStack;
0025     BOOST_CHECK(EvalScript(directStack, CScript(&direct[0], &direct[sizeof(direct)]), CTransaction(), 0, 0));
0026 
0027     vector<vector<unsigned char> > pushdata1Stack;
0028     BOOST_CHECK(EvalScript(pushdata1Stack, CScript(&pushdata1[0], &pushdata1[sizeof(pushdata1)]), CTransaction(), 0, 0));
0029     BOOST_CHECK(pushdata1Stack == directStack);
0030 
0031     vector<vector<unsigned char> > pushdata2Stack;
0032     BOOST_CHECK(EvalScript(pushdata2Stack, CScript(&pushdata2[0], &pushdata2[sizeof(pushdata2)]), CTransaction(), 0, 0));
0033     BOOST_CHECK(pushdata2Stack == directStack);
0034 
0035     vector<vector<unsigned char> > pushdata4Stack;
0036     BOOST_CHECK(EvalScript(pushdata4Stack, CScript(&pushdata4[0], &pushdata4[sizeof(pushdata4)]), CTransaction(), 0, 0));
0037     BOOST_CHECK(pushdata4Stack == directStack);
0038 }
0039 
0040 CScript
0041 sign_multisig(CScript scriptPubKey, std::vector<CKey> keys, CTransaction transaction)
0042 {
0043     uint256 hash = SignatureHash(scriptPubKey, transaction, 0, SIGHASH_ALL);
0044 
0045     CScript result;
0046     //
0047     // NOTE: CHECKMULTISIG has an unfortunate bug; it requires
0048     // one extra item on the stack, before the signatures.
0049     // Putting OP_0 on the stack is the workaround;
0050     // fixing the bug would mean splitting the blockchain (old
0051     // clients would not accept new CHECKMULTISIG transactions,
0052     // and vice-versa)
0053     //
0054     result << OP_0;
0055     BOOST_FOREACH(CKey key, keys)
0056     {
0057         vector<unsigned char> vchSig;
0058         BOOST_CHECK(key.Sign(hash, vchSig));
0059         vchSig.push_back((unsigned char)SIGHASH_ALL);
0060         result << vchSig;
0061     }
0062     return result;
0063 }
0064 CScript
0065 sign_multisig(CScript scriptPubKey, CKey key, CTransaction transaction)
0066 {
0067     std::vector<CKey> keys;
0068     keys.push_back(key);
0069     return sign_multisig(scriptPubKey, keys, transaction);
0070 }
0071 
0072 BOOST_AUTO_TEST_CASE(script_CHECKMULTISIG12)
0073 {
0074     CKey key1, key2, key3;
0075     key1.MakeNewKey();
0076     key2.MakeNewKey();
0077     key3.MakeNewKey();
0078 
0079     CScript scriptPubKey12;
0080     scriptPubKey12 << OP_1 << key1.GetPubKey() << key2.GetPubKey() << OP_2 << OP_CHECKMULTISIG;
0081 
0082     CTransaction txFrom12;
0083     txFrom12.vout.resize(1);
0084     txFrom12.vout[0].scriptPubKey = scriptPubKey12;
0085 
0086     CTransaction txTo12;
0087     txTo12.vin.resize(1);
0088     txTo12.vout.resize(1);
0089     txTo12.vin[0].prevout.n = 0;
0090     txTo12.vin[0].prevout.hash = txFrom12.GetHash();
0091     txTo12.vout[0].nValue = 1;
0092 
0093     CScript goodsig1 = sign_multisig(scriptPubKey12, key1, txTo12);
0094     BOOST_CHECK(VerifyScript(goodsig1, scriptPubKey12, txTo12, 0, 0));
0095     txTo12.vout[0].nValue = 2;
0096     BOOST_CHECK(!VerifyScript(goodsig1, scriptPubKey12, txTo12, 0, 0));
0097 
0098     CScript goodsig2 = sign_multisig(scriptPubKey12, key2, txTo12);
0099     BOOST_CHECK(VerifyScript(goodsig2, scriptPubKey12, txTo12, 0, 0));
0100 
0101     CScript badsig1 = sign_multisig(scriptPubKey12, key3, txTo12);
0102     BOOST_CHECK(!VerifyScript(badsig1, scriptPubKey12, txTo12, 0, 0));
0103 }
0104 
0105 BOOST_AUTO_TEST_CASE(script_CHECKMULTISIG23)
0106 {
0107     CKey key1, key2, key3, key4;
0108     key1.MakeNewKey();
0109     key2.MakeNewKey();
0110     key3.MakeNewKey();
0111     key4.MakeNewKey();
0112 
0113     CScript scriptPubKey23;
0114     scriptPubKey23 << OP_2 << key1.GetPubKey() << key2.GetPubKey() << key3.GetPubKey() << OP_3 << OP_CHECKMULTISIG;
0115 
0116     CTransaction txFrom23;
0117     txFrom23.vout.resize(1);
0118     txFrom23.vout[0].scriptPubKey = scriptPubKey23;
0119 
0120     CTransaction txTo23;
0121     txTo23.vin.resize(1);
0122     txTo23.vout.resize(1);
0123     txTo23.vin[0].prevout.n = 0;
0124     txTo23.vin[0].prevout.hash = txFrom23.GetHash();
0125     txTo23.vout[0].nValue = 1;
0126 
0127     std::vector<CKey> keys;
0128     keys.push_back(key1); keys.push_back(key2);
0129     CScript goodsig1 = sign_multisig(scriptPubKey23, keys, txTo23);
0130     BOOST_CHECK(VerifyScript(goodsig1, scriptPubKey23, txTo23, 0, 0));
0131 
0132     keys.clear();
0133     keys.push_back(key1); keys.push_back(key3);
0134     CScript goodsig2 = sign_multisig(scriptPubKey23, keys, txTo23);
0135     BOOST_CHECK(VerifyScript(goodsig2, scriptPubKey23, txTo23, 0, 0));
0136 
0137     keys.clear();
0138     keys.push_back(key2); keys.push_back(key3);
0139     CScript goodsig3 = sign_multisig(scriptPubKey23, keys, txTo23);
0140     BOOST_CHECK(VerifyScript(goodsig3, scriptPubKey23, txTo23, 0, 0));
0141 
0142     keys.clear();
0143     keys.push_back(key2); keys.push_back(key2); // Can't re-use sig
0144     CScript badsig1 = sign_multisig(scriptPubKey23, keys, txTo23);
0145     BOOST_CHECK(!VerifyScript(badsig1, scriptPubKey23, txTo23, 0, 0));
0146 
0147     keys.clear();
0148     keys.push_back(key2); keys.push_back(key1); // sigs must be in correct order
0149     CScript badsig2 = sign_multisig(scriptPubKey23, keys, txTo23);
0150     BOOST_CHECK(!VerifyScript(badsig2, scriptPubKey23, txTo23, 0, 0));
0151 
0152     keys.clear();
0153     keys.push_back(key3); keys.push_back(key2); // sigs must be in correct order
0154     CScript badsig3 = sign_multisig(scriptPubKey23, keys, txTo23);
0155     BOOST_CHECK(!VerifyScript(badsig3, scriptPubKey23, txTo23, 0, 0));
0156 
0157     keys.clear();
0158     keys.push_back(key4); keys.push_back(key2); // sigs must match pubkeys
0159     CScript badsig4 = sign_multisig(scriptPubKey23, keys, txTo23);
0160     BOOST_CHECK(!VerifyScript(badsig4, scriptPubKey23, txTo23, 0, 0));
0161 
0162     keys.clear();
0163     keys.push_back(key1); keys.push_back(key4); // sigs must match pubkeys
0164     CScript badsig5 = sign_multisig(scriptPubKey23, keys, txTo23);
0165     BOOST_CHECK(!VerifyScript(badsig5, scriptPubKey23, txTo23, 0, 0));
0166 
0167     keys.clear(); // Must have signatures
0168     CScript badsig6 = sign_multisig(scriptPubKey23, keys, txTo23);
0169     BOOST_CHECK(!VerifyScript(badsig6, scriptPubKey23, txTo23, 0, 0));
0170 }    
0171 
0172 
0173 BOOST_AUTO_TEST_SUITE_END()