2016-12-31 19:01:21 +01:00
|
|
|
// Copyright (c) 2013-2016 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.
|
|
|
|
|
2017-11-10 01:57:53 +01:00
|
|
|
#include <consensus/tx_verify.h>
|
|
|
|
#include <consensus/validation.h>
|
2017-08-15 21:22:09 +02:00
|
|
|
#include <test/data/sighash.json.h>
|
2017-11-10 01:57:53 +01:00
|
|
|
#include <hash.h>
|
|
|
|
#include <script/interpreter.h>
|
|
|
|
#include <script/script.h>
|
|
|
|
#include <serialize.h>
|
|
|
|
#include <streams.h>
|
|
|
|
#include <test/test_bitcoin.h>
|
|
|
|
#include <util.h>
|
|
|
|
#include <utilstrencodings.h>
|
|
|
|
#include <version.h>
|
2014-03-28 20:28:13 +01:00
|
|
|
|
2014-06-26 14:41:53 +02:00
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
#include <boost/test/unit_test.hpp>
|
2014-03-28 20:28:13 +01:00
|
|
|
|
2015-09-04 16:11:34 +02:00
|
|
|
#include <univalue.h>
|
2015-05-18 14:02:18 +02:00
|
|
|
|
2015-05-13 21:29:19 +02:00
|
|
|
extern UniValue read_json(const std::string& jsondata);
|
2013-05-04 19:32:33 +02:00
|
|
|
|
|
|
|
// Old script.cpp SignatureHash function
|
|
|
|
uint256 static SignatureHashOld(CScript scriptCode, const CTransaction& txTo, unsigned int nIn, int nHashType)
|
|
|
|
{
|
2014-12-16 14:50:05 +01:00
|
|
|
static const uint256 one(uint256S("0000000000000000000000000000000000000000000000000000000000000001"));
|
2013-05-04 19:32:33 +02:00
|
|
|
if (nIn >= txTo.vin.size())
|
|
|
|
{
|
2014-12-15 09:17:25 +01:00
|
|
|
return one;
|
2013-05-04 19:32:33 +02:00
|
|
|
}
|
2014-06-07 13:53:27 +02:00
|
|
|
CMutableTransaction txTmp(txTo);
|
2013-05-04 19:32:33 +02:00
|
|
|
|
|
|
|
// In case concatenating two scripts ends up with two codeseparators,
|
|
|
|
// or an extra one at the end, this prevents all those possible incompatibilities.
|
|
|
|
scriptCode.FindAndDelete(CScript(OP_CODESEPARATOR));
|
|
|
|
|
|
|
|
// Blank out other inputs' signatures
|
|
|
|
for (unsigned int i = 0; i < txTmp.vin.size(); i++)
|
|
|
|
txTmp.vin[i].scriptSig = CScript();
|
|
|
|
txTmp.vin[nIn].scriptSig = scriptCode;
|
|
|
|
|
|
|
|
// Blank out some of the outputs
|
|
|
|
if ((nHashType & 0x1f) == SIGHASH_NONE)
|
|
|
|
{
|
|
|
|
// Wildcard payee
|
|
|
|
txTmp.vout.clear();
|
|
|
|
|
|
|
|
// Let the others update at will
|
|
|
|
for (unsigned int i = 0; i < txTmp.vin.size(); i++)
|
|
|
|
if (i != nIn)
|
|
|
|
txTmp.vin[i].nSequence = 0;
|
|
|
|
}
|
|
|
|
else if ((nHashType & 0x1f) == SIGHASH_SINGLE)
|
|
|
|
{
|
|
|
|
// Only lock-in the txout payee at same index as txin
|
|
|
|
unsigned int nOut = nIn;
|
|
|
|
if (nOut >= txTmp.vout.size())
|
|
|
|
{
|
2014-12-15 09:17:25 +01:00
|
|
|
return one;
|
2013-05-04 19:32:33 +02:00
|
|
|
}
|
|
|
|
txTmp.vout.resize(nOut+1);
|
|
|
|
for (unsigned int i = 0; i < nOut; i++)
|
|
|
|
txTmp.vout[i].SetNull();
|
|
|
|
|
|
|
|
// Let the others update at will
|
|
|
|
for (unsigned int i = 0; i < txTmp.vin.size(); i++)
|
|
|
|
if (i != nIn)
|
|
|
|
txTmp.vin[i].nSequence = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Blank out other inputs completely, not recommended for open transactions
|
|
|
|
if (nHashType & SIGHASH_ANYONECANPAY)
|
|
|
|
{
|
|
|
|
txTmp.vin[0] = txTmp.vin[nIn];
|
|
|
|
txTmp.vin.resize(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Serialize and hash
|
2015-11-06 01:32:04 +01:00
|
|
|
CHashWriter ss(SER_GETHASH, SERIALIZE_TRANSACTION_NO_WITNESS);
|
2013-05-04 19:32:33 +02:00
|
|
|
ss << txTmp << nHashType;
|
|
|
|
return ss.GetHash();
|
|
|
|
}
|
|
|
|
|
|
|
|
void static RandomScript(CScript &script) {
|
|
|
|
static const opcodetype oplist[] = {OP_FALSE, OP_1, OP_2, OP_3, OP_CHECKSIG, OP_IF, OP_VERIF, OP_RETURN, OP_CODESEPARATOR};
|
|
|
|
script = CScript();
|
2017-06-07 21:03:17 +02:00
|
|
|
int ops = (InsecureRandRange(10));
|
2013-05-04 19:32:33 +02:00
|
|
|
for (int i=0; i<ops; i++)
|
2017-06-07 21:03:17 +02:00
|
|
|
script << oplist[InsecureRandRange(sizeof(oplist)/sizeof(oplist[0]))];
|
2013-05-04 19:32:33 +02:00
|
|
|
}
|
|
|
|
|
2014-06-07 13:53:27 +02:00
|
|
|
void static RandomTransaction(CMutableTransaction &tx, bool fSingle) {
|
2017-06-07 21:03:17 +02:00
|
|
|
tx.nVersion = InsecureRand32();
|
2013-05-04 19:32:33 +02:00
|
|
|
tx.vin.clear();
|
|
|
|
tx.vout.clear();
|
2017-06-07 21:03:17 +02:00
|
|
|
tx.nLockTime = (InsecureRandBool()) ? InsecureRand32() : 0;
|
|
|
|
int ins = (InsecureRandBits(2)) + 1;
|
|
|
|
int outs = fSingle ? ins : (InsecureRandBits(2)) + 1;
|
2013-05-04 19:32:33 +02:00
|
|
|
for (int in = 0; in < ins; in++) {
|
|
|
|
tx.vin.push_back(CTxIn());
|
|
|
|
CTxIn &txin = tx.vin.back();
|
2017-06-07 21:03:17 +02:00
|
|
|
txin.prevout.hash = InsecureRand256();
|
|
|
|
txin.prevout.n = InsecureRandBits(2);
|
2013-05-04 19:32:33 +02:00
|
|
|
RandomScript(txin.scriptSig);
|
2017-06-07 21:03:17 +02:00
|
|
|
txin.nSequence = (InsecureRandBool()) ? InsecureRand32() : (unsigned int)-1;
|
2013-05-04 19:32:33 +02:00
|
|
|
}
|
|
|
|
for (int out = 0; out < outs; out++) {
|
|
|
|
tx.vout.push_back(CTxOut());
|
|
|
|
CTxOut &txout = tx.vout.back();
|
2017-06-07 21:03:17 +02:00
|
|
|
txout.nValue = InsecureRandRange(100000000);
|
2013-05-04 19:32:33 +02:00
|
|
|
RandomScript(txout.scriptPubKey);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-12 09:34:42 +01:00
|
|
|
BOOST_FIXTURE_TEST_SUITE(sighash_tests, BasicTestingSetup)
|
2014-03-31 17:39:32 +02:00
|
|
|
|
2013-05-04 19:32:33 +02:00
|
|
|
BOOST_AUTO_TEST_CASE(sighash_test)
|
|
|
|
{
|
2017-06-07 21:03:17 +02:00
|
|
|
SeedInsecureRand(false);
|
2014-06-26 14:41:53 +02:00
|
|
|
|
2014-03-31 17:39:32 +02:00
|
|
|
#if defined(PRINT_SIGHASH_JSON)
|
|
|
|
std::cout << "[\n";
|
|
|
|
std::cout << "\t[\"raw_transaction, script, input_index, hashType, signature_hash (result)\"],\n";
|
2017-07-09 13:05:01 +02:00
|
|
|
int nRandomTests = 500;
|
|
|
|
#else
|
2014-03-31 17:39:32 +02:00
|
|
|
int nRandomTests = 50000;
|
|
|
|
#endif
|
|
|
|
for (int i=0; i<nRandomTests; i++) {
|
2017-06-07 21:03:17 +02:00
|
|
|
int nHashType = InsecureRand32();
|
2014-06-07 13:53:27 +02:00
|
|
|
CMutableTransaction txTo;
|
2013-05-04 19:32:33 +02:00
|
|
|
RandomTransaction(txTo, (nHashType & 0x1f) == SIGHASH_SINGLE);
|
|
|
|
CScript scriptCode;
|
|
|
|
RandomScript(scriptCode);
|
2017-06-07 21:03:17 +02:00
|
|
|
int nIn = InsecureRandRange(txTo.vin.size());
|
2014-03-28 20:28:13 +01:00
|
|
|
|
|
|
|
uint256 sh, sho;
|
|
|
|
sho = SignatureHashOld(scriptCode, txTo, nIn, nHashType);
|
2015-12-27 19:49:08 +01:00
|
|
|
sh = SignatureHash(scriptCode, txTo, nIn, nHashType, 0, SIGVERSION_BASE);
|
2014-03-31 17:39:32 +02:00
|
|
|
#if defined(PRINT_SIGHASH_JSON)
|
|
|
|
CDataStream ss(SER_NETWORK, PROTOCOL_VERSION);
|
|
|
|
ss << txTo;
|
|
|
|
|
|
|
|
std::cout << "\t[\"" ;
|
|
|
|
std::cout << HexStr(ss.begin(), ss.end()) << "\", \"";
|
|
|
|
std::cout << HexStr(scriptCode) << "\", ";
|
|
|
|
std::cout << nIn << ", ";
|
|
|
|
std::cout << nHashType << ", \"";
|
|
|
|
std::cout << sho.GetHex() << "\"]";
|
|
|
|
if (i+1 != nRandomTests) {
|
|
|
|
std::cout << ",";
|
|
|
|
}
|
|
|
|
std::cout << "\n";
|
|
|
|
#endif
|
2014-03-28 20:28:13 +01:00
|
|
|
BOOST_CHECK(sh == sho);
|
2013-05-04 19:32:33 +02:00
|
|
|
}
|
2014-03-31 17:39:32 +02:00
|
|
|
#if defined(PRINT_SIGHASH_JSON)
|
|
|
|
std::cout << "]\n";
|
|
|
|
#endif
|
2013-05-04 19:32:33 +02:00
|
|
|
}
|
|
|
|
|
2014-03-28 20:28:13 +01:00
|
|
|
// Goal: check that SignatureHash generates correct hash
|
|
|
|
BOOST_AUTO_TEST_CASE(sighash_from_data)
|
|
|
|
{
|
2015-05-13 21:29:19 +02:00
|
|
|
UniValue tests = read_json(std::string(json_tests::sighash, json_tests::sighash + sizeof(json_tests::sighash)));
|
2014-03-28 20:28:13 +01:00
|
|
|
|
2014-08-20 21:15:16 +02:00
|
|
|
for (unsigned int idx = 0; idx < tests.size(); idx++) {
|
2015-05-13 21:29:19 +02:00
|
|
|
UniValue test = tests[idx];
|
2014-08-20 21:15:16 +02:00
|
|
|
std::string strTest = test.write();
|
2014-03-28 20:28:13 +01:00
|
|
|
if (test.size() < 1) // Allow for extra stuff (useful for comments)
|
|
|
|
{
|
|
|
|
BOOST_ERROR("Bad test: " << strTest);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (test.size() == 1) continue; // comment
|
|
|
|
|
2014-03-31 20:07:46 +02:00
|
|
|
std::string raw_tx, raw_script, sigHashHex;
|
|
|
|
int nIn, nHashType;
|
2014-03-28 20:28:13 +01:00
|
|
|
uint256 sh;
|
2016-11-12 01:23:17 +01:00
|
|
|
CTransactionRef tx;
|
2014-03-28 20:28:13 +01:00
|
|
|
CScript scriptCode = CScript();
|
|
|
|
|
2014-03-31 20:07:46 +02:00
|
|
|
try {
|
|
|
|
// deserialize test data
|
|
|
|
raw_tx = test[0].get_str();
|
|
|
|
raw_script = test[1].get_str();
|
|
|
|
nIn = test[2].get_int();
|
|
|
|
nHashType = test[3].get_int();
|
|
|
|
sigHashHex = test[4].get_str();
|
|
|
|
|
|
|
|
CDataStream stream(ParseHex(raw_tx), SER_NETWORK, PROTOCOL_VERSION);
|
|
|
|
stream >> tx;
|
|
|
|
|
|
|
|
CValidationState state;
|
2016-11-12 01:23:17 +01:00
|
|
|
BOOST_CHECK_MESSAGE(CheckTransaction(*tx, state), strTest);
|
2014-03-31 20:07:46 +02:00
|
|
|
BOOST_CHECK(state.IsValid());
|
|
|
|
|
|
|
|
std::vector<unsigned char> raw = ParseHex(raw_script);
|
|
|
|
scriptCode.insert(scriptCode.end(), raw.begin(), raw.end());
|
|
|
|
} catch (...) {
|
|
|
|
BOOST_ERROR("Bad test, couldn't deserialize data: " << strTest);
|
|
|
|
continue;
|
|
|
|
}
|
2014-06-26 14:41:53 +02:00
|
|
|
|
2016-11-12 01:23:17 +01:00
|
|
|
sh = SignatureHash(scriptCode, *tx, nIn, nHashType, 0, SIGVERSION_BASE);
|
2014-03-28 20:28:13 +01:00
|
|
|
BOOST_CHECK_MESSAGE(sh.GetHex() == sigHashHex, strTest);
|
|
|
|
}
|
|
|
|
}
|
2013-05-04 19:32:33 +02:00
|
|
|
BOOST_AUTO_TEST_SUITE_END()
|