All articles

Pay-to-Script-Hash (P2SH)

Pay to script hash (P2SH) is a type of bitcoin transaction that allows the sender to commit to a script, or set of conditions, that must be met in order to spend the funds.

This can be used to create more complex transactions, such as multi-signature transactions or transactions that can only be spent after a certain time has passed.

To use P2SH with a Trezor device, you will first need to create a P2SH address. This can be done using Trezor Suite or a third party application which supports P2SH such as Electrum. 

Once you have created a P2SH address, you can use it to receive funds in the same way you would use any other bitcoin address.


Generating a P2SH address

To get a Pay-to-Script-Hash (P2SH) address with Trezor Suite, follow these steps:

  • Connect your Trezor device to your computer and open Trezor Suite.
  • Click on the plus [+] icon next to My accounts in the side menu.
 
 
  • Select Bitcoin and from the drop down menu choose "Legacy SegWit" for a compatible P2SH address.
 
 
 
  • Click on the Add account button.
  • Now you should see your Legacy SegWit account added to the menu on the left side of Trezor Suite. With your Legacy Segwit account selected choose receive and continue to confirm on your device and generate your P2SH compatible address. 
   
  • Once the address is generated, you will see it displayed on the screen.
 
P2SH addresses start with the number "3" and are followed by a series of letters and numbers. For example, a P2SH address might look something like this: "3AdD7ZaJQw9m1maN39CeJ1zVyXQLn2MEHR".


You can use this address to receive payments to a script that is hashed and stored on the blockchain. Keep in mind that P2SH addresses are generally more complex and less widely used than regular addresses, and may not be supported by all wallets and exchanges.


Transaction scripts

An output that pays to a P2SH scriptPubKey is spent by an input with a scriptSig that provides the correct redeem script along with all the data (like signatures and corresponding public keys) necessary for the successfull script evaluation:
 

scriptPubKey: OP_HASH160 <redeemScriptHash> OP_EQUAL
scriptSig: <singatures> <publicKeys> <redeemScript>
 


P2SH-multisig

A P2SH address can represent a multi-signature script. The script requires multiple signatures to prove ownership and therefore spend funds. The bitcoin multi-signature feature is designed to require M signatures from a total of N keys, known as an M-of-N multisig, where M is equal to or less than N.


P2WPKH-in-P2SH

P2WPKH-in-P2SH (Pay-to-Witness-Public-Key-Hash-in-Pay-to-Script-Hash) is a type of transaction on the Bitcoin blockchain that allows a user to send funds to a pay to witness public key hash (P2WPKH) address using a version of the script hash (P2SH) format. This allows the user to send funds to a SegWit (Segregated Witness) address using a non-SegWit transaction, which can be useful in cases where the user's wallet or software does not support SegWit transactions.
 
ScriptPubKey: OP_HASH160 <20-byte-redeemScriptHash> OP_EQUAL 
ScriptSig:  <0 <20-byte-PublicKeyHash>> 
Witness:  <Signature> <PublicKey>
 
RedeemScript: 0 <20-byte-PublicKeyHash>
 
The P2SH redeem script is equal to 0 <20-byte-PublicKeyHash> which is exactly the same as P2WPKH scriptPubKey. The scriptSig is a canonical push of data, which contains another canonical push of data.

To understand more about how transactions work on a basic technical level, check out this blog post on types of bitcoin transactions: Part IPart 2. More detailed description can be found in Bitcoin Wiki or in Bitcoin.org Developer guide.
 
See also: What is Multisig?
 
For even more check out this blog post to learn more about using multisignature transations with Trezor: Multi-signature hardware wallets with Electrum