mirror of
https://github.com/ElementsProject/lightning.git
synced 2025-01-17 19:03:42 +01:00
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:
parent
04fd2c861f
commit
3a803eefbb
5
Makefile
5
Makefile
@ -62,7 +62,7 @@ CORE_SRC := \
|
|||||||
version.c
|
version.c
|
||||||
CORE_OBJS := $(CORE_SRC:.c=.o)
|
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)
|
TEST_CLI_OBJS := $(TEST_CLI_SRC:.c=.o)
|
||||||
|
|
||||||
CCAN_OBJS := \
|
CCAN_OBJS := \
|
||||||
@ -152,7 +152,8 @@ CCAN_HEADERS := \
|
|||||||
$(CCANDIR)/ccan/typesafe_cb/typesafe_cb.h
|
$(CCANDIR)/ccan/typesafe_cb/typesafe_cb.h
|
||||||
|
|
||||||
TEST_CLI_HEADERS := test-cli/gather_updates.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_HEADERS := bitcoin/address.h \
|
||||||
bitcoin/base58.h \
|
bitcoin/base58.h \
|
||||||
|
48
bitcoin/tx.c
48
bitcoin/tx.c
@ -1,12 +1,12 @@
|
|||||||
#include "tx.h"
|
#include "tx.h"
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
|
#include <ccan/cast/cast.h>
|
||||||
#include <ccan/crypto/sha256/sha256.h>
|
#include <ccan/crypto/sha256/sha256.h>
|
||||||
#include <ccan/endian/endian.h>
|
#include <ccan/endian/endian.h>
|
||||||
#include <ccan/err/err.h>
|
|
||||||
#include <ccan/mem/mem.h>
|
#include <ccan/mem/mem.h>
|
||||||
#include <ccan/read_write_all/read_write_all.h>
|
#include <ccan/read_write_all/read_write_all.h>
|
||||||
#include <ccan/str/hex/hex.h>
|
#include <ccan/str/hex/hex.h>
|
||||||
#include <ccan/tal/grab_file/grab_file.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
enum styles {
|
enum styles {
|
||||||
/* Add the CT padding stuff to amount. */
|
/* Add the CT padding stuff to amount. */
|
||||||
@ -432,35 +432,29 @@ static struct bitcoin_tx *pull_bitcoin_tx(const tal_t *ctx,
|
|||||||
return tx;
|
return tx;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct bitcoin_tx *bitcoin_tx_from_file(const tal_t *ctx,
|
struct bitcoin_tx *bitcoin_tx_from_hex(const tal_t *ctx, const char *hex)
|
||||||
const char *filename)
|
|
||||||
{
|
{
|
||||||
char *hex, *end;
|
char *end;
|
||||||
u8 *linear_tx;
|
u8 *linear_tx;
|
||||||
const u8 *p;
|
const u8 *p;
|
||||||
struct bitcoin_tx *tx;
|
struct bitcoin_tx *tx;
|
||||||
size_t len;
|
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, ':');
|
end = strchr(hex, ':');
|
||||||
if (!end)
|
if (!end) {
|
||||||
end = hex + strlen(hex);
|
end = cast_const(char *, hex) + strlen(hex);
|
||||||
|
if (strends(hex, "\n"))
|
||||||
|
end--;
|
||||||
|
}
|
||||||
|
|
||||||
len = hex_data_size(end - hex);
|
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))
|
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);
|
tx = pull_bitcoin_tx(ctx, &p, &len);
|
||||||
if (!tx)
|
if (!tx)
|
||||||
errx(1, "Bad transaction in %s", filename);
|
goto fail;
|
||||||
|
|
||||||
/* Optional appended [:input-amount]* */
|
/* Optional appended [:input-amount]* */
|
||||||
for (len = 0; len < tx->input_count; len++) {
|
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);
|
tx->input[len].input_amount = strtoull(end + 1, &end, 10);
|
||||||
}
|
}
|
||||||
if (len == tx->input_count) {
|
if (len == tx->input_count) {
|
||||||
if (*end != '\0')
|
if (*end != '\0' && *end != '\n')
|
||||||
errx(1, "Additional input amounts appended to %s",
|
goto fail_free_tx;
|
||||||
filename);
|
|
||||||
} else {
|
} else {
|
||||||
/* Input amounts are compulsory for alpha, to generate sigs */
|
/* Input amounts are compulsory for alpha, to generate sigs */
|
||||||
#ifdef ALPHA_TXSTYLE
|
#ifdef ALPHA_TXSTYLE
|
||||||
errx(1, "No input amount #%zu in %s", len, filename);
|
goto fail_free_tx;
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
tal_free(hex);
|
tal_free(linear_tx);
|
||||||
|
|
||||||
return 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
|
/* <sigh>. Bitcoind represents hashes as little-endian for RPC. This didn't
|
||||||
|
@ -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,
|
struct bitcoin_tx *bitcoin_tx(const tal_t *ctx, varint_t input_count,
|
||||||
varint_t output_count);
|
varint_t output_count);
|
||||||
|
|
||||||
struct bitcoin_tx *bitcoin_tx_from_file(const tal_t *ctx,
|
/* This takes a raw bitcoin tx in hex, with [:<64-bit-satoshi>] appended
|
||||||
const char *filename);
|
* 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);
|
bool bitcoin_tx_write(int fd, const struct bitcoin_tx *tx);
|
||||||
|
|
||||||
|
@ -22,6 +22,7 @@
|
|||||||
#include "funding.h"
|
#include "funding.h"
|
||||||
#include "version.h"
|
#include "version.h"
|
||||||
#include "bitcoin/locktime.h"
|
#include "bitcoin/locktime.h"
|
||||||
|
#include "tx_from_file.h"
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
|
@ -18,6 +18,7 @@
|
|||||||
#include "find_p2sh_out.h"
|
#include "find_p2sh_out.h"
|
||||||
#include "bitcoin/locktime.h"
|
#include "bitcoin/locktime.h"
|
||||||
#include "version.h"
|
#include "version.h"
|
||||||
|
#include "tx_from_file.h"
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
|
@ -16,6 +16,7 @@
|
|||||||
#include "protobuf_convert.h"
|
#include "protobuf_convert.h"
|
||||||
#include "version.h"
|
#include "version.h"
|
||||||
#include "bitcoin/locktime.h"
|
#include "bitcoin/locktime.h"
|
||||||
|
#include "tx_from_file.h"
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
|
@ -22,6 +22,7 @@
|
|||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include "opt_bits.h"
|
#include "opt_bits.h"
|
||||||
#include "version.h"
|
#include "version.h"
|
||||||
|
#include "tx_from_file.h"
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
|
21
test-cli/tx_from_file.c
Normal file
21
test-cli/tx_from_file.c
Normal 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
7
test-cli/tx_from_file.h
Normal 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 */
|
@ -7,6 +7,7 @@
|
|||||||
#include <ccan/err/err.h>
|
#include <ccan/err/err.h>
|
||||||
#include "bitcoin/tx.h"
|
#include "bitcoin/tx.h"
|
||||||
#include "version.h"
|
#include "version.h"
|
||||||
|
#include "tx_from_file.h"
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
|
Loading…
Reference in New Issue
Block a user