Abstract out SignatureChecker
This commit is contained in:
parent
6faee79426
commit
c7829ea797
4 changed files with 37 additions and 14 deletions
|
@ -132,7 +132,7 @@ bool IsCanonicalSignature(const valtype &vchSig, unsigned int flags) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool EvalScript(vector<vector<unsigned char> >& stack, const CScript& script, const CTransaction& txTo, unsigned int nIn, unsigned int flags)
|
bool EvalScript(vector<vector<unsigned char> >& stack, const CScript& script, unsigned int flags, const SignatureChecker& checker)
|
||||||
{
|
{
|
||||||
CScript::const_iterator pc = script.begin();
|
CScript::const_iterator pc = script.begin();
|
||||||
CScript::const_iterator pend = script.end();
|
CScript::const_iterator pend = script.end();
|
||||||
|
@ -675,7 +675,7 @@ bool EvalScript(vector<vector<unsigned char> >& stack, const CScript& script, co
|
||||||
scriptCode.FindAndDelete(CScript(vchSig));
|
scriptCode.FindAndDelete(CScript(vchSig));
|
||||||
|
|
||||||
bool fSuccess = IsCanonicalSignature(vchSig, flags) && IsCanonicalPubKey(vchPubKey, flags) &&
|
bool fSuccess = IsCanonicalSignature(vchSig, flags) && IsCanonicalPubKey(vchPubKey, flags) &&
|
||||||
CheckSig(vchSig, vchPubKey, scriptCode, txTo, nIn, flags);
|
checker.CheckSig(vchSig, vchPubKey, scriptCode, flags);
|
||||||
|
|
||||||
popstack(stack);
|
popstack(stack);
|
||||||
popstack(stack);
|
popstack(stack);
|
||||||
|
@ -736,7 +736,7 @@ bool EvalScript(vector<vector<unsigned char> >& stack, const CScript& script, co
|
||||||
|
|
||||||
// Check signature
|
// Check signature
|
||||||
bool fOk = IsCanonicalSignature(vchSig, flags) && IsCanonicalPubKey(vchPubKey, flags) &&
|
bool fOk = IsCanonicalSignature(vchSig, flags) && IsCanonicalPubKey(vchPubKey, flags) &&
|
||||||
CheckSig(vchSig, vchPubKey, scriptCode, txTo, nIn, flags);
|
checker.CheckSig(vchSig, vchPubKey, scriptCode, flags);
|
||||||
|
|
||||||
if (fOk) {
|
if (fOk) {
|
||||||
isig++;
|
isig++;
|
||||||
|
@ -897,7 +897,7 @@ public:
|
||||||
|
|
||||||
} // anon namespace
|
} // anon namespace
|
||||||
|
|
||||||
uint256 SignatureHash(const CScript &scriptCode, const CTransaction& txTo, unsigned int nIn, int nHashType)
|
uint256 SignatureHash(const CScript& scriptCode, const CTransaction& txTo, unsigned int nIn, int nHashType)
|
||||||
{
|
{
|
||||||
if (nIn >= txTo.vin.size()) {
|
if (nIn >= txTo.vin.size()) {
|
||||||
LogPrintf("ERROR: SignatureHash() : nIn=%d out of range\n", nIn);
|
LogPrintf("ERROR: SignatureHash() : nIn=%d out of range\n", nIn);
|
||||||
|
@ -976,7 +976,7 @@ public:
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
bool CheckSig(vector<unsigned char> vchSig, const vector<unsigned char>& vchPubKey, const CScript& scriptCode, const CTransaction& txTo, unsigned int nIn, int flags)
|
bool SignatureChecker::CheckSig(const vector<unsigned char>& vchSigIn, const vector<unsigned char>& vchPubKey, const CScript& scriptCode, int flags) const
|
||||||
{
|
{
|
||||||
static CSignatureCache signatureCache;
|
static CSignatureCache signatureCache;
|
||||||
|
|
||||||
|
@ -985,6 +985,7 @@ bool CheckSig(vector<unsigned char> vchSig, const vector<unsigned char>& vchPubK
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
// Hash type is one byte tacked on to the end of the signature
|
// Hash type is one byte tacked on to the end of the signature
|
||||||
|
vector<unsigned char> vchSig(vchSigIn);
|
||||||
if (vchSig.empty())
|
if (vchSig.empty())
|
||||||
return false;
|
return false;
|
||||||
int nHashType = vchSig.back();
|
int nHashType = vchSig.back();
|
||||||
|
@ -1004,14 +1005,14 @@ bool CheckSig(vector<unsigned char> vchSig, const vector<unsigned char>& vchPubK
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool VerifyScript(const CScript& scriptSig, const CScript& scriptPubKey, const CTransaction& txTo, unsigned int nIn, unsigned int flags)
|
bool VerifyScript(const CScript& scriptSig, const CScript& scriptPubKey, unsigned int flags, const SignatureChecker& checker)
|
||||||
{
|
{
|
||||||
vector<vector<unsigned char> > stack, stackCopy;
|
vector<vector<unsigned char> > stack, stackCopy;
|
||||||
if (!EvalScript(stack, scriptSig, txTo, nIn, flags))
|
if (!EvalScript(stack, scriptSig, flags, checker))
|
||||||
return false;
|
return false;
|
||||||
if (flags & SCRIPT_VERIFY_P2SH)
|
if (flags & SCRIPT_VERIFY_P2SH)
|
||||||
stackCopy = stack;
|
stackCopy = stack;
|
||||||
if (!EvalScript(stack, scriptPubKey, txTo, nIn, flags))
|
if (!EvalScript(stack, scriptPubKey, flags, checker))
|
||||||
return false;
|
return false;
|
||||||
if (stack.empty())
|
if (stack.empty())
|
||||||
return false;
|
return false;
|
||||||
|
@ -1034,7 +1035,7 @@ bool VerifyScript(const CScript& scriptSig, const CScript& scriptPubKey, const C
|
||||||
CScript pubKey2(pubKeySerialized.begin(), pubKeySerialized.end());
|
CScript pubKey2(pubKeySerialized.begin(), pubKeySerialized.end());
|
||||||
popstack(stackCopy);
|
popstack(stackCopy);
|
||||||
|
|
||||||
if (!EvalScript(stackCopy, pubKey2, txTo, nIn, flags))
|
if (!EvalScript(stackCopy, pubKey2, flags, checker))
|
||||||
return false;
|
return false;
|
||||||
if (stackCopy.empty())
|
if (stackCopy.empty())
|
||||||
return false;
|
return false;
|
||||||
|
|
|
@ -38,8 +38,30 @@ bool IsCanonicalPubKey(const std::vector<unsigned char> &vchPubKey, unsigned int
|
||||||
bool IsCanonicalSignature(const std::vector<unsigned char> &vchSig, unsigned int flags);
|
bool IsCanonicalSignature(const std::vector<unsigned char> &vchSig, unsigned int flags);
|
||||||
|
|
||||||
uint256 SignatureHash(const CScript &scriptCode, const CTransaction& txTo, unsigned int nIn, int nHashType);
|
uint256 SignatureHash(const CScript &scriptCode, const CTransaction& txTo, unsigned int nIn, int nHashType);
|
||||||
bool CheckSig(std::vector<unsigned char> vchSig, const std::vector<unsigned char> &vchPubKey, const CScript &scriptCode, const CTransaction& txTo, unsigned int nIn, int flags);
|
|
||||||
bool EvalScript(std::vector<std::vector<unsigned char> >& stack, const CScript& script, const CTransaction& txTo, unsigned int nIn, unsigned int flags);
|
class SignatureChecker
|
||||||
bool VerifyScript(const CScript& scriptSig, const CScript& scriptPubKey, const CTransaction& txTo, unsigned int nIn, unsigned int flags);
|
{
|
||||||
|
private:
|
||||||
|
const CTransaction& txTo;
|
||||||
|
unsigned int nIn;
|
||||||
|
|
||||||
|
public:
|
||||||
|
SignatureChecker(const CTransaction& txToIn, unsigned int nInIn) : txTo(txToIn), nIn(nInIn) {}
|
||||||
|
bool CheckSig(const std::vector<unsigned char>& scriptSig, const std::vector<unsigned char>& vchPubKey, const CScript& scriptCode, int nFlags) const;
|
||||||
|
};
|
||||||
|
|
||||||
|
bool EvalScript(std::vector<std::vector<unsigned char> >& stack, const CScript& script, unsigned int flags, const SignatureChecker& checker);
|
||||||
|
bool VerifyScript(const CScript& scriptSig, const CScript& scriptPubKey, unsigned int flags, const SignatureChecker& checker);
|
||||||
|
|
||||||
|
// Wrappers using a default SignatureChecker.
|
||||||
|
bool inline EvalScript(std::vector<std::vector<unsigned char> >& stack, const CScript& script, const CTransaction& txTo, unsigned int nIn, unsigned int flags)
|
||||||
|
{
|
||||||
|
return EvalScript(stack, script, flags, SignatureChecker(txTo, nIn));
|
||||||
|
}
|
||||||
|
|
||||||
|
bool inline VerifyScript(const CScript& scriptSig, const CScript& scriptPubKey, const CTransaction& txTo, unsigned int nIn, unsigned int flags)
|
||||||
|
{
|
||||||
|
return VerifyScript(scriptSig, scriptPubKey, flags, SignatureChecker(txTo, nIn));
|
||||||
|
}
|
||||||
|
|
||||||
#endif // H_BITCOIN_SCRIPT_INTERPRETER
|
#endif // H_BITCOIN_SCRIPT_INTERPRETER
|
||||||
|
|
|
@ -174,7 +174,7 @@ static CScript CombineMultisig(CScript scriptPubKey, const CMutableTransaction&
|
||||||
if (sigs.count(pubkey))
|
if (sigs.count(pubkey))
|
||||||
continue; // Already got a sig for this pubkey
|
continue; // Already got a sig for this pubkey
|
||||||
|
|
||||||
if (CheckSig(sig, pubkey, scriptPubKey, txTo, nIn, 0))
|
if (SignatureChecker(txTo, nIn).CheckSig(sig, pubkey, scriptPubKey, 0))
|
||||||
{
|
{
|
||||||
sigs[pubkey] = sig;
|
sigs[pubkey] = sig;
|
||||||
break;
|
break;
|
||||||
|
|
|
@ -89,7 +89,7 @@ CMutableTransaction BuildSpendingTransaction(const CScript& scriptSig, const CTr
|
||||||
|
|
||||||
void DoTest(const CScript& scriptPubKey, const CScript& scriptSig, int flags, bool expect, const std::string& message)
|
void DoTest(const CScript& scriptPubKey, const CScript& scriptSig, int flags, bool expect, const std::string& message)
|
||||||
{
|
{
|
||||||
BOOST_CHECK_MESSAGE(VerifyScript(scriptSig, scriptPubKey, BuildSpendingTransaction(scriptSig, BuildCreditingTransaction(scriptPubKey)), 0, flags) == expect, message);
|
BOOST_CHECK_MESSAGE(VerifyScript(scriptSig, scriptPubKey, flags, SignatureChecker(BuildSpendingTransaction(scriptSig, BuildCreditingTransaction(scriptPubKey)), 0)) == expect, message);
|
||||||
}
|
}
|
||||||
|
|
||||||
namespace
|
namespace
|
||||||
|
|
Loading…
Reference in a new issue