From ecb39efed1b3f11fee4b479e4c2af55067b1eccb Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Mon, 8 Jun 2015 15:24:10 +0930 Subject: [PATCH] find_p2sh_out: extract helper. Really only for our silly little utils. Signed-off-by: Rusty Russell --- Makefile | 2 +- close-channel.c | 19 ++++--------------- create-close-tx.c | 19 ++++--------------- create-commit-spend-tx.c | 27 ++++++++------------------- find_p2sh_out.c | 24 ++++++++++++++++++++++++ find_p2sh_out.h | 11 +++++++++++ 6 files changed, 52 insertions(+), 50 deletions(-) create mode 100644 find_p2sh_out.c create mode 100644 find_p2sh_out.h diff --git a/Makefile b/Makefile index bdd1bf767..729015ca2 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,7 @@ PROTOCC:=protoc-c PROGRAMS := open-channel open-anchor-scriptsigs leak-anchor-sigs open-commit-sig check-commit-sig check-anchor-scriptsigs get-anchor-depth create-steal-tx create-commit-spend-tx close-channel create-close-tx -HELPER_OBJS := base58.o lightning.pb-c.o shadouble.o pkt.o bitcoin_script.o permute_tx.o signature.o bitcoin_tx.o bitcoin_address.o anchor.o commit_tx.o pubkey.o opt_bits.o close_tx.o +HELPER_OBJS := base58.o lightning.pb-c.o shadouble.o pkt.o bitcoin_script.o permute_tx.o signature.o bitcoin_tx.o bitcoin_address.o anchor.o commit_tx.o pubkey.o opt_bits.o close_tx.o find_p2sh_out.o CCAN_OBJS := ccan-crypto-sha256.o ccan-crypto-shachain.o ccan-err.o ccan-tal.o ccan-tal-str.o ccan-take.o ccan-list.o ccan-str.o ccan-opt-helpers.o ccan-opt.o ccan-opt-parse.o ccan-opt-usage.o ccan-read_write_all.o ccan-str-hex.o ccan-tal-grab_file.o ccan-noerr.o diff --git a/close-channel.c b/close-channel.c index 877007999..258e37655 100644 --- a/close-channel.c +++ b/close-channel.c @@ -18,6 +18,7 @@ #include "signature.h" #include "pubkey.h" #include "close_tx.h" +#include "find_p2sh_out.h" #include #include @@ -32,8 +33,7 @@ int main(int argc, char *argv[]) EC_KEY *privkey; bool testnet, complete = false; struct pubkey pubkey1, pubkey2; - u8 *redeemscript, *p2sh; - size_t i; + u8 *redeemscript; err_set_progname(argv[0]); @@ -74,20 +74,9 @@ int main(int argc, char *argv[]) /* This is what the anchor pays to; figure out whick output. */ redeemscript = bitcoin_redeem_2of2(ctx, &pubkey1, &pubkey2); - /* This is the scriptPubKey commit tx will have */ - p2sh = scriptpubkey_p2sh(ctx, redeemscript); - - for (i = 0; i < anchor->output_count; i++) { - if (anchor->output[i].script_length != tal_count(p2sh)) - continue; - if (memcmp(anchor->output[i].script, p2sh, tal_count(p2sh)) == 0) - break; - } - if (i == anchor->output_count) - errx(1, "No matching output in %s", argv[1]); - /* Now create the close tx to spend 2/2 output of anchor. */ - close_tx = create_close_tx(ctx, o1, o2, &anchor_txid, i); + close_tx = create_close_tx(ctx, o1, o2, &anchor_txid, + find_p2sh_out(anchor, redeemscript)); /* Sign it for them. */ sign_tx_input(ctx, close_tx, 0, redeemscript, tal_count(redeemscript), diff --git a/create-close-tx.c b/create-close-tx.c index 8bdc5f38a..1aa8c233c 100644 --- a/create-close-tx.c +++ b/create-close-tx.c @@ -17,6 +17,7 @@ #include "signature.h" #include "pubkey.h" #include "close_tx.h" +#include "find_p2sh_out.h" #include #include @@ -28,11 +29,10 @@ int main(int argc, char *argv[]) struct sha256_double anchor_txid; struct bitcoin_signature sig1, sig2; struct pubkey pubkey1, pubkey2; - u8 *redeemscript, *p2sh, *tx_arr; + u8 *redeemscript, *tx_arr; char *tx_hex; CloseChannel *close; CloseChannelComplete *closecomplete; - size_t i; err_set_progname(argv[0]); @@ -64,20 +64,9 @@ int main(int argc, char *argv[]) /* This is what the anchor pays to; figure out which output. */ redeemscript = bitcoin_redeem_2of2(ctx, &pubkey1, &pubkey2); - /* This is the scriptPubKey commit tx will have */ - p2sh = scriptpubkey_p2sh(ctx, redeemscript); - - for (i = 0; i < anchor->output_count; i++) { - if (anchor->output[i].script_length != tal_count(p2sh)) - continue; - if (memcmp(anchor->output[i].script, p2sh, tal_count(p2sh)) == 0) - break; - } - if (i == anchor->output_count) - errx(1, "No matching output in %s", argv[1]); - /* Now create the close tx to spend 2/2 output of anchor. */ - close_tx = create_close_tx(ctx, o1, o2, &anchor_txid, i); + close_tx = create_close_tx(ctx, o1, o2, &anchor_txid, + find_p2sh_out(anchor, redeemscript)); /* Signatures well-formed? */ sig1.stype = sig2.stype = SIGHASH_ALL; diff --git a/create-commit-spend-tx.c b/create-commit-spend-tx.c index 59d0f164c..3fb1098db 100644 --- a/create-commit-spend-tx.c +++ b/create-commit-spend-tx.c @@ -20,6 +20,7 @@ #include "pubkey.h" #include "bitcoin_address.h" #include "opt_bits.h" +#include "find_p2sh_out.h" #include #include @@ -32,10 +33,10 @@ int main(int argc, char *argv[]) EC_KEY *privkey; bool testnet; struct pubkey pubkey1, pubkey2, outpubkey; - u8 *redeemscript, *p2sh, *tx_arr; + u8 *redeemscript, *tx_arr; char *tx_hex; struct sha256 rhash; - size_t i; + size_t p2sh_out; u64 fee = 10000; err_set_progname(argv[0]); @@ -87,29 +88,17 @@ int main(int argc, char *argv[]) o2->locktime_seconds, &pubkey2, &rhash); - /* This is the scriptPubKey commit tx will have */ - p2sh = scriptpubkey_p2sh(ctx, redeemscript); - - /* Which output of commit tx are we spending? */ - for (i = 0; i < commit->output_count; i++) { - if (commit->output[i].script_length != tal_count(p2sh)) - continue; - if (memcmp(commit->output[i].script, p2sh, tal_count(p2sh)) == 0) - break; - } - if (i == commit->output_count) - errx(1, "No matching output in %s", argv[1]); - /* Now, create transaction to spend it. */ tx = bitcoin_tx(ctx, 1, 1); bitcoin_txid(commit, &tx->input[0].txid); - tx->input[0].index = i; + p2sh_out = find_p2sh_out(commit, redeemscript); + tx->input[0].index = p2sh_out; - if (commit->output[i].amount <= fee) + if (commit->output[p2sh_out].amount <= fee) errx(1, "Amount of %llu won't exceed fee", - (unsigned long long)commit->output[i].amount); + (unsigned long long)commit->output[p2sh_out].amount); - tx->output[0].amount = commit->output[i].amount - fee; + tx->output[0].amount = commit->output[p2sh_out].amount - fee; tx->output[0].script = scriptpubkey_p2sh(tx, bitcoin_redeem_single(tx, &outpubkey)); tx->output[0].script_length = cpu_to_le32(tal_count(tx->output[0].script)); diff --git a/find_p2sh_out.c b/find_p2sh_out.c new file mode 100644 index 000000000..41aa40be6 --- /dev/null +++ b/find_p2sh_out.c @@ -0,0 +1,24 @@ +#include "find_p2sh_out.h" +#include "bitcoin_tx.h" +#include "bitcoin_script.h" +#include +#include +#include + +u32 find_p2sh_out(const struct bitcoin_tx *tx, u8 *redeemscript) +{ + /* This is the scriptPubKey commit tx will have */ + u8 *p2sh = scriptpubkey_p2sh(NULL, redeemscript); + u32 i; + + for (i = 0; i < tx->output_count; i++) { + if (tx->output[i].script_length != tal_count(p2sh)) + continue; + if (memcmp(tx->output[i].script, p2sh, tal_count(p2sh)) == 0) + break; + } + if (i == tx->output_count) + errx(1, "No matching output in tx"); + tal_free(p2sh); + return i; +} diff --git a/find_p2sh_out.h b/find_p2sh_out.h new file mode 100644 index 000000000..5d2e1d0a0 --- /dev/null +++ b/find_p2sh_out.h @@ -0,0 +1,11 @@ +#ifndef LIGHTNING_FIND_P2SH_OUT_H +#define LIGHTNING_FIND_P2SH_OUT_H +#include + +struct bitcoin_tx; + +/* Normally we'd simply remember which output of the anchor or commit + * tx is the one which pays to this script. But for these examples, + * we have to figure it out by recreating the output and matching. */ +u32 find_p2sh_out(const struct bitcoin_tx *tx, u8 *redeemscript); +#endif /* LIGHTNING_FIND_P2SH_OUT_H */