Skip to content

Commit

Permalink
bitcoin/tx: move bitcoin_tx_from_file() to test-cli, expose bitcoin_t…
Browse files Browse the repository at this point in the history
…x_from_hex()

Signed-off-by: Rusty Russell <[email protected]>
  • Loading branch information
rustyrussell committed Jan 21, 2016
1 parent 04fd2c8 commit 3a803ee
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 29 deletions.
5 changes: 3 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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 := \
Expand Down Expand Up @@ -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 \
Expand Down
48 changes: 23 additions & 25 deletions bitcoin/tx.c
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#include "tx.h"
#include <assert.h>
#include <ccan/cast/cast.h>
#include <ccan/crypto/sha256/sha256.h>
#include <ccan/endian/endian.h>
#include <ccan/err/err.h>
#include <ccan/mem/mem.h>
#include <ccan/read_write_all/read_write_all.h>
#include <ccan/str/hex/hex.h>
#include <ccan/tal/grab_file/grab_file.h>
#include <stdio.h>

enum styles {
/* Add the CT padding stuff to amount. */
Expand Down Expand Up @@ -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++) {
Expand All @@ -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;
}

/* <sigh>. Bitcoind represents hashes as little-endian for RPC. This didn't
Expand Down
5 changes: 3 additions & 2 deletions bitcoin/tx.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
1 change: 1 addition & 0 deletions test-cli/create-commit-spend-tx.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "funding.h"
#include "version.h"
#include "bitcoin/locktime.h"
#include "tx_from_file.h"
#include <unistd.h>

int main(int argc, char *argv[])
Expand Down
1 change: 1 addition & 0 deletions test-cli/create-htlc-spend-tx.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "find_p2sh_out.h"
#include "bitcoin/locktime.h"
#include "version.h"
#include "tx_from_file.h"
#include <unistd.h>

int main(int argc, char *argv[])
Expand Down
1 change: 1 addition & 0 deletions test-cli/create-steal-tx.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "protobuf_convert.h"
#include "version.h"
#include "bitcoin/locktime.h"
#include "tx_from_file.h"
#include <unistd.h>

int main(int argc, char *argv[])
Expand Down
1 change: 1 addition & 0 deletions test-cli/open-anchor.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <time.h>
#include "opt_bits.h"
#include "version.h"
#include "tx_from_file.h"

int main(int argc, char *argv[])
{
Expand Down
21 changes: 21 additions & 0 deletions test-cli/tx_from_file.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include "tx_from_file.h"
#include "bitcoin/tx.h"
#include <ccan/err/err.h>
#include <ccan/tal/grab_file/grab_file.h>

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;
}
7 changes: 7 additions & 0 deletions test-cli/tx_from_file.h
Original file line number Diff line number Diff line change
@@ -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 */
1 change: 1 addition & 0 deletions test-cli/txid-of.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <ccan/err/err.h>
#include "bitcoin/tx.h"
#include "version.h"
#include "tx_from_file.h"
#include <unistd.h>

int main(int argc, char *argv[])
Expand Down

0 comments on commit 3a803ee

Please sign in to comment.