forked from ElementsProject/lightning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bolt12.c
452 lines (389 loc) · 11.4 KB
/
bolt12.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
#include <bitcoin/chainparams.h>
#include <ccan/tal/str/str.h>
#include <common/bech32_util.h>
#include <common/bolt12.h>
#include <common/bolt12_merkle.h>
#include <common/configdir.h>
#include <common/features.h>
#include <secp256k1_schnorrsig.h>
#include <time.h>
bool bolt12_chains_match(const struct bitcoin_blkid *chains,
const struct chainparams *must_be_chain)
{
size_t num_chains;
/* BOLT-offers #12:
* - if the chain for the invoice is not solely bitcoin:
* - MUST specify `chains` the offer is valid for.
* - otherwise:
* - the bitcoin chain is implied as the first and only entry.
*/
/* BOLT-offers #12:
* The reader of an invoice_request:
*...
* - MUST fail the request if `chains` does not include (or
* imply) a supported chain.
*/
/* BOLT-offers #12:
*
* - if the chain for the invoice is not solely bitcoin:
* - MUST specify `chains` the invoice is valid for.
* - otherwise:
* - the bitcoin chain is implied as the first and only entry.
*/
num_chains = tal_count(chains);
if (num_chains == 0) {
num_chains = 1;
chains = &chainparams_for_network("bitcoin")->genesis_blockhash;
}
for (size_t i = 0; i < num_chains; i++) {
if (bitcoin_blkid_eq(&chains[i],
&must_be_chain->genesis_blockhash))
return true;
}
return false;
}
bool bolt12_chain_matches(const struct bitcoin_blkid *chain,
const struct chainparams *must_be_chain,
const struct bitcoin_blkid *deprecated_chains)
{
/* Obsolete: We used to put an array in here, but we only ever
* used a single value */
if (deprecated_apis && !chain)
chain = deprecated_chains;
if (!chain)
chain = &chainparams_for_network("bitcoin")->genesis_blockhash;
return bitcoin_blkid_eq(chain, &must_be_chain->genesis_blockhash);
}
static char *check_features_and_chain(const tal_t *ctx,
const struct feature_set *our_features,
const struct chainparams *must_be_chain,
const u8 *features,
const struct bitcoin_blkid *chains)
{
if (must_be_chain) {
if (!bolt12_chains_match(chains, must_be_chain))
return tal_fmt(ctx, "wrong chain");
}
if (our_features) {
int badf = features_unsupported(our_features, features,
BOLT11_FEATURE);
if (badf != -1)
return tal_fmt(ctx, "unknown feature bit %i", badf);
}
return NULL;
}
bool bolt12_check_signature(const struct tlv_field *fields,
const char *messagename,
const char *fieldname,
const struct point32 *key,
const struct bip340sig *sig)
{
struct sha256 m, shash;
merkle_tlv(fields, &m);
sighash_from_merkle(messagename, fieldname, &m, &shash);
return secp256k1_schnorrsig_verify(secp256k1_ctx,
sig->u8,
shash.u.u8,
&key->pubkey) == 1;
}
static char *check_signature(const tal_t *ctx,
const struct tlv_field *fields,
const char *messagename,
const char *fieldname,
const struct point32 *node_id,
const struct bip340sig *sig)
{
if (!node_id)
return tal_fmt(ctx, "Missing node_id");
if (!sig)
return tal_fmt(ctx, "Missing signature");
if (!bolt12_check_signature(fields,
messagename, fieldname, node_id, sig))
return tal_fmt(ctx, "Invalid signature");
return NULL;
}
static const u8 *string_to_data(const tal_t *ctx,
const char *str,
size_t str_len,
const char *hrp_expected,
size_t *dlen,
char **fail)
{
char *hrp;
u8 *data;
char *bech32;
size_t bech32_len;
bool have_plus = false;
/* First we collapse +\s*, except at start/end. */
bech32 = tal_arr(tmpctx, char, str_len);
bech32_len = 0;
for (size_t i = 0; i < str_len; i++) {
if (i != 0 && i+1 != str_len && !have_plus && str[i] == '+') {
have_plus = true;
continue;
}
if (have_plus && cisspace(str[i]))
continue;
have_plus = false;
bech32[bech32_len++] = str[i];
}
if (have_plus) {
*fail = tal_fmt(ctx, "unfinished string");
return NULL;
}
if (!from_bech32_charset(ctx, bech32, bech32_len, &hrp, &data)) {
*fail = tal_fmt(ctx, "invalid bech32 string");
return NULL;
}
if (!streq(hrp, hrp_expected)) {
*fail = tal_fmt(ctx, "unexpected prefix %s", hrp);
data = tal_free(data);
} else
*dlen = tal_bytelen(data);
tal_free(hrp);
return data;
}
char *offer_encode(const tal_t *ctx, const struct tlv_offer *offer_tlv)
{
u8 *wire;
wire = tal_arr(tmpctx, u8, 0);
towire_offer(&wire, offer_tlv);
return to_bech32_charset(ctx, "lno", wire);
}
struct tlv_offer *offer_decode(const tal_t *ctx,
const char *b12, size_t b12len,
const struct feature_set *our_features,
const struct chainparams *must_be_chain,
char **fail)
{
struct tlv_offer *offer = tlv_offer_new(ctx);
const u8 *data;
size_t dlen;
data = string_to_data(tmpctx, b12, b12len, "lno", &dlen, fail);
if (!data)
return tal_free(offer);
if (!fromwire_offer(&data, &dlen, offer)) {
*fail = tal_fmt(ctx, "invalid offer data");
return tal_free(offer);
}
*fail = check_features_and_chain(ctx,
our_features, must_be_chain,
offer->features,
offer->chains);
if (*fail)
return tal_free(offer);
/* BOLT-offers #12:
* - if `signature` is present, but is not a valid signature using
* `node_id` as described in [Signature Calculation](#signature-calculation):
* - MUST NOT respond to the offer.
*/
if (offer->signature) {
*fail = check_signature(ctx, offer->fields,
"offer", "signature",
offer->node_id, offer->signature);
if (*fail)
return tal_free(offer);
}
return offer;
}
char *invrequest_encode(const tal_t *ctx, const struct tlv_invoice_request *invrequest_tlv)
{
u8 *wire;
wire = tal_arr(tmpctx, u8, 0);
towire_invoice_request(&wire, invrequest_tlv);
return to_bech32_charset(ctx, "lnr", wire);
}
struct tlv_invoice_request *invrequest_decode(const tal_t *ctx,
const char *b12, size_t b12len,
const struct feature_set *our_features,
const struct chainparams *must_be_chain,
char **fail)
{
struct tlv_invoice_request *invrequest = tlv_invoice_request_new(ctx);
const u8 *data;
size_t dlen;
data = string_to_data(tmpctx, b12, b12len, "lnr", &dlen, fail);
if (!data)
return tal_free(invrequest);
if (!fromwire_invoice_request(&data, &dlen, invrequest)) {
*fail = tal_fmt(ctx, "invalid invoice_request data");
return tal_free(invrequest);
}
*fail = check_features_and_chain(ctx,
our_features, must_be_chain,
invrequest->features,
invrequest->chain
? invrequest->chain
: invrequest->chains);
if (*fail)
return tal_free(invrequest);
return invrequest;
}
char *invoice_encode(const tal_t *ctx, const struct tlv_invoice *invoice_tlv)
{
u8 *wire;
wire = tal_arr(tmpctx, u8, 0);
towire_invoice(&wire, invoice_tlv);
return to_bech32_charset(ctx, "lni", wire);
}
struct tlv_invoice *invoice_decode_nosig(const tal_t *ctx,
const char *b12, size_t b12len,
const struct feature_set *our_features,
const struct chainparams *must_be_chain,
char **fail)
{
struct tlv_invoice *invoice = tlv_invoice_new(ctx);
const u8 *data;
size_t dlen;
data = string_to_data(tmpctx, b12, b12len, "lni", &dlen, fail);
if (!data)
return tal_free(invoice);
if (!fromwire_invoice(&data, &dlen, invoice)) {
*fail = tal_fmt(ctx, "invalid invoice data");
return tal_free(invoice);
}
*fail = check_features_and_chain(ctx,
our_features, must_be_chain,
invoice->features,
invoice->chain
? invoice->chain : invoice->chains);
if (*fail)
return tal_free(invoice);
return invoice;
}
static void add_days(struct tm *tm, u32 number)
{
tm->tm_mday += number;
}
static void add_months(struct tm *tm, u32 number)
{
tm->tm_mon += number;
}
static void add_years(struct tm *tm, u32 number)
{
tm->tm_year += number;
}
static u64 time_change(u64 prevstart, u32 number,
void (*add_time)(struct tm *tm, u32 number),
bool day_const)
{
struct tm tm;
time_t prev = prevstart, ret;
tm = *gmtime(&prev);
for (;;) {
struct tm new_tm = tm;
add_time(&new_tm, number);
ret = mktime(&new_tm);
if (ret == (time_t)-1)
return 0;
/* If we overflowed that month, try one less. */
if (!day_const || new_tm.tm_mday == tm.tm_mday)
break;
tm.tm_mday--;
}
return ret;
}
u64 offer_period_start(u64 basetime, size_t n,
const struct tlv_offer_recurrence *recur)
{
/* BOLT-offers #12:
* 1. A `time_unit` defining 0 (seconds), 1 (days), 2 (months),
* 3 (years).
*/
switch (recur->time_unit) {
case 0:
return basetime + recur->period * n;
case 1:
return time_change(basetime, recur->period * n, add_days, false);
case 2:
return time_change(basetime, recur->period * n, add_months, true);
case 3:
return time_change(basetime, recur->period * n, add_years, true);
default:
/* This is our offer, how did we get here? */
return 0;
}
}
void offer_period_paywindow(const struct tlv_offer_recurrence *recurrence,
const struct tlv_offer_recurrence_paywindow *recurrence_paywindow,
const struct tlv_offer_recurrence_base *recurrence_base,
u64 basetime, u64 period_idx,
u64 *start, u64 *end)
{
/* BOLT-offers #12:
* - if the offer contains `recurrence_paywindow`:
*/
if (recurrence_paywindow) {
u64 pstart = offer_period_start(basetime, period_idx,
recurrence);
/* BOLT-offers #12:
* - if the offer has a `recurrence_basetime` or the
* `recurrence_counter` is non-zero:
* - SHOULD NOT send an `invoice_request` for a period prior to
* `seconds_before` seconds before that period start.
* - SHOULD NOT send an `invoice_request` for a period later
* than `seconds_after` seconds past that period start.
*/
*start = pstart - recurrence_paywindow->seconds_before;
*end = pstart + recurrence_paywindow->seconds_after;
/* First payment without recurrence_base, we give
* ourselves 60 seconds, since period will start
* now */
if (!recurrence_base && period_idx == 0
&& recurrence_paywindow->seconds_after < 60)
*end = pstart + 60;
} else {
/* BOLT-offers #12:
* - otherwise:
* - SHOULD NOT send an `invoice_request` with
* `recurrence_counter` is non-zero for a period whose
* immediate predecessor has not yet begun.
*/
if (period_idx == 0)
*start = 0;
else
*start = offer_period_start(basetime, period_idx-1,
recurrence);
/* BOLT-offers #12:
* - SHOULD NOT send an `invoice_request` for a period which
* has already passed.
*/
*end = offer_period_start(basetime, period_idx+1,
recurrence) - 1;
}
}
struct tlv_invoice *invoice_decode(const tal_t *ctx,
const char *b12, size_t b12len,
const struct feature_set *our_features,
const struct chainparams *must_be_chain,
char **fail)
{
struct tlv_invoice *invoice;
invoice = invoice_decode_nosig(ctx, b12, b12len, our_features,
must_be_chain, fail);
if (invoice) {
*fail = check_signature(ctx, invoice->fields,
"invoice", "signature",
invoice->node_id, invoice->signature);
if (*fail)
invoice = tal_free(invoice);
}
return invoice;
}
bool bolt12_has_invoice_prefix(const char *str)
{
return strstarts(str, "lni1") || strstarts(str, "LNI1");
}
bool bolt12_has_request_prefix(const char *str)
{
return strstarts(str, "lnr1") || strstarts(str, "LNR1");
}
bool bolt12_has_offer_prefix(const char *str)
{
return strstarts(str, "lno1") || strstarts(str, "LNO1");
}
bool bolt12_has_prefix(const char *str)
{
return bolt12_has_invoice_prefix(str) || bolt12_has_offer_prefix(str) ||
bolt12_has_request_prefix(str);
}