Skip to content

Commit 438c7e4

Browse files
committed
Merge pull request #4555
6dcfda2 Don't pass nHashType to EvalScript nor CheckSig (jtimon) 2b23a87 Don't pass nHashType to VerifyScript (jtimon) ce3649f Remove CScriptCheck::nHashType (was always 0) (jtimon) 358562b Remove unused function main:VerifySignature (jtimon)
2 parents 5b9d1e1 + 6dcfda2 commit 438c7e4

File tree

11 files changed

+57
-70
lines changed

11 files changed

+57
-70
lines changed

src/bitcoin-tx.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ static void MutateTxSign(CMutableTransaction& tx, const string& flagStr)
435435
BOOST_FOREACH(const CTransaction& txv, txVariants) {
436436
txin.scriptSig = CombineSignatures(prevPubKey, mergedTx, i, txin.scriptSig, txv.vin[i].scriptSig);
437437
}
438-
if (!VerifyScript(txin.scriptSig, prevPubKey, mergedTx, i, STANDARD_SCRIPT_VERIFY_FLAGS, 0))
438+
if (!VerifyScript(txin.scriptSig, prevPubKey, mergedTx, i, STANDARD_SCRIPT_VERIFY_FLAGS))
439439
fComplete = false;
440440
}
441441

src/main.cpp

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -697,7 +697,7 @@ bool AreInputsStandard(const CTransaction& tx, const CCoinsViewCache& mapInputs)
697697
// IsStandard() will have already returned false
698698
// and this method isn't called.
699699
vector<vector<unsigned char> > stack;
700-
if (!EvalScript(stack, tx.vin[i].scriptSig, tx, i, false, 0))
700+
if (!EvalScript(stack, tx.vin[i].scriptSig, tx, i, false))
701701
return false;
702702

703703
if (whichType == TX_SCRIPTHASH)
@@ -1367,16 +1367,11 @@ void UpdateCoins(const CTransaction& tx, CValidationState &state, CCoinsViewCach
13671367

13681368
bool CScriptCheck::operator()() const {
13691369
const CScript &scriptSig = ptxTo->vin[nIn].scriptSig;
1370-
if (!VerifyScript(scriptSig, scriptPubKey, *ptxTo, nIn, nFlags, nHashType))
1370+
if (!VerifyScript(scriptSig, scriptPubKey, *ptxTo, nIn, nFlags))
13711371
return error("CScriptCheck() : %s VerifySignature failed", ptxTo->GetHash().ToString());
13721372
return true;
13731373
}
13741374

1375-
bool VerifySignature(const CCoins& txFrom, const CTransaction& txTo, unsigned int nIn, unsigned int flags, int nHashType)
1376-
{
1377-
return CScriptCheck(txFrom, txTo, nIn, flags, nHashType)();
1378-
}
1379-
13801375
bool CheckInputs(const CTransaction& tx, CValidationState &state, const CCoinsViewCache &inputs, bool fScriptChecks, unsigned int flags, std::vector<CScriptCheck> *pvChecks)
13811376
{
13821377
if (!tx.IsCoinBase())
@@ -1445,7 +1440,7 @@ bool CheckInputs(const CTransaction& tx, CValidationState &state, const CCoinsVi
14451440
assert(coins);
14461441

14471442
// Verify signature
1448-
CScriptCheck check(*coins, tx, i, flags, 0);
1443+
CScriptCheck check(*coins, tx, i, flags);
14491444
if (pvChecks) {
14501445
pvChecks->push_back(CScriptCheck());
14511446
check.swap(pvChecks->back());
@@ -1458,7 +1453,7 @@ bool CheckInputs(const CTransaction& tx, CValidationState &state, const CCoinsVi
14581453
// avoid splitting the network between upgraded and
14591454
// non-upgraded nodes.
14601455
CScriptCheck check(*coins, tx, i,
1461-
flags & ~STANDARD_NOT_MANDATORY_VERIFY_FLAGS, 0);
1456+
flags & ~STANDARD_NOT_MANDATORY_VERIFY_FLAGS);
14621457
if (check())
14631458
return state.Invalid(false, REJECT_NONSTANDARD, "non-mandatory-script-verify-flag");
14641459
}

src/main.h

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,6 @@ int64_t GetBlockValue(int nHeight, int64_t nFees);
176176

177177
/** Create a new block index entry for a given block hash */
178178
CBlockIndex * InsertBlockIndex(uint256 hash);
179-
/** Verify a signature */
180-
bool VerifySignature(const CCoins& txFrom, const CTransaction& txTo, unsigned int nIn, unsigned int flags, int nHashType);
181179
/** Abort with a message */
182180
bool AbortNode(const std::string &msg);
183181
/** Get statistics from node state */
@@ -344,13 +342,12 @@ class CScriptCheck
344342
const CTransaction *ptxTo;
345343
unsigned int nIn;
346344
unsigned int nFlags;
347-
int nHashType;
348345

349346
public:
350-
CScriptCheck(): ptxTo(0), nIn(0), nFlags(0), nHashType(0) {}
351-
CScriptCheck(const CCoins& txFromIn, const CTransaction& txToIn, unsigned int nInIn, unsigned int nFlagsIn, int nHashTypeIn) :
347+
CScriptCheck(): ptxTo(0), nIn(0), nFlags(0) {}
348+
CScriptCheck(const CCoins& txFromIn, const CTransaction& txToIn, unsigned int nInIn, unsigned int nFlagsIn) :
352349
scriptPubKey(txFromIn.vout[txToIn.vin[nInIn].prevout.n].scriptPubKey),
353-
ptxTo(&txToIn), nIn(nInIn), nFlags(nFlagsIn), nHashType(nHashTypeIn) { }
350+
ptxTo(&txToIn), nIn(nInIn), nFlags(nFlagsIn) { }
354351

355352
bool operator()() const;
356353

@@ -359,7 +356,6 @@ class CScriptCheck
359356
std::swap(ptxTo, check.ptxTo);
360357
std::swap(nIn, check.nIn);
361358
std::swap(nFlags, check.nFlags);
362-
std::swap(nHashType, check.nHashType);
363359
}
364360
};
365361

src/rpcrawtransaction.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -687,7 +687,7 @@ Value signrawtransaction(const Array& params, bool fHelp)
687687
BOOST_FOREACH(const CMutableTransaction& txv, txVariants) {
688688
txin.scriptSig = CombineSignatures(prevPubKey, mergedTx, i, txin.scriptSig, txv.vin[i].scriptSig);
689689
}
690-
if (!VerifyScript(txin.scriptSig, prevPubKey, mergedTx, i, STANDARD_SCRIPT_VERIFY_FLAGS, 0))
690+
if (!VerifyScript(txin.scriptSig, prevPubKey, mergedTx, i, STANDARD_SCRIPT_VERIFY_FLAGS))
691691
fComplete = false;
692692
}
693693

src/script/interpreter.cpp

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ bool IsCanonicalSignature(const valtype &vchSig, unsigned int flags) {
132132
return true;
133133
}
134134

135-
bool EvalScript(vector<vector<unsigned char> >& stack, const CScript& script, const CTransaction& txTo, unsigned int nIn, unsigned int flags, int nHashType)
135+
bool EvalScript(vector<vector<unsigned char> >& stack, const CScript& script, const CTransaction& txTo, unsigned int nIn, unsigned int flags)
136136
{
137137
CScript::const_iterator pc = script.begin();
138138
CScript::const_iterator pend = script.end();
@@ -675,7 +675,7 @@ bool EvalScript(vector<vector<unsigned char> >& stack, const CScript& script, co
675675
scriptCode.FindAndDelete(CScript(vchSig));
676676

677677
bool fSuccess = IsCanonicalSignature(vchSig, flags) && IsCanonicalPubKey(vchPubKey, flags) &&
678-
CheckSig(vchSig, vchPubKey, scriptCode, txTo, nIn, nHashType, flags);
678+
CheckSig(vchSig, vchPubKey, scriptCode, txTo, nIn, flags);
679679

680680
popstack(stack);
681681
popstack(stack);
@@ -736,7 +736,7 @@ bool EvalScript(vector<vector<unsigned char> >& stack, const CScript& script, co
736736

737737
// Check signature
738738
bool fOk = IsCanonicalSignature(vchSig, flags) && IsCanonicalPubKey(vchPubKey, flags) &&
739-
CheckSig(vchSig, vchPubKey, scriptCode, txTo, nIn, nHashType, flags);
739+
CheckSig(vchSig, vchPubKey, scriptCode, txTo, nIn, flags);
740740

741741
if (fOk) {
742742
isig++;
@@ -975,7 +975,7 @@ class CSignatureCache
975975
}
976976
};
977977

978-
bool CheckSig(vector<unsigned char> vchSig, const vector<unsigned char> &vchPubKey, const CScript &scriptCode, const CTransaction& txTo, unsigned int nIn, int nHashType, int flags)
978+
bool CheckSig(vector<unsigned char> vchSig, const vector<unsigned char>& vchPubKey, const CScript& scriptCode, const CTransaction& txTo, unsigned int nIn, int flags)
979979
{
980980
static CSignatureCache signatureCache;
981981

@@ -986,10 +986,7 @@ bool CheckSig(vector<unsigned char> vchSig, const vector<unsigned char> &vchPubK
986986
// Hash type is one byte tacked on to the end of the signature
987987
if (vchSig.empty())
988988
return false;
989-
if (nHashType == 0)
990-
nHashType = vchSig.back();
991-
else if (nHashType != vchSig.back())
992-
return false;
989+
int nHashType = vchSig.back();
993990
vchSig.pop_back();
994991

995992
uint256 sighash = SignatureHash(scriptCode, txTo, nIn, nHashType);
@@ -1006,15 +1003,14 @@ bool CheckSig(vector<unsigned char> vchSig, const vector<unsigned char> &vchPubK
10061003
return true;
10071004
}
10081005

1009-
bool VerifyScript(const CScript& scriptSig, const CScript& scriptPubKey, const CTransaction& txTo, unsigned int nIn,
1010-
unsigned int flags, int nHashType)
1006+
bool VerifyScript(const CScript& scriptSig, const CScript& scriptPubKey, const CTransaction& txTo, unsigned int nIn, unsigned int flags)
10111007
{
10121008
vector<vector<unsigned char> > stack, stackCopy;
1013-
if (!EvalScript(stack, scriptSig, txTo, nIn, flags, nHashType))
1009+
if (!EvalScript(stack, scriptSig, txTo, nIn, flags))
10141010
return false;
10151011
if (flags & SCRIPT_VERIFY_P2SH)
10161012
stackCopy = stack;
1017-
if (!EvalScript(stack, scriptPubKey, txTo, nIn, flags, nHashType))
1013+
if (!EvalScript(stack, scriptPubKey, txTo, nIn, flags))
10181014
return false;
10191015
if (stack.empty())
10201016
return false;
@@ -1037,7 +1033,7 @@ bool VerifyScript(const CScript& scriptSig, const CScript& scriptPubKey, const C
10371033
CScript pubKey2(pubKeySerialized.begin(), pubKeySerialized.end());
10381034
popstack(stackCopy);
10391035

1040-
if (!EvalScript(stackCopy, pubKey2, txTo, nIn, flags, nHashType))
1036+
if (!EvalScript(stackCopy, pubKey2, txTo, nIn, flags))
10411037
return false;
10421038
if (stackCopy.empty())
10431039
return false;

src/script/interpreter.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ bool IsCanonicalPubKey(const std::vector<unsigned char> &vchPubKey, unsigned int
3838
bool IsCanonicalSignature(const std::vector<unsigned char> &vchSig, unsigned int flags);
3939

4040
uint256 SignatureHash(const CScript &scriptCode, const CTransaction& txTo, unsigned int nIn, int nHashType);
41-
bool CheckSig(std::vector<unsigned char> vchSig, const std::vector<unsigned char> &vchPubKey, const CScript &scriptCode, const CTransaction& txTo, unsigned int nIn, int nHashType, int flags);
42-
bool EvalScript(std::vector<std::vector<unsigned char> >& stack, const CScript& script, const CTransaction& txTo, unsigned int nIn, unsigned int flags, int nHashType);
43-
bool VerifyScript(const CScript& scriptSig, const CScript& scriptPubKey, const CTransaction& txTo, unsigned int nIn, unsigned int flags, int nHashType);
41+
bool CheckSig(std::vector<unsigned char> vchSig, const std::vector<unsigned char> &vchPubKey, const CScript &scriptCode, const CTransaction& txTo, unsigned int nIn, int flags);
42+
bool EvalScript(std::vector<std::vector<unsigned char> >& stack, const CScript& script, const CTransaction& txTo, unsigned int nIn, unsigned int flags);
43+
bool VerifyScript(const CScript& scriptSig, const CScript& scriptPubKey, const CTransaction& txTo, unsigned int nIn, unsigned int flags);
4444

4545
#endif // H_BITCOIN_SCRIPT_INTERPRETER

src/script/sign.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ bool SignSignature(const CKeyStore &keystore, const CScript& fromPubKey, CMutabl
123123
}
124124

125125
// Test solution
126-
return VerifyScript(txin.scriptSig, fromPubKey, txTo, nIn, STANDARD_SCRIPT_VERIFY_FLAGS, 0);
126+
return VerifyScript(txin.scriptSig, fromPubKey, txTo, nIn, STANDARD_SCRIPT_VERIFY_FLAGS);
127127
}
128128

129129
bool SignSignature(const CKeyStore &keystore, const CTransaction& txFrom, CMutableTransaction& txTo, unsigned int nIn, int nHashType)
@@ -174,7 +174,7 @@ static CScript CombineMultisig(CScript scriptPubKey, const CMutableTransaction&
174174
if (sigs.count(pubkey))
175175
continue; // Already got a sig for this pubkey
176176

177-
if (CheckSig(sig, pubkey, scriptPubKey, txTo, nIn, 0, 0))
177+
if (CheckSig(sig, pubkey, scriptPubKey, txTo, nIn, 0))
178178
{
179179
sigs[pubkey] = sig;
180180
break;
@@ -252,9 +252,9 @@ CScript CombineSignatures(CScript scriptPubKey, const CTransaction& txTo, unsign
252252
Solver(scriptPubKey, txType, vSolutions);
253253

254254
vector<valtype> stack1;
255-
EvalScript(stack1, scriptSig1, CTransaction(), 0, SCRIPT_VERIFY_STRICTENC, 0);
255+
EvalScript(stack1, scriptSig1, CTransaction(), 0, SCRIPT_VERIFY_STRICTENC);
256256
vector<valtype> stack2;
257-
EvalScript(stack2, scriptSig2, CTransaction(), 0, SCRIPT_VERIFY_STRICTENC, 0);
257+
EvalScript(stack2, scriptSig2, CTransaction(), 0, SCRIPT_VERIFY_STRICTENC);
258258

259259
return CombineSignatures(scriptPubKey, txTo, nIn, txType, vSolutions, stack1, stack2);
260260
}

src/test/multisig_tests.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -82,19 +82,19 @@ BOOST_AUTO_TEST_CASE(multisig_verify)
8282
keys.clear();
8383
keys += key[0],key[1]; // magic operator+= from boost.assign
8484
s = sign_multisig(a_and_b, keys, txTo[0], 0);
85-
BOOST_CHECK(VerifyScript(s, a_and_b, txTo[0], 0, flags, 0));
85+
BOOST_CHECK(VerifyScript(s, a_and_b, txTo[0], 0, flags));
8686

8787
for (int i = 0; i < 4; i++)
8888
{
8989
keys.clear();
9090
keys += key[i];
9191
s = sign_multisig(a_and_b, keys, txTo[0], 0);
92-
BOOST_CHECK_MESSAGE(!VerifyScript(s, a_and_b, txTo[0], 0, flags, 0), strprintf("a&b 1: %d", i));
92+
BOOST_CHECK_MESSAGE(!VerifyScript(s, a_and_b, txTo[0], 0, flags), strprintf("a&b 1: %d", i));
9393

9494
keys.clear();
9595
keys += key[1],key[i];
9696
s = sign_multisig(a_and_b, keys, txTo[0], 0);
97-
BOOST_CHECK_MESSAGE(!VerifyScript(s, a_and_b, txTo[0], 0, flags, 0), strprintf("a&b 2: %d", i));
97+
BOOST_CHECK_MESSAGE(!VerifyScript(s, a_and_b, txTo[0], 0, flags), strprintf("a&b 2: %d", i));
9898
}
9999

100100
// Test a OR b:
@@ -104,16 +104,16 @@ BOOST_AUTO_TEST_CASE(multisig_verify)
104104
keys += key[i];
105105
s = sign_multisig(a_or_b, keys, txTo[1], 0);
106106
if (i == 0 || i == 1)
107-
BOOST_CHECK_MESSAGE(VerifyScript(s, a_or_b, txTo[1], 0, flags, 0), strprintf("a|b: %d", i));
107+
BOOST_CHECK_MESSAGE(VerifyScript(s, a_or_b, txTo[1], 0, flags), strprintf("a|b: %d", i));
108108
else
109-
BOOST_CHECK_MESSAGE(!VerifyScript(s, a_or_b, txTo[1], 0, flags, 0), strprintf("a|b: %d", i));
109+
BOOST_CHECK_MESSAGE(!VerifyScript(s, a_or_b, txTo[1], 0, flags), strprintf("a|b: %d", i));
110110
}
111111
s.clear();
112112
s << OP_0 << OP_0;
113-
BOOST_CHECK(!VerifyScript(s, a_or_b, txTo[1], 0, flags, 0));
113+
BOOST_CHECK(!VerifyScript(s, a_or_b, txTo[1], 0, flags));
114114
s.clear();
115115
s << OP_0 << OP_1;
116-
BOOST_CHECK(!VerifyScript(s, a_or_b, txTo[1], 0, flags, 0));
116+
BOOST_CHECK(!VerifyScript(s, a_or_b, txTo[1], 0, flags));
117117

118118

119119
for (int i = 0; i < 4; i++)
@@ -123,9 +123,9 @@ BOOST_AUTO_TEST_CASE(multisig_verify)
123123
keys += key[i],key[j];
124124
s = sign_multisig(escrow, keys, txTo[2], 0);
125125
if (i < j && i < 3 && j < 3)
126-
BOOST_CHECK_MESSAGE(VerifyScript(s, escrow, txTo[2], 0, flags, 0), strprintf("escrow 1: %d %d", i, j));
126+
BOOST_CHECK_MESSAGE(VerifyScript(s, escrow, txTo[2], 0, flags), strprintf("escrow 1: %d %d", i, j));
127127
else
128-
BOOST_CHECK_MESSAGE(!VerifyScript(s, escrow, txTo[2], 0, flags, 0), strprintf("escrow 2: %d %d", i, j));
128+
BOOST_CHECK_MESSAGE(!VerifyScript(s, escrow, txTo[2], 0, flags), strprintf("escrow 2: %d %d", i, j));
129129
}
130130
}
131131

src/test/script_P2SH_tests.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ Verify(const CScript& scriptSig, const CScript& scriptPubKey, bool fStrict)
4242
txTo.vin[0].scriptSig = scriptSig;
4343
txTo.vout[0].nValue = 1;
4444

45-
return VerifyScript(scriptSig, scriptPubKey, txTo, 0, fStrict ? SCRIPT_VERIFY_P2SH : SCRIPT_VERIFY_NONE, 0);
45+
return VerifyScript(scriptSig, scriptPubKey, txTo, 0, fStrict ? SCRIPT_VERIFY_P2SH : SCRIPT_VERIFY_NONE);
4646
}
4747

4848

@@ -113,7 +113,7 @@ BOOST_AUTO_TEST_CASE(sign)
113113
{
114114
CScript sigSave = txTo[i].vin[0].scriptSig;
115115
txTo[i].vin[0].scriptSig = txTo[j].vin[0].scriptSig;
116-
bool sigOK = VerifySignature(CCoins(txFrom, 0), txTo[i], 0, SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_STRICTENC, 0);
116+
bool sigOK = CScriptCheck(CCoins(txFrom, 0), txTo[i], 0, SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_STRICTENC)();
117117
if (i == j)
118118
BOOST_CHECK_MESSAGE(sigOK, strprintf("VerifySignature %d %d", i, j));
119119
else

0 commit comments

Comments
 (0)