This commit is contained in:
Shannon Appelcline 2020-08-26 12:38:01 -10:00 committed by GitHub
parent 4e3ce6378d
commit 02bb5dbecf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -6,7 +6,7 @@ Way back in Part 3, while introducing Scripts, we said that you were likely to a
## Creating the Script ## Creating the Script
Really, creating the script is the _easiest_ thing to do in Libwally. Take the following example, a simple [Puzzle Script](/13_1_Writing_Puzzle_Scripts.md) that we've returned to from time to time: Creating the script is the _easiest_ thing to do in Libwally. Take the following example, a simple [Puzzle Script](/13_1_Writing_Puzzle_Scripts.md) that we've returned to from time to time:
``` ```
OP_ADD 99 OP_EQUAL OP_ADD 99 OP_EQUAL
``` ```
@ -16,7 +16,7 @@ $ btcc OP_ADD 99 OP_EQUAL
warning: ambiguous input 99 is interpreted as a numeric value; use 0x99 to force into hexadecimal interpretation warning: ambiguous input 99 is interpreted as a numeric value; use 0x99 to force into hexadecimal interpretation
93016387 93016387
``` ```
Previously we also built the standard P2SH script by hand, but Libwally can actually do that for you. Previously we built the standard P2SH script by hand, but Libwally can actually do that for you.
First, Libwally has to convert the hex into bytes, since bytes are most of what it works with: First, Libwally has to convert the hex into bytes, since bytes are most of what it works with:
``` ```
@ -35,13 +35,13 @@ If you looked at the results of `p2sh`, you'd see it was:
``` ```
a9143f58b4f7b14847a9083694b9b3b52a4cea2569ed87 a9143f58b4f7b14847a9083694b9b3b52a4cea2569ed87
``` ```
Which you may recall breaks apart to: Which [you may recall](10_2_Building_the_Structure_of_P2SH.md) breaks apart to:
``` ```
a9 / 14 / 3f58b4f7b14847a9083694b9b3b52a4cea2569ed / 87 a9 / 14 / 3f58b4f7b14847a9083694b9b3b52a4cea2569ed / 87
``` ```
That's our old friend `OP_HASH160 3f58b4f7b14847a9083694b9b3b52a4cea2569ed OP_EQUAL`. That's our old friend `OP_HASH160 3f58b4f7b14847a9083694b9b3b52a4cea2569ed OP_EQUAL`.
Basically, Libwally took your redeem script, applied the standard framing to turn it into a proper P2SH, and then hashed it for you with SHA-256 and RIPEMD-160; You did similar work in [§10.2](10_2_Building_the_Structure_of_P2SH.md), but only with an excess of shell commands. Basically, Libwally took your serialized redeem script, hashed it for you with SHA-256 and RIPEMD-160, and the applied the standard framing to turn it into a proper P2SH; You did similar work in [§10.2](10_2_Building_the_Structure_of_P2SH.md), but with an excess of shell commands.
In fact, you can double-check your work using the same commands from §10.2: In fact, you can double-check your work using the same commands from §10.2:
``` ```
@ -52,9 +52,9 @@ $ echo -n $redeemScript | xxd -r -p | openssl dgst -sha256 -binary | openssl dgs
## Creating a Transaction ## Creating a Transaction
In order to make use of that `pubScriptKey` that you just created, you need to create a transaction and embed the `pubScriptKey` within (and this is the big change from `bitcoin-cli`: you can actually hand enter a P2SH script). In order to make use of that `pubScriptKey` that you just created, you need to create a transaction and embed the `pubScriptKey` within (and this is the big change from `bitcoin-cli`: you can actually hand create a transaction with a P2SH script).
The process of creating a transaction in Libwally is very intensive, just as the process for creating a PSBT, and so we're just going to outline it, taking one major shortcut, and then leave the rest for you. The process of creating a transaction in Libwally is very intensive, just like the process for creating a PSBT, and so we're just going to outline it, taking one major shortcut, and then leave a method without shortcuts for future investigation.
Creating a transaction itself is easy enough: you just need to tell `wally_tx_init_alloc` your version number, your locktime, and your number of inputs and outputs: Creating a transaction itself is easy enough: you just need to tell `wally_tx_init_alloc` your version number, your locktime, and your number of inputs and outputs:
``` ```
@ -80,7 +80,7 @@ One more command adds it to your transaction:
### Creating a Transaction Input ### Creating a Transaction Input
Creating the input is much harder because you have to pile information into the creation routines, not all of which is intuitively accessible when you're using Libwally. So, rather than going that deep into the weeds, here's where we take our shortcut. We require that we be passed the hex code for a transaction that's already been created, and then we just reuse the input. Creating the input is much harder because you have to pile information into the creation routines, not all of which is intuitively accessible when you're using Libwally. So, rather than going that deep into the weeds, here's where we take our shortcut. We write our code so that it's passed the hex code for a transaction that's already been created, and then we just reuse the input.
The conversion from the hex code is done with `wally_tx_from_hex: The conversion from the hex code is done with `wally_tx_from_hex:
``` ```
@ -98,24 +98,26 @@ As you might expect, you then add that input to your transaction:
lw_response = wally_tx_add_input(tx,tx_input); lw_response = wally_tx_add_input(tx,tx_input);
``` ```
> :note: **NOTE** Obviously, you'll want to be able to create your own inputs if you're using Libwally for real applications, but this is intended as a first step. And, it can actually be useful without further work, as we'll see in [§16.7]. > :note: **NOTE** Obviously, you'll want to be able to create your own inputs if you're using Libwally for real applications, but this is intended as a first step. And, it can actually be useful for integrating with `bitcoin-cli`, as we'll see in [§16.7](16_7_Integrating_Libwally_and_Bitcoin-CLI.md).
### Printing a Transaction ### Printing a Transaction
You theoretically could sign and send this transaction from your C program built on Libwally, but in keeping with the idea that we're just using a simple C program to substitute in a P2SH, we're going to print out our new hex. This is done with the help of `wally_tx_to_hex`: You theoretically could sign and send this transaction from your C program built on Libwally, but in keeping with the idea that we're just using a simple C program to substitute in a P2SH, we're going to print out the new hex. This is done with the help of `wally_tx_to_hex`:
``` ```
char *tx_hex; char *tx_hex;
lw_response = wally_tx_to_hex(tx,0, &tx_hex); lw_response = wally_tx_to_hex(tx,0, &tx_hex);
printf("%s\n",tx_hex); printf("%s\n",tx_hex);
``` ```
We'll show how to make use of that in §16.7.
## Testing Your Replacement Script ## Testing Your Replacement Script
You can grab the test code from the [src directory](src/16_5_replacewithscript.c) and compile it: You can grab the test code from the [src directory](src/16_5_replacewithscript.c) and compile it:
``` ```
$ cc replacewithscript.c -lwallycore -o replacewithscript $ cc replacewithscript.c -lwallycore -o replacewithscript
``` ```
Afterward, prepare a transaction and a script: Afterward, prepare a hex transaction and a serialized hex script:
``` ```
hex=020000000001019527cebb072524a7961b1ba1e58fc18dd7c6fc58cd6c1c45d7e1d8fc690b006e0000000017160014cc6e8522f0287b87b7d0a83629049c2f2b0e972dfeffffff026f8460000000000017a914ba421212a629a840492acb2324b497ab95da7d1e87306f0100000000001976a914a2a68c5f9b8e25fdd1213c38d952ab2be2e271be88ac02463043021f757054fa61cfb75b64b17230b041b6d73f25ff9c018457cf95c9490d173fb4022075970f786f24502290e8a5ed0f0a85a9a6776d3730287935fb23aa817791c01701210293fef93f52e6ce8be581db62229baf116714fcb24419042ffccc762acc958294e6921b00 hex=020000000001019527cebb072524a7961b1ba1e58fc18dd7c6fc58cd6c1c45d7e1d8fc690b006e0000000017160014cc6e8522f0287b87b7d0a83629049c2f2b0e972dfeffffff026f8460000000000017a914ba421212a629a840492acb2324b497ab95da7d1e87306f0100000000001976a914a2a68c5f9b8e25fdd1213c38d952ab2be2e271be88ac02463043021f757054fa61cfb75b64b17230b041b6d73f25ff9c018457cf95c9490d173fb4022075970f786f24502290e8a5ed0f0a85a9a6776d3730287935fb23aa817791c01701210293fef93f52e6ce8be581db62229baf116714fcb24419042ffccc762acc958294e6921b00
@ -169,7 +171,7 @@ The `vin` should just match the input you substituted in, but it's the `vout` th
## Summary: Using Scripts in Libwally ## Summary: Using Scripts in Libwally
Creating transactions in Libwally is another topic that could take up a whole chapter, but the great thing is that you can introduce a P2SH `scriptPubKey` while doing so, and that part is pretty easy. Though the methodology detailed in this chapter requires you to have a transaction hex already in hand (probably created with `bitcoin-cli`) if you dig further into Libwally, you can do it all yourself. Creating transactions in Libwally is another topic that could take up a whole chapter, but the great thing is that once you make this leap, you can introduce a P2SH `scriptPubKey`, and that part alone is pretty easy. Though the methodology detailed in this chapter requires you to have a transaction hex already in hand (probably created with `bitcoin-cli`) if you dig further into Libwally, you can do it all yourself.
> :fire: ***What is the Power of Scripts in Libwally?*** Quite simply, you can do something you couldn't before: create a transaction locked with an arbitrary P2SH. > :fire: ***What is the Power of Scripts in Libwally?*** Quite simply, you can do something you couldn't before: create a transaction locked with an arbitrary P2SH.