diff --git a/Makefile b/Makefile index 3d035e7ca..e8d531bf4 100644 --- a/Makefile +++ b/Makefile @@ -45,6 +45,7 @@ CORE_SRC := \ permute_tx.c \ protobuf_convert.c \ remove_dust.c \ + utils.c \ version.c CORE_OBJS := $(CORE_SRC:.c=.o) @@ -156,6 +157,7 @@ CORE_HEADERS := close_tx.h \ remove_dust.h \ state.h \ state_types.h \ + utils.h \ version.h GEN_HEADERS := gen_state_names.h \ diff --git a/bitcoin/test/run-tx-encode.c b/bitcoin/test/run-tx-encode.c index f8048b011..bab07c6e2 100644 --- a/bitcoin/test/run-tx-encode.c +++ b/bitcoin/test/run-tx-encode.c @@ -1,6 +1,7 @@ #include "bitcoin/tx.c" #include "bitcoin/shadouble.c" #include "bitcoin/varint.c" +#include "utils.c" #include #include @@ -8,8 +9,7 @@ const char extended_tx[] = "02000000000101b5bef485c41d0d1f58d1e8a561924ece5c476d static void hexeq(const void *p, size_t len, const char *hex) { - char *tmphex = tal_arr(NULL, char, hex_str_size(len)); - hex_encode(p, len, tmphex, tal_count(tmphex)); + char *tmphex = tal_hexstr(NULL, p, len); if (strcmp(hex, tmphex)) { fprintf(stderr, "Expected '%s' got '%s'", hex, tmphex); diff --git a/daemon/bitcoind.c b/daemon/bitcoind.c index a6a1c1ce6..f39a2fc64 100644 --- a/daemon/bitcoind.c +++ b/daemon/bitcoind.c @@ -7,6 +7,7 @@ #include "json.h" #include "lightningd.h" #include "log.h" +#include "utils.h" #include #include #include @@ -240,9 +241,8 @@ void bitcoind_send_tx(struct lightningd_state *dstate, const struct bitcoin_tx *tx) { u8 *raw = linearize_tx(dstate, tx); - char *hex = tal_arr(raw, char, hex_str_size(tal_count(raw))); + char *hex = tal_hexstr(raw, raw, tal_count(raw)); - hex_encode(raw, tal_count(raw), hex, tal_count(hex)); start_bitcoin_cli(dstate, process_sendrawrx, NULL, NULL, "sendrawtransaction", hex, NULL); tal_free(raw); diff --git a/daemon/packets.c b/daemon/packets.c index a2e7b09b7..26ff5b808 100644 --- a/daemon/packets.c +++ b/daemon/packets.c @@ -11,6 +11,7 @@ #include "protobuf_convert.h" #include "secrets.h" #include "state.h" +#include "utils.h" #include #include #include @@ -22,23 +23,16 @@ #define FIXME_STUB(peer) do { log_broken((peer)->dstate->base_log, "%s:%u: Implement %s!", __FILE__, __LINE__, __func__); abort(); } while(0) -static char *hex_of(const tal_t *ctx, const void *p, size_t n) -{ - char *hex = tal_arr(ctx, char, hex_str_size(n)); - hex_encode(p, n, hex, hex_str_size(n)); - return hex; -} - static void dump_tx(const char *str, const struct bitcoin_tx *tx) { u8 *linear = linearize_tx(NULL, tx); - printf("%s:%s\n", str, hex_of(linear, linear, tal_count(linear))); + printf("%s:%s\n", str, tal_hexstr(linear, linear, tal_count(linear))); tal_free(linear); } static void dump_key(const char *str, const struct pubkey *key) { - printf("%s:%s\n", str, hex_of(NULL, key->der, sizeof(key->der))); + printf("%s:%s\n", str, tal_hexstr(NULL, key->der, sizeof(key->der))); } /* Wrap (and own!) member inside Pkt */ diff --git a/utils.c b/utils.c new file mode 100644 index 000000000..86d9790d4 --- /dev/null +++ b/utils.c @@ -0,0 +1,9 @@ +#include "utils.h" +#include + +char *tal_hexstr(const tal_t *ctx, const void *data, size_t len) +{ + char *str = tal_arr(ctx, char, hex_str_size(len)); + hex_encode(data, len, str, hex_str_size(len)); + return str; +} diff --git a/utils.h b/utils.h new file mode 100644 index 000000000..f89af8c95 --- /dev/null +++ b/utils.h @@ -0,0 +1,9 @@ +#ifndef LIGHTNING_UTILS_H +#define LIGHTNING_UTILS_H +#include "config.h" +#include + +/* Allocate and fill in a hex-encoded string of this data. */ +char *tal_hexstr(const tal_t *ctx, const void *data, size_t len); + +#endif /* LIGHTNING_UTILS_H */