Inside Bitcoin
Opening the selected file…
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
502bool GetOp(const_iterator& pc, opcodetype& opcodeRet) const503{504return GetScriptOp(pc, end(), opcodeRet, nullptr);505}507/** Encode/decode small integers: */508static int DecodeOP_N(opcodetype opcode)509{510if (opcode == OP_0)511return 0;512assert(opcode >= OP_1 && opcode <= OP_16);513return (int)opcode - (int)(OP_1 - 1);514}515static opcodetype EncodeOP_N(int n)516{517assert(n >= 0 && n <= 16);518if (n == 0)519return OP_0;520return (opcodetype)(OP_1+n-1);521}523/**524* Pre-version-0.6, Bitcoin always counted CHECKMULTISIGs525* as 20 sigops. With pay-to-script-hash, that changed:526* CHECKMULTISIGs serialized in scriptSigs are527* counted more accurately, assuming they are of the form528* ... OP_N CHECKMULTISIG ...529*/530unsigned int GetSigOpCount(bool fAccurate) const;532/**533* Accurately count sigOps, including sigOps in534* pay-to-script-hash transactions:535*/536unsigned int GetSigOpCount(const CScript& scriptSig) const;538/*539* OP_1 <0x4e73>540*/541bool IsPayToAnchor() const;542/** Checks if output of IsWitnessProgram comes from a P2A output script543*/544static bool IsPayToAnchor(int version, const std::vector<unsigned char>& program);546bool IsPayToScriptHash() const;547bool IsPayToWitnessScriptHash() const;548bool IsWitnessProgram(int& version, std::vector<unsigned char>& program) const;550bool IsPayToTaproot() const;552/** Called by IsStandardTx and P2SH/BIP62 VerifyScript (which makes it consensus-critical). */553bool IsPushOnly(const_iterator pc) const;554bool IsPushOnly() const;556/** Check if the script contains valid OP_CODES */557bool HasValidOps() const;559/**560* Returns whether the script is guaranteed to fail at execution,561* regardless of the initial stack. This allows outputs to be pruned562* instantly when entering the UTXO set.563*/564bool IsUnspendable() const565{566return (size() > 0 && *begin() == OP_RETURN) || (size() > MAX_SCRIPT_SIZE);567}569void clear()570{571// The default prevector::clear() does not release memory572CScriptBase::clear();573shrink_to_fit();574}575};577struct CScriptWitness578{579// Note that this encodes the data elements being pushed, rather than580// encoding them as a CScript that pushes them.581std::vector<std::vector<unsigned char> > stack;583// Some compilers complain without a default constructor584CScriptWitness() = default;586bool IsNull() const { return stack.empty(); }588void SetNull() { stack.clear(); stack.shrink_to_fit(); }590std::string ToString() const;592bool operator==(const CScriptWitness&) const = default;593};595/** A reference to a CScript: the Hash160 of its serialization */596class CScriptID : public BaseHash<uint160>597{598public:599CScriptID() : BaseHash() {}600explicit CScriptID(const CScript& in);601explicit CScriptID(const uint160& in) : BaseHash(in) {}602};604/** Test for OP_SUCCESSx opcodes as defined by BIP342. */605bool IsOpSuccess(const opcodetype& opcode);607bool CheckMinimalPush(const std::vector<unsigned char>& data, opcodetype opcode);609/** Build a script by concatenating other scripts, or any argument accepted by CScript::operator<<. */610template<typename... Ts>611CScript BuildScript(Ts&&... inputs)612{613CScript ret;614int cnt{0};616([&ret, &cnt] (Ts&& input) {617if constexpr (std::is_same_v<std::remove_cv_t<std::remove_reference_t<Ts>>, CScript>) {618// If it is a CScript, extend ret with it. Move or copy the first element instead.619if (cnt == 0) {620ret = std::forward<Ts>(input);621} else {622ret.insert(ret.end(), input.begin(), input.end());623}624} else {625// Otherwise invoke CScript::operator<<.626ret << input;627}628cnt++;629} (std::forward<Ts>(inputs)), ...);631return ret;632}634#endif // BITCOIN_SCRIPT_SCRIPT_H