2018-01-02 18:12:05 +01:00
|
|
|
// Copyright (c) 2011-2017 The Bitcoin Core developers
|
2014-12-13 05:09:33 +01:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
2014-03-18 10:11:00 +01:00
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
2011-09-28 18:30:06 +02:00
|
|
|
|
2017-11-10 01:57:53 +01:00
|
|
|
#include <key.h>
|
|
|
|
#include <keystore.h>
|
|
|
|
#include <policy/policy.h>
|
|
|
|
#include <script/script.h>
|
|
|
|
#include <script/script_error.h>
|
|
|
|
#include <script/interpreter.h>
|
|
|
|
#include <script/sign.h>
|
|
|
|
#include <script/ismine.h>
|
|
|
|
#include <uint256.h>
|
|
|
|
#include <test/test_bitcoin.h>
|
2013-04-13 07:13:08 +02:00
|
|
|
|
2014-08-29 22:07:39 +02:00
|
|
|
|
2013-04-13 07:13:08 +02:00
|
|
|
#include <boost/test/unit_test.hpp>
|
2011-09-28 18:30:06 +02:00
|
|
|
|
2015-03-12 09:34:42 +01:00
|
|
|
BOOST_FIXTURE_TEST_SUITE(multisig_tests, BasicTestingSetup)
|
2011-09-28 18:30:06 +02:00
|
|
|
|
|
|
|
CScript
|
2016-12-05 08:03:53 +01:00
|
|
|
sign_multisig(CScript scriptPubKey, std::vector<CKey> keys, CTransaction transaction, int whichIn)
|
2011-09-28 18:30:06 +02:00
|
|
|
{
|
2015-12-27 19:49:08 +01:00
|
|
|
uint256 hash = SignatureHash(scriptPubKey, transaction, whichIn, SIGHASH_ALL, 0, SIGVERSION_BASE);
|
2011-09-28 18:30:06 +02:00
|
|
|
|
|
|
|
CScript result;
|
|
|
|
result << OP_0; // CHECKMULTISIG bug workaround
|
2017-06-02 03:18:57 +02:00
|
|
|
for (const CKey &key : keys)
|
2011-09-28 18:30:06 +02:00
|
|
|
{
|
2016-12-05 08:03:53 +01:00
|
|
|
std::vector<unsigned char> vchSig;
|
2011-09-28 18:30:06 +02:00
|
|
|
BOOST_CHECK(key.Sign(hash, vchSig));
|
|
|
|
vchSig.push_back((unsigned char)SIGHASH_ALL);
|
|
|
|
result << vchSig;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(multisig_verify)
|
|
|
|
{
|
2012-11-13 23:03:25 +01:00
|
|
|
unsigned int flags = SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_STRICTENC;
|
|
|
|
|
2014-11-13 20:27:38 +01:00
|
|
|
ScriptError err;
|
2011-09-28 18:30:06 +02:00
|
|
|
CKey key[4];
|
2016-03-31 14:51:29 +02:00
|
|
|
CAmount amount = 0;
|
2011-09-28 18:30:06 +02:00
|
|
|
for (int i = 0; i < 4; i++)
|
2012-02-20 18:32:33 +01:00
|
|
|
key[i].MakeNewKey(true);
|
2011-09-28 18:30:06 +02:00
|
|
|
|
|
|
|
CScript a_and_b;
|
2014-09-25 04:54:08 +02:00
|
|
|
a_and_b << OP_2 << ToByteVector(key[0].GetPubKey()) << ToByteVector(key[1].GetPubKey()) << OP_2 << OP_CHECKMULTISIG;
|
2011-09-28 18:30:06 +02:00
|
|
|
|
|
|
|
CScript a_or_b;
|
2014-09-25 04:54:08 +02:00
|
|
|
a_or_b << OP_1 << ToByteVector(key[0].GetPubKey()) << ToByteVector(key[1].GetPubKey()) << OP_2 << OP_CHECKMULTISIG;
|
2011-09-28 18:30:06 +02:00
|
|
|
|
|
|
|
CScript escrow;
|
2014-09-25 04:54:08 +02:00
|
|
|
escrow << OP_2 << ToByteVector(key[0].GetPubKey()) << ToByteVector(key[1].GetPubKey()) << ToByteVector(key[2].GetPubKey()) << OP_3 << OP_CHECKMULTISIG;
|
2011-09-28 18:30:06 +02:00
|
|
|
|
2014-06-07 13:53:27 +02:00
|
|
|
CMutableTransaction txFrom; // Funding transaction
|
2011-09-28 18:30:06 +02:00
|
|
|
txFrom.vout.resize(3);
|
|
|
|
txFrom.vout[0].scriptPubKey = a_and_b;
|
|
|
|
txFrom.vout[1].scriptPubKey = a_or_b;
|
|
|
|
txFrom.vout[2].scriptPubKey = escrow;
|
|
|
|
|
2014-06-07 13:53:27 +02:00
|
|
|
CMutableTransaction txTo[3]; // Spending transaction
|
2011-09-28 18:30:06 +02:00
|
|
|
for (int i = 0; i < 3; i++)
|
|
|
|
{
|
|
|
|
txTo[i].vin.resize(1);
|
|
|
|
txTo[i].vout.resize(1);
|
|
|
|
txTo[i].vin[0].prevout.n = i;
|
|
|
|
txTo[i].vin[0].prevout.hash = txFrom.GetHash();
|
|
|
|
txTo[i].vout[0].nValue = 1;
|
|
|
|
}
|
|
|
|
|
2016-12-05 08:03:53 +01:00
|
|
|
std::vector<CKey> keys;
|
2011-09-28 18:30:06 +02:00
|
|
|
CScript s;
|
|
|
|
|
|
|
|
// Test a AND b:
|
2014-12-19 22:49:00 +01:00
|
|
|
keys.assign(1,key[0]);
|
|
|
|
keys.push_back(key[1]);
|
2011-09-28 18:30:06 +02:00
|
|
|
s = sign_multisig(a_and_b, keys, txTo[0], 0);
|
2017-08-07 07:36:37 +02:00
|
|
|
BOOST_CHECK(VerifyScript(s, a_and_b, nullptr, flags, MutableTransactionSignatureChecker(&txTo[0], 0, amount), &err));
|
2014-11-13 20:27:38 +01:00
|
|
|
BOOST_CHECK_MESSAGE(err == SCRIPT_ERR_OK, ScriptErrorString(err));
|
2011-09-28 18:30:06 +02:00
|
|
|
|
|
|
|
for (int i = 0; i < 4; i++)
|
|
|
|
{
|
2014-12-19 22:49:00 +01:00
|
|
|
keys.assign(1,key[i]);
|
2011-09-28 18:30:06 +02:00
|
|
|
s = sign_multisig(a_and_b, keys, txTo[0], 0);
|
2017-08-07 07:36:37 +02:00
|
|
|
BOOST_CHECK_MESSAGE(!VerifyScript(s, a_and_b, nullptr, flags, MutableTransactionSignatureChecker(&txTo[0], 0, amount), &err), strprintf("a&b 1: %d", i));
|
2014-11-13 20:27:38 +01:00
|
|
|
BOOST_CHECK_MESSAGE(err == SCRIPT_ERR_INVALID_STACK_OPERATION, ScriptErrorString(err));
|
2011-09-28 18:30:06 +02:00
|
|
|
|
2014-12-19 22:49:00 +01:00
|
|
|
keys.assign(1,key[1]);
|
|
|
|
keys.push_back(key[i]);
|
2011-09-28 18:30:06 +02:00
|
|
|
s = sign_multisig(a_and_b, keys, txTo[0], 0);
|
2017-08-07 07:36:37 +02:00
|
|
|
BOOST_CHECK_MESSAGE(!VerifyScript(s, a_and_b, nullptr, flags, MutableTransactionSignatureChecker(&txTo[0], 0, amount), &err), strprintf("a&b 2: %d", i));
|
2014-11-13 20:27:38 +01:00
|
|
|
BOOST_CHECK_MESSAGE(err == SCRIPT_ERR_EVAL_FALSE, ScriptErrorString(err));
|
2011-09-28 18:30:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Test a OR b:
|
|
|
|
for (int i = 0; i < 4; i++)
|
|
|
|
{
|
2014-12-19 22:49:00 +01:00
|
|
|
keys.assign(1,key[i]);
|
2011-09-28 18:30:06 +02:00
|
|
|
s = sign_multisig(a_or_b, keys, txTo[1], 0);
|
|
|
|
if (i == 0 || i == 1)
|
2014-11-13 20:27:38 +01:00
|
|
|
{
|
2017-08-07 07:36:37 +02:00
|
|
|
BOOST_CHECK_MESSAGE(VerifyScript(s, a_or_b, nullptr, flags, MutableTransactionSignatureChecker(&txTo[1], 0, amount), &err), strprintf("a|b: %d", i));
|
2014-11-13 20:27:38 +01:00
|
|
|
BOOST_CHECK_MESSAGE(err == SCRIPT_ERR_OK, ScriptErrorString(err));
|
|
|
|
}
|
2011-09-28 18:30:06 +02:00
|
|
|
else
|
2014-11-13 20:27:38 +01:00
|
|
|
{
|
2017-08-07 07:36:37 +02:00
|
|
|
BOOST_CHECK_MESSAGE(!VerifyScript(s, a_or_b, nullptr, flags, MutableTransactionSignatureChecker(&txTo[1], 0, amount), &err), strprintf("a|b: %d", i));
|
2014-11-13 20:27:38 +01:00
|
|
|
BOOST_CHECK_MESSAGE(err == SCRIPT_ERR_EVAL_FALSE, ScriptErrorString(err));
|
|
|
|
}
|
2011-09-28 18:30:06 +02:00
|
|
|
}
|
|
|
|
s.clear();
|
|
|
|
s << OP_0 << OP_1;
|
2017-08-07 07:36:37 +02:00
|
|
|
BOOST_CHECK(!VerifyScript(s, a_or_b, nullptr, flags, MutableTransactionSignatureChecker(&txTo[1], 0, amount), &err));
|
2014-11-13 20:27:38 +01:00
|
|
|
BOOST_CHECK_MESSAGE(err == SCRIPT_ERR_SIG_DER, ScriptErrorString(err));
|
2011-09-28 18:30:06 +02:00
|
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < 4; i++)
|
|
|
|
for (int j = 0; j < 4; j++)
|
|
|
|
{
|
2014-12-19 22:49:00 +01:00
|
|
|
keys.assign(1,key[i]);
|
|
|
|
keys.push_back(key[j]);
|
2011-09-28 18:30:06 +02:00
|
|
|
s = sign_multisig(escrow, keys, txTo[2], 0);
|
|
|
|
if (i < j && i < 3 && j < 3)
|
2014-11-13 20:27:38 +01:00
|
|
|
{
|
2017-08-07 07:36:37 +02:00
|
|
|
BOOST_CHECK_MESSAGE(VerifyScript(s, escrow, nullptr, flags, MutableTransactionSignatureChecker(&txTo[2], 0, amount), &err), strprintf("escrow 1: %d %d", i, j));
|
2014-11-13 20:27:38 +01:00
|
|
|
BOOST_CHECK_MESSAGE(err == SCRIPT_ERR_OK, ScriptErrorString(err));
|
|
|
|
}
|
2011-09-28 18:30:06 +02:00
|
|
|
else
|
2014-11-13 20:27:38 +01:00
|
|
|
{
|
2017-08-07 07:36:37 +02:00
|
|
|
BOOST_CHECK_MESSAGE(!VerifyScript(s, escrow, nullptr, flags, MutableTransactionSignatureChecker(&txTo[2], 0, amount), &err), strprintf("escrow 2: %d %d", i, j));
|
2014-11-13 20:27:38 +01:00
|
|
|
BOOST_CHECK_MESSAGE(err == SCRIPT_ERR_EVAL_FALSE, ScriptErrorString(err));
|
|
|
|
}
|
2011-09-28 18:30:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(multisig_IsStandard)
|
|
|
|
{
|
2011-10-03 19:05:43 +02:00
|
|
|
CKey key[4];
|
|
|
|
for (int i = 0; i < 4; i++)
|
2012-02-20 18:32:33 +01:00
|
|
|
key[i].MakeNewKey(true);
|
2011-09-28 18:30:06 +02:00
|
|
|
|
2013-06-24 21:09:50 +02:00
|
|
|
txnouttype whichType;
|
|
|
|
|
2011-09-28 18:30:06 +02:00
|
|
|
CScript a_and_b;
|
2014-09-25 04:54:08 +02:00
|
|
|
a_and_b << OP_2 << ToByteVector(key[0].GetPubKey()) << ToByteVector(key[1].GetPubKey()) << OP_2 << OP_CHECKMULTISIG;
|
2013-06-24 21:09:50 +02:00
|
|
|
BOOST_CHECK(::IsStandard(a_and_b, whichType));
|
2011-09-28 18:30:06 +02:00
|
|
|
|
|
|
|
CScript a_or_b;
|
2014-09-25 04:54:08 +02:00
|
|
|
a_or_b << OP_1 << ToByteVector(key[0].GetPubKey()) << ToByteVector(key[1].GetPubKey()) << OP_2 << OP_CHECKMULTISIG;
|
2013-06-24 21:09:50 +02:00
|
|
|
BOOST_CHECK(::IsStandard(a_or_b, whichType));
|
2011-09-28 18:30:06 +02:00
|
|
|
|
|
|
|
CScript escrow;
|
2014-09-25 04:54:08 +02:00
|
|
|
escrow << OP_2 << ToByteVector(key[0].GetPubKey()) << ToByteVector(key[1].GetPubKey()) << ToByteVector(key[2].GetPubKey()) << OP_3 << OP_CHECKMULTISIG;
|
2013-06-24 21:09:50 +02:00
|
|
|
BOOST_CHECK(::IsStandard(escrow, whichType));
|
2011-10-03 19:05:43 +02:00
|
|
|
|
|
|
|
CScript one_of_four;
|
2014-09-25 04:54:08 +02:00
|
|
|
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;
|
2013-06-24 21:09:50 +02:00
|
|
|
BOOST_CHECK(!::IsStandard(one_of_four, whichType));
|
2011-10-03 19:05:43 +02:00
|
|
|
|
|
|
|
CScript malformed[6];
|
2014-09-25 04:54:08 +02:00
|
|
|
malformed[0] << OP_3 << ToByteVector(key[0].GetPubKey()) << ToByteVector(key[1].GetPubKey()) << OP_2 << OP_CHECKMULTISIG;
|
|
|
|
malformed[1] << OP_2 << ToByteVector(key[0].GetPubKey()) << ToByteVector(key[1].GetPubKey()) << OP_3 << OP_CHECKMULTISIG;
|
|
|
|
malformed[2] << OP_0 << ToByteVector(key[0].GetPubKey()) << ToByteVector(key[1].GetPubKey()) << OP_2 << OP_CHECKMULTISIG;
|
|
|
|
malformed[3] << OP_1 << ToByteVector(key[0].GetPubKey()) << ToByteVector(key[1].GetPubKey()) << OP_0 << OP_CHECKMULTISIG;
|
|
|
|
malformed[4] << OP_1 << ToByteVector(key[0].GetPubKey()) << ToByteVector(key[1].GetPubKey()) << OP_CHECKMULTISIG;
|
|
|
|
malformed[5] << OP_1 << ToByteVector(key[0].GetPubKey()) << ToByteVector(key[1].GetPubKey());
|
2011-10-03 19:05:43 +02:00
|
|
|
|
|
|
|
for (int i = 0; i < 6; i++)
|
2013-06-24 21:09:50 +02:00
|
|
|
BOOST_CHECK(!::IsStandard(malformed[i], whichType));
|
2011-09-28 18:30:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(multisig_Sign)
|
|
|
|
{
|
|
|
|
// Test SignSignature() (and therefore the version of Solver() that signs transactions)
|
|
|
|
CBasicKeyStore keystore;
|
|
|
|
CKey key[4];
|
|
|
|
for (int i = 0; i < 4; i++)
|
|
|
|
{
|
2012-02-20 18:32:33 +01:00
|
|
|
key[i].MakeNewKey(true);
|
2011-09-28 18:30:06 +02:00
|
|
|
keystore.AddKey(key[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
CScript a_and_b;
|
2014-09-25 04:54:08 +02:00
|
|
|
a_and_b << OP_2 << ToByteVector(key[0].GetPubKey()) << ToByteVector(key[1].GetPubKey()) << OP_2 << OP_CHECKMULTISIG;
|
2011-09-28 18:30:06 +02:00
|
|
|
|
|
|
|
CScript a_or_b;
|
2014-09-25 04:54:08 +02:00
|
|
|
a_or_b << OP_1 << ToByteVector(key[0].GetPubKey()) << ToByteVector(key[1].GetPubKey()) << OP_2 << OP_CHECKMULTISIG;
|
2011-09-28 18:30:06 +02:00
|
|
|
|
|
|
|
CScript escrow;
|
2014-09-25 04:54:08 +02:00
|
|
|
escrow << OP_2 << ToByteVector(key[0].GetPubKey()) << ToByteVector(key[1].GetPubKey()) << ToByteVector(key[2].GetPubKey()) << OP_3 << OP_CHECKMULTISIG;
|
2011-09-28 18:30:06 +02:00
|
|
|
|
2014-06-07 13:53:27 +02:00
|
|
|
CMutableTransaction txFrom; // Funding transaction
|
2011-09-28 18:30:06 +02:00
|
|
|
txFrom.vout.resize(3);
|
|
|
|
txFrom.vout[0].scriptPubKey = a_and_b;
|
|
|
|
txFrom.vout[1].scriptPubKey = a_or_b;
|
|
|
|
txFrom.vout[2].scriptPubKey = escrow;
|
|
|
|
|
2014-06-07 13:53:27 +02:00
|
|
|
CMutableTransaction txTo[3]; // Spending transaction
|
2011-09-28 18:30:06 +02:00
|
|
|
for (int i = 0; i < 3; i++)
|
|
|
|
{
|
|
|
|
txTo[i].vin.resize(1);
|
|
|
|
txTo[i].vout.resize(1);
|
|
|
|
txTo[i].vin[0].prevout.n = i;
|
|
|
|
txTo[i].vin[0].prevout.hash = txFrom.GetHash();
|
|
|
|
txTo[i].vout[0].nValue = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < 3; i++)
|
|
|
|
{
|
2016-03-31 14:54:58 +02:00
|
|
|
BOOST_CHECK_MESSAGE(SignSignature(keystore, txFrom, txTo[i], 0, SIGHASH_ALL), strprintf("SignSignature %d", i));
|
2011-09-28 18:30:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_SUITE_END()
|