bitcoind.app
CommandsGuidesContact ↗
Home/Guides/Introduction to Bitcoin Script
Blockchain

Introduction to Bitcoin Script

Understanding Bitcoin's scripting language, script types (P2PKH, P2SH, P2WPKH, P2WSH, P2TR), and how to decode and analyze scripts using Bitcoin Core RPC.

In this guide
What is Bitcoin Script?Script Types and Address FormatsDecoding ScriptsDescriptors: The Modern Approach

What is Bitcoin Script?

Every Bitcoin transaction output includes a small program written in Bitcoin Script — a stack-based, intentionally limited programming language. This script defines the conditions under which the output can be spent. The most common condition is 'provide a valid signature from this public key,' but Script supports more complex logic including multisig, time locks, and hash locks.

Script is deliberately not Turing-complete: no loops, no recursion, and bounded execution time. This makes it analyzable and predictable — you can always determine whether a script will succeed or fail without running it.

Script Types and Address Formats

Bitcoin has evolved through several standard script types, each with its own address format:

P2PKH (Pay-to-Public-Key-Hash) — the original format, producing addresses starting with '1'. The script checks that the spender provides a public key matching the hash and a valid signature.

P2SH (Pay-to-Script-Hash) — wraps any script behind a hash, producing addresses starting with '3'. The script itself is only revealed when spending. This enabled multisig to use simple addresses.

P2WPKH (Pay-to-Witness-Public-Key-Hash) — native SegWit, producing bech32 addresses starting with 'bc1q'. Moves the signature data to the witness section, reducing effective transaction size.

P2WSH (Pay-to-Witness-Script-Hash) — SegWit version of P2SH for complex scripts.

P2TR (Pay-to-Taproot) — the newest format, producing bech32m addresses starting with 'bc1p'. Uses Schnorr signatures and enables advanced features like MAST (Merkelized Abstract Syntax Trees) for efficient complex spending conditions.

Decoding Scripts

The decodescript command takes a hex-encoded script and returns the human-readable assembly (ASM), the script type, associated addresses, and the P2SH address if applicable.

This is essential for debugging: when a transaction fails or you're inspecting someone else's transaction, decodescript tells you exactly what spending conditions are required.

For full transaction analysis, decoderawtransaction breaks down the entire transaction including the scripts on every input and output. The scriptPubKey field on each output shows the locking script, and the scriptSig (or witness data for SegWit) shows how inputs satisfy their conditions.

validateaddress reveals the script type behind any address, and getdescriptorinfo analyzes output descriptors — the modern way to describe scripts and key derivation paths.

Descriptors: The Modern Approach

Output descriptors are a compact language for describing which scripts your wallet tracks. Instead of importing raw keys or scripts, you describe them with expressions like:

wpkh(xpub.../0/*) — watch for P2WPKH outputs derived from this extended public key. sortedmulti(2,key1,key2,key3) — watch for 2-of-3 multisig with these keys. tr(internal_key) — watch for Taproot outputs with this internal key.

getdescriptorinfo analyzes any descriptor, telling you whether it's valid, solvable (you have enough information to spend), and ranged (generates multiple addresses).

deriveaddresses converts a descriptor into the actual Bitcoin addresses it represents. This is the bridge between the abstract descriptor language and concrete addresses you can share with senders.

Related RPC Commands

decodescriptDecode a hex-encoded script.decoderawtransactionReturn a JSON object representing the serialized, hex-encoded transaction.validateaddressReturn information about the given bitcoin address.getaddressinfoReturn information about the given bitcoin address. Some info requires the address to be in the wallet.deriveaddressesDerives one or more addresses corresponding to an output descriptor.getdescriptorinfoAnalyses a descriptor.createmultisigCreates a multi-signature address with n signature of m keys required.
← Previous Guide
Bitcoin Regtest Mode: Development and Testing Guide

bitcoind.app — Bitcoin Core RPC Reference

Contact ↗