Outputs: ScriptPubKey Size

TransactionScriptPubKey Size
Structure
Version
4 bytes
Segwit (Optional)
Marker
1 byte
Flag
1 byte
Structure
Input Count
variable
Input Structure
Previous TXID
32 bytes
Output Index
4 bytes
ScriptSig Size
variable
ScriptSig
variable
Sequence
4 bytes
Structure
Output Count
variable
Output Structure
Amount
8 bytes
ScriptPubKey Size
variable
ScriptPubKey
variable
Structure
Witness
variable
Structure
Locktime
4 bytes

Size

variable

Format

Compact Size

Description

The size of the output script.

Example

19

Byte Visualization

19
Byte 1

The ScriptPubKey size is stored as a variable-length integer (varint) that indicates the length of the following ScriptPubKey field in bytes.

Let's examine a real transaction to understand this better:

SVG Image

Common ScriptPubKey Sizes

Different types of Bitcoin addresses result in different ScriptPubKey sizes:

Script TypeSize (bytes)Compact Size Value
P2PK35 or 670x23 or 0x43
P2PKH250x19
P2SH230x17
P2WPKH220x16
P2WSH340x22

Note

The most common script type today is P2WPKH (Pay to Witness Public Key Hash), which has a ScriptPubKey size of 22 bytes (0x16).

Variable Integer Encoding

Like other variable-length fields in Bitcoin transactions, the ScriptPubKey size uses varint encoding:

Decimal RangeHex RangeBytes UsedFormat
0 to 2520x00 to 0xfc1uint8_t
253 to 2¹⁶-10xfd to 0xffff30xfd + uint16_t
2¹⁶ to 2³²-10x10000 to 0xffffffff50xfe + uint32_t
2³² to 2⁶⁴-10x100000000 to 0xffffffffffffffff90xff + uint64_t

Implementation Example

Here's how you might parse a ScriptPubKey size:

python
def parse_scriptpubkey_size(raw_tx: bytes, offset: int = 0) -> tuple[int, int]:
    """
    Parse ScriptPubKey size from raw transaction bytes
    
    Args:
        raw_tx: Raw transaction bytes
        offset: Starting position in bytes
        
    Returns:
        (size, new_offset)
    """
    # Read the varint
    first_byte = raw_tx[offset]
    
    if first_byte < 0xfd:
        size = first_byte
        offset += 1
    elif first_byte == 0xfd:
        size = int.from_bytes(raw_tx[offset+1:offset+3], 'little')
        offset += 3
    elif first_byte == 0xfe:
        size = int.from_bytes(raw_tx[offset+1:offset+5], 'little')
        offset += 5
    else:  # 0xff
        size = int.from_bytes(raw_tx[offset+1:offset+9], 'little')
        offset += 9
        
    return size, offset

Warning

The ScriptPubKey size must accurately reflect the length of the following ScriptPubKey field. If these don't match, the transaction is invalid.

Suggest Edits
View our public visitor count
Built with 🧡 by the Bitcoin Dev Project
We'd love to hear your feedback on this project?Give Feedback