Skip to content

Commit

Permalink
[squash]: add UdjinM6 suggestion
Browse files Browse the repository at this point in the history
  • Loading branch information
panleone committed Feb 22, 2024
1 parent d0af6db commit 1a5e8bd
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 11 deletions.
28 changes: 18 additions & 10 deletions src/script/interpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,11 @@ static bool EvalChecksig(const valtype& vchSig, const valtype& vchPubKey, CScrip
// Subset of script starting at the most recent codeseparator
CScript scriptCode(pbegincodehash, pend);

int nHashType = vchSig.empty() ? 0 : vchSig.back();
// Can't use using SIGHASH_DIP0143 hash type without SCRIPT_ENABLE_DIP0143 flag
if ((nHashType & SIGHASH_DIP0143) && (~flags & SCRIPT_ENABLE_DIP0143)) {
return set_error(serror, SCRIPT_ERR_SIGHASHTYPE_DIP0143);
}
// Drop the signature, since there's no way for a signature to sign itself
int found = FindAndDelete(scriptCode, CScript() << vchSig);
if (found > 0 && (flags & SCRIPT_VERIFY_CONST_SCRIPTCODE))
Expand All @@ -357,7 +362,7 @@ static bool EvalChecksig(const valtype& vchSig, const valtype& vchPubKey, CScrip
//serror is set
return false;
}
fSuccess = checker.CheckSig(vchSig, vchPubKey, scriptCode, GetSigVersionFromFlags(flags));
fSuccess = checker.CheckSig(vchSig, vchPubKey, scriptCode, GetSigVersion(flags, nHashType));

if (!fSuccess && (flags & SCRIPT_VERIFY_NULLFAIL) && vchSig.size())
return set_error(serror, SCRIPT_ERR_SIG_NULLFAIL);
Expand Down Expand Up @@ -1162,10 +1167,15 @@ bool EvalScript(std::vector<std::vector<unsigned char> >& stack, const CScript&
// Subset of script starting at the most recent codeseparator
CScript scriptCode(pbegincodehash, pend);

// Drop the signatures, since there's no way for a signature to sign itself
for (int k = 0; k < nSigsCount; k++)
{
valtype& vchSig = stacktop(-isig-k);
int nHashType = vchSig.empty() ? 0 : vchSig.back();
// Can't use using SIGHASH_DIP0143 hash type without SCRIPT_ENABLE_DIP0143 flag
if ((nHashType & SIGHASH_DIP0143) && (~flags & SCRIPT_ENABLE_DIP0143)) {
return set_error(serror, SCRIPT_ERR_SIGHASHTYPE_DIP0143);
}
// Drop the signatures, since there's no way for a signature to sign itself
int found = FindAndDelete(scriptCode, CScript() << vchSig);
if (found > 0 && (flags & SCRIPT_VERIFY_CONST_SCRIPTCODE))
return set_error(serror, SCRIPT_ERR_SIG_FINDANDDELETE);
Expand All @@ -1186,7 +1196,8 @@ bool EvalScript(std::vector<std::vector<unsigned char> >& stack, const CScript&
}

// Check signature
bool fOk = checker.CheckSig(vchSig, vchPubKey, scriptCode, GetSigVersionFromFlags(flags));
int nHashType = vchSig.empty() ? 0 : vchSig.back();
bool fOk = checker.CheckSig(vchSig, vchPubKey, scriptCode, GetSigVersion(flags, nHashType));

if (fOk) {
isig++;
Expand Down Expand Up @@ -1537,16 +1548,16 @@ template PrecomputedTransactionData::PrecomputedTransactionData(const CMutableTr
template void PrecomputedTransactionData::Init(const CTransaction& txTo, std::vector<CTxOut>&& spent_outputs);
template void PrecomputedTransactionData::Init(const CMutableTransaction& txTo, std::vector<CTxOut>&& spent_outputs);

SigVersion GetSigVersionFromFlags(unsigned int flags){
return (flags | SCRIPT_ENABLE_DIP0143) ? SigVersion::DIP0143 : SigVersion::BASE;
SigVersion GetSigVersion(unsigned int flags, int nHashType)
{
return (flags & SCRIPT_ENABLE_DIP0143) && (nHashType & SIGHASH_DIP0143) ? SigVersion::DIP0143 : SigVersion::BASE;
}

template <class T>
uint256 SignatureHash(const CScript& scriptCode, const T& txTo, unsigned int nIn, int nHashType, const CAmount& amount, SigVersion sigversion, const PrecomputedTransactionData* cache)
{
assert(nIn < txTo.vin.size());

if (nHashType & SIGHASH_DIP0143 && sigversion == SigVersion::DIP0143) {
if (sigversion == SigVersion::DIP0143) {
int32_t n32bitVersion = txTo.nVersion | (txTo.nType << 16);
uint256 hashPrevouts;
uint256 hashSequence;
Expand Down Expand Up @@ -1636,9 +1647,6 @@ bool GenericTransactionSignatureChecker<T>::CheckSig(const std::vector<unsigned
return false;
int nHashType = vchSig.back();
vchSig.pop_back();
if ((nHashType & SIGHASH_DIP0143) && sigversion != SigVersion::DIP0143){
return false;
}

uint256 sighash = SignatureHash(scriptCode, *txTo, nIn, nHashType, amount, sigversion, this->txdata);

Expand Down
2 changes: 1 addition & 1 deletion src/script/interpreter.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ enum class SigVersion
DIP0143 = 1,
};

SigVersion GetSigVersionFromFlags(unsigned int flags);
SigVersion GetSigVersion(unsigned int flags, int nHashType);

template <class T>
uint256 SignatureHash(const CScript& scriptCode, const T& txTo, unsigned int nIn, int nHashType, const CAmount& amount, SigVersion sigversion, const PrecomputedTransactionData* cache = nullptr);
Expand Down
3 changes: 3 additions & 0 deletions src/script/script_error.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ typedef enum ScriptError_t
SCRIPT_ERR_OP_CODESEPARATOR,
SCRIPT_ERR_SIG_FINDANDDELETE,

/* DIP0143 */
SCRIPT_ERR_SIGHASHTYPE_DIP0143,

SCRIPT_ERR_ERROR_COUNT
} ScriptError;

Expand Down

0 comments on commit 1a5e8bd

Please sign in to comment.