MCPcopy Create free account
hub / github.com/bitcoin/bitcoin / SignatureHash

Function SignatureHash

src/script/interpreter.cpp:1610–1687  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1608
1609template <class T>
1610uint256 SignatureHash(const CScript& scriptCode, const T& txTo, unsigned int nIn, int32_t nHashType, const CAmount& amount, SigVersion sigversion, const PrecomputedTransactionData* cache, SigHashCache* sighash_cache)
1611{
1612 assert(nIn < txTo.vin.size());
1613
1614 if (sigversion != SigVersion::WITNESS_V0) {
1615 // Check for invalid use of SIGHASH_SINGLE
1616 if ((nHashType & 0x1f) == SIGHASH_SINGLE) {
1617 if (nIn >= txTo.vout.size()) {
1618 // nOut out of range
1619 return uint256::ONE;
1620 }
1621 }
1622 }
1623
1624 HashWriter ss{};
1625
1626 // Try to compute using cached SHA256 midstate.
1627 if (sighash_cache && sighash_cache->Load(nHashType, scriptCode, ss)) {
1628 // Add sighash type and hash.
1629 ss << nHashType;
1630 return ss.GetHash();
1631 }
1632
1633 if (sigversion == SigVersion::WITNESS_V0) {
1634 uint256 hashPrevouts;
1635 uint256 hashSequence;
1636 uint256 hashOutputs;
1637 const bool cacheready = cache && cache->m_bip143_segwit_ready;
1638
1639 if (!(nHashType & SIGHASH_ANYONECANPAY)) {
1640 hashPrevouts = cacheready ? cache->hashPrevouts : SHA256Uint256(GetPrevoutsSHA256(txTo));
1641 }
1642
1643 if (!(nHashType & SIGHASH_ANYONECANPAY) && (nHashType & 0x1f) != SIGHASH_SINGLE && (nHashType & 0x1f) != SIGHASH_NONE) {
1644 hashSequence = cacheready ? cache->hashSequence : SHA256Uint256(GetSequencesSHA256(txTo));
1645 }
1646
1647 if ((nHashType & 0x1f) != SIGHASH_SINGLE && (nHashType & 0x1f) != SIGHASH_NONE) {
1648 hashOutputs = cacheready ? cache->hashOutputs : SHA256Uint256(GetOutputsSHA256(txTo));
1649 } else if ((nHashType & 0x1f) == SIGHASH_SINGLE && nIn < txTo.vout.size()) {
1650 HashWriter inner_ss{};
1651 inner_ss << txTo.vout[nIn];
1652 hashOutputs = inner_ss.GetHash();
1653 }
1654
1655 // Version
1656 ss << txTo.version;
1657 // Input prevouts/nSequence (none/all, depending on flags)
1658 ss << hashPrevouts;
1659 ss << hashSequence;
1660 // The input being signed (replacing the scriptSig with scriptCode + amount)
1661 // The prevout may already be contained in hashPrevout, and the nSequence
1662 // may already be contain in hashSequence.
1663 ss << txTo.vin[nIn].prevout;
1664 ss << scriptCode;
1665 ss << amount;
1666 ss << txTo.vin[nIn].nSequence;
1667 // Outputs (none/one/all, depending on flags)

Callers 10

CheckECDSASignatureMethod · 0.85
CreateSigMethod · 0.85
BOOST_FIXTURE_TEST_CASEFunction · 0.85
BOOST_AUTO_TEST_CASEFunction · 0.85
BOOST_FIXTURE_TEST_CASEFunction · 0.85
TestBuilderClass · 0.85
sign_multisigFunction · 0.85
BOOST_AUTO_TEST_CASEFunction · 0.85
sign_multisigFunction · 0.85
FUZZ_TARGETFunction · 0.85

Calls 8

SHA256Uint256Function · 0.85
GetPrevoutsSHA256Function · 0.85
GetSequencesSHA256Function · 0.85
GetOutputsSHA256Function · 0.85
StoreMethod · 0.80
sizeMethod · 0.45
LoadMethod · 0.45
GetHashMethod · 0.45

Tested by 7

BOOST_FIXTURE_TEST_CASEFunction · 0.68
BOOST_AUTO_TEST_CASEFunction · 0.68
BOOST_FIXTURE_TEST_CASEFunction · 0.68
sign_multisigFunction · 0.68
BOOST_AUTO_TEST_CASEFunction · 0.68
sign_multisigFunction · 0.68
FUZZ_TARGETFunction · 0.68