Files

209 lines
6.4 KiB
C++
Raw Permalink Normal View History

2017-11-09 19:34:51 +01:00
/*
* This file is part of the Flowee project
* Copyright (C) 2013-2015 The Bitcoin Core developers
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
2014-03-18 10:11:00 +01:00
#include "data/sighash.json.h"
2015-07-05 14:17:46 +02:00
#include "hash.h"
2026-04-14 23:43:29 +02:00
#include "validation/Engine.h" // For checkTransaction
#include "random.h"
2014-08-27 20:11:41 +02:00
#include "script/interpreter.h"
#include "primitives/script.h"
#include "serialize.h"
#include "streaming/streams.h"
#include "test/test_bitcoin.h"
#include "transaction_utils.h"
2015-07-05 14:17:46 +02:00
#include "utilstrencodings.h"
2026-05-13 16:35:51 +02:00
#include <univalue.h>
2013-05-04 19:32:33 +02:00
#include <boost/test/unit_test.hpp>
2014-03-28 16:28:13 -03:00
2026-05-13 16:35:51 +02:00
#ifdef PRINT_SIGHASH_JSON
# include <iostream>
#endif
2022-12-10 00:22:40 +01:00
UniValue read_json(const std::string& jsondata)
{
UniValue v;
if (!v.read(jsondata) || !v.isArray())
{
BOOST_ERROR("Parse error.");
return UniValue(UniValue::VARR);
}
return v.get_array();
}
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
}
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
CHashWriter ss(SER_GETHASH, 0);
ss << txTmp << nHashType;
return ss.finalizeHash();
2013-05-04 19:32:33 +02:00
}
BOOST_FIXTURE_TEST_SUITE(sighash_tests, BasicTestingSetup)
2013-05-04 19:32:33 +02:00
BOOST_AUTO_TEST_CASE(sighash_test)
{
seed_insecure_rand(false);
#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;
2013-05-04 19:32:33 +02:00
#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();
2017-07-24 22:40:28 +02:00
// Clear forkid
nHashType &= ~SIGHASH_FORKID;
CMutableTransaction txTo;
2016-11-07 16:22:29 +01:00
TxUtils::RandomTransaction(txTo, (nHashType & 0x1f) == SIGHASH_SINGLE ? TxUtils::SingleOutput: TxUtils::AnyOutputCount);
2013-05-04 19:32:33 +02:00
CScript scriptCode;
TxUtils::RandomScript(scriptCode);
2013-05-04 19:32:33 +02:00
int nIn = insecure_rand() % txTo.vin.size();
2014-03-28 16:28:13 -03:00
uint256 sh, sho;
sho = SignatureHashOld(scriptCode, txTo, nIn, nHashType);
sh = SignatureHash(scriptCode, txTo, nIn, 0, nHashType);
#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 16:28:13 -03:00
BOOST_CHECK(sh == sho);
2013-05-04 19:32:33 +02:00
}
#if defined(PRINT_SIGHASH_JSON)
std::cout << "]\n";
#endif
2013-05-04 19:32:33 +02:00
}
2014-03-28 16:28:13 -03: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 16:28:13 -03:00
for (unsigned int idx = 0; idx < tests.size(); idx++) {
2015-05-13 21:29:19 +02:00
UniValue test = tests[idx];
std::string strTest = test.write();
2014-03-28 16:28:13 -03: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 15:07:46 -03:00
std::string raw_tx, raw_script, sigHashHex;
int nIn, nHashType;
2014-03-28 16:28:13 -03:00
uint256 sh;
CTransaction tx;
CScript scriptCode = CScript();
2014-03-31 15:07:46 -03: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();
uint256 sh;
CDataStream stream(ParseHex(raw_tx), SER_NETWORK, PROTOCOL_VERSION);
stream >> tx;
2026-04-14 23:43:29 +02:00
Validation::checkTransaction(Tx::fromOldTransaction(tx));
2014-03-31 15:07:46 -03:00
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;
}
sh = SignatureHash(scriptCode, tx, nIn, 0, nHashType);
2014-03-28 16:28:13 -03:00
BOOST_CHECK_MESSAGE(sh.GetHex() == sigHashHex, strTest);
}
}
2013-05-04 19:32:33 +02:00
BOOST_AUTO_TEST_SUITE_END()