Complex Scripts

Let's finish by learning about conditional clauses and flow control, and taking a look at some examples of complex scripts.

Segregated Witness addresses

Even after segwit activation, it will take some time until most wallets are upgraded. At first, segwit will be embedded in P2SH, as we saw in the previous section, to ease compatibility between segwit-aware and unaware wallets.

However, once wallets are broadly supporting segwit, it makes sense to encode witness scripts directly in a native address format designed for segwit, rather than embed it in P2SH.

The native segwit address format is defined in BIP-173:



Base32 address format for native v0-16 witness outputs

BIP-173 only encodes witness (P2WPKH and P2WSH) scripts. It is not compatible with non-segwit P2PKH or P2SH scripts. BIP-173 is a checksummed Base32 encoding, as compared to the Base58 encoding of a "traditional" bitcoin address. BIP-173 addresses are also called bech32 addresses, pronounced "beh-ch thirty two", alluding to the use of a "BCH" error detection algorithm and 32-character encoding set.

BIP-173 addresses use 32 lower-case-only alphanumeric character set, carefully selected to reduce errors from misreading or mistyping. By choosing a lower-case-only character set, bech32 is easier to read, speak, and 45% more efficient to encode in QR codes.

The BCH error detection algorithm is a vast improvement over the previous checksum algorithm (from Base58Check), allowing not only detection but also correction of errors. Address-input interfaces (such as text-fields in forms) can detect and highlight which character was most likely mistyped when they detect an error.

From the BIP-173 specification, here are some examples of bech32 addresses:


Mainnet P2WPKH



Testnet P2WPKH



Mainnet P2WSH



Testnet P2WSH



As you can see in these examples, a segwit bech32 string is up to 90 characters long and consists of three parts:


The human readable part

This prefix "bc" or "tb" identifying mainnet or testnet


The separator

The digit "1", which is not part of the 32-character encoding set and can only appear in this position as a separator


The data part

A minimum of 6 alphanumeric characters, the checksum encoded witness script


At this time, only a few wallets accept or produce native segwit bech32 addresses, but as segwit adoption increases, you will see these more and more often.

Bitcoin non-segwit (legacy) and segwit addresses shows bitcoin non-segwit (legacy) and segwit addresses.


Table 3. Bitcoin non-segwit (legacy) and segwit addresses

Type Encoding Prefix
Legacy P2PKH Address Base58 1
Legacy Testnet P2PKH Address Base58 m or n
Legacy P2SH Address Base58 3
Legacy Testnet P2SH Address Base58 2
Nested (embedded) Segwit P2SH(P2WPKH) Address Base58 3
Nested (embedded) Segwit P2SH(P2WSH) Address Base58 3
Native Segwit P2WPKH Address Bech32 bc1
Native Segwit Testnet P2WPKH Address Bech32 tb1
Native Segwit P2WSH Address Bech32 bc1
Native Segwit Testnet P2WSH Address Bech32 tb1