From c6353967660d3b214ebaaa59739b8a4fcdbb5a20 Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Mon, 23 Apr 2018 16:20:55 +0200 Subject: [PATCH] common: Moving some bech32 related utilities to bech32_util These were so far only used for bolt11 construction, but we'll need them for the DNS seed as well, so here we just pull them out into their own unit and prefix them. Signed-off-by: Christian Decker --- common/Makefile | 1 + common/bech32_util.c | 25 +++++++++++++++++++++++++ common/bech32_util.h | 13 +++++++++++++ common/bolt11.c | 33 +++++---------------------------- common/test/run-bolt11.c | 1 + devtools/Makefile | 1 + gossipd/Makefile | 2 ++ lightningd/Makefile | 1 + 8 files changed, 49 insertions(+), 28 deletions(-) create mode 100644 common/bech32_util.c create mode 100644 common/bech32_util.h diff --git a/common/Makefile b/common/Makefile index dccc2b405..17f043b23 100644 --- a/common/Makefile +++ b/common/Makefile @@ -1,5 +1,6 @@ COMMON_SRC_NOGEN := \ common/bech32.c \ + common/bech32_util.c \ common/bip32.c \ common/bolt11.c \ common/channel_config.c \ diff --git a/common/bech32_util.c b/common/bech32_util.c new file mode 100644 index 000000000..4c9eb23b8 --- /dev/null +++ b/common/bech32_util.c @@ -0,0 +1,25 @@ +#include "bech32_util.h" +#include + +static u8 get_bit(const u8 *src, size_t bitoff) +{ + return ((src[bitoff / 8] >> (7 - (bitoff % 8))) & 1); +} + +void bech32_push_bits(u5 **data, const void *src, size_t nbits) +{ + size_t i, b; + size_t data_len = tal_len(*data); + + for (i = 0; i < nbits; i += b) { + tal_resize(data, data_len+1); + (*data)[data_len] = 0; + for (b = 0; b < 5; b++) { + (*data)[data_len] <<= 1; + /* If we need bits we don't have, zero */ + if (i+b < nbits) + (*data)[data_len] |= get_bit(src, i+b); + } + data_len++; + } +} diff --git a/common/bech32_util.h b/common/bech32_util.h new file mode 100644 index 000000000..803d2f312 --- /dev/null +++ b/common/bech32_util.h @@ -0,0 +1,13 @@ +#ifndef LIGHTNING_COMMON_BECH32_UTIL_H +#define LIGHTNING_COMMON_BECH32_UTIL_H +#include "config.h" + +#include +#include + +/** + * Push the bytes in src in 5 bit format onto the end of data. + */ +void bech32_push_bits(u5 **data, const void *src, size_t nbits); + +#endif /* LIGHTNING_COMMON_BECH32_UTIL_H */ diff --git a/common/bolt11.c b/common/bolt11.c index 9aef0e444..4c12e1008 100644 --- a/common/bolt11.c +++ b/common/bolt11.c @@ -8,6 +8,7 @@ #include #include #include +#include #include #include #include @@ -700,35 +701,11 @@ struct bolt11 *bolt11_decode(const tal_t *ctx, const char *str, return b11; } -static u8 get_bit(const u8 *src, size_t bitoff) -{ - return ((src[bitoff / 8] >> (7 - (bitoff % 8))) & 1); -} - -/* Returns now many u5s were appended. */ -static void push_bits(u5 **data, const void *src, size_t nbits) -{ - size_t i, b; - size_t data_len = tal_len(*data); - - for (i = 0; i < nbits; i += b) { - tal_resize(data, data_len+1); - (*data)[data_len] = 0; - for (b = 0; b < 5; b++) { - (*data)[data_len] <<= 1; - /* If we need bits we don't have, zero */ - if (i+b < nbits) - (*data)[data_len] |= get_bit(src, i+b); - } - data_len++; - } -} - /* Helper for pushing a variable-length big-endian int. */ static void push_varlen_uint(u5 **data, u64 val, size_t nbits) { be64 be_val = cpu_to_be64(val << (64 - nbits)); - push_bits(data, &be_val, nbits); + bech32_push_bits(data, &be_val, nbits); } /* BOLT #11: @@ -744,7 +721,7 @@ static void push_field(u5 **data, char type, const void *src, size_t nbits) assert(bech32_charset_rev[(unsigned char)type] >= 0); push_varlen_uint(data, bech32_charset_rev[(unsigned char)type], 5); push_varlen_uint(data, (nbits + 4) / 5, 10); - push_bits(data, src, nbits); + bech32_push_bits(data, src, nbits); } /* BOLT #11: @@ -781,7 +758,7 @@ static void push_fallback_addr(u5 **data, u5 version, const void *addr, u16 addr push_varlen_uint(data, bech32_charset_rev[(unsigned char)'f'], 5); push_varlen_uint(data, ((5 + addr_len * CHAR_BIT) + 4) / 5, 10); push_varlen_uint(data, version, 5); - push_bits(data, addr, addr_len * CHAR_BIT); + bech32_push_bits(data, addr, addr_len * CHAR_BIT); } static void encode_p(u5 **data, const struct sha256 *hash) @@ -974,7 +951,7 @@ char *bolt11_encode_(const tal_t *ctx, &rsig); sig_and_recid[64] = recid; - push_bits(&data, sig_and_recid, sizeof(sig_and_recid) * CHAR_BIT); + bech32_push_bits(&data, sig_and_recid, sizeof(sig_and_recid) * CHAR_BIT); output = tal_arr(ctx, char, strlen(hrp) + tal_count(data) + 8); if (!bech32_encode(output, hrp, data, tal_count(data), (size_t)-1)) diff --git a/common/test/run-bolt11.c b/common/test/run-bolt11.c index a30d1dd68..aae727ad2 100644 --- a/common/test/run-bolt11.c +++ b/common/test/run-bolt11.c @@ -1,4 +1,5 @@ #include "../bech32.c" +#include "../bech32_util.c" #include "../bolt11.c" #include "../hash_u5.c" #include diff --git a/devtools/Makefile b/devtools/Makefile index 677e921a3..5241d4014 100644 --- a/devtools/Makefile +++ b/devtools/Makefile @@ -5,6 +5,7 @@ DEVTOOLS_TOOL_OBJS := $(DEVTOOLS_TOOL_SRC:.c=.o) DEVTOOLS_COMMON_OBJS := \ common/bech32.o \ + common/bech32_util.o \ common/bolt11.o \ common/hash_u5.o \ common/type_to_string.o \ diff --git a/gossipd/Makefile b/gossipd/Makefile index 80803dc8e..8d9274d40 100644 --- a/gossipd/Makefile +++ b/gossipd/Makefile @@ -36,6 +36,8 @@ LIGHTNINGD_HEADERS_GEN += $(LIGHTNINGD_GOSSIP_HEADERS) # Common source we use. GOSSIPD_COMMON_OBJS := \ + common/bech32.o \ + common/bech32_util.o \ common/bip32.o \ common/crypto_state.o \ common/crypto_sync.o \ diff --git a/lightningd/Makefile b/lightningd/Makefile index 6451104a5..d93ea956b 100644 --- a/lightningd/Makefile +++ b/lightningd/Makefile @@ -15,6 +15,7 @@ default: lightningd-all # Common source we use. LIGHTNINGD_COMMON_OBJS := \ common/bech32.o \ + common/bech32_util.o \ common/bip32.o \ common/bolt11.o \ common/channel_config.o \