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.
1#!/usr/bin/env python32# Copyright (c) 2015-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.5"""Functionality to build scripts, as well as signature hash functions.7This file is modified from python-bitcoinlib.8"""10from collections import namedtuple11import unittest13from .key import TaggedHash, tweak_add_pubkey, compute_xonly_pubkey15from .messages import (16CTransaction,17CTxOut,18hash256,19ser_string,20sha256,21)23from .crypto.ripemd160 import ripemd16024from .util import assert_equal26MAX_SCRIPT_ELEMENT_SIZE = 52027MAX_SCRIPT_SIZE = 1000028MAX_PUBKEYS_PER_MULTI_A = 99929LOCKTIME_THRESHOLD = 50000000030ANNEX_TAG = 0x5032SEQUENCE_LOCKTIME_DISABLE_FLAG = (1<<31)33SEQUENCE_LOCKTIME_TYPE_FLAG = (1<<22) # this means use time (0 means height)34SEQUENCE_LOCKTIME_GRANULARITY = 9 # this is a bit-shift35SEQUENCE_LOCKTIME_MASK = 0x0000ffff37LEAF_VERSION_TAPSCRIPT = 0xc039def hash160(s):40return ripemd160(sha256(s))42def bn2vch(v):43"""Convert number to bitcoin-specific little endian format."""44# We need v.bit_length() bits, plus a sign bit for every nonzero number.45n_bits = v.bit_length() + (v != 0)46# The number of bytes for that is:47n_bytes = (n_bits + 7) // 848# Convert number to absolute value + sign in top bit.49encoded_v = 0 if v == 0 else abs(v) | ((v < 0) << (n_bytes * 8 - 1))50# Serialize to bytes51return encoded_v.to_bytes(n_bytes, 'little')53class CScriptOp(int):54"""A single script opcode"""55__slots__ = ()57@staticmethod58def encode_op_pushdata(d):59"""Encode a PUSHDATA op, returning bytes"""60if len(d) < 0x4c:61return b'' + bytes([len(d)]) + d # OP_PUSHDATA62elif len(d) <= 0xff:63return b'\x4c' + bytes([len(d)]) + d # OP_PUSHDATA164elif len(d) <= 0xffff:65return b'\x4d' + len(d).to_bytes(2, "little") + d # OP_PUSHDATA266elif len(d) <= 0xffffffff:67return b'\x4e' + len(d).to_bytes(4, "little") + d # OP_PUSHDATA468else:69raise ValueError("Data too long to encode in a PUSHDATA op")71@staticmethod72def encode_op_n(n):73"""Encode a small integer op, returning an opcode"""74if not (0 <= n <= 16):75raise ValueError('Integer must be in range 0 <= n <= 16, got %d' % n)77if n == 0:78return OP_079else:80return CScriptOp(OP_1 + n - 1)82def decode_op_n(self):83"""Decode a small integer opcode, returning an integer"""84if self == OP_0:85return 087if not (self == OP_0 or OP_1 <= self <= OP_16):88raise ValueError('op %r is not an OP_N' % self)90return int(self - OP_1 + 1)92def is_small_int(self):93"""Return true if the op pushes a small integer to the stack"""94if 0x51 <= self <= 0x60 or self == 0:95return True96else:97return False99def __str__(self):100return repr(self)102def __repr__(self):103if self in OPCODE_NAMES:104return OPCODE_NAMES[self]105else:106return 'CScriptOp(0x%x)' % self108def __new__(cls, n):109try:110return _opcode_instances[n]111except IndexError:112assert_equal(len(_opcode_instances), n)113_opcode_instances.append(super().__new__(cls, n))114return _opcode_instances[n]116OPCODE_NAMES: dict[CScriptOp, str] = {}117_opcode_instances: list[CScriptOp] = []119# Populate opcode instance table120for n in range(0xff + 1):121CScriptOp(n)124# push value125OP_0 = CScriptOp(0x00)126OP_FALSE = OP_0127OP_PUSHDATA1 = CScriptOp(0x4c)128OP_PUSHDATA2 = CScriptOp(0x4d)129OP_PUSHDATA4 = CScriptOp(0x4e)130OP_1NEGATE = CScriptOp(0x4f)131OP_RESERVED = CScriptOp(0x50)132OP_1 = CScriptOp(0x51)133OP_TRUE = OP_1134OP_2 = CScriptOp(0x52)135OP_3 = CScriptOp(0x53)136OP_4 = CScriptOp(0x54)137OP_5 = CScriptOp(0x55)138OP_6 = CScriptOp(0x56)139OP_7 = CScriptOp(0x57)140OP_8 = CScriptOp(0x58)141OP_9 = CScriptOp(0x59)142OP_10 = CScriptOp(0x5a)143OP_11 = CScriptOp(0x5b)144OP_12 = CScriptOp(0x5c)145OP_13 = CScriptOp(0x5d)146OP_14 = CScriptOp(0x5e)147OP_15 = CScriptOp(0x5f)148OP_16 = CScriptOp(0x60)150# control151OP_NOP = CScriptOp(0x61)152OP_VER = CScriptOp(0x62)153OP_IF = CScriptOp(0x63)154OP_NOTIF = CScriptOp(0x64)155OP_VERIF = CScriptOp(0x65)156OP_VERNOTIF = CScriptOp(0x66)157OP_ELSE = CScriptOp(0x67)158OP_ENDIF = CScriptOp(0x68)159OP_VERIFY = CScriptOp(0x69)160OP_RETURN = CScriptOp(0x6a)162# stack ops163OP_TOALTSTACK = CScriptOp(0x6b)164OP_FROMALTSTACK = CScriptOp(0x6c)165OP_2DROP = CScriptOp(0x6d)166OP_2DUP = CScriptOp(0x6e)167OP_3DUP = CScriptOp(0x6f)168OP_2OVER = CScriptOp(0x70)169OP_2ROT = CScriptOp(0x71)170OP_2SWAP = CScriptOp(0x72)171OP_IFDUP = CScriptOp(0x73)172OP_DEPTH = CScriptOp(0x74)173OP_DROP = CScriptOp(0x75)174OP_DUP = CScriptOp(0x76)175OP_NIP = CScriptOp(0x77)176OP_OVER = CScriptOp(0x78)177OP_PICK = CScriptOp(0x79)178OP_ROLL = CScriptOp(0x7a)179OP_ROT = CScriptOp(0x7b)180OP_SWAP = CScriptOp(0x7c)181OP_TUCK = CScriptOp(0x7d)183# splice ops184OP_CAT = CScriptOp(0x7e)185OP_SUBSTR = CScriptOp(0x7f)186OP_LEFT = CScriptOp(0x80)187OP_RIGHT = CScriptOp(0x81)188OP_SIZE = CScriptOp(0x82)190# bit logic191OP_INVERT = CScriptOp(0x83)192OP_AND = CScriptOp(0x84)193OP_OR = CScriptOp(0x85)194OP_XOR = CScriptOp(0x86)195OP_EQUAL = CScriptOp(0x87)196OP_EQUALVERIFY = CScriptOp(0x88)197OP_RESERVED1 = CScriptOp(0x89)198OP_RESERVED2 = CScriptOp(0x8a)200# numeric201OP_1ADD = CScriptOp(0x8b)202OP_1SUB = CScriptOp(0x8c)203OP_2MUL = CScriptOp(0x8d)204OP_2DIV = CScriptOp(0x8e)205OP_NEGATE = CScriptOp(0x8f)206OP_ABS = CScriptOp(0x90)207OP_NOT = CScriptOp(0x91)208OP_0NOTEQUAL = CScriptOp(0x92)210OP_ADD = CScriptOp(0x93)211OP_SUB = CScriptOp(0x94)212OP_MUL = CScriptOp(0x95)213OP_DIV = CScriptOp(0x96)214OP_MOD = CScriptOp(0x97)215OP_LSHIFT = CScriptOp(0x98)216OP_RSHIFT = CScriptOp(0x99)218OP_BOOLAND = CScriptOp(0x9a)219OP_BOOLOR = CScriptOp(0x9b)220OP_NUMEQUAL = CScriptOp(0x9c)221OP_NUMEQUALVERIFY = CScriptOp(0x9d)222OP_NUMNOTEQUAL = CScriptOp(0x9e)223OP_LESSTHAN = CScriptOp(0x9f)224OP_GREATERTHAN = CScriptOp(0xa0)225OP_LESSTHANOREQUAL = CScriptOp(0xa1)226OP_GREATERTHANOREQUAL = CScriptOp(0xa2)227OP_MIN = CScriptOp(0xa3)228OP_MAX = CScriptOp(0xa4)230OP_WITHIN = CScriptOp(0xa5)232# crypto233OP_RIPEMD160 = CScriptOp(0xa6)234OP_SHA1 = CScriptOp(0xa7)235OP_SHA256 = CScriptOp(0xa8)236OP_HASH160 = CScriptOp(0xa9)237OP_HASH256 = CScriptOp(0xaa)238OP_CODESEPARATOR = CScriptOp(0xab)239OP_CHECKSIG = CScriptOp(0xac)240OP_CHECKSIGVERIFY = CScriptOp(0xad)241OP_CHECKMULTISIG = CScriptOp(0xae)242OP_CHECKMULTISIGVERIFY = CScriptOp(0xaf)244# expansion245OP_NOP1 = CScriptOp(0xb0)246OP_CHECKLOCKTIMEVERIFY = CScriptOp(0xb1)247OP_CHECKSEQUENCEVERIFY = CScriptOp(0xb2)248OP_NOP4 = CScriptOp(0xb3)249OP_NOP5 = CScriptOp(0xb4)250OP_NOP6 = CScriptOp(0xb5)
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.