find_p2sh_out: extract helper.

Really only for our silly little utils.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell 2015-06-08 15:24:10 +09:30
parent 7175d73573
commit ecb39efed1
6 changed files with 52 additions and 50 deletions

View File

@ -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 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 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

View File

@ -18,6 +18,7 @@
#include "signature.h" #include "signature.h"
#include "pubkey.h" #include "pubkey.h"
#include "close_tx.h" #include "close_tx.h"
#include "find_p2sh_out.h"
#include <openssl/ec.h> #include <openssl/ec.h>
#include <unistd.h> #include <unistd.h>
@ -32,8 +33,7 @@ int main(int argc, char *argv[])
EC_KEY *privkey; EC_KEY *privkey;
bool testnet, complete = false; bool testnet, complete = false;
struct pubkey pubkey1, pubkey2; struct pubkey pubkey1, pubkey2;
u8 *redeemscript, *p2sh; u8 *redeemscript;
size_t i;
err_set_progname(argv[0]); 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. */ /* This is what the anchor pays to; figure out whick output. */
redeemscript = bitcoin_redeem_2of2(ctx, &pubkey1, &pubkey2); 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. */ /* 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 it for them. */
sign_tx_input(ctx, close_tx, 0, redeemscript, tal_count(redeemscript), sign_tx_input(ctx, close_tx, 0, redeemscript, tal_count(redeemscript),

View File

@ -17,6 +17,7 @@
#include "signature.h" #include "signature.h"
#include "pubkey.h" #include "pubkey.h"
#include "close_tx.h" #include "close_tx.h"
#include "find_p2sh_out.h"
#include <openssl/ec.h> #include <openssl/ec.h>
#include <unistd.h> #include <unistd.h>
@ -28,11 +29,10 @@ int main(int argc, char *argv[])
struct sha256_double anchor_txid; struct sha256_double anchor_txid;
struct bitcoin_signature sig1, sig2; struct bitcoin_signature sig1, sig2;
struct pubkey pubkey1, pubkey2; struct pubkey pubkey1, pubkey2;
u8 *redeemscript, *p2sh, *tx_arr; u8 *redeemscript, *tx_arr;
char *tx_hex; char *tx_hex;
CloseChannel *close; CloseChannel *close;
CloseChannelComplete *closecomplete; CloseChannelComplete *closecomplete;
size_t i;
err_set_progname(argv[0]); 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. */ /* This is what the anchor pays to; figure out which output. */
redeemscript = bitcoin_redeem_2of2(ctx, &pubkey1, &pubkey2); 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. */ /* 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? */ /* Signatures well-formed? */
sig1.stype = sig2.stype = SIGHASH_ALL; sig1.stype = sig2.stype = SIGHASH_ALL;

View File

@ -20,6 +20,7 @@
#include "pubkey.h" #include "pubkey.h"
#include "bitcoin_address.h" #include "bitcoin_address.h"
#include "opt_bits.h" #include "opt_bits.h"
#include "find_p2sh_out.h"
#include <openssl/ec.h> #include <openssl/ec.h>
#include <unistd.h> #include <unistd.h>
@ -32,10 +33,10 @@ int main(int argc, char *argv[])
EC_KEY *privkey; EC_KEY *privkey;
bool testnet; bool testnet;
struct pubkey pubkey1, pubkey2, outpubkey; struct pubkey pubkey1, pubkey2, outpubkey;
u8 *redeemscript, *p2sh, *tx_arr; u8 *redeemscript, *tx_arr;
char *tx_hex; char *tx_hex;
struct sha256 rhash; struct sha256 rhash;
size_t i; size_t p2sh_out;
u64 fee = 10000; u64 fee = 10000;
err_set_progname(argv[0]); err_set_progname(argv[0]);
@ -87,29 +88,17 @@ int main(int argc, char *argv[])
o2->locktime_seconds, o2->locktime_seconds,
&pubkey2, &rhash); &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. */ /* Now, create transaction to spend it. */
tx = bitcoin_tx(ctx, 1, 1); tx = bitcoin_tx(ctx, 1, 1);
bitcoin_txid(commit, &tx->input[0].txid); 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", 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, tx->output[0].script = scriptpubkey_p2sh(tx,
bitcoin_redeem_single(tx, &outpubkey)); bitcoin_redeem_single(tx, &outpubkey));
tx->output[0].script_length = cpu_to_le32(tal_count(tx->output[0].script)); tx->output[0].script_length = cpu_to_le32(tal_count(tx->output[0].script));

24
find_p2sh_out.c Normal file
View File

@ -0,0 +1,24 @@
#include "find_p2sh_out.h"
#include "bitcoin_tx.h"
#include "bitcoin_script.h"
#include <string.h>
#include <ccan/err/err.h>
#include <ccan/tal/tal.h>
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;
}

11
find_p2sh_out.h Normal file
View File

@ -0,0 +1,11 @@
#ifndef LIGHTNING_FIND_P2SH_OUT_H
#define LIGHTNING_FIND_P2SH_OUT_H
#include <ccan/short_types/short_types.h>
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 */