1 #include <boost/assert.hpp>
2 #include <boost/assign/list_of.hpp>
3 #include <boost/assign/list_inserter.hpp>
4 #include <boost/assign/std/vector.hpp>
5 #include <boost/test/unit_test.hpp>
6 #include <boost/foreach.hpp>
7 #include <boost/tuple/tuple.hpp>
9 #include <openssl/ec.h>
10 #include <openssl/err.h>
18 using namespace boost::assign;
20 typedef vector<unsigned char> valtype;
22 extern uint256 SignatureHash(CScript scriptCode, const CTransaction& txTo, unsigned int nIn, int nHashType);
23 extern bool VerifyScript(const CScript& scriptSig, const CScript& scriptPubKey, const CTransaction& txTo, unsigned int nIn,
24 bool fValidatePayToScriptHash, int nHashType);
26 BOOST_AUTO_TEST_SUITE(multisig_tests)
29 sign_multisig(CScript scriptPubKey, vector<CKey> keys, CTransaction transaction, int whichIn)
31 uint256 hash = SignatureHash(scriptPubKey, transaction, whichIn, SIGHASH_ALL);
34 result << OP_0; // CHECKMULTISIG bug workaround
35 BOOST_FOREACH(CKey key, keys)
37 vector<unsigned char> vchSig;
38 BOOST_CHECK(key.Sign(hash, vchSig));
39 vchSig.push_back((unsigned char)SIGHASH_ALL);
45 BOOST_AUTO_TEST_CASE(multisig_verify)
48 for (int i = 0; i < 4; i++)
49 key[i].MakeNewKey(true);
52 a_and_b << OP_2 << key[0].GetPubKey() << key[1].GetPubKey() << OP_2 << OP_CHECKMULTISIG;
55 a_or_b << OP_1 << key[0].GetPubKey() << key[1].GetPubKey() << OP_2 << OP_CHECKMULTISIG;
58 escrow << OP_2 << key[0].GetPubKey() << key[1].GetPubKey() << key[2].GetPubKey() << OP_3 << OP_CHECKMULTISIG;
60 CTransaction txFrom; // Funding transaction
61 txFrom.vout.resize(3);
62 txFrom.vout[0].scriptPubKey = a_and_b;
63 txFrom.vout[1].scriptPubKey = a_or_b;
64 txFrom.vout[2].scriptPubKey = escrow;
66 CTransaction txTo[3]; // Spending transaction
67 for (int i = 0; i < 3; i++)
69 txTo[i].vin.resize(1);
70 txTo[i].vout.resize(1);
71 txTo[i].vin[0].prevout.n = i;
72 txTo[i].vin[0].prevout.hash = txFrom.GetHash();
73 txTo[i].vout[0].nValue = 1;
81 keys += key[0],key[1]; // magic operator+= from boost.assign
82 s = sign_multisig(a_and_b, keys, txTo[0], 0);
83 BOOST_CHECK(VerifyScript(s, a_and_b, txTo[0], 0, true, 0));
85 for (int i = 0; i < 4; i++)
89 s = sign_multisig(a_and_b, keys, txTo[0], 0);
90 BOOST_CHECK_MESSAGE(!VerifyScript(s, a_and_b, txTo[0], 0, true, 0), strprintf("a&b 1: %d", i));
93 keys += key[1],key[i];
94 s = sign_multisig(a_and_b, keys, txTo[0], 0);
95 BOOST_CHECK_MESSAGE(!VerifyScript(s, a_and_b, txTo[0], 0, true, 0), strprintf("a&b 2: %d", i));
99 for (int i = 0; i < 4; i++)
103 s = sign_multisig(a_or_b, keys, txTo[1], 0);
104 if (i == 0 || i == 1)
105 BOOST_CHECK_MESSAGE(VerifyScript(s, a_or_b, txTo[1], 0, true, 0), strprintf("a|b: %d", i));
107 BOOST_CHECK_MESSAGE(!VerifyScript(s, a_or_b, txTo[1], 0, true, 0), strprintf("a|b: %d", i));
111 BOOST_CHECK(!VerifyScript(s, a_or_b, txTo[1], 0, true, 0));
114 BOOST_CHECK(!VerifyScript(s, a_or_b, txTo[1], 0, true, 0));
117 for (int i = 0; i < 4; i++)
118 for (int j = 0; j < 4; j++)
121 keys += key[i],key[j];
122 s = sign_multisig(escrow, keys, txTo[2], 0);
123 if (i < j && i < 3 && j < 3)
124 BOOST_CHECK_MESSAGE(VerifyScript(s, escrow, txTo[2], 0, true, 0), strprintf("escrow 1: %d %d", i, j));
126 BOOST_CHECK_MESSAGE(!VerifyScript(s, escrow, txTo[2], 0, true, 0), strprintf("escrow 2: %d %d", i, j));
130 BOOST_AUTO_TEST_CASE(multisig_IsStandard)
133 for (int i = 0; i < 4; i++)
134 key[i].MakeNewKey(true);
137 a_and_b << OP_2 << key[0].GetPubKey() << key[1].GetPubKey() << OP_2 << OP_CHECKMULTISIG;
138 BOOST_CHECK(::IsStandard(a_and_b));
141 a_or_b << OP_1 << key[0].GetPubKey() << key[1].GetPubKey() << OP_2 << OP_CHECKMULTISIG;
142 BOOST_CHECK(::IsStandard(a_or_b));
145 escrow << OP_2 << key[0].GetPubKey() << key[1].GetPubKey() << key[2].GetPubKey() << OP_3 << OP_CHECKMULTISIG;
146 BOOST_CHECK(::IsStandard(escrow));
149 one_of_four << OP_1 << key[0].GetPubKey() << key[1].GetPubKey() << key[2].GetPubKey() << key[3].GetPubKey() << OP_4 << OP_CHECKMULTISIG;
150 BOOST_CHECK(!::IsStandard(one_of_four));
152 CScript malformed[6];
153 malformed[0] << OP_3 << key[0].GetPubKey() << key[1].GetPubKey() << OP_2 << OP_CHECKMULTISIG;
154 malformed[1] << OP_2 << key[0].GetPubKey() << key[1].GetPubKey() << OP_3 << OP_CHECKMULTISIG;
155 malformed[2] << OP_0 << key[0].GetPubKey() << key[1].GetPubKey() << OP_2 << OP_CHECKMULTISIG;
156 malformed[3] << OP_1 << key[0].GetPubKey() << key[1].GetPubKey() << OP_0 << OP_CHECKMULTISIG;
157 malformed[4] << OP_1 << key[0].GetPubKey() << key[1].GetPubKey() << OP_CHECKMULTISIG;
158 malformed[5] << OP_1 << key[0].GetPubKey() << key[1].GetPubKey();
160 for (int i = 0; i < 6; i++)
161 BOOST_CHECK(!::IsStandard(malformed[i]));
164 BOOST_AUTO_TEST_CASE(multisig_Solver1)
166 // Tests Solver() that returns lists of keys that are
167 // required to satisfy a ScriptPubKey
169 // Also tests IsMine() and ExtractAddress()
171 // Note: ExtractAddress for the multisignature transactions
172 // always returns false for this release, even if you have
173 // one key that would satisfy an (a|b) or 2-of-3 keys needed
174 // to spend an escrow transaction.
176 CBasicKeyStore keystore, emptykeystore, partialkeystore;
178 CTxDestination keyaddr[3];
179 for (int i = 0; i < 3; i++)
181 key[i].MakeNewKey(true);
182 keystore.AddKey(key[i]);
183 keyaddr[i] = key[i].GetPubKey().GetID();
185 partialkeystore.AddKey(key[0]);
188 vector<valtype> solutions;
189 txnouttype whichType;
191 s << key[0].GetPubKey() << OP_CHECKSIG;
192 BOOST_CHECK(Solver(s, whichType, solutions));
193 BOOST_CHECK(solutions.size() == 1);
195 BOOST_CHECK(ExtractDestination(s, addr));
196 BOOST_CHECK(addr == keyaddr[0]);
197 BOOST_CHECK(IsMine(keystore, s));
198 BOOST_CHECK(!IsMine(emptykeystore, s));
201 vector<valtype> solutions;
202 txnouttype whichType;
204 s << OP_DUP << OP_HASH160 << key[0].GetPubKey().GetID() << OP_EQUALVERIFY << OP_CHECKSIG;
205 BOOST_CHECK(Solver(s, whichType, solutions));
206 BOOST_CHECK(solutions.size() == 1);
208 BOOST_CHECK(ExtractDestination(s, addr));
209 BOOST_CHECK(addr == keyaddr[0]);
210 BOOST_CHECK(IsMine(keystore, s));
211 BOOST_CHECK(!IsMine(emptykeystore, s));
214 vector<valtype> solutions;
215 txnouttype whichType;
217 s << OP_2 << key[0].GetPubKey() << key[1].GetPubKey() << OP_2 << OP_CHECKMULTISIG;
218 BOOST_CHECK(Solver(s, whichType, solutions));
219 BOOST_CHECK_EQUAL(solutions.size(), 4);
221 BOOST_CHECK(!ExtractDestination(s, addr));
222 BOOST_CHECK(IsMine(keystore, s));
223 BOOST_CHECK(!IsMine(emptykeystore, s));
224 BOOST_CHECK(!IsMine(partialkeystore, s));
227 vector<valtype> solutions;
228 txnouttype whichType;
230 s << OP_1 << key[0].GetPubKey() << key[1].GetPubKey() << OP_2 << OP_CHECKMULTISIG;
231 BOOST_CHECK(Solver(s, whichType, solutions));
232 BOOST_CHECK_EQUAL(solutions.size(), 4);
233 vector<CTxDestination> addrs;
235 BOOST_CHECK(ExtractDestinations(s, whichType, addrs, nRequired));
236 BOOST_CHECK(addrs[0] == keyaddr[0]);
237 BOOST_CHECK(addrs[1] == keyaddr[1]);
238 BOOST_CHECK(nRequired == 1);
239 BOOST_CHECK(IsMine(keystore, s));
240 BOOST_CHECK(!IsMine(emptykeystore, s));
241 BOOST_CHECK(!IsMine(partialkeystore, s));
244 vector<valtype> solutions;
245 txnouttype whichType;
247 s << OP_2 << key[0].GetPubKey() << key[1].GetPubKey() << key[2].GetPubKey() << OP_3 << OP_CHECKMULTISIG;
248 BOOST_CHECK(Solver(s, whichType, solutions));
249 BOOST_CHECK(solutions.size() == 5);
253 BOOST_AUTO_TEST_CASE(multisig_Sign)
255 // Test SignSignature() (and therefore the version of Solver() that signs transactions)
256 CBasicKeyStore keystore;
258 for (int i = 0; i < 4; i++)
260 key[i].MakeNewKey(true);
261 keystore.AddKey(key[i]);
265 a_and_b << OP_2 << key[0].GetPubKey() << key[1].GetPubKey() << OP_2 << OP_CHECKMULTISIG;
268 a_or_b << OP_1 << key[0].GetPubKey() << key[1].GetPubKey() << OP_2 << OP_CHECKMULTISIG;
271 escrow << OP_2 << key[0].GetPubKey() << key[1].GetPubKey() << key[2].GetPubKey() << OP_3 << OP_CHECKMULTISIG;
273 CTransaction txFrom; // Funding transaction
274 txFrom.vout.resize(3);
275 txFrom.vout[0].scriptPubKey = a_and_b;
276 txFrom.vout[1].scriptPubKey = a_or_b;
277 txFrom.vout[2].scriptPubKey = escrow;
279 CTransaction txTo[3]; // Spending transaction
280 for (int i = 0; i < 3; i++)
282 txTo[i].vin.resize(1);
283 txTo[i].vout.resize(1);
284 txTo[i].vin[0].prevout.n = i;
285 txTo[i].vin[0].prevout.hash = txFrom.GetHash();
286 txTo[i].vout[0].nValue = 1;
289 for (int i = 0; i < 3; i++)
291 BOOST_CHECK_MESSAGE(SignSignature(keystore, txFrom, txTo[i], 0), strprintf("SignSignature %d", i));
296 BOOST_AUTO_TEST_SUITE_END()