2015-12-13 17:58:29 +01:00
|
|
|
// Copyright (c) 2013-2015 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.
|
|
|
|
|
2015-01-24 15:57:12 +01:00
|
|
|
#include "consensus/validation.h"
|
2014-06-26 14:41:53 +02:00
|
|
|
#include "data/sighash.json.h"
|
2015-07-05 14:17:46 +02:00
|
|
|
#include "hash.h"
|
2016-12-02 01:06:41 +01:00
|
|
|
#include "validation.h" // For CheckTransaction
|
2014-08-27 20:11:41 +02:00
|
|
|
#include "script/interpreter.h"
|
2015-01-24 15:57:12 +01:00
|
|
|
#include "script/script.h"
|
|
|
|
#include "serialize.h"
|
2015-07-05 14:17:46 +02:00
|
|
|
#include "streams.h"
|
2015-01-24 15:57:12 +01:00
|
|
|
#include "test/test_bitcoin.h"
|
2016-11-07 15:04:57 +01:00
|
|
|
#include "test/test_random.h"
|
2014-06-26 14:41:53 +02:00
|
|
|
#include "util.h"
|
2015-07-05 14:17:46 +02:00
|
|
|
#include "utilstrencodings.h"
|
2014-03-28 20:28:13 +01:00
|
|
|
#include "version.h"
|
|
|
|
|
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())
|
|
|
|
{
|
2015-01-08 11:44:25 +01:00
|
|
|
printf("ERROR: SignatureHash(): nIn=%d out of range\n", nIn);
|
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())
|
|
|
|
{
|
2015-01-08 11:44:25 +01:00
|
|
|
printf("ERROR: SignatureHash(): nOut=%d out of range\n", nOut);
|
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();
|
|
|
|
int ops = (insecure_rand() % 10);
|
|
|
|
for (int i=0; i<ops; i++)
|
|
|
|
script << oplist[insecure_rand() % (sizeof(oplist)/sizeof(oplist[0]))];
|
|
|
|
}
|
|
|
|
|
2014-06-07 13:53:27 +02:00
|
|
|
void static RandomTransaction(CMutableTransaction &tx, bool fSingle) {
|
2013-05-04 19:32:33 +02:00
|
|
|
tx.nVersion = insecure_rand();
|
|
|
|
tx.vin.clear();
|
|
|
|
tx.vout.clear();
|
|
|
|
tx.nLockTime = (insecure_rand() % 2) ? insecure_rand() : 0;
|
|
|
|
int ins = (insecure_rand() % 4) + 1;
|
|
|
|
int outs = fSingle ? ins : (insecure_rand() % 4) + 1;
|
|
|
|
for (int in = 0; in < ins; in++) {
|
|
|
|
tx.vin.push_back(CTxIn());
|
|
|
|
CTxIn &txin = tx.vin.back();
|
|
|
|
txin.prevout.hash = GetRandHash();
|
|
|
|
txin.prevout.n = insecure_rand() % 4;
|
|
|
|
RandomScript(txin.scriptSig);
|
|
|
|
txin.nSequence = (insecure_rand() % 2) ? insecure_rand() : (unsigned int)-1;
|
|
|
|
}
|
|
|
|
for (int out = 0; out < outs; out++) {
|
|
|
|
tx.vout.push_back(CTxOut());
|
|
|
|
CTxOut &txout = tx.vout.back();
|
|
|
|
txout.nValue = insecure_rand() % 100000000;
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
seed_insecure_rand(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";
|
|
|
|
#endif
|
|
|
|
int nRandomTests = 50000;
|
|
|
|
|
|
|
|
#if defined(PRINT_SIGHASH_JSON)
|
|
|
|
nRandomTests = 500;
|
|
|
|
#endif
|
|
|
|
for (int i=0; i<nRandomTests; i++) {
|
2013-05-04 19:32:33 +02:00
|
|
|
int nHashType = insecure_rand();
|
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);
|
|
|
|
int nIn = insecure_rand() % 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()
|