mirror of
https://github.com/ChristopherA/Learning-Bitcoin-from-the-Command-Line.git
synced 2025-06-08 16:36:32 +00:00
Update 8_2_Building_a_Bitcoin_Script_with_P2SH.md
This commit is contained in:
parent
c14e93c802
commit
b7373b9f07
@ -2,32 +2,35 @@
|
||||
|
||||
> **NOTE:** This is a draft in progress, so that I can get some feedback from early reviewers. It is not yet ready for learning.
|
||||
|
||||
You know that Bitcoin Scripts can be used to control the redemption of UTXOs, and that standard Bitcoin transactions depend on very specific locking scripts. The next step is creating Scripts of your own ... but that requires a very specific techniques.
|
||||
Now that you know the theory behind P2SH addresses, you're ready to turn a non-standard Bitcoin Script into an actual transaction. We'll be doing so with our simple locking script from [§7.2: Running a Bitcoin Script](7_2_Running_a_Bitcoin_Script.md), `OP_ADD 99 OP_EQUAL`.
|
||||
|
||||
## Rebuild a Script as a P2SH
|
||||
## Create a P2SH Transaction
|
||||
|
||||
In [§7.2: Running a Bitcoin Script](7_2_Running_a_Bitcoin_Script.md), we offered a simple example of a non-standard locking script, `OP_ADD 99 OP_EQUAL`, and we executed it as a simple concatenation of an unlocking script with that locking script. We're now going to repeat that exercise, this time within the constraints of a P2SH transaction.
|
||||
To lock a transaction with this Script, do the following:
|
||||
|
||||
### Create the Lock for the P2SH Transaction
|
||||
|
||||
To lock this transaction do the following:
|
||||
|
||||
1. Serialize `OP_ADD 99 OP_EQUAL` (`<serialized99Equal>`) then SHA-256 and RIPEMD-160 hash it (`<hashed99Equal>`).
|
||||
1. OP_ADD = 0x93
|
||||
2. 99 = 0x01, 0x63, the first to push one byte onto the stack, the second as the hex translation of 99
|
||||
3. OP_EQUAL = 0x87
|
||||
1. Serialize `OP_ADD 99 OP_EQUAL`:
|
||||
1. OP_ADD = 0x93 — a simple opcode translation
|
||||
2. 99 = 0x01, 0x63 — this opcode pushes one byte onto the stack, 99 (hex: 0x63)
|
||||
3. OP_EQUAL = 0x87 — a simple opcode translation
|
||||
4. `<serialized99Equal>` = "93016387"
|
||||
5. `<hashed99Equal>` = "3f58b4f7b14847a9083694b9b3b52a4cea2569ed"
|
||||
2. Save `<serialized99Equal>` for future reference as the `redeemScript`.
|
||||
3. Produce a P2SH locking script that includes the hashed script (`OP_HASH160 <hashed99Equal> OP_EQUAL`).
|
||||
2. SHA-256 and RIPEMD-160 hash the serialized script.
|
||||
1. `<hashed99Equal>` = "3f58b4f7b14847a9083694b9b3b52a4cea2569ed"
|
||||
3. Save `<serialized99Equal>` for future reference as the `redeemScript`.
|
||||
1. `<redeemScript>` = "93016387"
|
||||
4. Produce a P2SH locking script that includes the `<hashed99Equal>`.
|
||||
1. `scriptPubKey` = "a9143f58b4f7b14847a9083694b9b3b52a4cea2569ed87"
|
||||
4. Create a transaction using that `scriptPubKey`.
|
||||
|
||||
### Run the First Round of Validation
|
||||
You can then create a transaction using that `scriptPubKey`, probably via an API.
|
||||
|
||||
## Unlock the P2SH Transaction
|
||||
|
||||
To unlock this transaction requires that the recipient produce a `scriptSig` that prepends two constants totalling ninety-nine to the serialized script: `1 98 <serialized99Equal>`.
|
||||
|
||||
The validation to unlock the P2SH transaction then begins with a first round of validation. Concatenate `scriptSig` and `scriptPubKey` and execute them, as normal:
|
||||
### Run the First Round of Validation
|
||||
|
||||
The process of unlocking the P2SH transaction then begins with a first round of validation.
|
||||
|
||||
Concatenate `scriptSig` and `scriptPubKey` and execute them, as normal:
|
||||
```
|
||||
Script: 1 98 <serialized99Equal> OP_HASH160 <hashed99Equal> OP_EQUAL
|
||||
Stack: []
|
||||
@ -56,7 +59,7 @@ However, because this was a P2SH script, the execution isn't done.
|
||||
|
||||
### Run the Second Round of Validation
|
||||
|
||||
For the second round of validation, deserialize the `redeemScript`, then execute it using the items in the `scriptSig` before the serialized script:
|
||||
For the second round of validation, deserialize the `redeemScript` ("93016387" = "OP_ADD 99 OP_EQUAL"), then execute it using the items in the `scriptSig` prior to the serialized script:
|
||||
|
||||
```
|
||||
Script: 1 98 OP_ADD 99 OP_EQUAL
|
||||
@ -81,6 +84,4 @@ With that second validation _also_ true, the UTXO can now be spent!
|
||||
|
||||
## Summary: Building a Bitcoin Script with 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 redemption script. As long as you can also satisfy the script, 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.
|
||||
Once you know the technique of building P2SHes, any Script can be embedded in a Bitcoin transaction; and once you understand the technique of validating P2SHes, it's easy to run the scripts in two rounds.
|
||||
|
Loading…
x
Reference in New Issue
Block a user