experimental-genesis 1
experimental-genesis 2
experimental-genesis 3
experimental-genesis 4
experimental-genesis 5
experimental-genesis 6
experimental-genesis 7
experimental-genesis 8
experimental-genesis 9
experimental-genesis 10
experimental-genesis 11
experimental-genesis 12
experimental-genesis 13
experimental-genesis 14
experimental-genesis 15
experimental-genesis 16
experimental-genesis 17
experimental-genesis 18
experimental-genesis 19
experimental-genesis 20
experimental-genesis 21
experimental-genesis 22
experimental-genesis 23
experimental-genesis 24
experimental-genesis 25
experimental-genesis 26
experimental-genesis 27
experimental-genesis 28
experimental-genesis 29
experimental-genesis 30
experimental-genesis 31
experimental-genesis 32 #include <vector>
experimental-genesis 33 #include <boost/test/unit_test.hpp>
experimental-genesis 34 #include <boost/foreach.hpp>
experimental-genesis 35
experimental-genesis 36 #include "../main.h"
experimental-genesis 37 #include "../wallet.h"
experimental-genesis 38
experimental-genesis 39 using namespace std;
experimental-genesis 40 extern uint256 SignatureHash(CScript scriptCode, const CTransaction& txTo, unsigned int nIn, int nHashType);
experimental-genesis 41 extern bool VerifyScript(const CScript& scriptSig, const CScript& scriptPubKey, const CTransaction& txTo, unsigned int nIn, int nHashType);
experimental-genesis 42 extern bool VerifySignature(const CTransaction& txFrom, const CTransaction& txTo, unsigned int nIn, int nHashType);
experimental-genesis 43
experimental-genesis 44 BOOST_AUTO_TEST_SUITE(script_tests)
experimental-genesis 45
experimental-genesis 46 BOOST_AUTO_TEST_CASE(script_PushData)
experimental-genesis 47 {
experimental-genesis 48
experimental-genesis 49
experimental-genesis 50 static const unsigned char direct[] = { 1, 0x5a };
experimental-genesis 51 static const unsigned char pushdata1[] = { OP_PUSHDATA1, 1, 0x5a };
experimental-genesis 52 static const unsigned char pushdata2[] = { OP_PUSHDATA2, 1, 0, 0x5a };
experimental-genesis 53 static const unsigned char pushdata4[] = { OP_PUSHDATA4, 1, 0, 0, 0, 0x5a };
experimental-genesis 54
experimental-genesis 55 vector<vector<unsigned char> > directStack;
experimental-genesis 56 BOOST_CHECK(EvalScript(directStack, CScript(&direct[0], &direct[sizeof(direct)]), CTransaction(), 0, 0));
experimental-genesis 57
experimental-genesis 58 vector<vector<unsigned char> > pushdata1Stack;
experimental-genesis 59 BOOST_CHECK(EvalScript(pushdata1Stack, CScript(&pushdata1[0], &pushdata1[sizeof(pushdata1)]), CTransaction(), 0, 0));
experimental-genesis 60 BOOST_CHECK(pushdata1Stack == directStack);
experimental-genesis 61
experimental-genesis 62 vector<vector<unsigned char> > pushdata2Stack;
experimental-genesis 63 BOOST_CHECK(EvalScript(pushdata2Stack, CScript(&pushdata2[0], &pushdata2[sizeof(pushdata2)]), CTransaction(), 0, 0));
experimental-genesis 64 BOOST_CHECK(pushdata2Stack == directStack);
experimental-genesis 65
experimental-genesis 66 vector<vector<unsigned char> > pushdata4Stack;
experimental-genesis 67 BOOST_CHECK(EvalScript(pushdata4Stack, CScript(&pushdata4[0], &pushdata4[sizeof(pushdata4)]), CTransaction(), 0, 0));
experimental-genesis 68 BOOST_CHECK(pushdata4Stack == directStack);
experimental-genesis 69 }
experimental-genesis 70
experimental-genesis 71 CScript
experimental-genesis 72 sign_multisig(CScript scriptPubKey, std::vector<CKey> keys, CTransaction transaction)
experimental-genesis 73 {
experimental-genesis 74 uint256 hash = SignatureHash(scriptPubKey, transaction, 0, SIGHASH_ALL);
experimental-genesis 75
experimental-genesis 76 CScript result;
experimental-genesis 77
experimental-genesis 78
experimental-genesis 79
experimental-genesis 80
experimental-genesis 81
experimental-genesis 82
experimental-genesis 83
experimental-genesis 84
experimental-genesis 85 result << OP_0;
experimental-genesis 86 BOOST_FOREACH(CKey key, keys)
experimental-genesis 87 {
experimental-genesis 88 vector<unsigned char> vchSig;
experimental-genesis 89 BOOST_CHECK(key.Sign(hash, vchSig));
experimental-genesis 90 vchSig.push_back((unsigned char)SIGHASH_ALL);
experimental-genesis 91 result << vchSig;
experimental-genesis 92 }
experimental-genesis 93 return result;
experimental-genesis 94 }
experimental-genesis 95 CScript
experimental-genesis 96 sign_multisig(CScript scriptPubKey, CKey key, CTransaction transaction)
experimental-genesis 97 {
experimental-genesis 98 std::vector<CKey> keys;
experimental-genesis 99 keys.push_back(key);
experimental-genesis 100 return sign_multisig(scriptPubKey, keys, transaction);
experimental-genesis 101 }
experimental-genesis 102
experimental-genesis 103 BOOST_AUTO_TEST_CASE(script_CHECKMULTISIG12)
experimental-genesis 104 {
experimental-genesis 105 CKey key1, key2, key3;
experimental-genesis 106 key1.MakeNewKey();
experimental-genesis 107 key2.MakeNewKey();
experimental-genesis 108 key3.MakeNewKey();
experimental-genesis 109
experimental-genesis 110 CScript scriptPubKey12;
experimental-genesis 111 scriptPubKey12 << OP_1 << key1.GetPubKey() << key2.GetPubKey() << OP_2 << OP_CHECKMULTISIG;
experimental-genesis 112
experimental-genesis 113 CTransaction txFrom12;
experimental-genesis 114 txFrom12.vout.resize(1);
experimental-genesis 115 txFrom12.vout[0].scriptPubKey = scriptPubKey12;
experimental-genesis 116
experimental-genesis 117 CTransaction txTo12;
experimental-genesis 118 txTo12.vin.resize(1);
experimental-genesis 119 txTo12.vout.resize(1);
experimental-genesis 120 txTo12.vin[0].prevout.n = 0;
experimental-genesis 121 txTo12.vin[0].prevout.hash = txFrom12.GetHash();
experimental-genesis 122 txTo12.vout[0].nValue = 1;
experimental-genesis 123
experimental-genesis 124 CScript goodsig1 = sign_multisig(scriptPubKey12, key1, txTo12);
experimental-genesis 125 BOOST_CHECK(VerifyScript(goodsig1, scriptPubKey12, txTo12, 0, 0));
experimental-genesis 126 txTo12.vout[0].nValue = 2;
experimental-genesis 127 BOOST_CHECK(!VerifyScript(goodsig1, scriptPubKey12, txTo12, 0, 0));
experimental-genesis 128
experimental-genesis 129 CScript goodsig2 = sign_multisig(scriptPubKey12, key2, txTo12);
experimental-genesis 130 BOOST_CHECK(VerifyScript(goodsig2, scriptPubKey12, txTo12, 0, 0));
experimental-genesis 131
experimental-genesis 132 CScript badsig1 = sign_multisig(scriptPubKey12, key3, txTo12);
experimental-genesis 133 BOOST_CHECK(!VerifyScript(badsig1, scriptPubKey12, txTo12, 0, 0));
experimental-genesis 134 }
experimental-genesis 135
experimental-genesis 136 BOOST_AUTO_TEST_CASE(script_CHECKMULTISIG23)
experimental-genesis 137 {
experimental-genesis 138 CKey key1, key2, key3, key4;
experimental-genesis 139 key1.MakeNewKey();
experimental-genesis 140 key2.MakeNewKey();
experimental-genesis 141 key3.MakeNewKey();
experimental-genesis 142 key4.MakeNewKey();
experimental-genesis 143
experimental-genesis 144 CScript scriptPubKey23;
experimental-genesis 145 scriptPubKey23 << OP_2 << key1.GetPubKey() << key2.GetPubKey() << key3.GetPubKey() << OP_3 << OP_CHECKMULTISIG;
experimental-genesis 146
experimental-genesis 147 CTransaction txFrom23;
experimental-genesis 148 txFrom23.vout.resize(1);
experimental-genesis 149 txFrom23.vout[0].scriptPubKey = scriptPubKey23;
experimental-genesis 150
experimental-genesis 151 CTransaction txTo23;
experimental-genesis 152 txTo23.vin.resize(1);
experimental-genesis 153 txTo23.vout.resize(1);
experimental-genesis 154 txTo23.vin[0].prevout.n = 0;
experimental-genesis 155 txTo23.vin[0].prevout.hash = txFrom23.GetHash();
experimental-genesis 156 txTo23.vout[0].nValue = 1;
experimental-genesis 157
experimental-genesis 158 std::vector<CKey> keys;
experimental-genesis 159 keys.push_back(key1); keys.push_back(key2);
experimental-genesis 160 CScript goodsig1 = sign_multisig(scriptPubKey23, keys, txTo23);
experimental-genesis 161 BOOST_CHECK(VerifyScript(goodsig1, scriptPubKey23, txTo23, 0, 0));
experimental-genesis 162
experimental-genesis 163 keys.clear();
experimental-genesis 164 keys.push_back(key1); keys.push_back(key3);
experimental-genesis 165 CScript goodsig2 = sign_multisig(scriptPubKey23, keys, txTo23);
experimental-genesis 166 BOOST_CHECK(VerifyScript(goodsig2, scriptPubKey23, txTo23, 0, 0));
experimental-genesis 167
experimental-genesis 168 keys.clear();
experimental-genesis 169 keys.push_back(key2); keys.push_back(key3);
experimental-genesis 170 CScript goodsig3 = sign_multisig(scriptPubKey23, keys, txTo23);
experimental-genesis 171 BOOST_CHECK(VerifyScript(goodsig3, scriptPubKey23, txTo23, 0, 0));
experimental-genesis 172
experimental-genesis 173 keys.clear();
experimental-genesis 174 keys.push_back(key2); keys.push_back(key2);
experimental-genesis 175 CScript badsig1 = sign_multisig(scriptPubKey23, keys, txTo23);
experimental-genesis 176 BOOST_CHECK(!VerifyScript(badsig1, scriptPubKey23, txTo23, 0, 0));
experimental-genesis 177
experimental-genesis 178 keys.clear();
experimental-genesis 179 keys.push_back(key2); keys.push_back(key1);
experimental-genesis 180 CScript badsig2 = sign_multisig(scriptPubKey23, keys, txTo23);
experimental-genesis 181 BOOST_CHECK(!VerifyScript(badsig2, scriptPubKey23, txTo23, 0, 0));
experimental-genesis 182
experimental-genesis 183 keys.clear();
experimental-genesis 184 keys.push_back(key3); keys.push_back(key2);
experimental-genesis 185 CScript badsig3 = sign_multisig(scriptPubKey23, keys, txTo23);
experimental-genesis 186 BOOST_CHECK(!VerifyScript(badsig3, scriptPubKey23, txTo23, 0, 0));
experimental-genesis 187
experimental-genesis 188 keys.clear();
experimental-genesis 189 keys.push_back(key4); keys.push_back(key2);
experimental-genesis 190 CScript badsig4 = sign_multisig(scriptPubKey23, keys, txTo23);
experimental-genesis 191 BOOST_CHECK(!VerifyScript(badsig4, scriptPubKey23, txTo23, 0, 0));
experimental-genesis 192
experimental-genesis 193 keys.clear();
experimental-genesis 194 keys.push_back(key1); keys.push_back(key4);
experimental-genesis 195 CScript badsig5 = sign_multisig(scriptPubKey23, keys, txTo23);
experimental-genesis 196 BOOST_CHECK(!VerifyScript(badsig5, scriptPubKey23, txTo23, 0, 0));
experimental-genesis 197
experimental-genesis 198 keys.clear();
experimental-genesis 199 CScript badsig6 = sign_multisig(scriptPubKey23, keys, txTo23);
experimental-genesis 200 BOOST_CHECK(!VerifyScript(badsig6, scriptPubKey23, txTo23, 0, 0));
experimental-genesis 201 }
experimental-genesis 202
experimental-genesis 203
experimental-genesis 204 BOOST_AUTO_TEST_SUITE_END()