Script Serialization & Addresses
Script Serialization and Addresses
A Bitcoin Address is simply a human-readable representation of a ScriptPubKey. Understanding how the script is serialized into bytes—and how those bytes become an address—is key to understanding the network's identity system.
1. Serialization in the Output
In the raw binary of a transaction, a script is prefixed by its length using a VarInt.
Example of a P2PKH output:
-
Value:
00e1f50500000000(1 BTC) -
Length:
19(25 bytes in hex) -
Script:
76a914[20-byte hash]88ac
2. From Script to Address
The network doesn't actually use "Addresses" internally. It only uses ScriptPubKeys. Your wallet converts the script into an address for your convenience using specific encoding rules.
-
P2PKH (Legacy): Uses Base58Check encoding. It takes the hash from the middle of the script, adds a version byte (
0x00), and a checksum. -
P2WPKH (SegWit): Uses Bech32 encoding. It takes the version byte (
0) and the hash, and converts them into a string likebc1q....
3. The "Silent" Script
When you give someone your address, you are effectively giving them the blueprint for a ScriptPubKey.
-
You give Address
1ABC.... -
The sender's wallet decodes
1ABC...back into the patternOP_DUP OP_HASH160 <Hash> OP_EQUALVERIFY OP_CHECKSIG. -
The wallet places that raw byte pattern into the transaction output.
4. Why Encapsulate?
We use addresses instead of raw scripts to:
-
Reduce Errors: Checksums prevent you from sending money to a mistyped address.
-
Shorten Data: Base58/Bech32 are more compact than raw hex.
-
Future Proofing: We can change the underlying script pattern (like moving from Legacy to Taproot) while keeping a similar address format.
| Format | Internal Representation | External Representation |
|---|---|---|
| Script | Raw Opcodes \u0026 Hash | 76a914...88ac |
| Address | Human-readable string | 1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa |
In the final section, we will build a Python ScriptPubKey Decoder to identify these patterns.
TeachMeBitcoin is an ad-free, open-source educational repository curated by a passionate team of Bitcoin researchers and educators for public benefit. If you found our articles helpful, please consider supporting our hosting and ongoing content updates with a clean donation: