From a253e8ac422a8cc8f9df779974bb7657f371fe1b Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Tue, 4 Feb 2020 10:33:22 +1030 Subject: [PATCH] common: use tabs everywhere. Didn't generally fixup inside comments and the bech32 code: reformatting that is just anti-social. Signed-off-by: Rusty Russell --- common/addr.h | 2 +- common/bolt11.c | 1196 +++++++++++++++++++++---------------------- common/daemon.c | 2 +- common/hash_u5.c | 52 +- common/hash_u5.h | 2 +- common/io_lock.h | 2 +- common/json.c | 56 +- common/json.h | 2 +- common/json_tok.c | 2 +- common/keyset.c | 2 +- common/param.h | 2 +- common/permute_tx.c | 2 +- common/utils.c | 2 +- common/wallet_tx.c | 2 +- common/wireaddr.c | 2 +- common/wireaddr.h | 2 +- 16 files changed, 665 insertions(+), 665 deletions(-) diff --git a/common/addr.h b/common/addr.h index 1890e53a5581..ba241784a579 100644 --- a/common/addr.h +++ b/common/addr.h @@ -8,6 +8,6 @@ /* Given a scriptPubkey, return an encoded address */ char *encode_scriptpubkey_to_addr(const tal_t *ctx, const struct chainparams *chainparams, - const u8 *scriptPubkey); + const u8 *scriptPubkey); #endif /* LIGHTNING_COMMON_ADDR_H */ diff --git a/common/bolt11.c b/common/bolt11.c index 1874d50896a2..5efa638aeff2 100644 --- a/common/bolt11.c +++ b/common/bolt11.c @@ -24,9 +24,9 @@ #include struct multiplier { - const char letter; - /* We can't represent p postfix to msat, so we multiply this by 10 */ - u64 m10; + const char letter; + /* We can't represent p postfix to msat, so we multiply this by 10 */ + u64 m10; }; /* BOLT #11: @@ -39,79 +39,79 @@ struct multiplier { * * `p` (pico): multiply by 0.000000000001 */ static struct multiplier multipliers[] = { - { 'm', 10 * MSAT_PER_BTC / 1000 }, - { 'u', 10 * MSAT_PER_BTC / 1000000 }, - { 'n', 10 * MSAT_PER_BTC / 1000000000 }, - { 'p', 10 * MSAT_PER_BTC / 1000000000000ULL } + { 'm', 10 * MSAT_PER_BTC / 1000 }, + { 'u', 10 * MSAT_PER_BTC / 1000000 }, + { 'n', 10 * MSAT_PER_BTC / 1000000000 }, + { 'p', 10 * MSAT_PER_BTC / 1000000000000ULL } }; /* If pad is false, we discard any bits which don't fit in the last byte. * Otherwise we add an extra byte */ static bool pull_bits(struct hash_u5 *hu5, - u5 **data, size_t *data_len, void *dst, size_t nbits, - bool pad) + u5 **data, size_t *data_len, void *dst, size_t nbits, + bool pad) { - size_t n5 = nbits / 5; - size_t len = 0; - - if (nbits % 5) - n5++; - - if (*data_len < n5) - return false; - if (!bech32_convert_bits(dst, &len, 8, *data, n5, 5, pad)) - return false; - if (hu5) - hash_u5(hu5, *data, n5); - *data += n5; - *data_len -= n5; - - return true; + size_t n5 = nbits / 5; + size_t len = 0; + + if (nbits % 5) + n5++; + + if (*data_len < n5) + return false; + if (!bech32_convert_bits(dst, &len, 8, *data, n5, 5, pad)) + return false; + if (hu5) + hash_u5(hu5, *data, n5); + *data += n5; + *data_len -= n5; + + return true; } /* For pulling fields where we should have checked it will succeed already. */ #ifndef NDEBUG -#define pull_bits_certain(hu5, data, data_len, dst, nbits, pad) \ - assert(pull_bits((hu5), (data), (data_len), (dst), (nbits), (pad))) +#define pull_bits_certain(hu5, data, data_len, dst, nbits, pad) \ + assert(pull_bits((hu5), (data), (data_len), (dst), (nbits), (pad))) #else #define pull_bits_certain pull_bits #endif /* Helper for pulling a variable-length big-endian int. */ static bool pull_uint(struct hash_u5 *hu5, - u5 **data, size_t *data_len, - u64 *val, size_t databits) + u5 **data, size_t *data_len, + u64 *val, size_t databits) { - be64 be_val; - - /* Too big. */ - if (databits > sizeof(be_val) * CHAR_BIT) - return false; - if (!pull_bits(hu5, data, data_len, &be_val, databits, true)) - return false; - *val = be64_to_cpu(be_val) >> (sizeof(be_val) * CHAR_BIT - databits); - return true; + be64 be_val; + + /* Too big. */ + if (databits > sizeof(be_val) * CHAR_BIT) + return false; + if (!pull_bits(hu5, data, data_len, &be_val, databits, true)) + return false; + *val = be64_to_cpu(be_val) >> (sizeof(be_val) * CHAR_BIT - databits); + return true; } static size_t num_u8(size_t num_u5) { - return (num_u5 * 5 + 4) / 8; + return (num_u5 * 5 + 4) / 8; } /* Frees bolt11, returns NULL. */ static struct bolt11 *decode_fail(struct bolt11 *b11, char **fail, - const char *fmt, ...) - PRINTF_FMT(3,4); + const char *fmt, ...) + PRINTF_FMT(3,4); static struct bolt11 *decode_fail(struct bolt11 *b11, char **fail, - const char *fmt, ...) + const char *fmt, ...) { - va_list ap; + va_list ap; - va_start(ap, fmt); - *fail = tal_vfmt(tal_parent(b11), fmt, ap); - va_end(ap); - return tal_free(b11); + va_start(ap, fmt); + *fail = tal_vfmt(tal_parent(b11), fmt, ap); + va_end(ap); + return tal_free(b11); } /* @@ -119,19 +119,19 @@ static struct bolt11 *decode_fail(struct bolt11 *b11, char **fail, * if any, or NULL. */ static char *unknown_field(struct bolt11 *b11, - struct hash_u5 *hu5, - u5 **data, size_t *data_len, - u5 type, size_t length) + struct hash_u5 *hu5, + u5 **data, size_t *data_len, + u5 type, size_t length) { - struct bolt11_field *extra = tal(b11, struct bolt11_field); - u8 u8data[num_u8(length)]; + struct bolt11_field *extra = tal(b11, struct bolt11_field); + u8 u8data[num_u8(length)]; - extra->tag = type; - extra->data = tal_dup_arr(extra, u5, *data, length, 0); - list_add_tail(&b11->extra_fields, &extra->list); + extra->tag = type; + extra->data = tal_dup_arr(extra, u5, *data, length, 0); + list_add_tail(&b11->extra_fields, &extra->list); - pull_bits_certain(hu5, data, data_len, u8data, length * 5, true); - return NULL; + pull_bits_certain(hu5, data, data_len, u8data, length * 5, true); + return NULL; } /* BOLT #11: @@ -140,33 +140,33 @@ static char *unknown_field(struct bolt11 *b11, * provides proof of payment */ static void decode_p(struct bolt11 *b11, - struct hash_u5 *hu5, - u5 **data, size_t *data_len, - size_t data_length, bool *have_p) + struct hash_u5 *hu5, + u5 **data, size_t *data_len, + size_t data_length, bool *have_p) { - /* BOLT #11: - * - * A payer... SHOULD use the first `p` field that it did NOT - * skip as the payment hash. - */ - if (*have_p) { - unknown_field(b11, hu5, data, data_len, 'p', data_length); - return; - } - - /* BOLT #11: - * - * A reader... MUST skip over unknown fields, OR an `f` field + /* BOLT #11: + * + * A payer... SHOULD use the first `p` field that it did NOT + * skip as the payment hash. + */ + if (*have_p) { + unknown_field(b11, hu5, data, data_len, 'p', data_length); + return; + } + + /* BOLT #11: + * + * A reader... MUST skip over unknown fields, OR an `f` field * with unknown `version`, OR `p`, `h`, `s` or `n` fields that do * NOT have `data_length`s of 52, 52, 52 or 53, respectively. */ - if (data_length != 52) { - unknown_field(b11, hu5, data, data_len, 'p', data_length); - return; - } + if (data_length != 52) { + unknown_field(b11, hu5, data, data_len, 'p', data_length); + return; + } - pull_bits_certain(hu5, data, data_len, &b11->payment_hash, 256, false); - *have_p = true; + pull_bits_certain(hu5, data, data_len, &b11->payment_hash, 256, false); + *have_p = true; } /* BOLT #11: @@ -175,19 +175,19 @@ static void decode_p(struct bolt11 *b11, * (UTF-8), e.g. '1 cup of coffee' or 'ナンセンス 1杯' */ static void decode_d(struct bolt11 *b11, - struct hash_u5 *hu5, - u5 **data, size_t *data_len, - size_t data_length, bool *have_d) + struct hash_u5 *hu5, + u5 **data, size_t *data_len, + size_t data_length, bool *have_d) { - if (*have_d) { - unknown_field(b11, hu5, data, data_len, 'd', data_length); - return; - } - - b11->description = tal_arrz(b11, char, num_u8(data_length) + 1); - pull_bits_certain(hu5, data, data_len, (u8 *)b11->description, - data_length*5, false); - *have_d = true; + if (*have_d) { + unknown_field(b11, hu5, data, data_len, 'd', data_length); + return; + } + + b11->description = tal_arrz(b11, char, num_u8(data_length) + 1); + pull_bits_certain(hu5, data, data_len, (u8 *)b11->description, + data_length*5, false); + *have_d = true; } /* BOLT #11: @@ -198,29 +198,29 @@ static void decode_d(struct bolt11 *b11, * transport specific and not defined here. */ static void decode_h(struct bolt11 *b11, - struct hash_u5 *hu5, - u5 **data, size_t *data_len, - size_t data_length, bool *have_h) + struct hash_u5 *hu5, + u5 **data, size_t *data_len, + size_t data_length, bool *have_h) { - if (*have_h) { - unknown_field(b11, hu5, data, data_len, 'h', data_length); - return; - } + if (*have_h) { + unknown_field(b11, hu5, data, data_len, 'h', data_length); + return; + } - /* BOLT #11: + /* BOLT #11: * * A reader... MUST skip over unknown fields, OR an `f` field * with unknown `version`, OR `p`, `h`, `s` or `n` fields that do * NOT have `data_length`s of 52, 52, 52 or 53, respectively. */ - if (data_length != 52) { - unknown_field(b11, hu5, data, data_len, 'h', data_length); - return; - } - - b11->description_hash = tal(b11, struct sha256); - pull_bits_certain(hu5, data, data_len, b11->description_hash, 256, - false); - *have_h = true; + if (data_length != 52) { + unknown_field(b11, hu5, data, data_len, 'h', data_length); + return; + } + + b11->description_hash = tal(b11, struct sha256); + pull_bits_certain(hu5, data, data_len, b11->description_hash, 256, + false); + *have_h = true; } /* BOLT #11: @@ -230,19 +230,19 @@ static void decode_h(struct bolt11 *b11, */ #define DEFAULT_X 3600 static char *decode_x(struct bolt11 *b11, - struct hash_u5 *hu5, - u5 **data, size_t *data_len, - size_t data_length, const bool *have_x) + struct hash_u5 *hu5, + u5 **data, size_t *data_len, + size_t data_length, const bool *have_x) { - if (*have_x) - return unknown_field(b11, hu5, data, data_len, 'x', - data_length); - - /* FIXME: Put upper limit in bolt 11 */ - if (!pull_uint(hu5, data, data_len, &b11->expiry, data_length * 5)) - return tal_fmt(b11, "x: length %zu chars is excessive", - *data_len); - return NULL; + if (*have_x) + return unknown_field(b11, hu5, data, data_len, 'x', + data_length); + + /* FIXME: Put upper limit in bolt 11 */ + if (!pull_uint(hu5, data, data_len, &b11->expiry, data_length * 5)) + return tal_fmt(b11, "x: length %zu chars is excessive", + *data_len); + return NULL; } /* BOLT #11: @@ -252,54 +252,54 @@ static char *decode_x(struct bolt11 *b11, */ #define DEFAULT_C 9 static char *decode_c(struct bolt11 *b11, - struct hash_u5 *hu5, - u5 **data, size_t *data_len, - size_t data_length, const bool *have_c) + struct hash_u5 *hu5, + u5 **data, size_t *data_len, + size_t data_length, const bool *have_c) { - u64 c; - if (*have_c) - return unknown_field(b11, hu5, data, data_len, 'c', - data_length); - - /* FIXME: Put upper limit in bolt 11 */ - if (!pull_uint(hu5, data, data_len, &c, data_length * 5)) - return tal_fmt(b11, "c: length %zu chars is excessive", - *data_len); - b11->min_final_cltv_expiry = c; - /* Can overflow, since c is 64 bits but value must be < 32 bits */ - if (b11->min_final_cltv_expiry != c) - return tal_fmt(b11, "c: %"PRIu64" is too large", c); - - return NULL; + u64 c; + if (*have_c) + return unknown_field(b11, hu5, data, data_len, 'c', + data_length); + + /* FIXME: Put upper limit in bolt 11 */ + if (!pull_uint(hu5, data, data_len, &c, data_length * 5)) + return tal_fmt(b11, "c: length %zu chars is excessive", + *data_len); + b11->min_final_cltv_expiry = c; + /* Can overflow, since c is 64 bits but value must be < 32 bits */ + if (b11->min_final_cltv_expiry != c) + return tal_fmt(b11, "c: %"PRIu64" is too large", c); + + return NULL; } static char *decode_n(struct bolt11 *b11, - struct hash_u5 *hu5, - u5 **data, size_t *data_len, - size_t data_length, bool *have_n) + struct hash_u5 *hu5, + u5 **data, size_t *data_len, + size_t data_length, bool *have_n) { - if (*have_n) - return unknown_field(b11, hu5, data, data_len, 'n', - data_length); + if (*have_n) + return unknown_field(b11, hu5, data, data_len, 'n', + data_length); - /* BOLT #11: + /* BOLT #11: * * A reader... MUST skip over unknown fields, OR an `f` field * with unknown `version`, OR `p`, `h`, `s` or `n` fields that do * NOT have `data_length`s of 52, 52, 52 or 53, respectively. */ - if (data_length != 53) - return unknown_field(b11, hu5, data, data_len, 'n', - data_length); + if (data_length != 53) + return unknown_field(b11, hu5, data, data_len, 'n', + data_length); - pull_bits_certain(hu5, data, data_len, &b11->receiver_id.k, + pull_bits_certain(hu5, data, data_len, &b11->receiver_id.k, data_length * 5, false); - if (!node_id_valid(&b11->receiver_id)) - return tal_fmt(b11, "n: invalid pubkey %s", + if (!node_id_valid(&b11->receiver_id)) + return tal_fmt(b11, "n: invalid pubkey %s", type_to_string(tmpctx, struct node_id, &b11->receiver_id)); - *have_n = true; - return NULL; + *have_n = true; + return NULL; } /* BOLT #11: @@ -308,29 +308,29 @@ static char *decode_n(struct bolt11 *b11, * forwarding nodes from probing the payment recipient. */ static char *decode_s(struct bolt11 *b11, - struct hash_u5 *hu5, - u5 **data, size_t *data_len, - size_t data_length, + struct hash_u5 *hu5, + u5 **data, size_t *data_len, + size_t data_length, bool *have_s) { - if (*have_s) - return unknown_field(b11, hu5, data, data_len, 's', - data_length); + if (*have_s) + return unknown_field(b11, hu5, data, data_len, 's', + data_length); - /* BOLT #11: + /* BOLT #11: * * A reader... MUST skip over unknown fields, OR an `f` field * with unknown `version`, OR `p`, `h`, `s` or `n` fields that do * NOT have `data_length`s of 52, 52, 52 or 53, respectively. */ - if (data_length != 52) - return unknown_field(b11, hu5, data, data_len, 's', - data_length); + if (data_length != 52) + return unknown_field(b11, hu5, data, data_len, 's', + data_length); - b11->payment_secret = tal(b11, struct secret); - pull_bits_certain(hu5, data, data_len, b11->payment_secret, 256, - false); + b11->payment_secret = tal(b11, struct secret); + pull_bits_certain(hu5, data, data_len, b11->payment_secret, 256, + false); *have_s = true; - return NULL; + return NULL; } /* BOLT #11: @@ -340,63 +340,63 @@ static char *decode_s(struct bolt11 *b11, * and contains a witness program or P2PKH or P2SH address. */ static char *decode_f(struct bolt11 *b11, - struct hash_u5 *hu5, - u5 **data, size_t *data_len, - size_t data_length) + struct hash_u5 *hu5, + u5 **data, size_t *data_len, + size_t data_length) { - u64 version; + u64 version; u8 *fallback; - if (!pull_uint(hu5, data, data_len, &version, 5)) - return tal_fmt(b11, "f: data_length %zu short", data_length); - data_length--; + if (!pull_uint(hu5, data, data_len, &version, 5)) + return tal_fmt(b11, "f: data_length %zu short", data_length); + data_length--; - /* BOLT #11: - * + /* BOLT #11: + * * for Bitcoin payments... MUST set an `f` field to a valid * witness version and program, OR to `17` followed by a * public key hash, OR to `18` followed by a script hash. */ - if (version == 17) { - /* Pay to pubkey hash (P2PKH) */ - struct bitcoin_address pkhash; - if (num_u8(data_length) != sizeof(pkhash)) - return tal_fmt(b11, "f: pkhash length %zu", - data_length); - - pull_bits_certain(hu5, data, data_len, &pkhash, data_length*5, - false); - fallback = scriptpubkey_p2pkh(b11, &pkhash); - } else if (version == 18) { - /* Pay to pubkey script hash (P2SH) */ - struct ripemd160 shash; - if (num_u8(data_length) != sizeof(shash)) - return tal_fmt(b11, "f: p2sh length %zu", - data_length); - - pull_bits_certain(hu5, data, data_len, &shash, data_length*5, - false); - fallback = scriptpubkey_p2sh_hash(b11, &shash); - } else if (version < 17) { - u8 *f = tal_arr(b11, u8, data_length * 5 / 8); - if (version == 0) { - if (tal_count(f) != 20 && tal_count(f) != 32) - return tal_fmt(b11, - "f: witness v0 bad length %zu", - data_length); - } - pull_bits_certain(hu5, data, data_len, f, data_length * 5, - false); - fallback = scriptpubkey_witness_raw(b11, version, + if (version == 17) { + /* Pay to pubkey hash (P2PKH) */ + struct bitcoin_address pkhash; + if (num_u8(data_length) != sizeof(pkhash)) + return tal_fmt(b11, "f: pkhash length %zu", + data_length); + + pull_bits_certain(hu5, data, data_len, &pkhash, data_length*5, + false); + fallback = scriptpubkey_p2pkh(b11, &pkhash); + } else if (version == 18) { + /* Pay to pubkey script hash (P2SH) */ + struct ripemd160 shash; + if (num_u8(data_length) != sizeof(shash)) + return tal_fmt(b11, "f: p2sh length %zu", + data_length); + + pull_bits_certain(hu5, data, data_len, &shash, data_length*5, + false); + fallback = scriptpubkey_p2sh_hash(b11, &shash); + } else if (version < 17) { + u8 *f = tal_arr(b11, u8, data_length * 5 / 8); + if (version == 0) { + if (tal_count(f) != 20 && tal_count(f) != 32) + return tal_fmt(b11, + "f: witness v0 bad length %zu", + data_length); + } + pull_bits_certain(hu5, data, data_len, f, data_length * 5, + false); + fallback = scriptpubkey_witness_raw(b11, version, f, tal_count(f)); - tal_free(f); - } else { + tal_free(f); + } else { /* Restore version for unknown field! */ (*data)--; (*data_len)++; data_length++; - return unknown_field(b11, hu5, data, data_len, 'f', - data_length); + return unknown_field(b11, hu5, data, data_len, 'f', + data_length); } if (b11->fallbacks == NULL) @@ -406,27 +406,27 @@ static char *decode_f(struct bolt11 *b11, b11->fallbacks[tal_count(b11->fallbacks)-1] = tal_steal(b11->fallbacks, fallback); - return NULL; + return NULL; } static bool fromwire_route_info(const u8 **cursor, size_t *max, - struct route_info *route_info) + struct route_info *route_info) { - fromwire_node_id(cursor, max, &route_info->pubkey); - fromwire_short_channel_id(cursor, max, &route_info->short_channel_id); - route_info->fee_base_msat = fromwire_u32(cursor, max); - route_info->fee_proportional_millionths = fromwire_u32(cursor, max); - route_info->cltv_expiry_delta = fromwire_u16(cursor, max); - return *cursor != NULL; + fromwire_node_id(cursor, max, &route_info->pubkey); + fromwire_short_channel_id(cursor, max, &route_info->short_channel_id); + route_info->fee_base_msat = fromwire_u32(cursor, max); + route_info->fee_proportional_millionths = fromwire_u32(cursor, max); + route_info->cltv_expiry_delta = fromwire_u16(cursor, max); + return *cursor != NULL; } static void towire_route_info(u8 **pptr, const struct route_info *route_info) { - towire_node_id(pptr, &route_info->pubkey); - towire_short_channel_id(pptr, &route_info->short_channel_id); - towire_u32(pptr, route_info->fee_base_msat); - towire_u32(pptr, route_info->fee_proportional_millionths); - towire_u16(pptr, route_info->cltv_expiry_delta); + towire_node_id(pptr, &route_info->pubkey); + towire_short_channel_id(pptr, &route_info->short_channel_id); + towire_u32(pptr, route_info->fee_base_msat); + towire_u32(pptr, route_info->fee_proportional_millionths); + towire_u16(pptr, route_info->cltv_expiry_delta); } /* BOLT #11: @@ -442,30 +442,30 @@ static void towire_route_info(u8 **pptr, const struct route_info *route_info) * * `cltv_expiry_delta` (16 bits, big-endian) */ static char *decode_r(struct bolt11 *b11, - struct hash_u5 *hu5, - u5 **data, size_t *data_len, - size_t data_length) + struct hash_u5 *hu5, + u5 **data, size_t *data_len, + size_t data_length) { - size_t rlen = data_length * 5 / 8; - u8 *r8 = tal_arr(tmpctx, u8, rlen); - size_t n = 0; - struct route_info *r = tal_arr(tmpctx, struct route_info, n); - const u8 *cursor = r8; + size_t rlen = data_length * 5 / 8; + u8 *r8 = tal_arr(tmpctx, u8, rlen); + size_t n = 0; + struct route_info *r = tal_arr(tmpctx, struct route_info, n); + const u8 *cursor = r8; - /* Route hops don't split in 5 bit boundaries, so convert whole thing */ - pull_bits_certain(hu5, data, data_len, r8, data_length * 5, false); + /* Route hops don't split in 5 bit boundaries, so convert whole thing */ + pull_bits_certain(hu5, data, data_len, r8, data_length * 5, false); - do { + do { struct route_info ri; - if (!fromwire_route_info(&cursor, &rlen, &ri)) { - return tal_fmt(b11, "r: hop %zu truncated", n); - } + if (!fromwire_route_info(&cursor, &rlen, &ri)) { + return tal_fmt(b11, "r: hop %zu truncated", n); + } tal_arr_expand(&r, ri); - } while (rlen); + } while (rlen); - /* Append route */ + /* Append route */ tal_arr_expand(&b11->routes, tal_steal(b11, r)); - return NULL; + return NULL; } static void shift_bitmap_down(u8 *bitmap, size_t bits) @@ -489,15 +489,15 @@ static void shift_bitmap_down(u8 *bitmap, size_t bits) * See [Feature Bits](#feature-bits). */ static char *decode_9(struct bolt11 *b11, - struct hash_u5 *hu5, - u5 **data, size_t *data_len, - size_t data_length) + struct hash_u5 *hu5, + u5 **data, size_t *data_len, + size_t data_length) { - size_t flen = (data_length * 5 + 7) / 8; + size_t flen = (data_length * 5 + 7) / 8; int badf; - b11->features = tal_arr(b11, u8, flen); - pull_bits_certain(hu5, data, data_len, b11->features, + b11->features = tal_arr(b11, u8, flen); + pull_bits_certain(hu5, data, data_len, b11->features, data_length * 5, true); /* pull_bits pads with zero bits: we need to remove them. */ @@ -525,40 +525,40 @@ static char *decode_9(struct bolt11 *b11, struct bolt11 *new_bolt11(const tal_t *ctx, const struct amount_msat *msat TAKES) { - struct bolt11 *b11 = tal(ctx, struct bolt11); - - list_head_init(&b11->extra_fields); - b11->description = NULL; - b11->description_hash = NULL; - b11->fallbacks = NULL; - b11->routes = NULL; - b11->msat = NULL; - b11->expiry = DEFAULT_X; + struct bolt11 *b11 = tal(ctx, struct bolt11); + + list_head_init(&b11->extra_fields); + b11->description = NULL; + b11->description_hash = NULL; + b11->fallbacks = NULL; + b11->routes = NULL; + b11->msat = NULL; + b11->expiry = DEFAULT_X; b11->features = tal_arr(b11, u8, 0); - b11->min_final_cltv_expiry = DEFAULT_C; + b11->min_final_cltv_expiry = DEFAULT_C; b11->payment_secret = NULL; - if (msat) - b11->msat = tal_dup(b11, struct amount_msat, msat); - return b11; + if (msat) + b11->msat = tal_dup(b11, struct amount_msat, msat); + return b11; } /* Decodes and checks signature; returns NULL on error. */ struct bolt11 *bolt11_decode(const tal_t *ctx, const char *str, - const char *description, char **fail) + const char *description, char **fail) { - char *hrp, *amountstr, *prefix; - u5 *data; - size_t data_len; - struct bolt11 *b11 = new_bolt11(ctx, NULL); - u8 sig_and_recid[65]; - secp256k1_ecdsa_recoverable_signature sig; - struct hash_u5 hu5; - struct sha256 hash; - bool have_p = false, have_n = false, have_d = false, have_h = false, - have_x = false, have_c = false, have_s = false; - - b11->routes = tal_arr(b11, struct route_info *, 0); + char *hrp, *amountstr, *prefix; + u5 *data; + size_t data_len; + struct bolt11 *b11 = new_bolt11(ctx, NULL); + u8 sig_and_recid[65]; + secp256k1_ecdsa_recoverable_signature sig; + struct hash_u5 hu5; + struct sha256 hash; + bool have_p = false, have_n = false, have_d = false, have_h = false, + have_x = false, have_c = false, have_s = false; + + b11->routes = tal_arr(b11, struct route_info *, 0); /* BOLT #11: * @@ -568,87 +568,87 @@ struct bolt11 *bolt11_decode(const tal_t *ctx, const char *str, if (strstarts(str, "lightning:") || strstarts(str, "LIGHTNING:")) str += strlen("lightning:"); - if (strlen(str) < 8) - return decode_fail(b11, fail, "Bad bech32 string"); + if (strlen(str) < 8) + return decode_fail(b11, fail, "Bad bech32 string"); - hrp = tal_arr(tmpctx, char, strlen(str) - 6); - data = tal_arr(tmpctx, u5, strlen(str) - 8); + hrp = tal_arr(tmpctx, char, strlen(str) - 6); + data = tal_arr(tmpctx, u5, strlen(str) - 8); - if (!bech32_decode(hrp, data, &data_len, str, (size_t)-1)) - return decode_fail(b11, fail, "Bad bech32 string"); + if (!bech32_decode(hrp, data, &data_len, str, (size_t)-1)) + return decode_fail(b11, fail, "Bad bech32 string"); - /* For signature checking at the end. */ - hash_u5_init(&hu5, hrp); + /* For signature checking at the end. */ + hash_u5_init(&hu5, hrp); - /* BOLT #11: - * + /* BOLT #11: + * * The human-readable part of a Lightning invoice consists of two sections: * 1. `prefix`: `ln` + BIP-0173 currency prefix (e.g. `lnbc` for Bitcoin mainnet, * `lntb` for Bitcoin testnet, and `lnbcrt` for Bitcoin regtest) * 1. `amount`: optional number in that currency, followed by an optional * `multiplier` letter. The unit encoded here is the 'social' convention of a payment unit -- in the case of Bitcoin the unit is 'bitcoin' NOT satoshis. */ - prefix = tal_strndup(tmpctx, hrp, strcspn(hrp, "0123456789")); + prefix = tal_strndup(tmpctx, hrp, strcspn(hrp, "0123456789")); - /* BOLT #11: - * + /* BOLT #11: + * * A reader...if it does NOT understand the `prefix`... MUST fail the payment. */ - if (!strstarts(prefix, "ln")) - return decode_fail(b11, fail, - "Prefix '%s' does not start with ln", prefix); - - b11->chain = chainparams_by_bip173(prefix + 2); - if (!b11->chain) - return decode_fail(b11, fail, "Unknown chain %s", prefix + 2); - - /* BOLT #11: - * - * - if the `amount` is empty: - * */ - amountstr = tal_strdup(tmpctx, hrp + strlen(prefix)); - if (streq(amountstr, "")) { - /* BOLT #11: - * - * - SHOULD indicate to the payer that amount is unspecified. - */ - b11->msat = NULL; - } else { - u64 m10 = 10; - u64 amount; - char *end; - - /* Gather and trim multiplier */ - end = amountstr + strlen(amountstr)-1; - for (size_t i = 0; i < ARRAY_SIZE(multipliers); i++) { - if (*end == multipliers[i].letter) { - m10 = multipliers[i].m10; - *end = '\0'; - break; - } - } - - /* BOLT #11: - * - * if `amount` contains a non-digit OR is followed by + if (!strstarts(prefix, "ln")) + return decode_fail(b11, fail, + "Prefix '%s' does not start with ln", prefix); + + b11->chain = chainparams_by_bip173(prefix + 2); + if (!b11->chain) + return decode_fail(b11, fail, "Unknown chain %s", prefix + 2); + + /* BOLT #11: + * + * - if the `amount` is empty: + * */ + amountstr = tal_strdup(tmpctx, hrp + strlen(prefix)); + if (streq(amountstr, "")) { + /* BOLT #11: + * + * - SHOULD indicate to the payer that amount is unspecified. + */ + b11->msat = NULL; + } else { + u64 m10 = 10; + u64 amount; + char *end; + + /* Gather and trim multiplier */ + end = amountstr + strlen(amountstr)-1; + for (size_t i = 0; i < ARRAY_SIZE(multipliers); i++) { + if (*end == multipliers[i].letter) { + m10 = multipliers[i].m10; + *end = '\0'; + break; + } + } + + /* BOLT #11: + * + * if `amount` contains a non-digit OR is followed by * anything except a `multiplier` (see table above)... MUST fail the * payment. - **/ - amount = strtoull(amountstr, &end, 10); - if (amount == ULLONG_MAX && errno == ERANGE) - return decode_fail(b11, fail, - "Invalid amount '%s'", amountstr); - if (!*amountstr || *end) - return decode_fail(b11, fail, - "Invalid amount postfix '%s'", end); - - /* BOLT #11: - * - * if the `multiplier` is present... MUST multiply + **/ + amount = strtoull(amountstr, &end, 10); + if (amount == ULLONG_MAX && errno == ERANGE) + return decode_fail(b11, fail, + "Invalid amount '%s'", amountstr); + if (!*amountstr || *end) + return decode_fail(b11, fail, + "Invalid amount postfix '%s'", end); + + /* BOLT #11: + * + * if the `multiplier` is present... MUST multiply * `amount` by the `multiplier` value to derive the * amount required for payment. */ - b11->msat = tal(b11, struct amount_msat); + b11->msat = tal(b11, struct amount_msat); /* BOLT-50143e388e16a449a92ed574fc16eb35b51426b9 #11: * * - if multiplier is `p` and the last decimal of `amount` is @@ -660,84 +660,84 @@ struct bolt11 *bolt11_decode(const tal_t *ctx, const char *str, "Invalid sub-millisatoshi amount" " '%sp'", amountstr); - b11->msat->millisatoshis = amount * m10 / 10; /* Raw: raw amount multiplier calculation */ - } + b11->msat->millisatoshis = amount * m10 / 10; /* Raw: raw amount multiplier calculation */ + } - /* BOLT #11: - * + /* BOLT #11: + * * The data part of a Lightning invoice consists of multiple sections: * * 1. `timestamp`: seconds-since-1970 (35 bits, big-endian) * 1. zero or more tagged parts * 1. `signature`: Bitcoin-style signature of above (520 bits) */ - if (!pull_uint(&hu5, &data, &data_len, &b11->timestamp, 35)) - return decode_fail(b11, fail, "Can't get 35-bit timestamp"); - - while (data_len > 520 / 5) { - const char *problem = NULL; - u64 type, data_length; - - /* BOLT #11: - * - * Each Tagged Field is of the form: - * - * 1. `type` (5 bits) - * 1. `data_length` (10 bits, big-endian) - * 1. `data` (`data_length` x 5 bits) - */ - if (!pull_uint(&hu5, &data, &data_len, &type, 5) - || !pull_uint(&hu5, &data, &data_len, &data_length, 10)) - return decode_fail(b11, fail, - "Can't get tag and length"); - - /* Can't exceed total data remaining. */ - if (data_length > data_len) - return decode_fail(b11, fail, "%c: truncated", - bech32_charset[type]); - - switch (bech32_charset[type]) { - case 'p': - decode_p(b11, &hu5, &data, &data_len, data_length, - &have_p); - break; - - case 'd': - decode_d(b11, &hu5, &data, &data_len, data_length, - &have_d); - break; - - case 'h': - decode_h(b11, &hu5, &data, &data_len, data_length, - &have_h); - break; - - case 'n': - problem = decode_n(b11, &hu5, &data, - &data_len, data_length, - &have_n); - break; - - case 'x': - problem = decode_x(b11, &hu5, &data, - &data_len, data_length, - &have_x); - break; - - case 'c': - problem = decode_c(b11, &hu5, &data, - &data_len, data_length, - &have_c); - break; - - case 'f': - problem = decode_f(b11, &hu5, &data, - &data_len, data_length); - break; - case 'r': - problem = decode_r(b11, &hu5, &data, &data_len, - data_length); - break; + if (!pull_uint(&hu5, &data, &data_len, &b11->timestamp, 35)) + return decode_fail(b11, fail, "Can't get 35-bit timestamp"); + + while (data_len > 520 / 5) { + const char *problem = NULL; + u64 type, data_length; + + /* BOLT #11: + * + * Each Tagged Field is of the form: + * + * 1. `type` (5 bits) + * 1. `data_length` (10 bits, big-endian) + * 1. `data` (`data_length` x 5 bits) + */ + if (!pull_uint(&hu5, &data, &data_len, &type, 5) + || !pull_uint(&hu5, &data, &data_len, &data_length, 10)) + return decode_fail(b11, fail, + "Can't get tag and length"); + + /* Can't exceed total data remaining. */ + if (data_length > data_len) + return decode_fail(b11, fail, "%c: truncated", + bech32_charset[type]); + + switch (bech32_charset[type]) { + case 'p': + decode_p(b11, &hu5, &data, &data_len, data_length, + &have_p); + break; + + case 'd': + decode_d(b11, &hu5, &data, &data_len, data_length, + &have_d); + break; + + case 'h': + decode_h(b11, &hu5, &data, &data_len, data_length, + &have_h); + break; + + case 'n': + problem = decode_n(b11, &hu5, &data, + &data_len, data_length, + &have_n); + break; + + case 'x': + problem = decode_x(b11, &hu5, &data, + &data_len, data_length, + &have_x); + break; + + case 'c': + problem = decode_c(b11, &hu5, &data, + &data_len, data_length, + &have_c); + break; + + case 'f': + problem = decode_f(b11, &hu5, &data, + &data_len, data_length); + break; + case 'r': + problem = decode_r(b11, &hu5, &data, &data_len, + data_length); + break; case '9': problem = decode_9(b11, &hu5, &data, &data_len, data_length); @@ -746,37 +746,37 @@ struct bolt11 *bolt11_decode(const tal_t *ctx, const char *str, problem = decode_s(b11, &hu5, &data, &data_len, data_length, &have_s); break; - default: - unknown_field(b11, &hu5, &data, &data_len, - bech32_charset[type], data_length); - } - if (problem) - return decode_fail(b11, fail, "%s", problem); - } + default: + unknown_field(b11, &hu5, &data, &data_len, + bech32_charset[type], data_length); + } + if (problem) + return decode_fail(b11, fail, "%s", problem); + } - if (!have_p) - return decode_fail(b11, fail, "No valid 'p' field found"); + if (!have_p) + return decode_fail(b11, fail, "No valid 'p' field found"); - if (have_h && description) { - struct sha256 sha; + if (have_h && description) { + struct sha256 sha; - /* BOLT #11: + /* BOLT #11: * - * A reader... MUST check that the SHA2 256-bit hash + * A reader... MUST check that the SHA2 256-bit hash * in the `h` field exactly matches the hashed * description. - */ - sha256(&sha, description, strlen(description)); - if (!sha256_eq(b11->description_hash, &sha)) - return decode_fail(b11, fail, - "h: does not match description"); - } - - hash_u5_done(&hu5, &hash); - - /* BOLT #11: - * - * A writer...MUST set `signature` to a valid 512-bit + */ + sha256(&sha, description, strlen(description)); + if (!sha256_eq(b11->description_hash, &sha)) + return decode_fail(b11, fail, + "h: does not match description"); + } + + hash_u5_done(&hu5, &hash); + + /* BOLT #11: + * + * A writer...MUST set `signature` to a valid 512-bit * secp256k1 signature of the SHA2 256-bit hash of the * human-readable part, represented as UTF-8 bytes, * concatenated with the data part (excluding the signature) @@ -784,53 +784,53 @@ struct bolt11 *bolt11_decode(const tal_t *ctx, const char *str, * boundary, with a trailing byte containing the recovery ID * (0, 1, 2, or 3). */ - if (!pull_bits(NULL, &data, &data_len, sig_and_recid, 520, false)) - return decode_fail(b11, fail, "signature truncated"); + if (!pull_bits(NULL, &data, &data_len, sig_and_recid, 520, false)) + return decode_fail(b11, fail, "signature truncated"); - assert(data_len == 0); + assert(data_len == 0); - if (!secp256k1_ecdsa_recoverable_signature_parse_compact - (secp256k1_ctx, &sig, sig_and_recid, sig_and_recid[64])) - return decode_fail(b11, fail, "signature invalid"); + if (!secp256k1_ecdsa_recoverable_signature_parse_compact + (secp256k1_ctx, &sig, sig_and_recid, sig_and_recid[64])) + return decode_fail(b11, fail, "signature invalid"); - secp256k1_ecdsa_recoverable_signature_convert(secp256k1_ctx, - &b11->sig, &sig); + secp256k1_ecdsa_recoverable_signature_convert(secp256k1_ctx, + &b11->sig, &sig); - /* BOLT #11: + /* BOLT #11: * - * A reader... MUST check that the `signature` is valid (see + * A reader... MUST check that the `signature` is valid (see * the `n` tagged field specified below). ... A reader... * MUST use the `n` field to validate the signature instead of * performing signature recovery. */ - if (!have_n) { + if (!have_n) { struct pubkey k; - if (!secp256k1_ecdsa_recover(secp256k1_ctx, - &k.pubkey, - &sig, - (const u8 *)&hash)) - return decode_fail(b11, fail, - "signature recovery failed"); + if (!secp256k1_ecdsa_recover(secp256k1_ctx, + &k.pubkey, + &sig, + (const u8 *)&hash)) + return decode_fail(b11, fail, + "signature recovery failed"); node_id_from_pubkey(&b11->receiver_id, &k); - } else { + } else { struct pubkey k; /* n parsing checked this! */ if (!pubkey_from_node_id(&k, &b11->receiver_id)) abort(); - if (!secp256k1_ecdsa_verify(secp256k1_ctx, &b11->sig, - (const u8 *)&hash, - &k.pubkey)) - return decode_fail(b11, fail, "invalid signature"); - } + if (!secp256k1_ecdsa_verify(secp256k1_ctx, &b11->sig, + (const u8 *)&hash, + &k.pubkey)) + return decode_fail(b11, fail, "invalid signature"); + } - return b11; + return b11; } /* 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)); - bech32_push_bits(data, &be_val, nbits); + be64 be_val = cpu_to_be64(val << (64 - nbits)); + bech32_push_bits(data, &be_val, nbits); } /* BOLT #11: @@ -843,15 +843,15 @@ static void push_varlen_uint(u5 **data, u64 val, size_t nbits) */ static void push_field_type_and_len(u5 **data, char type, 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); + 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); } static void push_field(u5 **data, char type, const void *src, size_t nbits) { push_field_type_and_len(data, type, nbits); - bech32_push_bits(data, src, nbits); + bech32_push_bits(data, src, nbits); } /* BOLT #11: @@ -864,18 +864,18 @@ static void push_field(u5 **data, char type, const void *src, size_t nbits) */ static void push_varlen_field(u5 **data, char type, u64 val) { - assert(bech32_charset_rev[(unsigned char)type] >= 0); - push_varlen_uint(data, bech32_charset_rev[(unsigned char)type], 5); - - for (size_t nbits = 5; nbits < 65; nbits += 5) { - if ((val >> nbits) == 0) { - push_varlen_uint(data, nbits / 5, 10); - push_varlen_uint(data, val, nbits); - return; - } - } - /* Can't be encoded in <= 60 bits. */ - abort(); + assert(bech32_charset_rev[(unsigned char)type] >= 0); + push_varlen_uint(data, bech32_charset_rev[(unsigned char)type], 5); + + for (size_t nbits = 5; nbits < 65; nbits += 5) { + if ((val >> nbits) == 0) { + push_varlen_uint(data, nbits / 5, 10); + push_varlen_uint(data, val, nbits); + return; + } + } + /* Can't be encoded in <= 60 bits. */ + abort(); } /* BOLT #11: @@ -886,90 +886,90 @@ static void push_varlen_field(u5 **data, char type, u64 val) */ static void push_fallback_addr(u5 **data, u5 version, const void *addr, u16 addr_len) { - 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); - bech32_push_bits(data, addr, addr_len * CHAR_BIT); + 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); + bech32_push_bits(data, addr, addr_len * CHAR_BIT); } static void encode_p(u5 **data, const struct sha256 *hash) { - push_field(data, 'p', hash, 256); + push_field(data, 'p', hash, 256); } static void encode_d(u5 **data, const char *description) { - push_field(data, 'd', description, strlen(description) * CHAR_BIT); + push_field(data, 'd', description, strlen(description) * CHAR_BIT); } static void encode_h(u5 **data, const struct sha256 *hash) { - push_field(data, 'h', hash, 256); + push_field(data, 'h', hash, 256); } static void encode_n(u5 **data, const struct node_id *id) { - assert(node_id_valid(id)); - push_field(data, 'n', id->k, sizeof(id->k) * CHAR_BIT); + assert(node_id_valid(id)); + push_field(data, 'n', id->k, sizeof(id->k) * CHAR_BIT); } static void encode_x(u5 **data, u64 expiry) { - push_varlen_field(data, 'x', expiry); + push_varlen_field(data, 'x', expiry); } static void encode_c(u5 **data, u16 min_final_cltv_expiry) { - push_varlen_field(data, 'c', min_final_cltv_expiry); + push_varlen_field(data, 'c', min_final_cltv_expiry); } static void encode_s(u5 **data, const struct secret *payment_secret) { - push_field(data, 's', payment_secret, 256); + push_field(data, 's', payment_secret, 256); } static void encode_f(u5 **data, const u8 *fallback) { - struct bitcoin_address pkh; - struct ripemd160 sh; - struct sha256 wsh; + struct bitcoin_address pkh; + struct ripemd160 sh; + struct sha256 wsh; - /* BOLT #11: - * + /* BOLT #11: + * * for Bitcoin payments... MUST set an `f` field to a valid * witness version and program, OR to `17` followed by a * public key hash, OR to `18` followed by a script hash. - */ - if (is_p2pkh(fallback, &pkh)) { - push_fallback_addr(data, 17, &pkh, sizeof(pkh)); - } else if (is_p2sh(fallback, &sh)) { - push_fallback_addr(data, 18, &sh, sizeof(sh)); - } else if (is_p2wpkh(fallback, &pkh)) { - push_fallback_addr(data, 0, &pkh, sizeof(pkh)); - } else if (is_p2wsh(fallback, &wsh)) { - push_fallback_addr(data, 0, &wsh, sizeof(wsh)); - } else if (tal_count(fallback) - && fallback[0] >= 0x50 - && fallback[0] < (0x50+16)) { - /* Other (future) witness versions: turn OP_N into N */ - push_fallback_addr(data, fallback[0] - 0x50, fallback + 1, - tal_count(fallback) - 1); - } else { - /* Copy raw. */ - push_field(data, 'f', - fallback, tal_count(fallback) * CHAR_BIT); - } + */ + if (is_p2pkh(fallback, &pkh)) { + push_fallback_addr(data, 17, &pkh, sizeof(pkh)); + } else if (is_p2sh(fallback, &sh)) { + push_fallback_addr(data, 18, &sh, sizeof(sh)); + } else if (is_p2wpkh(fallback, &pkh)) { + push_fallback_addr(data, 0, &pkh, sizeof(pkh)); + } else if (is_p2wsh(fallback, &wsh)) { + push_fallback_addr(data, 0, &wsh, sizeof(wsh)); + } else if (tal_count(fallback) + && fallback[0] >= 0x50 + && fallback[0] < (0x50+16)) { + /* Other (future) witness versions: turn OP_N into N */ + push_fallback_addr(data, fallback[0] - 0x50, fallback + 1, + tal_count(fallback) - 1); + } else { + /* Copy raw. */ + push_field(data, 'f', + fallback, tal_count(fallback) * CHAR_BIT); + } } static void encode_r(u5 **data, const struct route_info *r) { - u8 *rinfo = tal_arr(NULL, u8, 0); + u8 *rinfo = tal_arr(NULL, u8, 0); - for (size_t i = 0; i < tal_count(r); i++) - towire_route_info(&rinfo, &r[i]); + for (size_t i = 0; i < tal_count(r); i++) + towire_route_info(&rinfo, &r[i]); - push_field(data, 'r', rinfo, tal_count(rinfo) * CHAR_BIT); - tal_free(rinfo); + push_field(data, 'r', rinfo, tal_count(rinfo) * CHAR_BIT); + tal_free(rinfo); } static void maybe_encode_9(u5 **data, const u8 *features) @@ -992,49 +992,49 @@ static void maybe_encode_9(u5 **data, const u8 *features) */ if (tal_count(f5) != 0) { push_field_type_and_len(data, '9', tal_count(f5) * 5); - tal_expand(data, f5, tal_count(f5)); + tal_expand(data, f5, tal_count(f5)); } tal_free(f5); } static bool encode_extra(u5 **data, const struct bolt11_field *extra) { - size_t len; + size_t len; /* Can't encode an invalid tag. */ if (bech32_charset_rev[(unsigned char)extra->tag] == -1) return false; - push_varlen_uint(data, bech32_charset_rev[(unsigned char)extra->tag], 5); - push_varlen_uint(data, tal_count(extra->data), 10); + push_varlen_uint(data, bech32_charset_rev[(unsigned char)extra->tag], 5); + push_varlen_uint(data, tal_count(extra->data), 10); - /* extra->data is already u5s, so do this raw. */ - len = tal_count(*data); - tal_resize(data, len + tal_count(extra->data)); - memcpy(*data + len, extra->data, tal_count(extra->data)); + /* extra->data is already u5s, so do this raw. */ + len = tal_count(*data); + tal_resize(data, len + tal_count(extra->data)); + memcpy(*data + len, extra->data, tal_count(extra->data)); return true; } /* Encodes, even if it's nonsense. */ char *bolt11_encode_(const tal_t *ctx, - const struct bolt11 *b11, bool n_field, + const struct bolt11 *b11, bool n_field, bool (*sign)(const u5 *u5bytes, const u8 *hrpu8, secp256k1_ecdsa_recoverable_signature *rsig, - void *arg), + void *arg), void *arg) { - u5 *data = tal_arr(tmpctx, u5, 0); - char *hrp, *output; - u64 amount; - struct bolt11_field *extra; - secp256k1_ecdsa_recoverable_signature rsig; - u8 sig_and_recid[65]; - u8 *hrpu8; - int recid; - - /* BOLT #11: - * + u5 *data = tal_arr(tmpctx, u5, 0); + char *hrp, *output; + u64 amount; + struct bolt11_field *extra; + secp256k1_ecdsa_recoverable_signature rsig; + u8 sig_and_recid[65]; + u8 *hrpu8; + int recid; + + /* BOLT #11: + * * A writer: * - MUST encode `prefix` using the currency required for successful payment. * - if a specific minimum `amount` is required for successful payment: @@ -1043,94 +1043,94 @@ char *bolt11_encode_(const tal_t *ctx, * - If the `p` multiplier is used the `amount` the last decimal MUST be `0`. * - SHOULD use the shortest representation possible, by using the largest multiplier or omitting the multiplier. */ - if (b11->msat) { - char postfix; + if (b11->msat) { + char postfix; u64 msat = b11->msat->millisatoshis; /* Raw: best-multiplier calc */ - if (msat % MSAT_PER_BTC == 0) { - postfix = '\0'; - amount = msat / MSAT_PER_BTC; - } else { - size_t i; - for (i = 0; i < ARRAY_SIZE(multipliers)-1; i++) { - if (!(msat * 10 % multipliers[i].m10)) - break; - } - postfix = multipliers[i].letter; - amount = msat * 10 / multipliers[i].m10; - } - hrp = tal_fmt(tmpctx, "ln%s%"PRIu64"%c", - b11->chain->bip173_name, amount, postfix); - } else - hrp = tal_fmt(tmpctx, "ln%s", b11->chain->bip173_name); - - /* BOLT #11: - * + if (msat % MSAT_PER_BTC == 0) { + postfix = '\0'; + amount = msat / MSAT_PER_BTC; + } else { + size_t i; + for (i = 0; i < ARRAY_SIZE(multipliers)-1; i++) { + if (!(msat * 10 % multipliers[i].m10)) + break; + } + postfix = multipliers[i].letter; + amount = msat * 10 / multipliers[i].m10; + } + hrp = tal_fmt(tmpctx, "ln%s%"PRIu64"%c", + b11->chain->bip173_name, amount, postfix); + } else + hrp = tal_fmt(tmpctx, "ln%s", b11->chain->bip173_name); + + /* BOLT #11: + * * 1. `timestamp`: seconds-since-1970 (35 bits, big-endian) * 1. zero or more tagged parts * 1. `signature`: Bitcoin-style signature of above (520 bits) - */ - push_varlen_uint(&data, b11->timestamp, 35); + */ + push_varlen_uint(&data, b11->timestamp, 35); - /* BOLT #11: - * + /* BOLT #11: + * * if a writer offers more than one of any field type, * it... MUST specify the most-preferred field first, followed * by less-preferred fields, in order. */ - /* Thus we do built-in fields, then extras last. */ - encode_p(&data, &b11->payment_hash); + /* Thus we do built-in fields, then extras last. */ + encode_p(&data, &b11->payment_hash); - if (b11->description) - encode_d(&data, b11->description); + if (b11->description) + encode_d(&data, b11->description); - if (b11->description_hash) - encode_h(&data, b11->description_hash); + if (b11->description_hash) + encode_h(&data, b11->description_hash); - if (n_field) - encode_n(&data, &b11->receiver_id); + if (n_field) + encode_n(&data, &b11->receiver_id); - if (b11->expiry != DEFAULT_X) - encode_x(&data, b11->expiry); + if (b11->expiry != DEFAULT_X) + encode_x(&data, b11->expiry); - if (b11->min_final_cltv_expiry != DEFAULT_C) - encode_c(&data, b11->min_final_cltv_expiry); + if (b11->min_final_cltv_expiry != DEFAULT_C) + encode_c(&data, b11->min_final_cltv_expiry); if (b11->payment_secret) encode_s(&data, b11->payment_secret); - for (size_t i = 0; i < tal_count(b11->fallbacks); i++) - encode_f(&data, b11->fallbacks[i]); + for (size_t i = 0; i < tal_count(b11->fallbacks); i++) + encode_f(&data, b11->fallbacks[i]); - for (size_t i = 0; i < tal_count(b11->routes); i++) - encode_r(&data, b11->routes[i]); + for (size_t i = 0; i < tal_count(b11->routes); i++) + encode_r(&data, b11->routes[i]); maybe_encode_9(&data, b11->features); - list_for_each(&b11->extra_fields, extra, list) - if (!encode_extra(&data, extra)) + list_for_each(&b11->extra_fields, extra, list) + if (!encode_extra(&data, extra)) return NULL; - /* FIXME: towire_ should check this? */ - if (tal_count(data) > 65535) - return NULL; + /* FIXME: towire_ should check this? */ + if (tal_count(data) > 65535) + return NULL; - /* Need exact length here */ - hrpu8 = tal_dup_arr(tmpctx, u8, (const u8 *)hrp, strlen(hrp), 0); - if (!sign(data, hrpu8, &rsig, arg)) - return NULL; + /* Need exact length here */ + hrpu8 = tal_dup_arr(tmpctx, u8, (const u8 *)hrp, strlen(hrp), 0); + if (!sign(data, hrpu8, &rsig, arg)) + return NULL; - secp256k1_ecdsa_recoverable_signature_serialize_compact( - secp256k1_ctx, - sig_and_recid, - &recid, - &rsig); - sig_and_recid[64] = recid; + secp256k1_ecdsa_recoverable_signature_serialize_compact( + secp256k1_ctx, + sig_and_recid, + &recid, + &rsig); + sig_and_recid[64] = recid; - bech32_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)) - output = tal_free(output); + output = tal_arr(ctx, char, strlen(hrp) + tal_count(data) + 8); + if (!bech32_encode(output, hrp, data, tal_count(data), (size_t)-1)) + output = tal_free(output); - return output; + return output; } diff --git a/common/daemon.c b/common/daemon.c index 6ebd7394912f..efc3d2c8c0f3 100644 --- a/common/daemon.c +++ b/common/daemon.c @@ -149,7 +149,7 @@ void daemon_setup(const char *argv0, * not start if it cannot do its job correctly. */ if (sodium_init() == -1) errx(1, "Could not initialize libsodium. Maybe not enough entropy" - " available ?"); + " available ?"); /* We handle write returning errors! */ signal(SIGPIPE, SIG_IGN); diff --git a/common/hash_u5.c b/common/hash_u5.c index 0d3eb91a6bb0..00a1dea57e14 100644 --- a/common/hash_u5.c +++ b/common/hash_u5.c @@ -4,44 +4,44 @@ void hash_u5_init(struct hash_u5 *hu5, const char *hrp) { - hu5->buf = 0; - hu5->num_bits = 0; - sha256_init(&hu5->hash); - sha256_update(&hu5->hash, hrp, strlen(hrp)); + hu5->buf = 0; + hu5->num_bits = 0; + sha256_init(&hu5->hash); + sha256_update(&hu5->hash, hrp, strlen(hrp)); } void hash_u5(struct hash_u5 *hu5, const u8 *u5, size_t len) { - size_t bits = len * 5; + size_t bits = len * 5; - while (bits) { - size_t n = 5; + while (bits) { + size_t n = 5; - if (bits < n) - n = bits; + if (bits < n) + n = bits; - hu5->buf <<= n; - hu5->buf |= (*u5 >> (5-n)); - bits -= n; - hu5->num_bits += n; + hu5->buf <<= n; + hu5->buf |= (*u5 >> (5-n)); + bits -= n; + hu5->num_bits += n; - if (n == 5) - u5++; + if (n == 5) + u5++; - if (hu5->num_bits >= 32) { - be32 be32 = cpu_to_be32(hu5->buf >> (hu5->num_bits-32)); - sha256_update(&hu5->hash, &be32, sizeof(be32)); - hu5->num_bits -= 32; - } - } + if (hu5->num_bits >= 32) { + be32 be32 = cpu_to_be32(hu5->buf >> (hu5->num_bits-32)); + sha256_update(&hu5->hash, &be32, sizeof(be32)); + hu5->num_bits -= 32; + } + } } void hash_u5_done(struct hash_u5 *hu5, struct sha256 *res) { - if (hu5->num_bits) { - be32 be32 = cpu_to_be32(hu5->buf << (32 - hu5->num_bits)); + if (hu5->num_bits) { + be32 be32 = cpu_to_be32(hu5->buf << (32 - hu5->num_bits)); - sha256_update(&hu5->hash, &be32, (hu5->num_bits + 7) / 8); - } - sha256_done(&hu5->hash, res); + sha256_update(&hu5->hash, &be32, (hu5->num_bits + 7) / 8); + } + sha256_done(&hu5->hash, res); } diff --git a/common/hash_u5.h b/common/hash_u5.h index 65ded9a6df55..b85bd13fe8d9 100644 --- a/common/hash_u5.h +++ b/common/hash_u5.h @@ -9,7 +9,7 @@ typedef unsigned char u5; struct hash_u5 { - u64 buf; + u64 buf; unsigned int num_bits; struct sha256_ctx hash; }; diff --git a/common/io_lock.h b/common/io_lock.h index a1130d85483d..f995f17aee24 100644 --- a/common/io_lock.h +++ b/common/io_lock.h @@ -31,7 +31,7 @@ struct io_plan *io_lock_acquire_out_(struct io_conn *conn, struct io_lock *lock, void *arg); #define io_lock_acquire_in(conn, lock, next, arg) \ - io_lock_acquire_in_((conn), (lock), \ + io_lock_acquire_in_((conn), (lock), \ typesafe_cb_preargs(struct io_plan *, void *, \ (next), (arg), \ struct io_conn *), \ diff --git a/common/json.c b/common/json.c index cd504a5f3d41..0bfe18a0409a 100644 --- a/common/json.c +++ b/common/json.c @@ -115,7 +115,7 @@ bool json_to_number(const char *buffer, const jsmntok_t *tok, } bool json_to_u16(const char *buffer, const jsmntok_t *tok, - short unsigned int *num) + short unsigned int *num) { uint64_t u64; @@ -347,7 +347,7 @@ void json_tok_print(const char *buffer, const jsmntok_t *tok) printf("size: %d, count: %td\n", tok->size, last - first); while (first != last) { printf("%td. %.*s, %s\n", first - tok, - first->end - first->start, buffer + first->start, + first->end - first->start, buffer + first->start, jsmntype_to_string(first->type)); first++; } @@ -397,32 +397,32 @@ const jsmntok_t *json_delve(const char *buffer, const char *guide) { while (*guide) { - const char *key; - size_t len = strcspn(guide+1, ".[]"); - - key = tal_strndup(tmpctx, guide+1, len); - switch (guide[0]) { - case '.': - if (tok->type != JSMN_OBJECT) - return NULL; - tok = json_get_member(buffer, tok, key); - if (!tok) - return NULL; - break; - case '[': - if (tok->type != JSMN_ARRAY) - return NULL; - tok = json_get_arr(tok, atol(key)); - if (!tok) - return NULL; - /* Must be terminated */ - assert(guide[1+strlen(key)] == ']'); - len++; - break; - default: - abort(); - } - guide += len + 1; + const char *key; + size_t len = strcspn(guide+1, ".[]"); + + key = tal_strndup(tmpctx, guide+1, len); + switch (guide[0]) { + case '.': + if (tok->type != JSMN_OBJECT) + return NULL; + tok = json_get_member(buffer, tok, key); + if (!tok) + return NULL; + break; + case '[': + if (tok->type != JSMN_ARRAY) + return NULL; + tok = json_get_arr(tok, atol(key)); + if (!tok) + return NULL; + /* Must be terminated */ + assert(guide[1+strlen(key)] == ']'); + len++; + break; + default: + abort(); + } + guide += len + 1; } return tok; diff --git a/common/json.h b/common/json.h index 857686d259f3..d8eea25ad4f2 100644 --- a/common/json.h +++ b/common/json.h @@ -44,7 +44,7 @@ bool json_to_s64(const char *buffer, const jsmntok_t *tok, s64 *num); /* Extract number from this (may be a string, or a number literal) */ bool json_to_u32(const char *buffer, const jsmntok_t *tok, - uint32_t *num); + uint32_t *num); /* Extract number from this (may be a string, or a number literal) */ bool json_to_u16(const char *buffer, const jsmntok_t *tok, diff --git a/common/json_tok.c b/common/json_tok.c index 4531eac7af25..d30ce4f22a5a 100644 --- a/common/json_tok.c +++ b/common/json_tok.c @@ -202,7 +202,7 @@ struct command_result *param_sat_or_all(struct command *cmd, const char *name, } struct command_result *param_node_id(struct command *cmd, const char *name, - const char *buffer, const jsmntok_t *tok, + const char *buffer, const jsmntok_t *tok, struct node_id **id) { *id = tal(cmd, struct node_id); diff --git a/common/keyset.c b/common/keyset.c index 10bf3bba9626..bf35656ff64e 100644 --- a/common/keyset.c +++ b/common/keyset.c @@ -8,7 +8,7 @@ bool derive_keyset(const struct pubkey *per_commitment_point, bool option_static_remotekey, struct keyset *keyset) { - /* BOLT #3: + /* BOLT #3: * * ### `localpubkey`, `local_htlcpubkey`, `remote_htlcpubkey`, `local_delayedpubkey`, and `remote_delayedpubkey` Derivation * diff --git a/common/param.h b/common/param.h index f4e2b40d2b77..f301b6132681 100644 --- a/common/param.h +++ b/common/param.h @@ -73,7 +73,7 @@ const char *param_subcommand(struct command *cmd, const char *buffer, /* * Add a required parameter. */ -#define p_req(name, cbx, arg) \ +#define p_req(name, cbx, arg) \ name"", \ true, \ (param_cbx)(cbx), \ diff --git a/common/permute_tx.c b/common/permute_tx.c index c1f70570fc26..6166f98222db 100644 --- a/common/permute_tx.c +++ b/common/permute_tx.c @@ -35,7 +35,7 @@ static size_t find_best_in(struct wally_tx_input *inputs, size_t num) } static void swap_wally_inputs(struct wally_tx_input *inputs, - const void **map, + const void **map, size_t i1, size_t i2) { struct wally_tx_input tmpinput; diff --git a/common/utils.c b/common/utils.c index 7c7f05ecc51a..07a3a13c738c 100644 --- a/common/utils.c +++ b/common/utils.c @@ -138,6 +138,6 @@ void tal_arr_remove_(void *p, size_t elemsize, size_t n) assert(len % elemsize == 0); assert((n + 1) * elemsize <= len); memmove(objp + elemsize * n, objp + elemsize * (n+1), - len - (elemsize * (n+1))); + len - (elemsize * (n+1))); tal_resize((char **)p, len - elemsize); } diff --git a/common/wallet_tx.c b/common/wallet_tx.c index c44ca94189ed..ef53dd1056c8 100644 --- a/common/wallet_tx.c +++ b/common/wallet_tx.c @@ -37,7 +37,7 @@ struct command_result *param_wtx(struct command *cmd, buffer + tok->start); if (amount_sat_greater(wtx->amount, max)) - return command_fail(wtx->cmd, FUND_MAX_EXCEEDED, + return command_fail(wtx->cmd, FUND_MAX_EXCEEDED, "Amount exceeded %s", type_to_string(tmpctx, struct amount_sat, &max)); diff --git a/common/wireaddr.c b/common/wireaddr.c index 041513368603..75c74ff39c4d 100644 --- a/common/wireaddr.c +++ b/common/wireaddr.c @@ -311,7 +311,7 @@ static bool separate_address_and_port(const tal_t *ctx, const char *arg, struct wireaddr * wireaddr_from_hostname(const tal_t *ctx, - const char *hostname, + const char *hostname, const u16 port, bool *no_dns, struct sockaddr *broken_reply, const char **err_msg) diff --git a/common/wireaddr.h b/common/wireaddr.h index f0aaa1986be9..5ef494f4799c 100644 --- a/common/wireaddr.h +++ b/common/wireaddr.h @@ -90,7 +90,7 @@ char *fmt_wireaddr_without_port(const tal_t *ctx, const struct wireaddr *a); * we wanted to do a DNS lookup. */ struct wireaddr * wireaddr_from_hostname(const tal_t *ctx, - const char *hostname, + const char *hostname, const u16 port, bool *no_dns, struct sockaddr *broken_reply, const char **err_msg);