You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
178 lines
5.7 KiB
178 lines
5.7 KiB
// Copyright (c) 2013 The Bitcoin Core developers |
|
// Distributed under the MIT/X11 software license, see the accompanying |
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php. |
|
|
|
#include <boost/test/unit_test.hpp> |
|
#include <iostream> |
|
|
|
#include "main.h" |
|
#include "util.h" |
|
#include "serialize.h" |
|
#include "version.h" |
|
#include "data/sighash.json.h" |
|
|
|
#include "json/json_spirit_reader_template.h" |
|
#include "json/json_spirit_utils.h" |
|
#include "json/json_spirit_writer_template.h" |
|
|
|
using namespace json_spirit; |
|
extern Array read_json(const std::string& jsondata); |
|
|
|
extern uint256 SignatureHash(const CScript &scriptCode, const CTransaction& txTo, unsigned int nIn, int nHashType); |
|
|
|
// Old script.cpp SignatureHash function |
|
uint256 static SignatureHashOld(CScript scriptCode, const CTransaction& txTo, unsigned int nIn, int nHashType) |
|
{ |
|
if (nIn >= txTo.vin.size()) |
|
{ |
|
printf("ERROR: SignatureHash() : nIn=%d out of range\n", nIn); |
|
return 1; |
|
} |
|
CTransaction txTmp(txTo); |
|
|
|
// 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()) |
|
{ |
|
printf("ERROR: SignatureHash() : nOut=%d out of range\n", nOut); |
|
return 1; |
|
} |
|
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.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]))]; |
|
} |
|
|
|
void static RandomTransaction(CTransaction &tx, bool fSingle) { |
|
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); |
|
} |
|
} |
|
|
|
BOOST_AUTO_TEST_SUITE(sighash_tests) |
|
BOOST_AUTO_TEST_CASE(sighash_test) |
|
{ |
|
seed_insecure_rand(false); |
|
|
|
for (int i=0; i<50000; i++) { |
|
int nHashType = insecure_rand(); |
|
CTransaction txTo; |
|
RandomTransaction(txTo, (nHashType & 0x1f) == SIGHASH_SINGLE); |
|
CScript scriptCode; |
|
RandomScript(scriptCode); |
|
int nIn = insecure_rand() % txTo.vin.size(); |
|
|
|
uint256 sh, sho; |
|
sho = SignatureHashOld(scriptCode, txTo, nIn, nHashType); |
|
sh = SignatureHash(scriptCode, txTo, nIn, nHashType); |
|
BOOST_CHECK(sh == sho); |
|
} |
|
} |
|
|
|
// Goal: check that SignatureHash generates correct hash |
|
|
|
BOOST_AUTO_TEST_CASE(sighash_from_data) |
|
{ |
|
Array tests = read_json(std::string(json_tests::sighash, json_tests::sighash + sizeof(json_tests::sighash))); |
|
|
|
BOOST_FOREACH(Value& tv, tests) |
|
{ |
|
Array test = tv.get_array(); |
|
std::string strTest = write_string(tv, false); |
|
if (test.size() < 1) // Allow for extra stuff (useful for comments) |
|
{ |
|
BOOST_ERROR("Bad test: " << strTest); |
|
continue; |
|
} |
|
if (test.size() == 1) continue; // comment |
|
|
|
std::string raw_tx = test[0].get_str(); |
|
std::string raw_script = test[1].get_str(); |
|
int nIn = test[2].get_int(); |
|
int nHashType = test[3].get_int(); |
|
std::string sigHashHex = test[4].get_str(); |
|
|
|
uint256 sh; |
|
CDataStream stream(ParseHex(raw_tx), SER_NETWORK, PROTOCOL_VERSION); |
|
CTransaction tx; |
|
stream >> tx; |
|
|
|
CValidationState state; |
|
BOOST_CHECK_MESSAGE(CheckTransaction(tx, state), strTest); |
|
BOOST_CHECK(state.IsValid()); |
|
|
|
CScript scriptCode = CScript(); |
|
std::vector<unsigned char> raw = ParseHex(raw_script); |
|
scriptCode.insert(scriptCode.end(), raw.begin(), raw.end()); |
|
|
|
sh = SignatureHash(scriptCode, tx, nIn, nHashType); |
|
|
|
BOOST_CHECK_MESSAGE(sh.GetHex() == sigHashHex, strTest); |
|
} |
|
} |
|
BOOST_AUTO_TEST_SUITE_END() |
|
|
|
|