@@ -1569,6 +1569,18 @@ uint256 SignatureHash(const CScript& scriptCode, const T& txTo, unsigned int nIn
15691569{
15701570 assert (nIn < txTo.vin .size ());
15711571
1572+ if (sigversion != SigVersion::WITNESS_V0) {
1573+ // Check for invalid use of SIGHASH_SINGLE
1574+ if ((nHashType & 0x1f ) == SIGHASH_SINGLE) {
1575+ if (nIn >= txTo.vout .size ()) {
1576+ // nOut out of range
1577+ return uint256::ONE;
1578+ }
1579+ }
1580+ }
1581+
1582+ HashWriter ss{};
1583+
15721584 if (sigversion == SigVersion::WITNESS_V0) {
15731585 uint256 hashPrevouts;
15741586 uint256 hashSequence;
@@ -1583,16 +1595,14 @@ uint256 SignatureHash(const CScript& scriptCode, const T& txTo, unsigned int nIn
15831595 hashSequence = cacheready ? cache->hashSequence : SHA256Uint256 (GetSequencesSHA256 (txTo));
15841596 }
15851597
1586-
15871598 if ((nHashType & 0x1f ) != SIGHASH_SINGLE && (nHashType & 0x1f ) != SIGHASH_NONE) {
15881599 hashOutputs = cacheready ? cache->hashOutputs : SHA256Uint256 (GetOutputsSHA256 (txTo));
15891600 } else if ((nHashType & 0x1f ) == SIGHASH_SINGLE && nIn < txTo.vout .size ()) {
1590- HashWriter ss {};
1591- ss << txTo.vout [nIn];
1592- hashOutputs = ss .GetHash ();
1601+ HashWriter inner_ss {};
1602+ inner_ss << txTo.vout [nIn];
1603+ hashOutputs = inner_ss .GetHash ();
15931604 }
15941605
1595- HashWriter ss{};
15961606 // Version
15971607 ss << txTo.version ;
15981608 // Input prevouts/nSequence (none/all, depending on flags)
@@ -1609,26 +1619,16 @@ uint256 SignatureHash(const CScript& scriptCode, const T& txTo, unsigned int nIn
16091619 ss << hashOutputs;
16101620 // Locktime
16111621 ss << txTo.nLockTime ;
1612- // Sighash type
1613- ss << nHashType;
1614-
1615- return ss.GetHash ();
1616- }
1622+ } else {
1623+ // Wrapper to serialize only the necessary parts of the transaction being signed
1624+ CTransactionSignatureSerializer<T> txTmp (txTo, scriptCode, nIn, nHashType);
16171625
1618- // Check for invalid use of SIGHASH_SINGLE
1619- if ((nHashType & 0x1f ) == SIGHASH_SINGLE) {
1620- if (nIn >= txTo.vout .size ()) {
1621- // nOut out of range
1622- return uint256::ONE;
1623- }
1626+ // Serialize
1627+ ss << txTmp;
16241628 }
16251629
1626- // Wrapper to serialize only the necessary parts of the transaction being signed
1627- CTransactionSignatureSerializer<T> txTmp (txTo, scriptCode, nIn, nHashType);
1628-
1629- // Serialize and hash
1630- HashWriter ss{};
1631- ss << txTmp << nHashType;
1630+ // Add sighash type and hash.
1631+ ss << nHashType;
16321632 return ss.GetHash ();
16331633}
16341634
0 commit comments