From 3a803eefbb5a09543417ffc71288cf48b56b6410 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Fri, 22 Jan 2016 06:41:46 +1030 Subject: [PATCH] bitcoin/tx: move bitcoin_tx_from_file() to test-cli, expose bitcoin_tx_from_hex() Signed-off-by: Rusty Russell --- Makefile | 5 ++-- bitcoin/tx.c | 48 +++++++++++++++---------------- bitcoin/tx.h | 5 ++-- test-cli/create-commit-spend-tx.c | 1 + test-cli/create-htlc-spend-tx.c | 1 + test-cli/create-steal-tx.c | 1 + test-cli/open-anchor.c | 1 + test-cli/tx_from_file.c | 21 ++++++++++++++ test-cli/tx_from_file.h | 7 +++++ test-cli/txid-of.c | 1 + 10 files changed, 62 insertions(+), 29 deletions(-) create mode 100644 test-cli/tx_from_file.c create mode 100644 test-cli/tx_from_file.h diff --git a/Makefile b/Makefile index 4462c0236..326ede9d1 100644 --- a/Makefile +++ b/Makefile @@ -62,7 +62,7 @@ CORE_SRC := \ version.c CORE_OBJS := $(CORE_SRC:.c=.o) -TEST_CLI_SRC := test-cli/gather_updates.c test-cli/pkt.c +TEST_CLI_SRC := test-cli/gather_updates.c test-cli/pkt.c test-cli/tx_from_file.c TEST_CLI_OBJS := $(TEST_CLI_SRC:.c=.o) CCAN_OBJS := \ @@ -152,7 +152,8 @@ CCAN_HEADERS := \ $(CCANDIR)/ccan/typesafe_cb/typesafe_cb.h TEST_CLI_HEADERS := test-cli/gather_updates.h \ - test-cli/pkt.h + test-cli/pkt.h \ + test-cli/tx_from_file.h BITCOIN_HEADERS := bitcoin/address.h \ bitcoin/base58.h \ diff --git a/bitcoin/tx.c b/bitcoin/tx.c index 0ef595490..b8ae8f194 100644 --- a/bitcoin/tx.c +++ b/bitcoin/tx.c @@ -1,12 +1,12 @@ #include "tx.h" #include +#include #include #include -#include #include #include #include -#include +#include enum styles { /* Add the CT padding stuff to amount. */ @@ -432,35 +432,29 @@ static struct bitcoin_tx *pull_bitcoin_tx(const tal_t *ctx, return tx; } -struct bitcoin_tx *bitcoin_tx_from_file(const tal_t *ctx, - const char *filename) +struct bitcoin_tx *bitcoin_tx_from_hex(const tal_t *ctx, const char *hex) { - char *hex, *end; + char *end; u8 *linear_tx; const u8 *p; struct bitcoin_tx *tx; size_t len; - /* Grabs file, add nul at end. */ - hex = grab_file(ctx, filename); - if (!hex) - err(1, "Opening %s", filename); - - if (strends(hex, "\n")) - hex[strlen(hex)-1] = '\0'; - end = strchr(hex, ':'); - if (!end) - end = hex + strlen(hex); - + if (!end) { + end = cast_const(char *, hex) + strlen(hex); + if (strends(hex, "\n")) + end--; + } + len = hex_data_size(end - hex); - p = linear_tx = tal_arr(hex, u8, len); + p = linear_tx = tal_arr(ctx, u8, len); if (!hex_decode(hex, end - hex, linear_tx, len)) - errx(1, "Bad hex string in %s", filename); + goto fail; tx = pull_bitcoin_tx(ctx, &p, &len); if (!tx) - errx(1, "Bad transaction in %s", filename); + goto fail; /* Optional appended [:input-amount]* */ for (len = 0; len < tx->input_count; len++) { @@ -469,18 +463,22 @@ struct bitcoin_tx *bitcoin_tx_from_file(const tal_t *ctx, tx->input[len].input_amount = strtoull(end + 1, &end, 10); } if (len == tx->input_count) { - if (*end != '\0') - errx(1, "Additional input amounts appended to %s", - filename); + if (*end != '\0' && *end != '\n') + goto fail_free_tx; } else { /* Input amounts are compulsory for alpha, to generate sigs */ #ifdef ALPHA_TXSTYLE - errx(1, "No input amount #%zu in %s", len, filename); + goto fail_free_tx; #endif } - tal_free(hex); - + tal_free(linear_tx); return tx; + +fail_free_tx: + tal_free(tx); +fail: + tal_free(linear_tx); + return NULL; } /* . Bitcoind represents hashes as little-endian for RPC. This didn't diff --git a/bitcoin/tx.h b/bitcoin/tx.h index 965e99d79..59563a89b 100644 --- a/bitcoin/tx.h +++ b/bitcoin/tx.h @@ -54,8 +54,9 @@ u8 *linearize_tx(const tal_t *ctx, const struct bitcoin_tx *tx); struct bitcoin_tx *bitcoin_tx(const tal_t *ctx, varint_t input_count, varint_t output_count); -struct bitcoin_tx *bitcoin_tx_from_file(const tal_t *ctx, - const char *filename); +/* This takes a raw bitcoin tx in hex, with [:<64-bit-satoshi>] appended + * for each input (required for -DALPHA). */ +struct bitcoin_tx *bitcoin_tx_from_hex(const tal_t *ctx, const char *hex); bool bitcoin_tx_write(int fd, const struct bitcoin_tx *tx); diff --git a/test-cli/create-commit-spend-tx.c b/test-cli/create-commit-spend-tx.c index c5c939036..79fbd56aa 100644 --- a/test-cli/create-commit-spend-tx.c +++ b/test-cli/create-commit-spend-tx.c @@ -22,6 +22,7 @@ #include "funding.h" #include "version.h" #include "bitcoin/locktime.h" +#include "tx_from_file.h" #include int main(int argc, char *argv[]) diff --git a/test-cli/create-htlc-spend-tx.c b/test-cli/create-htlc-spend-tx.c index 46565555a..8c78adca2 100644 --- a/test-cli/create-htlc-spend-tx.c +++ b/test-cli/create-htlc-spend-tx.c @@ -18,6 +18,7 @@ #include "find_p2sh_out.h" #include "bitcoin/locktime.h" #include "version.h" +#include "tx_from_file.h" #include int main(int argc, char *argv[]) diff --git a/test-cli/create-steal-tx.c b/test-cli/create-steal-tx.c index 890fda4d0..6d9088b68 100644 --- a/test-cli/create-steal-tx.c +++ b/test-cli/create-steal-tx.c @@ -16,6 +16,7 @@ #include "protobuf_convert.h" #include "version.h" #include "bitcoin/locktime.h" +#include "tx_from_file.h" #include int main(int argc, char *argv[]) diff --git a/test-cli/open-anchor.c b/test-cli/open-anchor.c index e21848d1a..8b9ba2399 100644 --- a/test-cli/open-anchor.c +++ b/test-cli/open-anchor.c @@ -22,6 +22,7 @@ #include #include "opt_bits.h" #include "version.h" +#include "tx_from_file.h" int main(int argc, char *argv[]) { diff --git a/test-cli/tx_from_file.c b/test-cli/tx_from_file.c new file mode 100644 index 000000000..6c64c56e1 --- /dev/null +++ b/test-cli/tx_from_file.c @@ -0,0 +1,21 @@ +#include "tx_from_file.h" +#include "bitcoin/tx.h" +#include +#include + +struct bitcoin_tx *bitcoin_tx_from_file(const tal_t *ctx, const char *filename) +{ + char *hex; + struct bitcoin_tx *tx; + + /* Grabs file, add nul at end. */ + hex = grab_file(ctx, filename); + if (!hex) + err(1, "Opening %s", filename); + + tx = bitcoin_tx_from_hex(ctx, hex); + if (!tx) + err(1, "Failed to decode tx '%s'", hex); + tal_free(hex); + return tx; +} diff --git a/test-cli/tx_from_file.h b/test-cli/tx_from_file.h new file mode 100644 index 000000000..76155326c --- /dev/null +++ b/test-cli/tx_from_file.h @@ -0,0 +1,7 @@ +#ifndef LIGHTNING_TEST_CLI_TX_FROM_FILE_H +#define LIGHTNING_TEST_CLI_TX_FROM_FILE_H +#include "config.h" +#include "bitcoin/tx.h" + +struct bitcoin_tx *bitcoin_tx_from_file(const tal_t *ctx, const char *filename); +#endif /* LIGHTNING_TEST_CLI_TX_FROM_FILE_H */ diff --git a/test-cli/txid-of.c b/test-cli/txid-of.c index 5ebee44e4..929ec18c3 100644 --- a/test-cli/txid-of.c +++ b/test-cli/txid-of.c @@ -7,6 +7,7 @@ #include #include "bitcoin/tx.h" #include "version.h" +#include "tx_from_file.h" #include int main(int argc, char *argv[])