utils: tal_hexstr() helper.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell 2016-05-03 10:59:20 +09:30
parent 6f2cb72c27
commit 9eabab78ab
6 changed files with 27 additions and 13 deletions

View File

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

View File

@ -1,6 +1,7 @@
#include "bitcoin/tx.c"
#include "bitcoin/shadouble.c"
#include "bitcoin/varint.c"
#include "utils.c"
#include <assert.h>
#include <ccan/str/hex/hex.h>
@ -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);

View File

@ -7,6 +7,7 @@
#include "json.h"
#include "lightningd.h"
#include "log.h"
#include "utils.h"
#include <ccan/cast/cast.h>
#include <ccan/io/io.h>
#include <ccan/pipecmd/pipecmd.h>
@ -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);

View File

@ -11,6 +11,7 @@
#include "protobuf_convert.h"
#include "secrets.h"
#include "state.h"
#include "utils.h"
#include <ccan/crypto/sha256/sha256.h>
#include <ccan/io/io.h>
#include <ccan/mem/mem.h>
@ -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 */

9
utils.c Normal file
View File

@ -0,0 +1,9 @@
#include "utils.h"
#include <ccan/str/hex/hex.h>
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;
}

9
utils.h Normal file
View File

@ -0,0 +1,9 @@
#ifndef LIGHTNING_UTILS_H
#define LIGHTNING_UTILS_H
#include "config.h"
#include <ccan/tal/tal.h>
/* 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 */