bitcoin/tx: move bitcoin_tx_from_file() to test-cli, expose bitcoin_tx_from_hex()

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell 2016-01-22 06:41:46 +10:30
parent 04fd2c861f
commit 3a803eefbb
10 changed files with 62 additions and 29 deletions

View File

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

View File

@ -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. */
@ -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;
}
/* <sigh>. Bitcoind represents hashes as little-endian for RPC. This didn't

View File

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

View File

@ -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[])

View File

@ -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[])

View File

@ -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[])

View File

@ -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[])
{

21
test-cli/tx_from_file.c Normal file
View File

@ -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
test-cli/tx_from_file.h Normal file
View File

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

View File

@ -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[])