Skip to content

Commit e561cf4

Browse files
committed
Merge #12939: Extract consts for WITNESS_V0 hash sizes
3450a9b Extract consts for WITNESS_V0 hash sizes (Ben Woosley) Pull request description: Tree-SHA512: 57ba84dfa36aa61cabffce747388143cf1c8724dd2fc42aecf93748158b75dbe278b21a32483a100b8c303f6ad01d048da03b0a5c172175febbe70938ed4339d
2 parents 979f598 + 3450a9b commit e561cf4

File tree

4 files changed

+11
-7
lines changed

4 files changed

+11
-7
lines changed

src/policy/policy.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ bool IsWitnessStandard(const CTransaction& tx, const CCoinsViewCache& mapInputs)
230230
return false;
231231

232232
// Check P2WSH standard limits
233-
if (witnessversion == 0 && witnessprogram.size() == 32) {
233+
if (witnessversion == 0 && witnessprogram.size() == WITNESS_V0_SCRIPTHASH_SIZE) {
234234
if (tx.vin[i].scriptWitness.stack.back().size() > MAX_STANDARD_P2WSH_SCRIPT_SIZE)
235235
return false;
236236
size_t sizeWitnessStack = tx.vin[i].scriptWitness.stack.size() - 1;

src/script/interpreter.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1361,7 +1361,7 @@ static bool VerifyWitnessProgram(const CScriptWitness& witness, int witversion,
13611361
CScript scriptPubKey;
13621362

13631363
if (witversion == 0) {
1364-
if (program.size() == 32) {
1364+
if (program.size() == WITNESS_V0_SCRIPTHASH_SIZE) {
13651365
// Version 0 segregated witness program: SHA256(CScript) inside the program, CScript + inputs in witness
13661366
if (witness.stack.size() == 0) {
13671367
return set_error(serror, SCRIPT_ERR_WITNESS_PROGRAM_WITNESS_EMPTY);
@@ -1373,7 +1373,7 @@ static bool VerifyWitnessProgram(const CScriptWitness& witness, int witversion,
13731373
if (memcmp(hashScriptPubKey.begin(), program.data(), 32)) {
13741374
return set_error(serror, SCRIPT_ERR_WITNESS_PROGRAM_MISMATCH);
13751375
}
1376-
} else if (program.size() == 20) {
1376+
} else if (program.size() == WITNESS_V0_KEYHASH_SIZE) {
13771377
// Special case for pay-to-pubkeyhash; signature + pubkey in witness
13781378
if (witness.stack.size() != 2) {
13791379
return set_error(serror, SCRIPT_ERR_WITNESS_PROGRAM_MISMATCH); // 2 items in witness
@@ -1530,10 +1530,10 @@ bool VerifyScript(const CScript& scriptSig, const CScript& scriptPubKey, const C
15301530
size_t static WitnessSigOps(int witversion, const std::vector<unsigned char>& witprogram, const CScriptWitness& witness, int flags)
15311531
{
15321532
if (witversion == 0) {
1533-
if (witprogram.size() == 20)
1533+
if (witprogram.size() == WITNESS_V0_KEYHASH_SIZE)
15341534
return 1;
15351535

1536-
if (witprogram.size() == 32 && witness.stack.size() > 0) {
1536+
if (witprogram.size() == WITNESS_V0_SCRIPTHASH_SIZE && witness.stack.size() > 0) {
15371537
CScript subscript(witness.stack.back().begin(), witness.stack.back().end());
15381538
return subscript.GetSigOpCount(true);
15391539
}

src/script/interpreter.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,10 @@ enum class SigVersion
129129
WITNESS_V0 = 1,
130130
};
131131

132+
/** Signature hash sizes */
133+
static constexpr size_t WITNESS_V0_SCRIPTHASH_SIZE = 32;
134+
static constexpr size_t WITNESS_V0_KEYHASH_SIZE = 20;
135+
132136
uint256 SignatureHash(const CScript &scriptCode, const CTransaction& txTo, unsigned int nIn, int nHashType, const CAmount& amount, SigVersion sigversion, const PrecomputedTransactionData* cache = nullptr);
133137

134138
class BaseSignatureChecker

src/script/standard.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,12 @@ bool Solver(const CScript& scriptPubKey, txnouttype& typeRet, std::vector<std::v
6666
int witnessversion;
6767
std::vector<unsigned char> witnessprogram;
6868
if (scriptPubKey.IsWitnessProgram(witnessversion, witnessprogram)) {
69-
if (witnessversion == 0 && witnessprogram.size() == 20) {
69+
if (witnessversion == 0 && witnessprogram.size() == WITNESS_V0_KEYHASH_SIZE) {
7070
typeRet = TX_WITNESS_V0_KEYHASH;
7171
vSolutionsRet.push_back(witnessprogram);
7272
return true;
7373
}
74-
if (witnessversion == 0 && witnessprogram.size() == 32) {
74+
if (witnessversion == 0 && witnessprogram.size() == WITNESS_V0_SCRIPTHASH_SIZE) {
7575
typeRet = TX_WITNESS_V0_SCRIPTHASH;
7676
vSolutionsRet.push_back(witnessprogram);
7777
return true;

0 commit comments

Comments
 (0)