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