mirror of
https://github.com/ChristopherA/Learning-Bitcoin-from-the-Command-Line.git
synced 2025-06-07 07:56:31 +00:00
Create 08_1_Understanding_the_Foundation_of_P2SH.md
This commit is contained in:
parent
dd25853eff
commit
33d71eb8cf
@ -10,7 +10,7 @@ Here's the gotcha for using Bitcoin Scripts: for security reasons, most Bitcoin
|
||||
|
||||
* __Pay to Public Key (P2PK)__ — An older, deprecated transaction (`<pubKey> OP_CHECKSIG`) that has been replaced by the better security of P2PKH.
|
||||
* __Pay to Public Key Hash (P2PKH)__ — A standard transaction (`OP_DUP OP_HASH160 <pubKeyHash> OP_EQUALVERIFY OP_CHECKSIG`) that pays to the hash of a public key.
|
||||
* __Multisig__ — A transaction for a group of keys, as explained more fully in [§8.4](8_4_Scripting_a_Multisig.md).
|
||||
* __Multisig__ — A transaction for a group of keys, as explained more fully in [§8.4](08_4_Scripting_a_Multisig.md).
|
||||
* __Null Data__ — An unspendable transaction (`OP_RETURN Data`).
|
||||
* __Pay to Script Hash (P2SH)__ — A transaction that pays out to a specific script, as explained more fully here.
|
||||
|
||||
@ -20,7 +20,7 @@ So how do you write a more complex Bitcoin Script? The answer is in that last so
|
||||
|
||||
## Understand the P2SH Script
|
||||
|
||||
You already saw a P2SH transaction when you created a multisig in [§6.1: Sending a Transaction to a Multisig](6_1_Sending_a_Transaction_to_a_Multisig.md). Though multisig is one of the standard transaction types, `bitcoin-cli` simplifies the usage of its multisigs by embedding them into P2SH transactions, as described more fully in [§8.4: Scripting a Multisig](8_4_Scripting_a_Multisig.md).
|
||||
You already saw a P2SH transaction when you created a multisig in [§6.1: Sending a Transaction to a Multisig](06_1_Sending_a_Transaction_to_a_Multisig.md). Though multisig is one of the standard transaction types, `bitcoin-cli` simplifies the usage of its multisigs by embedding them into P2SH transactions, as described more fully in [§8.4: Scripting a Multisig](08_4_Scripting_a_Multisig.md).
|
||||
|
||||
So, let's look one more time at the `scriptPubKey` of that P2SH multisig:
|
||||
```
|
||||
@ -57,7 +57,7 @@ Overall, four steps are required:
|
||||
|
||||
Each of those steps of course takes some work on its own, and some of them can be pretty intricate. The good news is that you don't really have to worry about them, because they're sufficiently complex that you'll usually have an API take care of it all for you.
|
||||
|
||||
So for now, we'll just provide you with an overview, so that you understand the general methodology. In [§8.2: Building the Structure of P2SH](8_2_Building_the_Structure_of_P2SH.md) we'll provide a more in-depth look at script creation, in case you ever want to understand the guts of this process.
|
||||
So for now, we'll just provide you with an overview, so that you understand the general methodology. In [§8.2: Building the Structure of P2SH](08_2_Building_the_Structure_of_P2SH.md) we'll provide a more in-depth look at script creation, in case you ever want to understand the guts of this process.
|
||||
|
||||
## Understand How to Send a P2SH Script Transaction
|
||||
|
||||
@ -76,7 +76,7 @@ The trick to redeeming a P2SH transaction is that the recipient must have saved
|
||||
|
||||
An unlocking `scriptSig` for a P2SH transaction is formed as: `... data ... <redeemScript>`. The `data` must _solely_ be data that is pushed onto the stack, not operators. ([BIP 16](https://github.com/bitcoin/bips/blob/master/bip-0016.mediawiki) calls them signatures, but that's not an actual requirement.)
|
||||
|
||||
> **WARNING:** Though signatures are not a requirement, a P2SH script actually isn't very secure if it doesn't require at least one signature in inputs. The reasons for this are described in [§11.1: Writing Puzzle Scripts](11_1_Writing_Puzzle_Scripts.md).
|
||||
> **WARNING:** Though signatures are not a requirement, a P2SH script actually isn't very secure if it doesn't require at least one signature in its inputs. The reasons for this are described in [§11.1: Writing Puzzle Scripts](11_1_Writing_Puzzle_Scripts.md).
|
||||
|
||||
When a UTXO is redeemed, it runs in two rounds of verification:
|
||||
|
||||
@ -85,12 +85,16 @@ When a UTXO is redeemed, it runs in two rounds of verification:
|
||||
3. Second, the redeemScript is run using the prior data that was pushed on the stack.
|
||||
4. If that second round of verification _also_ succeeds, the UTXO is unlocked.
|
||||
|
||||
Whereas you can't easily create a P2SH transaction without an API, you should be able to easily redeem a P2SH transaction with `bitcoin-cli`. In fact, you already have. The exact process is described in [§8.5: Spending a P2SH Transaction.md](8_5_Spending_a_P2SH_Transaction.md), after we've finished with all the intricacies of P2SH transaction creation.
|
||||
Whereas you can't easily create a P2SH transaction without an API, you should be able to easily redeem a P2SH transaction with `bitcoin-cli`. In fact, you already have. The exact process is described in [§8.5: Spending a P2SH Transaction.md](08_5_Spending_a_P2SH_Transaction.md), after we've finished with all the intricacies of P2SH transaction creation.
|
||||
|
||||
> **WARNING:** You can create a perfectly valid transaction with a hashed redeemScript, but if the redeemScript doesn't run, or doesn't run correctly, your funds are lost forever. That's why it is so important to test your Scripts, as discussed in [§7.4: Testing a Bitcoin Script](7_4_Testing_a_Bitcoin_Script.md)
|
||||
> **WARNING:** You can create a perfectly valid transaction with a hashed redeemScript, but if the redeemScript doesn't run, or doesn't run correctly, your funds are lost forever. That's why it is so important to test your Scripts, as discussed in [§7.4: Testing a Bitcoin Script](07_4_Testing_a_Bitcoin_Script.md)
|
||||
|
||||
## Summary: Understanding the Foundation of P2SH
|
||||
|
||||
Arbitrary Bitcoin Scripts are non-standard in Bitcoin. However, you can incorporate them into standard transactions by using the P2SH address type. You just hash your script as part of the locking script, then you reveal and run it as part of the unlocking script. As long as you can also satisfy the `redeemScript`, the UTXO can be spent.
|
||||
|
||||
_What is the power of P2SH?_ You already know the power of Bitcoin Script, which allows you to create more complex Smart Contracts of all sorts. P2SH is what actually unleashes that power by letting you include arbitrary Bitcoin Script in standard Bitcoin transactions.
|
||||
|
||||
## What's Next?
|
||||
|
||||
Continue "Embedding Bitcoin Scripts" with [§8.2: Building the Structure of P2SH](08_2_Building_the_Structure_of_P2SH.md).
|
||||
|
Loading…
x
Reference in New Issue
Block a user