P2SH, or "Pay to Script Hash" transactions in full form, are one specific type of payment in the Bitcoin script. P2SH was introduced to the Bitcoin network at Block 173805, which was mined on April 1, 2012. This article will explain everything you need to know about P2SH transactions.
Why Was P2SH Created?
P2SH wasn’t available as a standard script on the Bitcoin network until BIP 16 was implemented in 2012. It quickly gained support from users who wanted additional functionality that wasn’t possible with P2PK or P2PKH payments, which had been the only available payment types prior to P2SH’s implementation.
As we’ll explain later in the article, P2SH utilizes script hashing and often includes P2PKH or P2PK transactions within a script. The introduction of P2SH allows users to send BTC without having to worry about how a recipient unlocks access to the payment (e.g. via a passphrase or with multi-signature requirements in place).
P2SH also provides a method for senders to place the responsibility of paying additional network fees on the recipient rather than themselves. Prior to the activation of P2SH, senders had no standardized way to implement these scripts. It was possible to use customized scripts, but these were pretty complex and time-consuming to write for most users. P2SH vastly simplified the process through simple-to-use script hashes and identifiable Bitcoin wallet address formats.
How Does P2SH Work?
The first step of a P2SH transaction works in a similar fashion to that of a P2PKH transaction, in that both lock the payment to a specific hash. A hash is the output— formally known as the digest— of a cryptographic hash function.
Cryptographic hash functions are designed such that they can receive an input of virtually any length but always produce an output of a fixed length— in this case, 64 characters. So while the inputs can vary, all outputs are of uniform length and format.
For P2PKH transactions, the input is simply the recipient’s pubkey. The output, or hash, of that pub key is the hash to which the funds are locked (hence the name “pay to pub key hash”). All a recipient needs to do in order to claim the funds is produce the right pubkey (as well as a digital signature from the private key from which that pubkey was derived). The blockchain’s peer to peer network verifies that the hash of the pub key matches the hash of the payment. If the two hashes match, the funds are released to the user who owns that pub key.
For P2SH payments, the input is often more complex. It may add constraints in addition to an ordinary digital signature or pubkey verification, such as requiring digital signatures from multiple private keys. These constraints are created with a few lines of code known as a script. The hash of that script is used to lock a payment in a “Pay To Script Hash” transaction.
Once a script is hashed and the subsequent script hash has funds locked to it on-chain, the only way to spend those funds is to enter the original script used to create the hash in the first place. This is called the “redeem script.” The redeem script verifies the recipient is producing the correct data— the corresponding script, script hash, and signature script— before the funds are released. Note that the script can include digital signatures and pubkey verifications within it.
Advantages and Limitations of P2SH Payments
Because P2SH was developed a few years after the original Bitcoin scripts, developers had the ability to build upon the advantages of earlier designs and create a solution that addressed common transaction limitations of the network. As a result, P2SH has far more advantages than limitations.
Multisig: The main use case for P2SH is the ability to easily support multi-signature transactions. This can provide extra security for a group of individuals or companies that want to ensure that no one person can move or steal BTC from a shared wallet.
Security: Although P2SH transactions usually include a highly complex set of instructions, cryptographic hashing ensures that this script maintains the same level of security as P2PKH.
Easier For Senders: Senders no longer bear the burden of having to write complex logic on behalf of recipients. A P2SH transaction can be executed simply by sending to a standard address format that starts with the number ‘3’. Senders also don’t have to pay the extra costs that come with storing extra bytes corresponding to the more complex scripts on the blockchain.
SegWit and Non-SegWit Support: When Bitcoin private keys are used to create a wallet in P2SH format, it is also known as "Nested SegWit." Although it’s not as complete as Native SegWit (Bech32), P2SH is compatible with non-SegWit (P2PKH) and SegWit (Bech 32) addresses. This means users with funds stored in P2SH format wallets can easily send BTC to both.
Computationally Efficient: P2SH scripts are smaller than P2MS (Pay To Multi-Sig) scripts. This means less RAM is needed for the UTXO set, which can help increase the speed of transaction validation.
Data Size: A 2-of-3 multisig script using P2SH requires 278 bytes compared to only 253 bytes with P2MS. Taking up more data on the blockchain tends to create network congestion issues.
Single Signatures: P2SH was initially developed to provide a solution for multisig wallets. This means that other script types are more commonly used for single signature transactions. Most end users won’t have a need for complex scripts, so the adoption of P2SH has been somewhat limited.
Is P2SH Still Used Today?
The easiest way to identify P2SH transactions is via a blockchain explorer website like BTC.com. Transactions involving addresses that start with the number ‘3’ use P2SH. You will likely find that these addresses comprise a significant portion of recent transactions. According to txstats.com, over 5.8 million BTC (more than 31 percent of existing BTC supply) is currently held in P2SH outputs. The total number of P2SH unspent transaction outputs (UTXOs) has surpassed 10.5 million.
Komodo Developer Docs
Komodo is a multi-chain blockchain platform that allows every business, startup, and developer to launch an independent Smart Chain. These Smart Chains are of the Bitcoin protocol, so they support P2PK, P2PKH, and P2SH transactions. Smart Chains can also support tokens, games, apps, and other types of software, directly on the chain itself.
To learn how to launch a Smart Chain by checking out Komodo's Developer Documentation.
If you'd like to learn more about blockchain technology and keep up with Komodo's progress, subscribe to our newsletter. Begin your blockchain journey with Komodo today.