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.
1// Copyright (c) 2009-2010 Satoshi Nakamoto2// Copyright (c) 2009-present The Bitcoin Core developers3// Distributed under the MIT software license, see the accompanying4// file COPYING or http://www.opensource.org/licenses/mit-license.php.6#include <script/script.h>8#include <crypto/common.h>9#include <crypto/hex_base.h>10#include <hash.h>11#include <uint256.h>12#include <util/hash_type.h>14#include <compare>15#include <string>17CScriptID::CScriptID(const CScript& in) : BaseHash(Hash160(in)) {}19std::string GetOpName(opcodetype opcode)20{21switch (opcode)22{23// push value24case OP_0 : return "0";25case OP_PUSHDATA1 : return "OP_PUSHDATA1";26case OP_PUSHDATA2 : return "OP_PUSHDATA2";27case OP_PUSHDATA4 : return "OP_PUSHDATA4";28case OP_1NEGATE : return "-1";29case OP_RESERVED : return "OP_RESERVED";30case OP_1 : return "1";31case OP_2 : return "2";32case OP_3 : return "3";33case OP_4 : return "4";34case OP_5 : return "5";35case OP_6 : return "6";36case OP_7 : return "7";37case OP_8 : return "8";38case OP_9 : return "9";39case OP_10 : return "10";40case OP_11 : return "11";41case OP_12 : return "12";42case OP_13 : return "13";43case OP_14 : return "14";44case OP_15 : return "15";45case OP_16 : return "16";47// control48case OP_NOP : return "OP_NOP";49case OP_VER : return "OP_VER";50case OP_IF : return "OP_IF";51case OP_NOTIF : return "OP_NOTIF";52case OP_VERIF : return "OP_VERIF";53case OP_VERNOTIF : return "OP_VERNOTIF";54case OP_ELSE : return "OP_ELSE";55case OP_ENDIF : return "OP_ENDIF";56case OP_VERIFY : return "OP_VERIFY";57case OP_RETURN : return "OP_RETURN";59// stack ops60case OP_TOALTSTACK : return "OP_TOALTSTACK";61case OP_FROMALTSTACK : return "OP_FROMALTSTACK";62case OP_2DROP : return "OP_2DROP";63case OP_2DUP : return "OP_2DUP";64case OP_3DUP : return "OP_3DUP";65case OP_2OVER : return "OP_2OVER";66case OP_2ROT : return "OP_2ROT";67case OP_2SWAP : return "OP_2SWAP";68case OP_IFDUP : return "OP_IFDUP";69case OP_DEPTH : return "OP_DEPTH";70case OP_DROP : return "OP_DROP";71case OP_DUP : return "OP_DUP";72case OP_NIP : return "OP_NIP";73case OP_OVER : return "OP_OVER";74case OP_PICK : return "OP_PICK";75case OP_ROLL : return "OP_ROLL";76case OP_ROT : return "OP_ROT";77case OP_SWAP : return "OP_SWAP";78case OP_TUCK : return "OP_TUCK";80// splice ops81case OP_CAT : return "OP_CAT";82case OP_SUBSTR : return "OP_SUBSTR";83case OP_LEFT : return "OP_LEFT";84case OP_RIGHT : return "OP_RIGHT";85case OP_SIZE : return "OP_SIZE";87// bit logic88case OP_INVERT : return "OP_INVERT";89case OP_AND : return "OP_AND";90case OP_OR : return "OP_OR";91case OP_XOR : return "OP_XOR";92case OP_EQUAL : return "OP_EQUAL";93case OP_EQUALVERIFY : return "OP_EQUALVERIFY";94case OP_RESERVED1 : return "OP_RESERVED1";95case OP_RESERVED2 : return "OP_RESERVED2";97// numeric98case OP_1ADD : return "OP_1ADD";99case OP_1SUB : return "OP_1SUB";100case OP_2MUL : return "OP_2MUL";101case OP_2DIV : return "OP_2DIV";102case OP_NEGATE : return "OP_NEGATE";103case OP_ABS : return "OP_ABS";104case OP_NOT : return "OP_NOT";105case OP_0NOTEQUAL : return "OP_0NOTEQUAL";106case OP_ADD : return "OP_ADD";107case OP_SUB : return "OP_SUB";108case OP_MUL : return "OP_MUL";109case OP_DIV : return "OP_DIV";110case OP_MOD : return "OP_MOD";111case OP_LSHIFT : return "OP_LSHIFT";112case OP_RSHIFT : return "OP_RSHIFT";113case OP_BOOLAND : return "OP_BOOLAND";114case OP_BOOLOR : return "OP_BOOLOR";115case OP_NUMEQUAL : return "OP_NUMEQUAL";116case OP_NUMEQUALVERIFY : return "OP_NUMEQUALVERIFY";117case OP_NUMNOTEQUAL : return "OP_NUMNOTEQUAL";118case OP_LESSTHAN : return "OP_LESSTHAN";119case OP_GREATERTHAN : return "OP_GREATERTHAN";120case OP_LESSTHANOREQUAL : return "OP_LESSTHANOREQUAL";121case OP_GREATERTHANOREQUAL : return "OP_GREATERTHANOREQUAL";122case OP_MIN : return "OP_MIN";123case OP_MAX : return "OP_MAX";124case OP_WITHIN : return "OP_WITHIN";126// crypto127case OP_RIPEMD160 : return "OP_RIPEMD160";128case OP_SHA1 : return "OP_SHA1";129case OP_SHA256 : return "OP_SHA256";130case OP_HASH160 : return "OP_HASH160";131case OP_HASH256 : return "OP_HASH256";132case OP_CODESEPARATOR : return "OP_CODESEPARATOR";133case OP_CHECKSIG : return "OP_CHECKSIG";134case OP_CHECKSIGVERIFY : return "OP_CHECKSIGVERIFY";135case OP_CHECKMULTISIG : return "OP_CHECKMULTISIG";136case OP_CHECKMULTISIGVERIFY : return "OP_CHECKMULTISIGVERIFY";138// expansion139case OP_NOP1 : return "OP_NOP1";140case OP_CHECKLOCKTIMEVERIFY : return "OP_CHECKLOCKTIMEVERIFY";141case OP_CHECKSEQUENCEVERIFY : return "OP_CHECKSEQUENCEVERIFY";142case OP_NOP4 : return "OP_NOP4";143case OP_NOP5 : return "OP_NOP5";144case OP_NOP6 : return "OP_NOP6";145case OP_NOP7 : return "OP_NOP7";146case OP_NOP8 : return "OP_NOP8";147case OP_NOP9 : return "OP_NOP9";148case OP_NOP10 : return "OP_NOP10";150// Opcode added by BIP 342 (Tapscript)151case OP_CHECKSIGADD : return "OP_CHECKSIGADD";153case OP_INVALIDOPCODE : return "OP_INVALIDOPCODE";155} // no default case, so the compiler can warn about missing cases156return "OP_UNKNOWN";157}159unsigned int CScript::GetSigOpCount(bool fAccurate) const160{161unsigned int n = 0;162const_iterator pc = begin();163opcodetype lastOpcode = OP_INVALIDOPCODE;164while (pc < end())165{166opcodetype opcode;167if (!GetOp(pc, opcode))168break;169if (opcode == OP_CHECKSIG || opcode == OP_CHECKSIGVERIFY)170n++;171else if (opcode == OP_CHECKMULTISIG || opcode == OP_CHECKMULTISIGVERIFY)172{173if (fAccurate && lastOpcode >= OP_1 && lastOpcode <= OP_16)174n += DecodeOP_N(lastOpcode);175else176n += MAX_PUBKEYS_PER_MULTISIG;177}178lastOpcode = opcode;179}180return n;181}183unsigned int CScript::GetSigOpCount(const CScript& scriptSig) const184{185if (!IsPayToScriptHash())186return GetSigOpCount(true);188// This is a pay-to-script-hash scriptPubKey;189// get the last item that the scriptSig190// pushes onto the stack:191const_iterator pc = scriptSig.begin();192std::vector<unsigned char> vData;193while (pc < scriptSig.end())194{195opcodetype opcode;196if (!scriptSig.GetOp(pc, opcode, vData))197return 0;198if (opcode > OP_16)199return 0;200}202/// ... and return its opcount:203CScript subscript(vData.begin(), vData.end());204return subscript.GetSigOpCount(true);205}207bool CScript::IsPayToAnchor() const208{209return (this->size() == 4 &&210(*this)[0] == OP_1 &&211(*this)[1] == 0x02 &&212(*this)[2] == 0x4e &&213(*this)[3] == 0x73);214}216bool CScript::IsPayToAnchor(int version, const std::vector<unsigned char>& program)217{218return version == 1 &&219program.size() == 2 &&220program[0] == 0x4e &&221program[1] == 0x73;222}224bool CScript::IsPayToScriptHash() const225{226// Extra-fast test for pay-to-script-hash CScripts:227return (this->size() == 23 &&228(*this)[0] == OP_HASH160 &&229(*this)[1] == 0x14 &&230(*this)[22] == OP_EQUAL);231}233bool CScript::IsPayToWitnessScriptHash() const234{235// Extra-fast test for pay-to-witness-script-hash CScripts:236return (this->size() == 34 &&237(*this)[0] == OP_0 &&238(*this)[1] == 0x20);239}241bool CScript::IsPayToTaproot() const242{243return (this->size() == 34 &&244(*this)[0] == OP_1 &&245(*this)[1] == 0x20);246}248// A witness program is any valid CScript that consists of a 1-byte push opcode249// followed by a data push between 2 and 40 bytes.250bool CScript::IsWitnessProgram(int& version, std::vector<unsigned char>& program) const
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.