Pay to script hash

From Bitcoin Wiki
(Redirected from P2SH)
Jump to navigation Jump to search
Pay to Script Hash
BIP numberBIP 16
TypeMiner-activated softfork
PurposeAllow the recipient of a transaction to specify the redeem script instead of the sender

Deployment

CB signature/P2SH/
Supermajority55%
ActivatedBlocks with timestamps from 2012-04-01 00:00:00 UTC (first: block #173805)

Pay to script hash (P2SH) is a standard, specified in Bitcoin Improvement Proposal 16 (BIP 16), that lets bitcoins be sent to the hash of a script (address starting with 3) instead of to a public key hash (addresses starting with 1).[1] To spend bitcoins sent via P2SH, the recipient must provide a script matching the script hash and data which makes the script evaluate to true.[1]

The purpose of P2SH is to move the responsibility for supplying the spending conditions from the sender to the recipient. A sender can fund any arbitrary script, however complicated, using a fixed 20-byte hash that is short enough to copy or scan from a QR code.[1]

Using P2SH, you can send bitcoins to an address that is secured in various unusual ways without knowing anything about the details of how the security is set up. You just send bitcoins to the ~34-character P2SH address. The recipient may need the signatures of several people to spend these bitcoins, or a password may be required, or the requirements could be completely unique.

Specification

A P2SH output has the form:

OP_HASH160 [20-byte hash] OP_EQUAL

It is redeemed by a scriptSig of the form ...signatures... {serialized script}. The serialized script is also called the redeemScript. A spend is checked in two steps:[1]

  • The scriptSig may contain only data pushes. The stack is built, and the hash of the serialized script must match the hash in the output.
  • The serialized script is then popped off the stack, and the transaction is validated again using the remaining stack and the deserialized script as the scriptPubKey.

Spends are only standard if the redeemScript is itself one of the other standard script types. Signature operations inside the redeemScript count towards the per-block limit: OP_CHECKSIG and OP_CHECKSIGVERIFY count as one, OP_CHECKMULTISIG preceded by OP_1 to OP_16 counts as that number, and any other OP_CHECKMULTISIG counts as 20.[1]

Because the redeemScript is pushed onto the stack, it is subject to the 520-byte limit on stack elements. With 33-byte compressed public keys this allows at most a 15-key multisignature script.[1]

P2SH replaced an earlier proposal, BIP 12, which would have added a new OP_EVAL opcode.[1]

Addresses

BIP 13 specifies the address format. Bitcoin P2SH addresses always start with 3.[2] Nested SegWit outputs (P2SH-P2WPKH and P2SH-P2WSH) wrap a Segregated Witness program in a P2SH redeemScript, so they also use addresses that start with 3.

Example

Transaction 40eee3ae1760e3a8532263678cdf64569e6ad06abc133af64f735e52562bccc8 paid to P2SH address 3P14159f73E4gFr7JterCCQh9QjiTjiZrG. The redeem script is visible in transaction 7edb32d4ffd7a385b763c7a8e56b6358bcd729e747290624e18acdbe6209fc45, which spends that output using OP_FALSE <sig> { OP_1 <pubkey> OP_1 OP_CHECKMULTISIG }.[3]

History

Gavin Andresen wrote BIP 16, which was assigned on 3 January 2012.[1] Miners signalled support by putting the string "/P2SH/" in the coinbase. The BIP set a check on 1 February 2012: if 550 or more of the previous 7 days' blocks (about 55%) contained the string, the new rules would apply to blocks with timestamps after 15 February 2012.[1]

The switchover was moved. In the Bitcoin source code the mainnet switch time was changed from 1 March 2012 to 1 April 2012 in a commit by Gavin Andresen dated 27 February 2012.[4] On 7 March 2012, in the forum thread for 0.6.0 release candidate 1, a user reported that all of their rc1 nodes had stopped at block 170,059.[5] The next block, 170,060, contains transaction 6a26d2ecb67f27d1fa5524763b49029d7106e91e3cc05743073461a719776192, which BIP 16 cites as an earlier transaction that fails the new rules.[1] It spends a payment made in block 170,052 to the P2SH address 342ftSRCvFHfCeFFBuz4xwbeqnDw6BGUey.[6] Bitcoin Core still lists block 170,060 as the one mainnet block exempt from P2SH validation.[7]

The rules took effect for blocks with timestamps from 1 April 2012 (Unix time 1333238400).[1] Bitcoin Core has implemented them since 0.6.0.[8] The first block with such a timestamp is 173,805.[9] The 0.6.0 release notes describe multisignature transactions as standard from that release, limit their creation to the test network, and include short multisignature addresses as specified in BIP 13 and BIP 16.[10]

Later soft fork activation methods, such as BIP 34 and BIP 9, reflect on block history to determine activation (also from miner signaling) in order to prevent early-activation stuck-node problems.[11][12]

After activation, problems were caused by the remaining 45% of miners producing invalid blocks for several months.[13] Later soft fork activation techniques raised the signaling enforcement threshold from 55% to 95% in order to mitigate this problem.


Testnet activated following its first window, which was two weeks earlier than the first (failed) mainnet window.[14] On testnet the switch time was 15 February 2012.[4]

342ftSRCvFHfCeFFBuz4xwbeqnDw6BGUey is a Bitcoin address notable for being the first P2SH-compatible address receiving bitcoins on the production network. Its payment was mined in block 160720; note that it was spent prior to the enforcement of BIP 16, so it is not a good example to understand P2SH.

See also

References