Reading scriptPubKey Hex Byte by Byte
How to read a Bitcoin scriptPubKey in hex: what 76a914...88ac, a914...87, 0014, 0020, 5120 and 6a mean, how to tell the type from the first bytes and the length, how to turn the hex into an address, and where the scriptPubKey sits inside a raw transaction.
Contents
Decode a transaction with bitcoin-cli decoderawtransaction or open it in an explorer's API, and every output carries a hex string such as 76a91462e907b15cbf27d5425399ebf6f0fb50ebb88f1888ac. That string is the scriptPubKey, the condition someone has to meet to spend the output. The address your wallet shows is only a human-friendly encoding of these bytes. The blockchain stores the bytes, not the address.
The standard scriptPubKeys are short and have fixed shapes, so the first one or two bytes and the total length are almost always enough to tell which type you are looking at. This post covers how to read them that way.
Two Kinds of Bytes
A script is a sequence of bytes, and each byte is either an opcode (an instruction) or a push that puts the following data on the stack. You only need a handful of values to read the standard types.
| Hex | Name | Meaning |
|---|---|---|
00 | OP_0 | Pushes an empty value. In SegWit it is the witness version 0 |
01-4b | (push) | Push the next 1 to 75 bytes. 14 means 20 bytes and 20 means 32 bytes |
4c / 4d | OP_PUSHDATA1 / 2 | The next 1 or 2 bytes give the length of the data that follows |
51-60 | OP_1 to OP_16 | The numbers 1 to 16. In SegWit they are witness versions 1 to 16 |
6a | OP_RETURN | Marks the output as unspendable |
76 | OP_DUP | Duplicates the top stack item |
87 | OP_EQUAL | Compares two values |
88 | OP_EQUALVERIFY | Compares, and fails the script if they differ |
a9 | OP_HASH160 | SHA-256 followed by RIPEMD-160 |
ac | OP_CHECKSIG | Checks a signature |
ae | OP_CHECKMULTISIG | Checks several signatures |
Push lengths are hex too. 14 is 20 bytes, not 14, and 20 is 32 bytes. Most misreadings start here.
The Standard Types at a Glance
| Type | Hex shape | Length | Bitcoin Core type | Explorer label | Address |
|---|---|---|---|---|---|
| P2PKH | 76a914 + 20 bytes + 88ac | 25 bytes | pubkeyhash | p2pkh | starts with 1 |
| P2SH | a914 + 20 bytes + 87 | 23 bytes | scripthash | p2sh | starts with 3 |
| P2WPKH | 0014 + 20 bytes | 22 bytes | witness_v0_keyhash | v0_p2wpkh | starts with bc1q |
| P2WSH | 0020 + 32 bytes | 34 bytes | witness_v0_scripthash | v0_p2wsh | starts with bc1q (longer) |
| P2TR | 5120 + 32 bytes | 34 bytes | witness_v1_taproot | v1_p2tr | starts with bc1p |
| P2PK | 21 + 33 bytes + ac, or 41 + 65 bytes + ac | 35 or 67 bytes | pubkey | p2pk | none |
| Bare multisig | 51-53 + keys + 51-53 + ae | varies | multisig | multisig | none |
| OP_RETURN | 6a + pushes | varies | nulldata | op_return | none |
| P2A (anchor) | 51024e73 | 4 bytes | anchor | bc1pfeessrawgf |
The Core names are what decoderawtransaction and decodescript print, as defined in src/script/solver.cpp. The explorer labels are the scriptpubkey_type values in the Esplora API used by mempool.space and Blockstream's explorer.
Reading Each Type
P2PKH: 76a914 ... 88ac
76 OP_DUP
a9 OP_HASH160
14 push 20 bytes
62e907b15cbf27d5425399ebf6f0fb50ebb88f18 public key hash (HASH160)
88 OP_EQUALVERIFY
ac OP_CHECKSIG
To spend it, you show a public key, the script checks that its HASH160 matches these 20 bytes, and then checks the signature against that key. The 20 bytes in this example are the hash of the public key in the genesis block's coinbase. Encode them as an address and you get the well-known 1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa.
P2SH: a914 ... 87
a9 OP_HASH160
14 push 20 bytes
b472a266d0bd89c13706a4132ccfb16f7c3b9fcb script hash
87 OP_EQUAL
The 20 bytes are the HASH160 of another script (the redeemScript). That script could be a multisig, a P2WPKH nested inside P2SH, or a timelock. The scriptPubKey alone cannot tell you which. The script is only revealed when the output is spent. This example encodes to 3J98t1WpEZ73CNmQviecrnyiWrnqRhWNLy.
P2WPKH and P2WSH: 0014 and 0020
A SegWit scriptPubKey has a fixed shape: one version byte followed by one push of the witness program. Version 0 comes in two kinds, told apart only by the length of the program.
00 witness version 0
14 push 20 bytes
751e76e8199196d454941c45d1b3a323f1433bd6 public key hash -> P2WPKH
00 witness version 0
20 push 32 bytes
1863143c14c5166804bd19203356da136c985678cd4d27a1b8c6329604903262 script hash -> P2WSH
These two examples are the mainnet test vectors from BIP-173, bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4 and bc1qrp33g0q5c5txsp9arysrx4k6zdkfs4nce4xj0gdcccefvpysxf3qccfmv3. The P2WSH hash is a single SHA-256 of the script, not HASH160. That is why P2SH carries 20 bytes and P2WSH carries 32.
P2TR: 5120 ...
51 OP_1 (witness version 1)
20 push 32 bytes
79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798 output key (x coordinate only)
From version 1 onward the version byte is not 01 but 51 (OP_1). A program that begins 01 is a 1-byte push and not a SegWit output at all. The 32 bytes are the x coordinate of the output key defined in BIP-341. This is not the wallet's internal key as it is, but a key tweaked by the commitment to the script tree. So a Taproot output looks the same from the outside whether or not it hides scripts. The example is a BIP-350 test vector that uses the generator point G as the program; it encodes to bc1p0xlxvlhemja6c4dqv22uapctqupfhlxm9h8z3k2e72q4k9hcz7vqzk5jj0.
Versions 2 to 16 are not defined yet. Bitcoin Core labels them witness_unknown and treats outputs to them as standard so that future soft forks can use them, but they are not a type you should send to.
P2PK and Bare Multisig
The oldest shape just pushes a public key and ends with OP_CHECKSIG. Mining rewards in 2009 and the transfer from Satoshi to Hal Finney at block 170 were locked this way. An uncompressed key is 65 bytes, so the script starts with 41 (65); a compressed key is 33 bytes, so it starts with 21.
Bare multisig puts the keys directly into the scriptPubKey. A 2-of-3 looks like this.
52 OP_2 (signatures required)
21 <33-byte key> 21 <33-byte key> 21 <33-byte key>
53 OP_3 (number of keys)
ae OP_CHECKMULTISIG
Neither of these has a standard address format. The Esplora API, for example, returns no address field for a P2PK output. When 1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa is shown as the "genesis address", it is the P2PKH encoding of the same key's hash, which is a different script from the P2PK output actually recorded in the block. Bitcoin Core treats bare multisig with up to three keys as standard (policy.cpp). Today multisig is usually wrapped in P2SH, P2WSH or P2TR (multisig).
OP_RETURN: 6a ...
6a OP_RETURN
0b push 11 bytes
68656c6c6f20776f726c64 "hello world"
An output that starts with 6a can never be spent. Anything after it is data, and Core labels it nulldata as long as everything after 6a consists only of pushes. Data over 75 bytes is pushed with 4c (OP_PUSHDATA1) followed by a length byte. How much data relay policy allows changed in Core 30; see the OP_RETURN debate.
P2A: 51024e73
A 4-byte output: OP_1 followed by a 2-byte push of 4e73. It is a "pay-to-anchor" output that anyone can spend without a key, so a child transaction can attach to it and bump the parent's fee (CPFP). It is meant for protocols such as Lightning that sign transactions in advance without knowing the future fee. Bitcoin Core 28.0 began treating transactions that spend it as standard. Its address is the fixed string bc1pfeessrawgf.
From Hex to Address
An address carries the bytes inside the scriptPubKey plus a type marker.
- P2PKH - prefix the 20 bytes with the version byte
00and encode with Base58Check (a 4-byte checksum from double SHA-256 is appended). The result starts with 1. - P2SH - same, with version byte
05. The result starts with 3. - SegWit version 0 - encode the version (0) and the program with Bech32 under the
bcprefix. - SegWit version 1 and up - same, but with Bech32m (BIP-350). The checksum constant differs from Bech32, so encoding a Taproot program with plain Bech32 gives an address with an invalid checksum.
The reverse works the same way. Decode an address and you get the version and program, and from those you can rebuild the scriptPubKey exactly. In P2PKH and P2SH the 20 bytes between the version byte and the checksum are what goes into the script. In a bc1 address the character after 1 is the version: q is 0 and p is 1. The txid.uk Bech32 decoder shows this split, and how Bitcoin addresses work covers the key-to-address steps.
Finding the scriptPubKey in a Raw Transaction
In a raw transaction each output is "8-byte amount + script length + scriptPubKey". Here is the first output of the Satoshi to Hal Finney transaction at block 170 (TXID f4184fc5...1e9e16).
00ca9a3b00000000 amount, 8 bytes little-endian -> 1,000,000,000 sat (10 BTC)
43 script length 0x43 = 67 bytes
4104ae1a62fe...6cd84cac P2PK: 41 (65-byte push) + uncompressed key + ac
The input side of the same transaction holds the reference to the coin it spends (the outpoint).
c997a5e56e104102fa209c6a852dd90660a20b2d9c352423edce25857fcd3704 previous TXID (byte-reversed)
00000000 vout = 0
Reverse those 32 bytes and you get 0437cd7f...e5a597c9, the TXID you would search for in an explorer. Inside the transaction the hash is stored in little-endian order, while explorers and RPCs display it in reverse, so you have to flip it when copying a TXID out of raw hex. The vout is also a 4-byte little-endian number. Why the display order is flipped is explained in What Is a TXID?.
The script length is a variable-length integer (CompactSize). Values below 253 fit in one byte, so for the common types it reads 19 (25 bytes, P2PKH), 17 (23 bytes, P2SH), 16 (22 bytes, P2WPKH), or 22 (34 bytes, P2WSH and P2TR). The value is also hex: 22 here means 34 bytes.
Tools for Checking
bitcoin-cli decodescript <hex>- prints the opcodes (asm), the type, the address if there is one, and a descriptor.bitcoin-cli decoderawtransaction <hex>- breaks the whole transaction down, including thescriptPubKey.hexof every output.- The txid.uk Script Decoder - paste a scriptPubKey hex and see its type and structure in the browser.
Descriptors express the same thing too. addr(bc1q...) describes a script by its address, and raw(76a914...88ac) by its hex (output descriptors).
Easy Mistakes
- Reading push lengths as decimal.
14is 20 bytes,20is 32,22is 34. - Expecting
01for SegWit version 1. It is51, and version 2 is52. - Reading P2WSH and P2TR by prefix only. Both are 34 bytes; only the first byte,
00or51, tells them apart. - Assuming you can see what is inside P2SH, P2WSH or P2TR. They hold only a hash or a tweaked key until spent.
- Treating the TXID in raw hex as it is. It is stored reversed, so flip it before looking it up.
For how the opcodes run on the stack, see Bitcoin Script. For the fields of a transaction as an explorer shows them, see How to Read a Bitcoin Transaction.