1 // Copyright (c) 2011-2013 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.
8 #include "script/script.h"
9 #include "script/script_error.h"
10 #include "script/interpreter.h"
11 #include "script/sign.h"
13 #include "test/test_bitcoin.h"
16 #include "wallet/wallet_ismine.h"
19 #include <boost/foreach.hpp>
20 #include <boost/test/unit_test.hpp>
24 typedef vector<unsigned char> valtype;
26 BOOST_FIXTURE_TEST_SUITE(multisig_tests, BasicTestingSetup)
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(const 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)
47 unsigned int flags = SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_STRICTENC;
51 for (int i = 0; i < 4; i++)
52 key[i].MakeNewKey(true);
55 a_and_b << OP_2 << ToByteVector(key[0].GetPubKey()) << ToByteVector(key[1].GetPubKey()) << OP_2 << OP_CHECKMULTISIG;
58 a_or_b << OP_1 << ToByteVector(key[0].GetPubKey()) << ToByteVector(key[1].GetPubKey()) << OP_2 << OP_CHECKMULTISIG;
61 escrow << OP_2 << ToByteVector(key[0].GetPubKey()) << ToByteVector(key[1].GetPubKey()) << ToByteVector(key[2].GetPubKey()) << OP_3 << OP_CHECKMULTISIG;
63 CMutableTransaction txFrom; // Funding transaction
64 txFrom.vout.resize(3);
65 txFrom.vout[0].scriptPubKey = a_and_b;
66 txFrom.vout[1].scriptPubKey = a_or_b;
67 txFrom.vout[2].scriptPubKey = escrow;
69 CMutableTransaction txTo[3]; // Spending transaction
70 for (int i = 0; i < 3; i++)
72 txTo[i].vin.resize(1);
73 txTo[i].vout.resize(1);
74 txTo[i].vin[0].prevout.n = i;
75 txTo[i].vin[0].prevout.hash = txFrom.GetTxid();
76 txTo[i].vout[0].nValue = 1;
83 keys.assign(1,key[0]);
84 keys.push_back(key[1]);
85 s = sign_multisig(a_and_b, keys, txTo[0], 0);
86 BOOST_CHECK(VerifyScript(s, a_and_b, flags, MutableTransactionSignatureChecker(&txTo[0], 0), &err));
87 BOOST_CHECK_MESSAGE(err == SCRIPT_ERR_OK, ScriptErrorString(err));
89 for (int i = 0; i < 4; i++)
91 keys.assign(1,key[i]);
92 s = sign_multisig(a_and_b, keys, txTo[0], 0);
93 BOOST_CHECK_MESSAGE(!VerifyScript(s, a_and_b, flags, MutableTransactionSignatureChecker(&txTo[0], 0), &err), strprintf("a&b 1: %d", i));
94 BOOST_CHECK_MESSAGE(err == SCRIPT_ERR_INVALID_STACK_OPERATION, ScriptErrorString(err));
96 keys.assign(1,key[1]);
97 keys.push_back(key[i]);
98 s = sign_multisig(a_and_b, keys, txTo[0], 0);
99 BOOST_CHECK_MESSAGE(!VerifyScript(s, a_and_b, flags, MutableTransactionSignatureChecker(&txTo[0], 0), &err), strprintf("a&b 2: %d", i));
100 BOOST_CHECK_MESSAGE(err == SCRIPT_ERR_EVAL_FALSE, ScriptErrorString(err));
104 for (int i = 0; i < 4; i++)
106 keys.assign(1,key[i]);
107 s = sign_multisig(a_or_b, keys, txTo[1], 0);
108 if (i == 0 || i == 1)
110 BOOST_CHECK_MESSAGE(VerifyScript(s, a_or_b, flags, MutableTransactionSignatureChecker(&txTo[1], 0), &err), strprintf("a|b: %d", i));
111 BOOST_CHECK_MESSAGE(err == SCRIPT_ERR_OK, ScriptErrorString(err));
115 BOOST_CHECK_MESSAGE(!VerifyScript(s, a_or_b, flags, MutableTransactionSignatureChecker(&txTo[1], 0), &err), strprintf("a|b: %d", i));
116 BOOST_CHECK_MESSAGE(err == SCRIPT_ERR_EVAL_FALSE, ScriptErrorString(err));
121 BOOST_CHECK(!VerifyScript(s, a_or_b, flags, MutableTransactionSignatureChecker(&txTo[1], 0), &err));
122 BOOST_CHECK_MESSAGE(err == SCRIPT_ERR_SIG_DER, ScriptErrorString(err));
125 for (int i = 0; i < 4; i++)
126 for (int j = 0; j < 4; j++)
128 keys.assign(1,key[i]);
129 keys.push_back(key[j]);
130 s = sign_multisig(escrow, keys, txTo[2], 0);
131 if (i < j && i < 3 && j < 3)
133 BOOST_CHECK_MESSAGE(VerifyScript(s, escrow, flags, MutableTransactionSignatureChecker(&txTo[2], 0), &err), strprintf("escrow 1: %d %d", i, j));
134 BOOST_CHECK_MESSAGE(err == SCRIPT_ERR_OK, ScriptErrorString(err));
138 BOOST_CHECK_MESSAGE(!VerifyScript(s, escrow, flags, MutableTransactionSignatureChecker(&txTo[2], 0), &err), strprintf("escrow 2: %d %d", i, j));
139 BOOST_CHECK_MESSAGE(err == SCRIPT_ERR_EVAL_FALSE, ScriptErrorString(err));
144 BOOST_AUTO_TEST_CASE(multisig_IsStandard)
147 for (int i = 0; i < 4; i++)
148 key[i].MakeNewKey(true);
150 txnouttype whichType;
153 a_and_b << OP_2 << ToByteVector(key[0].GetPubKey()) << ToByteVector(key[1].GetPubKey()) << OP_2 << OP_CHECKMULTISIG;
154 BOOST_CHECK(::IsStandard(a_and_b, whichType));
157 a_or_b << OP_1 << ToByteVector(key[0].GetPubKey()) << ToByteVector(key[1].GetPubKey()) << OP_2 << OP_CHECKMULTISIG;
158 BOOST_CHECK(::IsStandard(a_or_b, whichType));
161 escrow << OP_2 << ToByteVector(key[0].GetPubKey()) << ToByteVector(key[1].GetPubKey()) << ToByteVector(key[2].GetPubKey()) << OP_3 << OP_CHECKMULTISIG;
162 BOOST_CHECK(::IsStandard(escrow, whichType));
165 one_of_four << OP_1 << ToByteVector(key[0].GetPubKey()) << ToByteVector(key[1].GetPubKey()) << ToByteVector(key[2].GetPubKey()) << ToByteVector(key[3].GetPubKey()) << OP_4 << OP_CHECKMULTISIG;
166 BOOST_CHECK(!::IsStandard(one_of_four, whichType));
168 CScript malformed[6];
169 malformed[0] << OP_3 << ToByteVector(key[0].GetPubKey()) << ToByteVector(key[1].GetPubKey()) << OP_2 << OP_CHECKMULTISIG;
170 malformed[1] << OP_2 << ToByteVector(key[0].GetPubKey()) << ToByteVector(key[1].GetPubKey()) << OP_3 << OP_CHECKMULTISIG;
171 malformed[2] << OP_0 << ToByteVector(key[0].GetPubKey()) << ToByteVector(key[1].GetPubKey()) << OP_2 << OP_CHECKMULTISIG;
172 malformed[3] << OP_1 << ToByteVector(key[0].GetPubKey()) << ToByteVector(key[1].GetPubKey()) << OP_0 << OP_CHECKMULTISIG;
173 malformed[4] << OP_1 << ToByteVector(key[0].GetPubKey()) << ToByteVector(key[1].GetPubKey()) << OP_CHECKMULTISIG;
174 malformed[5] << OP_1 << ToByteVector(key[0].GetPubKey()) << ToByteVector(key[1].GetPubKey());
176 for (int i = 0; i < 6; i++)
177 BOOST_CHECK(!::IsStandard(malformed[i], whichType));
180 BOOST_AUTO_TEST_CASE(multisig_Solver1)
182 // Tests Solver() that returns lists of keys that are
183 // required to satisfy a ScriptPubKey
185 // Also tests IsMine() and ExtractDestination()
187 // Note: ExtractDestination for the multisignature transactions
188 // always returns false for this release, even if you have
189 // one key that would satisfy an (a|b) or 2-of-3 keys needed
190 // to spend an escrow transaction.
192 CBasicKeyStore keystore, emptykeystore, partialkeystore;
194 CTxDestination keyaddr[3];
195 for (int i = 0; i < 3; i++)
197 key[i].MakeNewKey(true);
198 keystore.AddKey(key[i]);
199 keyaddr[i] = key[i].GetPubKey().GetID();
201 partialkeystore.AddKey(key[0]);
204 vector<valtype> solutions;
205 txnouttype whichType;
207 s << ToByteVector(key[0].GetPubKey()) << OP_CHECKSIG;
208 BOOST_CHECK(Solver(s, whichType, solutions));
209 BOOST_CHECK(solutions.size() == 1);
211 BOOST_CHECK(ExtractDestination(s, addr));
212 BOOST_CHECK(addr == keyaddr[0]);
214 BOOST_CHECK(IsMine(keystore, s));
215 BOOST_CHECK(!IsMine(emptykeystore, s));
219 vector<valtype> solutions;
220 txnouttype whichType;
222 s << OP_DUP << OP_HASH160 << ToByteVector(key[0].GetPubKey().GetID()) << OP_EQUALVERIFY << OP_CHECKSIG;
223 BOOST_CHECK(Solver(s, whichType, solutions));
224 BOOST_CHECK(solutions.size() == 1);
226 BOOST_CHECK(ExtractDestination(s, addr));
227 BOOST_CHECK(addr == keyaddr[0]);
229 BOOST_CHECK(IsMine(keystore, s));
230 BOOST_CHECK(!IsMine(emptykeystore, s));
234 vector<valtype> solutions;
235 txnouttype whichType;
237 s << OP_2 << ToByteVector(key[0].GetPubKey()) << ToByteVector(key[1].GetPubKey()) << OP_2 << OP_CHECKMULTISIG;
238 BOOST_CHECK(Solver(s, whichType, solutions));
239 BOOST_CHECK_EQUAL(solutions.size(), 4U);
241 BOOST_CHECK(!ExtractDestination(s, addr));
243 BOOST_CHECK(IsMine(keystore, s));
244 BOOST_CHECK(!IsMine(emptykeystore, s));
245 BOOST_CHECK(!IsMine(partialkeystore, s));
249 vector<valtype> solutions;
250 txnouttype whichType;
252 s << OP_1 << ToByteVector(key[0].GetPubKey()) << ToByteVector(key[1].GetPubKey()) << OP_2 << OP_CHECKMULTISIG;
253 BOOST_CHECK(Solver(s, whichType, solutions));
254 BOOST_CHECK_EQUAL(solutions.size(), 4U);
255 vector<CTxDestination> addrs;
257 BOOST_CHECK(ExtractDestinations(s, whichType, addrs, nRequired));
258 BOOST_CHECK(addrs[0] == keyaddr[0]);
259 BOOST_CHECK(addrs[1] == keyaddr[1]);
260 BOOST_CHECK(nRequired == 1);
262 BOOST_CHECK(IsMine(keystore, s));
263 BOOST_CHECK(!IsMine(emptykeystore, s));
264 BOOST_CHECK(!IsMine(partialkeystore, s));
268 vector<valtype> solutions;
269 txnouttype whichType;
271 s << OP_2 << ToByteVector(key[0].GetPubKey()) << ToByteVector(key[1].GetPubKey()) << ToByteVector(key[2].GetPubKey()) << OP_3 << OP_CHECKMULTISIG;
272 BOOST_CHECK(Solver(s, whichType, solutions));
273 BOOST_CHECK(solutions.size() == 5);
277 BOOST_AUTO_TEST_CASE(multisig_Sign)
279 // Test SignSignature() (and therefore the version of Solver() that signs transactions)
280 CBasicKeyStore keystore;
282 for (int i = 0; i < 4; i++)
284 key[i].MakeNewKey(true);
285 keystore.AddKey(key[i]);
289 a_and_b << OP_2 << ToByteVector(key[0].GetPubKey()) << ToByteVector(key[1].GetPubKey()) << OP_2 << OP_CHECKMULTISIG;
292 a_or_b << OP_1 << ToByteVector(key[0].GetPubKey()) << ToByteVector(key[1].GetPubKey()) << OP_2 << OP_CHECKMULTISIG;
295 escrow << OP_2 << ToByteVector(key[0].GetPubKey()) << ToByteVector(key[1].GetPubKey()) << ToByteVector(key[2].GetPubKey()) << OP_3 << OP_CHECKMULTISIG;
297 CMutableTransaction txFrom; // Funding transaction
298 txFrom.vout.resize(3);
299 txFrom.vout[0].scriptPubKey = a_and_b;
300 txFrom.vout[1].scriptPubKey = a_or_b;
301 txFrom.vout[2].scriptPubKey = escrow;
303 CMutableTransaction txTo[3]; // Spending transaction
304 for (int i = 0; i < 3; i++)
306 txTo[i].vin.resize(1);
307 txTo[i].vout.resize(1);
308 txTo[i].vin[0].prevout.n = i;
309 txTo[i].vin[0].prevout.hash = txFrom.GetTxid();
310 txTo[i].vout[0].nValue = 1;
313 for (int i = 0; i < 3; i++)
315 BOOST_CHECK_MESSAGE(SignSignature(keystore, txFrom, txTo[i], 0), strprintf("SignSignature %d", i));
320 BOOST_AUTO_TEST_SUITE_END()