P2SH - Pay To Script Hash (BIP 16)

This was the first transaction to use P2SH: a0f1aaa2fb4582c.....b55b7b71e90ce0f

Let's go through this transaction step by step to better understand P2SH.

TLDR: P2SH allows you to send funds to the hash of a script instead of a public key hash (P2PKH) or public key (P2PK) as we saw in previous topics.
*Remember: we are sending funds to the hash of the script, not the script itself

The type of script you use can vary based on your specific needs. Common types include: MultiSig (which we'll cover in this lesson), Time-Locked Script, HTLC

Can I use any type of script with P2SH?

Since bitcoin Core release 0.10.0, the IsStandard() rules have been removed for P2SH, allowing you to use any custom locking script.

An example of a non-standard locking script is Peter Todd's hash-collision challenge.


Let’s use Alice and Bob to illustrate this.

They decide to create a joint wallet for their shared savings. They agree that neither of them can spend the money alone; both must agree.

So, they set up a 2-of-2 multisig wallet that requires both of their signatures to unlock the funds.

First Step: Creating Redeem Script

Unlike previous transactions we've seen , P2SH adds an extra step: creating a redeem script.

A redeem script is a script that defines the conditions under which funds can be spent. It's called a "redeem" because it's used at redemption time (when spending) rather than as a locking script.

Alice and Bob want to create a 2-of-2 multisig address. Their redeem script looks like this:

text
1 
22
3<public key Alice>
4<public key Bob>
52
6OP_CHECKMULTISIG
7  

This means both Alice's and Bob's signatures are required to spend the funds.

Locking Script

Now it's time to lock the funds into this script.
The scriptPubKey for the P2SH looks like this:

text
1 
2OP_HASH160 <20-byte hash of redeem script> OP_EQUAL
3  

As you can see, we still have to hash the redeem script we created before:

  1. Apply SHA-256 to the redeem script
  2. Apply RIPEMD-160 to the SHA-256 hash

This will produce a 20-byte hash:

text
1 
2<20-byte hash of redeem script>
3

This is the hash we use to lock the funds.

click on output 2 of this Transaction to see it in action.

First P2SH transaction
Inputs ()
Outputs ()

ScriptSig

When either Alice or Bob wants to spend the funds, they must provide the redeem script and signatures that satisfy the redeem script. The scriptSig for P2SH looks like this:

text
1 
2<Alice signature> <Bob signature> <redeem script>
3  

Click on input 1 to see this in action

Inputs ()
Outputs ()

Validation

Configuration not found for type: p2sh

Address:

The P2SH locking script pattern has its own address format. Unlike P2PKH addresses that start with 1, P2SH addresses start with 3.

Here's how it works:

  1. Create the redeem script
  2. Hash the redeem script (SHA256 then RIPEMD160)
  3. Add version byte (0x05 for P2SH)
  4. Calculate checksum
  5. Encode in Base58Check

This results in an address that represents the hash of the script, not the script itself.
When funds are sent to this address, they are locked to the hash of the redeem script.

Use the tool below to see how a script hash translates to a P2SH address:

P2SH Address Encoder
Encode Base 58
Address ...

Still confused about Bitcoin addresses?

Don't worry if you're still struggling to understand how addresses work in Bitcoin. We'll cover this topic in more depth in a dedicated module later in the course.

Test Your Knowledge: Find the Script Hash

In this exercise, you'll find a script hash within a transaction and use it to calculate the related bitcoin address.

  1. Find the 20-byte hash of the redeem script from the transaction we discussed in this lesson.

  2. Enter the script hash into the tool above to find the corresponding bitcoin address.

  3. Verify Your Result: The address you should get is: 3CK4fEwbMP7heJarmU4eqA3sMbVJyEnU3V

Feel free to refer back to the article if you need a refresher on how P2SH works. Good luck!