2017-11-22 01:25:39 +01:00
|
|
|
#ifndef LIGHTNING_COMMON_BOLT11_H
|
|
|
|
#define LIGHTNING_COMMON_BOLT11_H
|
2017-10-26 05:04:19 +02:00
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <bitcoin/short_channel_id.h>
|
|
|
|
#include <ccan/list/list.h>
|
|
|
|
#include <common/hash_u5.h>
|
2019-04-08 11:58:32 +02:00
|
|
|
#include <common/node_id.h>
|
2017-11-22 01:25:39 +01:00
|
|
|
#include <secp256k1_recovery.h>
|
2017-10-26 05:04:19 +02:00
|
|
|
|
|
|
|
/* We only have 10 bits for the field length, meaning < 640 bytes */
|
|
|
|
#define BOLT11_FIELD_BYTE_LIMIT ((1 << 10) * 5 / 8)
|
|
|
|
|
2020-12-03 10:33:55 +01:00
|
|
|
/* BOLT #11:
|
|
|
|
* * `c` (24): `data_length` variable.
|
2023-04-07 08:54:26 +02:00
|
|
|
* `min_final_cltv_expiry_delta` to use for the last HTLC in the route.
|
2020-12-03 10:33:55 +01:00
|
|
|
* Default is 18 if not specified.
|
|
|
|
*/
|
|
|
|
#define DEFAULT_FINAL_CLTV_DELTA 18
|
|
|
|
|
2020-04-02 06:04:47 +02:00
|
|
|
struct feature_set;
|
|
|
|
|
2017-10-26 05:04:19 +02:00
|
|
|
struct bolt11_field {
|
|
|
|
struct list_node list;
|
|
|
|
|
|
|
|
char tag;
|
|
|
|
u5 *data;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* BOLT #11:
|
|
|
|
* * `pubkey` (264 bits)
|
|
|
|
* * `short_channel_id` (64 bits)
|
2017-12-21 12:10:24 +01:00
|
|
|
* * `fee_base_msat` (32 bits, big-endian)
|
2017-12-12 01:34:07 +01:00
|
|
|
* * `fee_proportional_millionths` (32 bits, big-endian)
|
2017-10-26 05:04:19 +02:00
|
|
|
* * `cltv_expiry_delta` (16 bits, big-endian)
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct route_info {
|
2019-04-08 11:58:32 +02:00
|
|
|
/* This is 33 bytes, so we pack cltv_expiry_delta next to it */
|
|
|
|
struct node_id pubkey;
|
|
|
|
u16 cltv_expiry_delta;
|
2017-10-26 05:04:19 +02:00
|
|
|
struct short_channel_id short_channel_id;
|
2017-12-12 01:34:07 +01:00
|
|
|
u32 fee_base_msat, fee_proportional_millionths;
|
2017-10-26 05:04:19 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
struct bolt11 {
|
|
|
|
const struct chainparams *chain;
|
|
|
|
u64 timestamp;
|
2019-02-21 03:38:35 +01:00
|
|
|
struct amount_msat *msat; /* NULL if not specified. */
|
2017-10-26 05:04:19 +02:00
|
|
|
|
|
|
|
struct sha256 payment_hash;
|
2019-04-08 11:58:32 +02:00
|
|
|
struct node_id receiver_id;
|
2017-10-26 05:04:19 +02:00
|
|
|
|
2018-01-29 05:46:54 +01:00
|
|
|
/* description_hash valid if and only if description is NULL. */
|
2017-10-26 05:04:19 +02:00
|
|
|
const char *description;
|
|
|
|
struct sha256 *description_hash;
|
|
|
|
|
|
|
|
/* How many seconds to pay from @timestamp above. */
|
|
|
|
u64 expiry;
|
|
|
|
|
2017-10-26 05:08:19 +02:00
|
|
|
/* How many blocks final hop requires. */
|
|
|
|
u32 min_final_cltv_expiry;
|
|
|
|
|
2018-04-05 07:13:51 +02:00
|
|
|
/* If non-NULL, indicates fallback addresses to pay to. */
|
|
|
|
const u8 **fallbacks;
|
2017-10-26 05:04:19 +02:00
|
|
|
|
|
|
|
/* If non-NULL: array of route arrays */
|
|
|
|
struct route_info **routes;
|
|
|
|
|
|
|
|
/* signature of sha256 of entire thing. */
|
|
|
|
secp256k1_ecdsa_signature sig;
|
|
|
|
|
2019-11-23 01:19:23 +01:00
|
|
|
/* payment secret, if any. */
|
|
|
|
struct secret *payment_secret;
|
|
|
|
|
2019-09-05 03:07:16 +02:00
|
|
|
/* Features bitmap, if any. */
|
|
|
|
u8 *features;
|
|
|
|
|
2022-03-31 11:10:50 +02:00
|
|
|
/* Optional metadata to send with payment. */
|
|
|
|
u8 *metadata;
|
|
|
|
|
2017-10-26 05:04:19 +02:00
|
|
|
struct list_head extra_fields;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Decodes and checks signature; returns NULL on error; description is
|
2020-04-02 06:04:47 +02:00
|
|
|
* (optional) out-of-band description of payment, for `h` field.
|
|
|
|
* fset is NULL to accept any features (usually not desirable!).
|
2020-09-23 03:07:19 +02:00
|
|
|
*
|
|
|
|
* if @must_be_chain is not NULL, fails unless it's this chain.
|
2020-04-02 06:04:47 +02:00
|
|
|
*/
|
2017-10-26 05:04:19 +02:00
|
|
|
struct bolt11 *bolt11_decode(const tal_t *ctx, const char *str,
|
2020-04-03 02:03:59 +02:00
|
|
|
const struct feature_set *our_features,
|
2020-09-23 03:07:19 +02:00
|
|
|
const char *description,
|
|
|
|
const struct chainparams *must_be_chain,
|
|
|
|
char **fail);
|
2017-10-26 05:04:19 +02:00
|
|
|
|
2020-12-14 02:21:19 +01:00
|
|
|
/* Extracts signature but does not check it. */
|
|
|
|
struct bolt11 *bolt11_decode_nosig(const tal_t *ctx, const char *str,
|
|
|
|
const struct feature_set *our_features,
|
|
|
|
const char *description,
|
|
|
|
const struct chainparams *must_be_chain,
|
|
|
|
struct sha256 *hash,
|
2023-01-12 02:01:21 +01:00
|
|
|
const u5 **sig,
|
2020-12-14 02:21:19 +01:00
|
|
|
bool *have_n,
|
|
|
|
char **fail);
|
|
|
|
|
2017-10-26 05:04:19 +02:00
|
|
|
/* Initialize an empty bolt11 struct with optional amount */
|
2019-02-21 03:38:35 +01:00
|
|
|
struct bolt11 *new_bolt11(const tal_t *ctx,
|
|
|
|
const struct amount_msat *msat TAKES);
|
2017-10-26 05:04:19 +02:00
|
|
|
|
|
|
|
/* Encodes and signs, even if it's nonsense. */
|
2017-11-22 01:25:39 +01:00
|
|
|
char *bolt11_encode_(const tal_t *ctx,
|
|
|
|
const struct bolt11 *b11, bool n_field,
|
|
|
|
bool (*sign)(const u5 *u5bytes,
|
|
|
|
const u8 *hrpu8,
|
|
|
|
secp256k1_ecdsa_recoverable_signature *rsig,
|
|
|
|
void *arg),
|
|
|
|
void *arg);
|
|
|
|
|
|
|
|
#define bolt11_encode(ctx, b11, n_field, sign, arg) \
|
|
|
|
bolt11_encode_((ctx), (b11), (n_field), \
|
|
|
|
typesafe_cb_preargs(bool, void *, (sign), (arg), \
|
|
|
|
const u5 *, \
|
|
|
|
const u8 *, \
|
|
|
|
secp256k1_ecdsa_recoverable_signature *rsig), \
|
|
|
|
(arg))
|
2017-10-26 05:04:19 +02:00
|
|
|
|
2023-06-13 12:33:31 +02:00
|
|
|
/** to_canonical_invstr - return a canonical string where the following constrains are follow:
|
|
|
|
* - There is no `lightning:` prefix;
|
|
|
|
* - all the string is in lower case.
|
|
|
|
*/
|
|
|
|
const char *to_canonical_invstr(const tal_t *ctx, const char *invstring);
|
|
|
|
|
2022-03-31 11:10:50 +02:00
|
|
|
#if DEVELOPER
|
|
|
|
/* Flag for tests to suppress `min_final_cltv_expiry` field generation, to match test vectors */
|
|
|
|
extern bool dev_bolt11_no_c_generation;
|
|
|
|
#endif
|
|
|
|
|
2018-03-22 11:36:25 +01:00
|
|
|
#endif /* LIGHTNING_COMMON_BOLT11_H */
|