Inside Bitcoin
Opening the selected file…
Inside Bitcoin
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.
251OP_NOP7 = CScriptOp(0xb6)252OP_NOP8 = CScriptOp(0xb7)253OP_NOP9 = CScriptOp(0xb8)254OP_NOP10 = CScriptOp(0xb9)256# BIP 342 opcodes (Tapscript)257OP_CHECKSIGADD = CScriptOp(0xba)259OP_INVALIDOPCODE = CScriptOp(0xff)261OPCODE_NAMES.update({262OP_0: 'OP_0',263OP_PUSHDATA1: 'OP_PUSHDATA1',264OP_PUSHDATA2: 'OP_PUSHDATA2',265OP_PUSHDATA4: 'OP_PUSHDATA4',266OP_1NEGATE: 'OP_1NEGATE',267OP_RESERVED: 'OP_RESERVED',268OP_1: 'OP_1',269OP_2: 'OP_2',270OP_3: 'OP_3',271OP_4: 'OP_4',272OP_5: 'OP_5',273OP_6: 'OP_6',274OP_7: 'OP_7',275OP_8: 'OP_8',276OP_9: 'OP_9',277OP_10: 'OP_10',278OP_11: 'OP_11',279OP_12: 'OP_12',280OP_13: 'OP_13',281OP_14: 'OP_14',282OP_15: 'OP_15',283OP_16: 'OP_16',284OP_NOP: 'OP_NOP',285OP_VER: 'OP_VER',286OP_IF: 'OP_IF',287OP_NOTIF: 'OP_NOTIF',288OP_VERIF: 'OP_VERIF',289OP_VERNOTIF: 'OP_VERNOTIF',290OP_ELSE: 'OP_ELSE',291OP_ENDIF: 'OP_ENDIF',292OP_VERIFY: 'OP_VERIFY',293OP_RETURN: 'OP_RETURN',294OP_TOALTSTACK: 'OP_TOALTSTACK',295OP_FROMALTSTACK: 'OP_FROMALTSTACK',296OP_2DROP: 'OP_2DROP',297OP_2DUP: 'OP_2DUP',298OP_3DUP: 'OP_3DUP',299OP_2OVER: 'OP_2OVER',300OP_2ROT: 'OP_2ROT',301OP_2SWAP: 'OP_2SWAP',302OP_IFDUP: 'OP_IFDUP',303OP_DEPTH: 'OP_DEPTH',304OP_DROP: 'OP_DROP',305OP_DUP: 'OP_DUP',306OP_NIP: 'OP_NIP',307OP_OVER: 'OP_OVER',308OP_PICK: 'OP_PICK',309OP_ROLL: 'OP_ROLL',310OP_ROT: 'OP_ROT',311OP_SWAP: 'OP_SWAP',312OP_TUCK: 'OP_TUCK',313OP_CAT: 'OP_CAT',314OP_SUBSTR: 'OP_SUBSTR',315OP_LEFT: 'OP_LEFT',316OP_RIGHT: 'OP_RIGHT',317OP_SIZE: 'OP_SIZE',318OP_INVERT: 'OP_INVERT',319OP_AND: 'OP_AND',320OP_OR: 'OP_OR',321OP_XOR: 'OP_XOR',322OP_EQUAL: 'OP_EQUAL',323OP_EQUALVERIFY: 'OP_EQUALVERIFY',324OP_RESERVED1: 'OP_RESERVED1',325OP_RESERVED2: 'OP_RESERVED2',326OP_1ADD: 'OP_1ADD',327OP_1SUB: 'OP_1SUB',328OP_2MUL: 'OP_2MUL',329OP_2DIV: 'OP_2DIV',330OP_NEGATE: 'OP_NEGATE',331OP_ABS: 'OP_ABS',332OP_NOT: 'OP_NOT',333OP_0NOTEQUAL: 'OP_0NOTEQUAL',334OP_ADD: 'OP_ADD',335OP_SUB: 'OP_SUB',336OP_MUL: 'OP_MUL',337OP_DIV: 'OP_DIV',338OP_MOD: 'OP_MOD',339OP_LSHIFT: 'OP_LSHIFT',340OP_RSHIFT: 'OP_RSHIFT',341OP_BOOLAND: 'OP_BOOLAND',342OP_BOOLOR: 'OP_BOOLOR',343OP_NUMEQUAL: 'OP_NUMEQUAL',344OP_NUMEQUALVERIFY: 'OP_NUMEQUALVERIFY',345OP_NUMNOTEQUAL: 'OP_NUMNOTEQUAL',346OP_LESSTHAN: 'OP_LESSTHAN',347OP_GREATERTHAN: 'OP_GREATERTHAN',348OP_LESSTHANOREQUAL: 'OP_LESSTHANOREQUAL',349OP_GREATERTHANOREQUAL: 'OP_GREATERTHANOREQUAL',350OP_MIN: 'OP_MIN',351OP_MAX: 'OP_MAX',352OP_WITHIN: 'OP_WITHIN',353OP_RIPEMD160: 'OP_RIPEMD160',354OP_SHA1: 'OP_SHA1',355OP_SHA256: 'OP_SHA256',356OP_HASH160: 'OP_HASH160',357OP_HASH256: 'OP_HASH256',358OP_CODESEPARATOR: 'OP_CODESEPARATOR',359OP_CHECKSIG: 'OP_CHECKSIG',360OP_CHECKSIGVERIFY: 'OP_CHECKSIGVERIFY',361OP_CHECKMULTISIG: 'OP_CHECKMULTISIG',362OP_CHECKMULTISIGVERIFY: 'OP_CHECKMULTISIGVERIFY',363OP_NOP1: 'OP_NOP1',364OP_CHECKLOCKTIMEVERIFY: 'OP_CHECKLOCKTIMEVERIFY',365OP_CHECKSEQUENCEVERIFY: 'OP_CHECKSEQUENCEVERIFY',366OP_NOP4: 'OP_NOP4',367OP_NOP5: 'OP_NOP5',368OP_NOP6: 'OP_NOP6',369OP_NOP7: 'OP_NOP7',370OP_NOP8: 'OP_NOP8',371OP_NOP9: 'OP_NOP9',372OP_NOP10: 'OP_NOP10',373OP_CHECKSIGADD: 'OP_CHECKSIGADD',374OP_INVALIDOPCODE: 'OP_INVALIDOPCODE',375})377class CScriptInvalidError(Exception):378"""Base class for CScript exceptions"""379pass381class CScriptTruncatedPushDataError(CScriptInvalidError):382"""Invalid pushdata due to truncation"""383def __init__(self, msg, data):384self.data = data385super().__init__(msg)388# This is used, eg, for blockchain heights in coinbase scripts (bip34)389class CScriptNum:390__slots__ = ("value",)392def __init__(self, d=0):393self.value = d395@staticmethod396def encode(obj):397r = bytearray(0)398if obj.value == 0:399return bytes(r)400neg = obj.value < 0401absvalue = -obj.value if neg else obj.value402while (absvalue):403r.append(absvalue & 0xff)404absvalue >>= 8405if r[-1] & 0x80:406r.append(0x80 if neg else 0)407elif neg:408r[-1] |= 0x80409return bytes([len(r)]) + r411@staticmethod412def decode(vch):413result = 0414# We assume valid push_size and minimal encoding415value = vch[1:]416if len(value) == 0:417return result418for i, byte in enumerate(value):419result |= int(byte) << 8 * i420if value[-1] >= 0x80:421# Mask for all but the highest result bit422num_mask = (2**(len(value) * 8) - 1) >> 1423result &= num_mask424result *= -1425return result428class CScript(bytes):429"""Serialized script431A bytes subclass, so you can use this directly whenever bytes are accepted.432Note that this means that indexing does *not* work - you'll get an index by433byte rather than opcode. This format was chosen for efficiency so that the434general case would not require creating a lot of little CScriptOP objects.436iter(script) however does iterate by opcode.437"""438__slots__ = ()440@classmethod441def __coerce_instance(cls, other):442# Coerce other into bytes443if isinstance(other, CScriptOp):444other = bytes([other])445elif isinstance(other, CScriptNum):446if (other.value == 0):447other = bytes([CScriptOp(OP_0)])448else:449other = CScriptNum.encode(other)450elif isinstance(other, int):451if 0 <= other <= 16:452other = bytes([CScriptOp.encode_op_n(other)])453elif other == -1:454other = bytes([OP_1NEGATE])455else:456other = CScriptOp.encode_op_pushdata(bn2vch(other))457elif isinstance(other, (bytes, bytearray)):458other = CScriptOp.encode_op_pushdata(other)459return other461def __add__(self, other):462# add makes no sense for a CScript()463raise NotImplementedError465def join(self, iterable):466# join makes no sense for a CScript()467raise NotImplementedError469def __new__(cls, value=b''):470if isinstance(value, bytes) or isinstance(value, bytearray):471return super().__new__(cls, value)472else:473def coerce_iterable(iterable):474for instance in iterable:475yield cls.__coerce_instance(instance)476# Annoyingly on both python2 and python3 bytes.join() always477# returns a bytes instance even when subclassed.478return super().__new__(cls, b''.join(coerce_iterable(value)))480def raw_iter(self):481"""Raw iteration483Yields tuples of (opcode, data, sop_idx) so that the different possible484PUSHDATA encodings can be accurately distinguished, as well as485determining the exact opcode byte indexes. (sop_idx)486"""487i = 0488while i < len(self):489sop_idx = i490opcode = CScriptOp(self[i])491i += 1493if opcode > OP_PUSHDATA4:494yield (opcode, None, sop_idx)495else:496datasize = None497pushdata_type = None498if opcode < OP_PUSHDATA1:499pushdata_type = 'PUSHDATA(%d)' % opcode500datasize = opcode
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.