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.
251{252if (this->size() < 4 || this->size() > 42) {253return false;254}255if ((*this)[0] != OP_0 && ((*this)[0] < OP_1 || (*this)[0] > OP_16)) {256return false;257}258if ((size_t)((*this)[1] + 2) == this->size()) {259version = DecodeOP_N((opcodetype)(*this)[0]);260program = std::vector<unsigned char>(this->begin() + 2, this->end());261return true;262}263return false;264}266bool CScript::IsPushOnly(const_iterator pc) const267{268while (pc < end())269{270opcodetype opcode;271if (!GetOp(pc, opcode))272return false;273// Note that IsPushOnly() *does* consider OP_RESERVED to be a274// push-type opcode, however execution of OP_RESERVED fails, so275// it's not relevant to P2SH/BIP62 as the scriptSig would fail prior to276// the P2SH special validation code being executed.277if (opcode > OP_16)278return false;279}280return true;281}283bool CScript::IsPushOnly() const284{285return this->IsPushOnly(begin());286}288std::string CScriptWitness::ToString() const289{290std::string ret = "CScriptWitness(";291for (unsigned int i = 0; i < stack.size(); i++) {292if (i) {293ret += ", ";294}295ret += HexStr(stack[i]);296}297return ret + ")";298}300bool CScript::HasValidOps() const301{302CScript::const_iterator it = begin();303while (it < end()) {304opcodetype opcode;305std::vector<unsigned char> item;306if (!GetOp(it, opcode, item) || opcode > MAX_OPCODE || item.size() > MAX_SCRIPT_ELEMENT_SIZE) {307return false;308}309}310return true;311}313bool GetScriptOp(CScriptBase::const_iterator& pc, CScriptBase::const_iterator end, opcodetype& opcodeRet, std::vector<unsigned char>* pvchRet)314{315opcodeRet = OP_INVALIDOPCODE;316if (pvchRet)317pvchRet->clear();318if (pc >= end)319return false;321// Read instruction322if (end - pc < 1)323return false;324unsigned int opcode = *pc++;326// Immediate operand327if (opcode <= OP_PUSHDATA4)328{329unsigned int nSize = 0;330if (opcode < OP_PUSHDATA1)331{332nSize = opcode;333}334else if (opcode == OP_PUSHDATA1)335{336if (end - pc < 1)337return false;338nSize = *pc++;339}340else if (opcode == OP_PUSHDATA2)341{342if (end - pc < 2)343return false;344nSize = ReadLE16(&pc[0]);345pc += 2;346}347else if (opcode == OP_PUSHDATA4)348{349if (end - pc < 4)350return false;351nSize = ReadLE32(&pc[0]);352pc += 4;353}354if (end - pc < 0 || (unsigned int)(end - pc) < nSize)355return false;356if (pvchRet)357pvchRet->assign(pc, pc + nSize);358pc += nSize;359}361opcodeRet = static_cast<opcodetype>(opcode);362return true;363}365bool IsOpSuccess(const opcodetype& opcode)366{367return opcode == 80 || opcode == 98 || (opcode >= 126 && opcode <= 129) ||368(opcode >= 131 && opcode <= 134) || (opcode >= 137 && opcode <= 138) ||369(opcode >= 141 && opcode <= 142) || (opcode >= 149 && opcode <= 153) ||370(opcode >= 187 && opcode <= 254);371}373bool CheckMinimalPush(const std::vector<unsigned char>& data, opcodetype opcode) {374// Excludes OP_1NEGATE, OP_1-16 since they are by definition minimal375assert(0 <= opcode && opcode <= OP_PUSHDATA4);376if (data.size() == 0) {377// Should have used OP_0.378return opcode == OP_0;379} else if (data.size() == 1 && data[0] >= 1 && data[0] <= 16) {380// Should have used OP_1 .. OP_16.381return false;382} else if (data.size() == 1 && data[0] == 0x81) {383// Should have used OP_1NEGATE.384return false;385} else if (data.size() <= 75) {386// Must have used a direct push (opcode indicating number of bytes pushed + those bytes).387return opcode == data.size();388} else if (data.size() <= 255) {389// Must have used OP_PUSHDATA.390return opcode == OP_PUSHDATA1;391} else if (data.size() <= 65535) {392// Must have used OP_PUSHDATA2.393return opcode == OP_PUSHDATA2;394}395return true;396}
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.