-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathoffers.c
More file actions
1647 lines (1478 loc) · 49.5 KB
/
Copy pathoffers.c
File metadata and controls
1647 lines (1478 loc) · 49.5 KB
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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* This plugin covers both sending and receiving offers */
#include "config.h"
#include <ccan/array_size/array_size.h>
#include <ccan/bitops/bitops.h>
#include <ccan/rune/rune.h>
#include <ccan/tal/str/str.h>
#include <common/bech32.h>
#include <common/bech32_util.h>
#include <common/bolt11_json.h>
#include <common/bolt12_id.h>
#include <common/bolt12_merkle.h>
#include <common/clock_time.h>
#include <common/features.h>
#include <common/gossmap.h>
#include <common/iso4217.h>
#include <common/json_blinded_path.h>
#include <common/json_param.h>
#include <common/json_stream.h>
#include <common/memleak.h>
#include <common/onion_message.h>
#include <errno.h>
#include <inttypes.h>
#include <plugins/establish_onion_path.h>
#include <plugins/fetchinvoice.h>
#include <plugins/offers.h>
#include <plugins/offers_inv_hook.h>
#include <plugins/offers_invreq_hook.h>
#include <plugins/offers_offer.h>
#include <sodium.h>
#define HEADER_LEN crypto_secretstream_xchacha20poly1305_HEADERBYTES
#define ABYTES crypto_secretstream_xchacha20poly1305_ABYTES
struct pubkey id;
u32 blockheight;
u16 cltv_final;
bool disable_connect;
bool dev_invoice_bpath_scid;
struct short_channel_id *dev_invoice_internal_scid;
struct secret invoicesecret_base;
struct secret offerblinding_base;
struct secret nodealias_base;
static struct gossmap *global_gossmap;
static void init_gossmap(struct plugin *plugin)
{
global_gossmap
= notleak_with_children(gossmap_load(plugin,
GOSSIP_STORE_FILENAME,
plugin_gossmap_logcb,
plugin));
if (!global_gossmap)
plugin_err(plugin, "Could not load gossmap %s: %s",
GOSSIP_STORE_FILENAME, strerror(errno));
}
struct gossmap *get_gossmap(struct plugin *plugin)
{
if (!global_gossmap)
init_gossmap(plugin);
else
gossmap_refresh(global_gossmap);
return global_gossmap;
}
/* BOLT #12:
* - if it is connected only by private channels:
* - MUST include `offer_paths` containing one or more paths to the node
* from publicly reachable nodes.
*/
bool we_want_blinded_path(struct plugin *plugin, bool for_payment)
{
struct node_id local_nodeid;
const struct gossmap_node *node;
const u8 *nannounce;
const struct gossmap *gossmap = get_gossmap(plugin);
struct node_id our_id;
secp256k1_ecdsa_signature signature;
u32 timestamp;
u8 *addresses, *features;
u8 rgb_color[3], alias[32];
struct tlv_node_ann_tlvs *na_tlvs;
node_id_from_pubkey(&local_nodeid, &id);
node = gossmap_find_node(gossmap, &local_nodeid);
if (!node)
return true;
/* Matt Corallo also suggests we do this (for now) if we don't
* advertize an address to connect to, so they can fetch the
* invoice for the offer, or send the invoice for the invoicerequest.
* For actual payments they can use any route. */
if (for_payment)
return false;
/* We expect to know our own node announcements, but just in case. */
nannounce = gossmap_node_get_announce(tmpctx, gossmap, node);
if (!nannounce)
return true;
if (!fromwire_node_announcement(tmpctx, nannounce,
&signature, &features,
×tamp,
&our_id, rgb_color, alias,
&addresses,
&na_tlvs))
return true;
return tal_count(fromwire_wireaddr_array(tmpctx, addresses)) == 0;
}
static struct command_result *finished(struct command *cmd,
const char *method,
const char *buf,
const jsmntok_t *result,
void *unused)
{
return command_hook_success(cmd);
}
static struct command_result *injectonionmessage_error(struct command *cmd,
const char *method,
const char *buf,
const jsmntok_t *err,
void *unused)
{
/* This can happen if the peer goes offline or wasn't directly
* connected: "Unknown first peer" */
plugin_log(cmd->plugin, LOG_DBG,
"injectonionmessage gave JSON error: %.*s",
json_tok_full_len(err),
json_tok_full(buf, err));
return command_hook_success(cmd);
}
struct command_result *
inject_onionmessage_(struct command *cmd,
const struct onion_message *omsg,
struct command_result *(*cb)(struct command *command,
const char *method,
const char *buf,
const jsmntok_t *result,
void *arg),
struct command_result *(*errcb)(struct command *command,
const char *method,
const char *buf,
const jsmntok_t *result,
void *arg),
void *arg)
{
struct out_req *req;
struct sphinx_path *sphinx_path;
struct onionpacket *op;
struct secret *path_secrets;
size_t onion_size;
/* Create an onion which encodes this. */
sphinx_path = sphinx_path_new(tmpctx, NULL, 0);
for (size_t i = 0; i < tal_count(omsg->hops); i++)
sphinx_add_hop(sphinx_path,
&omsg->hops[i]->pubkey, omsg->hops[i]->raw_payload);
/* BOLT #4:
* - SHOULD set `onion_message_packet` `len` to 1366 or 32834.
*/
if (sphinx_path_payloads_size(sphinx_path) <= ROUTING_INFO_SIZE)
onion_size = ROUTING_INFO_SIZE;
else
onion_size = 32768; /* VERSION_SIZE + HMAC_SIZE + PUBKEY_SIZE == 66 */
op = create_onionpacket(tmpctx, sphinx_path, onion_size, &path_secrets);
if (!op) {
/* errcb expects a JSON message. */
const char *err;
jsmntok_t *toks;
err = tal_fmt(tmpctx, "{\"code\":%d,\"message\":\"Creating onion failed (tlvs too long?)\"}",
JSONRPC2_INVALID_PARAMS);
toks = json_parse_simple(tmpctx, err, strlen(err));
assert(toks);
/* Not really an error from injectpaymentonion, but near enough */
return errcb(cmd, "injectpaymentonion", err, toks, arg);
}
req = jsonrpc_request_start(cmd, "injectonionmessage", cb, errcb, arg);
json_add_pubkey(req->js, "path_key", &omsg->first_path_key);
json_add_hex_talarr(req->js, "message", serialize_onionpacket(tmpctx, op));
return send_outreq(req);
}
/* Holds the details while we wait for establish_onion_path to connect */
struct onion_reply {
struct blinded_path *reply_path;
struct tlv_onionmsg_tlv *payload;
};
static struct command_result *send_onion_reply_after_established(struct command *cmd,
const struct pubkey *path,
struct onion_reply *onion_reply)
{
struct onion_message *omsg;
omsg = outgoing_onion_message(tmpctx, path, NULL, onion_reply->reply_path, onion_reply->payload);
return inject_onionmessage(cmd, omsg, finished, injectonionmessage_error, onion_reply);
}
static struct command_result *send_onion_reply_not_established(struct command *cmd,
const char *why,
struct onion_reply *onion_reply)
{
plugin_log(cmd->plugin, LOG_DBG,
"Failed to connect for reply via %s: %s",
fmt_sciddir_or_pubkey(tmpctx, &onion_reply->reply_path->first_node_id),
why);
return command_hook_success(cmd);
}
static struct blinded_path *blinded_path_dup(const tal_t *ctx,
const struct blinded_path *old)
{
struct blinded_path *bp = tal(ctx, struct blinded_path);
*bp = *old;
bp->path = tal_arr(bp, struct blinded_path_hop *, tal_count(old->path));
for (size_t i = 0; i < tal_count(bp->path); i++) {
bp->path[i] = tal(bp->path, struct blinded_path_hop);
bp->path[i]->blinded_node_id = old->path[i]->blinded_node_id;
bp->path[i]->encrypted_recipient_data = tal_dup_talarr(bp->path[i], u8,
old->path[i]->encrypted_recipient_data);
}
return bp;
}
struct command_result *
send_onion_reply(struct command *cmd,
struct blinded_path *reply_path,
struct tlv_onionmsg_tlv *payload)
{
struct onion_reply *onion_reply;
onion_reply = tal(cmd, struct onion_reply);
onion_reply->reply_path = blinded_path_dup(onion_reply, reply_path);
onion_reply->payload = tal_steal(onion_reply, payload);
if (!gossmap_scidd_pubkey(get_gossmap(cmd->plugin),
&onion_reply->reply_path->first_node_id)) {
plugin_log(cmd->plugin, LOG_DBG,
"Cannot resolve initial reply scidd %s",
fmt_short_channel_id_dir(tmpctx,
&onion_reply->reply_path->first_node_id.scidd));
return command_hook_success(cmd);
}
return establish_onion_path(cmd, get_gossmap(cmd->plugin),
&id, &onion_reply->reply_path->first_node_id.pubkey,
disable_connect,
send_onion_reply_after_established,
send_onion_reply_not_established,
onion_reply);
}
static struct command_result *onion_message_recv(struct command *cmd,
const char *buf,
const jsmntok_t *params)
{
const jsmntok_t *om, *secrettok, *nodeidtok, *pubkeytok, *replytok, *invreqtok, *invtok;
struct blinded_path *reply_path = NULL;
struct secret *secret;
struct pubkey *blinded_node_id;
struct pubkey *path_pubkey;
om = json_get_member(buf, params, "onion_message");
secrettok = json_get_member(buf, om, "pathsecret");
if (secrettok) {
secret = tal(tmpctx, struct secret);
json_to_secret(buf, secrettok, secret);
} else
secret = NULL;
nodeidtok = json_get_member(buf, om, "blinded_node_id");
if (!nodeidtok) plugin_err(cmd->plugin, "Missing blinded node id");
blinded_node_id = tal(tmpctx, struct pubkey);
json_to_pubkey(buf, nodeidtok, blinded_node_id);
pubkeytok = json_get_member(buf, om, "path_pubkey");
if (!pubkeytok) plugin_err(cmd->plugin, "Missing path pubkey");
path_pubkey = tal(tmpctx, struct pubkey);
json_to_pubkey(buf, pubkeytok, path_pubkey);
/* Might be reply for fetchinvoice (which always has a secret,
* so we can tell it's a response). */
if (secret) {
struct command_result *res;
res = handle_invoice_onion_message(cmd, buf, om, secret);
if (res)
return res;
}
replytok = json_get_member(buf, om, "reply_blindedpath");
if (replytok) {
reply_path = json_to_blinded_path(cmd, buf, replytok);
if (!reply_path)
plugin_err(cmd->plugin, "Invalid reply path %.*s?",
json_tok_full_len(replytok),
json_tok_full(buf, replytok));
}
invreqtok = json_get_member(buf, om, "invoice_request");
if (invreqtok) {
const u8 *invreqbin = json_tok_bin_from_hex(tmpctx, buf, invreqtok);
return handle_invoice_request(cmd,
invreqbin,
reply_path, secret, blinded_node_id, path_pubkey);
}
invtok = json_get_member(buf, om, "invoice");
if (invtok) {
const u8 *invbin = json_tok_bin_from_hex(tmpctx, buf, invtok);
if (invbin)
return handle_invoice(cmd, invbin, reply_path, secret);
}
return command_hook_success(cmd);
}
struct find_best_peer_data {
struct command_result *(*cb)(struct command *,
const struct chaninfo *,
void *);
u64 needed_features;
void *arg;
};
static struct command_result *listincoming_done(struct command *cmd,
const char *method,
const char *buf,
const jsmntok_t *result,
struct find_best_peer_data *data)
{
const jsmntok_t *arr, *t;
size_t i;
struct chaninfo *best = NULL;
arr = json_get_member(buf, result, "incoming");
json_for_each_arr(i, t, arr) {
struct chaninfo ci;
const jsmntok_t *pftok;
u8 *features;
u64 feature_bits;
const char *err;
struct amount_msat feebase;
bool enabled;
err = json_scan(tmpctx, buf, t,
"{id:%,"
"incoming_capacity_msat:%,"
"htlc_min_msat:%,"
"htlc_max_msat:%,"
"fee_base_msat:%,"
"fee_proportional_millionths:%,"
"cltv_expiry_delta:%,"
"enabled:%}",
JSON_SCAN(json_to_pubkey, &ci.id),
JSON_SCAN(json_to_msat, &ci.capacity),
JSON_SCAN(json_to_msat, &ci.htlc_min),
JSON_SCAN(json_to_msat, &ci.htlc_max),
JSON_SCAN(json_to_msat, &feebase),
JSON_SCAN(json_to_u32, &ci.feeppm),
JSON_SCAN(json_to_u32, &ci.cltv),
JSON_SCAN(json_to_bool, &enabled));
if (err) {
plugin_log(cmd->plugin, LOG_BROKEN,
"Could not parse listincoming: %s",
err);
continue;
}
ci.feebase = feebase.millisatoshis; /* Raw: feebase */
/* Don't pick a peer which is disconnected */
if (!enabled)
continue;
/* Not presented if there's no channel_announcement for peer:
* we could use listpeers, but if it's private we probably
* don't want to blinded route through it! */
pftok = json_get_member(buf, t, "peer_features");
if (!pftok)
continue;
features = json_tok_bin_from_hex(tmpctx, buf, pftok);
/* It must have all the features we need */
feature_bits = data->needed_features;
while (feature_bits) {
int feature = bitops_ls64(feature_bits);
if (!feature_offered(features, feature))
goto next;
feature_bits &= ~(1ULL << feature);
}
if (!best || amount_msat_greater(ci.capacity, best->capacity))
best = tal_dup(tmpctx, struct chaninfo, &ci);
next:;
}
/* Free data if they don't */
tal_steal(tmpctx, data);
return data->cb(cmd, best, data->arg);
}
struct command_result *find_best_peer_(struct command *cmd,
u64 needed_features,
struct command_result *(*cb)(struct command *,
const struct chaninfo *,
void *),
void *arg)
{
struct out_req *req;
struct find_best_peer_data *data = tal(cmd, struct find_best_peer_data);
data->cb = cb;
data->arg = arg;
data->needed_features = needed_features;
req = jsonrpc_request_start(cmd, "listincoming",
listincoming_done, forward_error, data);
return send_outreq(req);
}
static const struct plugin_hook hooks[] = {
{
"onion_message_recv",
onion_message_recv
},
{
"onion_message_recv_secret",
onion_message_recv
},
{
"invoice_payment",
invoice_payment,
},
};
static struct command_result *block_added_notify(struct command *cmd,
const char *buf,
const jsmntok_t *params)
{
const char *err = json_scan(cmd, buf, params, "{block_added:{height:%}}",
JSON_SCAN(json_to_u32, &blockheight));
if (err)
plugin_err(cmd->plugin, "Failed to parse block_added (%.*s): %s",
json_tok_full_len(params),
json_tok_full(buf, params),
err);
return notification_handled(cmd);
}
static const struct plugin_notification notifications[] = {
{
"block_added",
block_added_notify,
},
};
struct decodable {
const char *type;
struct bolt11 *b11;
struct tlv_offer *offer;
struct tlv_invoice *invoice;
struct tlv_invoice_request *invreq;
struct rune *rune;
u8 *emergency_recover;
};
static u8 *encrypted_decode(const tal_t *ctx, const char *str, char **fail) {
if (strlen(str) < 8) {
*fail = tal_fmt(ctx, "invalid payload");
return NULL;
}
size_t hrp_maxlen = strlen(str) - 6;
char *hrp = tal_arr(ctx, char, hrp_maxlen);
size_t data_maxlen = strlen(str) - 8;
u5 *data = tal_arr(ctx, u5, data_maxlen);
size_t datalen = 0;
if (bech32_decode(hrp, data, &datalen, str, (size_t)-1)
== BECH32_ENCODING_NONE) {
*fail = tal_fmt(ctx, "invalid bech32 encoding");
goto fail;
}
if (!streq(hrp, "clnemerg")) {
*fail = tal_fmt(ctx, "hrp should be `clnemerg`");
goto fail;
}
u8 *data8bit = tal_arr(data, u8, 0);
bech32_pull_bits(&data8bit, data, datalen*5);
return data8bit;
fail:
tal_free(data);
return NULL;
}
static struct command_result *param_decodable(struct command *cmd,
const char *name,
const char *buffer,
const jsmntok_t *token,
struct decodable *decodable)
{
char *likely_fail = NULL, *fail;
jsmntok_t tok;
/* BOLT #11:
*
* If a URI scheme is desired, the current recommendation is to either
* use 'lightning:' as a prefix before the BOLT-11 encoding
*/
tok = *token;
if (json_tok_startswith(buffer, &tok, "lightning:")
|| json_tok_startswith(buffer, &tok, "LIGHTNING:"))
tok.start += strlen("lightning:");
decodable->offer = offer_decode(cmd, buffer + tok.start,
tok.end - tok.start,
plugin_feature_set(cmd->plugin), NULL,
json_tok_startswith(buffer, &tok, "lno1")
? &likely_fail : &fail);
if (decodable->offer) {
decodable->type = "bolt12 offer";
return NULL;
}
decodable->invoice = invoice_decode(cmd, buffer + tok.start,
tok.end - tok.start,
plugin_feature_set(cmd->plugin),
NULL,
json_tok_startswith(buffer, &tok,
"lni1")
? &likely_fail : &fail);
if (decodable->invoice) {
decodable->type = "bolt12 invoice";
return NULL;
}
decodable->invreq = invrequest_decode(cmd, buffer + tok.start,
tok.end - tok.start,
plugin_feature_set(cmd->plugin),
NULL,
json_tok_startswith(buffer, &tok,
"lnr1")
? &likely_fail : &fail);
if (decodable->invreq) {
decodable->type = "bolt12 invoice_request";
return NULL;
}
decodable->emergency_recover = encrypted_decode(cmd, tal_strndup(tmpctx, buffer + tok.start,
tok.end - tok.start),
json_tok_startswith(buffer, &tok,
"clnemerg1")
? &likely_fail : &fail);
if (decodable->emergency_recover) {
decodable->type = "emergency recover";
return NULL;
}
/* If no other was likely, bolt11 decoder gives us failure string. */
decodable->b11 = bolt11_decode(cmd,
tal_strndup(tmpctx, buffer + tok.start,
tok.end - tok.start),
plugin_feature_set(cmd->plugin),
NULL, NULL,
likely_fail ? &fail : &likely_fail);
if (decodable->b11) {
decodable->type = "bolt11 invoice";
return NULL;
}
decodable->rune = rune_from_base64n(decodable, buffer + tok.start,
tok.end - tok.start);
if (decodable->rune) {
decodable->type = "rune";
return NULL;
}
/* Return failure message from most likely parsing candidate */
return command_fail_badparam(cmd, name, buffer, &tok, likely_fail);
}
static void json_add_chains(struct json_stream *js,
const char *fieldname,
const struct bitcoin_blkid *chains)
{
json_array_start(js, fieldname);
for (size_t i = 0; i < tal_count(chains); i++)
json_add_sha256(js, NULL, &chains[i].shad.sha);
json_array_end(js);
}
static void json_add_onionmsg_path(struct json_stream *js,
const char *fieldname,
const struct blinded_path_hop *hop)
{
json_object_start(js, fieldname);
json_add_pubkey(js, "blinded_node_id", &hop->blinded_node_id);
json_add_hex_talarr(js, "encrypted_recipient_data", hop->encrypted_recipient_data);
json_object_end(js);
}
/* Returns true if valid */
static bool json_add_blinded_paths(struct command *cmd,
struct json_stream *js,
const char *fieldname,
struct blinded_path **paths,
struct blinded_payinfo **blindedpay)
{
json_array_start(js, fieldname);
for (size_t i = 0; i < tal_count(paths); i++) {
json_object_start(js, NULL);
if (paths[i]->first_node_id.is_pubkey) {
json_add_pubkey(js, "first_node_id",
&paths[i]->first_node_id.pubkey);
} else {
json_add_short_channel_id(js, "first_scid",
paths[i]->first_node_id.scidd.scid);
json_add_u32(js, "first_scid_dir",
paths[i]->first_node_id.scidd.dir);
}
json_add_pubkey(js, "first_path_key", &paths[i]->first_path_key);
/* Don't crash if we're short a payinfo! */
if (i < tal_count(blindedpay)) {
json_object_start(js, "payinfo");
json_add_amount_msat(js, "fee_base_msat",
amount_msat(blindedpay[i]->fee_base_msat));
json_add_u32(js, "fee_proportional_millionths",
blindedpay[i]->fee_proportional_millionths);
json_add_u32(js, "cltv_expiry_delta",
blindedpay[i]->cltv_expiry_delta);
json_add_hex_talarr(js, "features", blindedpay[i]->features);
json_object_end(js);
}
json_array_start(js, "path");
for (size_t j = 0; j < tal_count(paths[i]->path); j++) {
json_add_onionmsg_path(js, NULL, paths[i]->path[j]);
}
json_array_end(js);
json_object_end(js);
}
json_array_end(js);
/* BOLT #12:
* - MUST reject the invoice if `invoice_blindedpay` does not contain
* exactly one `blinded_payinfo` per `invoice_paths`.`blinded_path`.
*/
if (blindedpay && tal_count(blindedpay) != tal_count(paths)) {
json_add_str_fmt(js, "warning_invalid_invoice_blindedpay",
"invoice has %zu blinded_payinfo but %zu paths",
tal_count(blindedpay), tal_count(paths));
return false;
}
/* BOLT #12:
* - if `num_hops` is 0 in any `blinded_path` in `offer_paths`:
* - MUST NOT respond to the offer.
*/
for (size_t i = 0; i < tal_count(paths); i++) {
if (tal_count(paths[i]->path) == 0) {
json_add_str_fmt(js, "warning_empty_blinded_path",
"blinded path %zu has 0 hops",
i);
return false;
}
}
return true;
}
static const char *recurrence_time_unit_name(u8 time_unit)
{
/* BOLT-recurrence #12:
* `time_unit` defining 0 (seconds), 1 (days), 2 (months).
*/
switch (time_unit) {
case 0:
return "seconds";
case 1:
return "days";
case 2:
return "months";
}
return NULL;
}
static bool json_add_utf8(struct json_stream *js,
const char *fieldname,
const char *utf8str)
{
if (utf8_check(utf8str, tal_bytelen(utf8str))) {
json_add_stringn(js, fieldname, utf8str, tal_bytelen(utf8str));
return true;
}
json_add_string(js, tal_fmt(tmpctx, "warning_invalid_%s", fieldname),
"invalid UTF8");
return false;
}
static void json_add_recurrence(struct json_stream *js,
const char *fieldname,
const struct recurrence *offer_recurrence,
const struct recurrence_paywindow *offer_recurrence_paywindow,
const u32 *offer_recurrence_limit,
const struct recurrence_base *offer_recurrence_base)
{
const char *name;
json_object_start(js, fieldname);
json_add_num(js, "time_unit", offer_recurrence->time_unit);
name = recurrence_time_unit_name(offer_recurrence->time_unit);
if (name)
json_add_string(js, "time_unit_name", name);
json_add_num(js, "period", offer_recurrence->period);
if (offer_recurrence_base) {
json_add_u64(js, "basetime",
offer_recurrence_base->basetime);
if (offer_recurrence_base->proportional_amount)
json_add_bool(js, "proportional_amount", true);
}
if (offer_recurrence_limit)
json_add_u32(js, "limit", *offer_recurrence_limit);
if (offer_recurrence_paywindow) {
json_object_start(js, "paywindow");
json_add_u32(js, "seconds_before",
offer_recurrence_paywindow->seconds_before);
json_add_u32(js, "seconds_after",
offer_recurrence_paywindow->seconds_after);
json_object_end(js);
}
json_object_end(js);
}
static bool json_add_offer_fields(struct command *cmd,
struct json_stream *js,
const struct bitcoin_blkid *offer_chains,
const u8 *offer_metadata,
const char *offer_currency,
const u64 *offer_amount,
const char *offer_description,
const u8 *offer_features,
const u64 *offer_absolute_expiry,
struct blinded_path **offer_paths,
const char *offer_issuer,
const u64 *offer_quantity_max,
const struct pubkey *offer_issuer_id,
const struct recurrence *offer_recurrence_compulsory,
const struct recurrence *offer_recurrence_optional,
const struct recurrence_paywindow *offer_recurrence_paywindow,
const u32 *offer_recurrence_limit,
const struct recurrence_base *offer_recurrence_base)
{
bool valid = true;
if (offer_chains)
json_add_chains(js, "offer_chains", offer_chains);
if (offer_metadata)
json_add_hex_talarr(js, "offer_metadata", offer_metadata);
if (offer_currency) {
const struct iso4217_name_and_divisor *iso4217;
valid &= json_add_utf8(js, "offer_currency", offer_currency);
if (offer_amount)
json_add_u64(js, "offer_amount", *offer_amount);
iso4217 = find_iso4217(offer_currency,
tal_bytelen(offer_currency));
if (iso4217)
json_add_num(js, "currency_minor_unit", iso4217->minor_unit);
else
json_add_string(js, "warning_unknown_offer_currency",
"unknown currency code");
} else if (offer_amount)
json_add_amount_msat(js, "offer_amount_msat",
amount_msat(*offer_amount));
/* BOLT #12:
*
* - if `offer_amount` is set and `offer_description` is not set:
* - MUST NOT respond to the offer.
*/
if (offer_description)
valid &= json_add_utf8(js, "offer_description",
offer_description);
else if (!offer_description && offer_amount) {
json_add_string(js, "warning_missing_offer_description",
"description is required if offer_amount is set (for the user to know what it was they paid for)");
valid = false;
}
if (offer_issuer)
valid &= json_add_utf8(js, "offer_issuer", offer_issuer);
if (offer_features)
json_add_hex_talarr(js, "offer_features", offer_features);
if (offer_absolute_expiry)
json_add_u64(js, "offer_absolute_expiry",
*offer_absolute_expiry);
if (offer_paths)
valid &= json_add_blinded_paths(cmd, js, "offer_paths",
offer_paths, NULL);
if (offer_quantity_max)
json_add_u64(js, "offer_quantity_max", *offer_quantity_max);
if (offer_recurrence_compulsory)
json_add_recurrence(js, "offer_recurrence_compulsory",
offer_recurrence_compulsory,
offer_recurrence_paywindow,
offer_recurrence_limit,
offer_recurrence_base);
if (offer_recurrence_optional)
json_add_recurrence(js, "offer_recurrence_optional",
offer_recurrence_optional,
offer_recurrence_paywindow,
offer_recurrence_limit,
offer_recurrence_base);
if (offer_issuer_id)
json_add_pubkey(js, "offer_issuer_id", offer_issuer_id);
return valid;
}
static void json_add_extra_fields(struct json_stream *js,
const char *fieldname,
const struct tlv_field *fields)
{
bool have_extra = false;
for (size_t i = 0; i < tal_count(fields); i++) {
if (fields[i].meta)
continue;
if (!have_extra) {
json_array_start(js, fieldname);
have_extra = true;
}
json_object_start(js, NULL);
json_add_u64(js, "type", fields[i].numtype);
json_add_u64(js, "length", fields[i].length);
json_add_hex(js, "value",
fields[i].value, fields[i].length);
json_object_end(js);
}
if (have_extra)
json_array_end(js);
}
static void json_add_offer(struct command *cmd, struct json_stream *js, const struct tlv_offer *offer)
{
struct sha256 offer_id;
bool valid = true;
offer_offer_id(offer, &offer_id);
json_add_sha256(js, "offer_id", &offer_id);
valid &= json_add_offer_fields(cmd, js,
offer->offer_chains,
offer->offer_metadata,
offer->offer_currency,
offer->offer_amount,
offer->offer_description,
offer->offer_features,
offer->offer_absolute_expiry,
offer->offer_paths,
offer->offer_issuer,
offer->offer_quantity_max,
offer->offer_issuer_id,
offer->offer_recurrence_compulsory,
offer->offer_recurrence_optional,
offer->offer_recurrence_paywindow,
offer->offer_recurrence_limit,
offer->offer_recurrence_base);
/* BOLT #12:
* - if neither `offer_issuer_id` nor `offer_paths` are set:
* - MUST NOT respond to the offer.
*/
if (!offer->offer_issuer_id && !offer->offer_paths) {
json_add_string(js, "warning_missing_offer_issuer_id",
"offers without an issuer_id or paths are invalid");
valid = false;
}
json_add_extra_fields(js, "unknown_offer_tlvs", offer->fields);
json_add_bool(js, "valid", valid);
}
static bool json_add_invreq_fields(struct command *cmd,
struct json_stream *js,
const u8 *invreq_metadata,
const struct bitcoin_blkid *invreq_chain,
const u64 *invreq_amount,
const u8 *invreq_features,
const u64 *invreq_quantity,
const struct pubkey *invreq_payer_id,
const utf8 *invreq_payer_note,
struct blinded_path **invreq_paths,
struct bip_353_name *bip353,
const u32 *invreq_recurrence_counter,
const u32 *invreq_recurrence_start)
{
bool valid = true;
/* BOLT #12:
* - MUST reject the invoice request if `invreq_payer_id` or `invreq_metadata` are not present.
*/
if (invreq_metadata)
json_add_hex_talarr(js, "invreq_metadata",
invreq_metadata);
else {
json_add_string(js, "warning_missing_invreq_metadata",
"invreq_metadata required");
valid = false;
}
/* This can be missing for an invoice though! */
if (invreq_payer_id)
json_add_pubkey(js, "invreq_payer_id", invreq_payer_id);
if (invreq_chain)
json_add_sha256(js, "invreq_chain", &invreq_chain->shad.sha);
if (invreq_amount)
json_add_amount_msat(js, "invreq_amount_msat",
amount_msat(*invreq_amount));
if (invreq_features)
json_add_hex_talarr(js, "invreq_features", invreq_features);
if (invreq_quantity)
json_add_u64(js, "invreq_quantity", *invreq_quantity);
if (invreq_payer_note)
valid &= json_add_utf8(js, "invreq_payer_note", invreq_payer_note);
if (invreq_paths)
valid &= json_add_blinded_paths(cmd, js, "invreq_paths",
invreq_paths, NULL);
if (bip353) {
json_object_start(js, "invreq_bip_353_name");
if (bip353->name) {
json_add_str_fmt(js, "name", "%.*s",
(int)tal_bytelen(bip353->name),
(char *)bip353->name);
}
if (bip353->domain) {
json_add_str_fmt(js, "domain", "%.*s",
(int)tal_bytelen(bip353->domain),
(char *)bip353->domain);
}
json_object_end(js);
if (!bolt12_bip353_valid_string(bip353->name,
tal_bytelen(bip353->name))) {
valid = false;
json_add_string(js, "warning_invreq_bip_353_name_name_invalid",
"bip353 name field contains invalid characters");
}
if (!bolt12_bip353_valid_string(bip353->domain,
tal_bytelen(bip353->domain))) {
valid = false;
json_add_string(js, "warning_invreq_bip_353_name_domain_invalid",
"bip353 domain field contains invalid characters");
}
}
if (invreq_recurrence_counter) {
json_add_u32(js, "invreq_recurrence_counter",
*invreq_recurrence_counter);
if (invreq_recurrence_start)
json_add_u32(js, "invreq_recurrence_start",
*invreq_recurrence_start);
}
return valid;
}
/* Returns true if valid */
static bool json_add_fallback_address(struct json_stream *js,
const struct chainparams *chain,
u8 version, const u8 *address)
{
char out[73 + strlen(chain->onchain_hrp)];
/* Does extra checks, in particular checks v0 sizes */
if (segwit_addr_encode(out, chain->onchain_hrp, version,
address, tal_bytelen(address))) {
json_add_string(js, "address", out);
return true;
}
json_add_string(js,
"warning_invalid_invoice_fallbacks_address",
"invalid fallback address for this version");
return false;
}
/* Returns true if valid */