Skip to content

Commit 9ab4caf

Browse files
committed
Refactor Get{Prevout,Sequence,Outputs}Hash to Get{Prevouts,Sequences,Outputs}SHA256.
Several proposals (Taproot, MuHash, CTV) require access to the single hash.
1 parent 6510d0f commit 9ab4caf

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed

src/script/interpreter.cpp

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1258,34 +1258,37 @@ class CTransactionSignatureSerializer
12581258
}
12591259
};
12601260

1261+
/** Compute the (single) SHA256 of the concatenation of all prevouts of a tx. */
12611262
template <class T>
1262-
uint256 GetPrevoutHash(const T& txTo)
1263+
uint256 GetPrevoutsSHA256(const T& txTo)
12631264
{
12641265
CHashWriter ss(SER_GETHASH, 0);
12651266
for (const auto& txin : txTo.vin) {
12661267
ss << txin.prevout;
12671268
}
1268-
return ss.GetHash();
1269+
return ss.GetSHA256();
12691270
}
12701271

1272+
/** Compute the (single) SHA256 of the concatenation of all nSequences of a tx. */
12711273
template <class T>
1272-
uint256 GetSequenceHash(const T& txTo)
1274+
uint256 GetSequencesSHA256(const T& txTo)
12731275
{
12741276
CHashWriter ss(SER_GETHASH, 0);
12751277
for (const auto& txin : txTo.vin) {
12761278
ss << txin.nSequence;
12771279
}
1278-
return ss.GetHash();
1280+
return ss.GetSHA256();
12791281
}
12801282

1283+
/** Compute the (single) SHA256 of the concatenation of all txouts of a tx. */
12811284
template <class T>
1282-
uint256 GetOutputsHash(const T& txTo)
1285+
uint256 GetOutputsSHA256(const T& txTo)
12831286
{
12841287
CHashWriter ss(SER_GETHASH, 0);
12851288
for (const auto& txout : txTo.vout) {
12861289
ss << txout;
12871290
}
1288-
return ss.GetHash();
1291+
return ss.GetSHA256();
12891292
}
12901293

12911294
} // namespace
@@ -1297,9 +1300,9 @@ void PrecomputedTransactionData::Init(const T& txTo)
12971300

12981301
// Cache is calculated only for transactions with witness
12991302
if (txTo.HasWitness()) {
1300-
hashPrevouts = GetPrevoutHash(txTo);
1301-
hashSequence = GetSequenceHash(txTo);
1302-
hashOutputs = GetOutputsHash(txTo);
1303+
hashPrevouts = SHA256Uint256(GetPrevoutsSHA256(txTo));
1304+
hashSequence = SHA256Uint256(GetSequencesSHA256(txTo));
1305+
hashOutputs = SHA256Uint256(GetOutputsSHA256(txTo));
13031306
}
13041307

13051308
m_ready = true;
@@ -1329,16 +1332,16 @@ uint256 SignatureHash(const CScript& scriptCode, const T& txTo, unsigned int nIn
13291332
const bool cacheready = cache && cache->m_ready;
13301333

13311334
if (!(nHashType & SIGHASH_ANYONECANPAY)) {
1332-
hashPrevouts = cacheready ? cache->hashPrevouts : GetPrevoutHash(txTo);
1335+
hashPrevouts = cacheready ? cache->hashPrevouts : SHA256Uint256(GetPrevoutsSHA256(txTo));
13331336
}
13341337

13351338
if (!(nHashType & SIGHASH_ANYONECANPAY) && (nHashType & 0x1f) != SIGHASH_SINGLE && (nHashType & 0x1f) != SIGHASH_NONE) {
1336-
hashSequence = cacheready ? cache->hashSequence : GetSequenceHash(txTo);
1339+
hashSequence = cacheready ? cache->hashSequence : SHA256Uint256(GetSequencesSHA256(txTo));
13371340
}
13381341

13391342

13401343
if ((nHashType & 0x1f) != SIGHASH_SINGLE && (nHashType & 0x1f) != SIGHASH_NONE) {
1341-
hashOutputs = cacheready ? cache->hashOutputs : GetOutputsHash(txTo);
1344+
hashOutputs = cacheready ? cache->hashOutputs : SHA256Uint256(GetOutputsSHA256(txTo));
13421345
} else if ((nHashType & 0x1f) == SIGHASH_SINGLE && nIn < txTo.vout.size()) {
13431346
CHashWriter ss(SER_GETHASH, 0);
13441347
ss << txTo.vout[nIn];

0 commit comments

Comments
 (0)