forked from ElementsProject/lightning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoffers_offer.c
428 lines (371 loc) · 12.2 KB
/
offers_offer.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
#include <bitcoin/chainparams.h>
#include <ccan/array_size/array_size.h>
#include <common/bolt12.h>
#include <common/iso4217.h>
#include <common/json_stream.h>
#include <common/overflows.h>
#include <plugins/offers_offer.h>
#include <wire/onion_wire.h>
static bool msat_or_any(const char *buffer,
const jsmntok_t *tok,
struct tlv_offer *offer)
{
struct amount_msat msat;
if (json_tok_streq(buffer, tok, "any"))
return true;
if (!parse_amount_msat(&msat,
buffer + tok->start, tok->end - tok->start))
return false;
offer->amount = tal_dup(offer, u64,
&msat.millisatoshis); /* Raw: other currencies */
return true;
}
static struct command_result *param_msat_or_any(struct command *cmd,
const char *name,
const char *buffer,
const jsmntok_t *tok,
struct tlv_offer *offer)
{
if (msat_or_any(buffer, tok, offer))
return NULL;
return command_fail_badparam(cmd, name, buffer, tok,
"should be 'any' or msatoshis");
}
static struct command_result *param_amount(struct command *cmd,
const char *name,
const char *buffer,
const jsmntok_t *tok,
struct tlv_offer *offer)
{
const struct iso4217_name_and_divisor *isocode;
jsmntok_t number, whole, frac;
u64 cents;
if (msat_or_any(buffer, tok, offer))
return NULL;
offer->amount = tal(offer, u64);
/* BOLT-offers #12:
*
* - MUST specify `iso4217` as an ISO 4712 three-letter code.
* - MUST specify `amount` in the currency unit adjusted by the ISO 4712
* exponent (e.g. USD cents).
*/
if (tok->end - tok->start < ISO4217_NAMELEN)
return command_fail_badparam(cmd, name, buffer, tok,
"should be 'any', msatoshis or <amount>[.<amount>]<ISO-4712>");
isocode = find_iso4217(buffer + tok->end - ISO4217_NAMELEN, ISO4217_NAMELEN);
if (!isocode)
return command_fail(cmd, JSONRPC2_INVALID_PARAMS,
"Unknown currency suffix %.*s",
ISO4217_NAMELEN,
buffer + tok->end - ISO4217_NAMELEN);
offer->currency
= tal_dup_arr(offer, utf8, isocode->name, ISO4217_NAMELEN, 0);
number = *tok;
number.end -= ISO4217_NAMELEN;
if (!split_tok(buffer, &number, '.', &whole, &frac)) {
whole = number;
cents = 0;
} else {
if (frac.end - frac.start != isocode->minor_unit)
return command_fail(cmd, JSONRPC2_INVALID_PARAMS,
"Currency %s requires %u minor units",
isocode->name, isocode->minor_unit);
if (!json_to_u64(buffer, &frac, ¢s))
return command_fail_badparam(cmd, name, buffer,
&number,
"Bad minor units");
}
if (!json_to_u64(buffer, &whole, offer->amount))
return command_fail_badparam(cmd, name, buffer, tok,
"should be 'any', msatoshis or <ISO-4712><amount>[.<amount>]");
for (size_t i = 0; i < isocode->minor_unit; i++) {
if (mul_overflows_u64(*offer->amount, 10))
return command_fail_badparam(cmd, name, buffer,
&whole,
"excessively large value");
*offer->amount *= 10;
}
*offer->amount += cents;
return NULL;
}
/* BOLT 13:
* - MUST set `time_unit` to 0 (seconds), 1 (days), 2 (months), 3 (years).
*/
struct time_string {
const char *suffix;
u32 unit;
u64 mul;
};
static const struct time_string *json_to_time(const char *buffer,
const jsmntok_t *tok,
u32 *mul)
{
static const struct time_string suffixes[] = {
{ "second", 0, 1 },
{ "seconds", 0, 1 },
{ "minute", 0, 60 },
{ "minutes", 0, 60 },
{ "hour", 0, 60*60 },
{ "hours", 0, 60*60 },
{ "day", 1, 1 },
{ "days", 1, 1 },
{ "week", 1, 7 },
{ "weeks", 1, 7 },
{ "month", 2, 1 },
{ "months", 2, 1 },
{ "year", 3, 1 },
{ "years", 3, 1 },
};
for (size_t i = 0; i < ARRAY_SIZE(suffixes); i++) {
if (json_tok_endswith(buffer, tok, suffixes[i].suffix)) {
jsmntok_t t = *tok;
t.end -= strlen(suffixes[i].suffix);
if (!json_to_u32(buffer, &t, mul))
return NULL;
return suffixes + i;
}
}
return NULL;
}
static struct command_result *param_recurrence(struct command *cmd,
const char *name,
const char *buffer,
const jsmntok_t *tok,
struct tlv_offer_recurrence
**recurrence)
{
u32 mul;
const struct time_string *ts;
ts = json_to_time(buffer, tok, &mul);
if (!ts)
return command_fail_badparam(cmd, name, buffer, tok,
"not a valid time");
*recurrence = tal(cmd, struct tlv_offer_recurrence);
(*recurrence)->time_unit = ts->unit;
(*recurrence)->period = ts->mul * mul;
return NULL;
}
static struct command_result *param_recurrence_base(struct command *cmd,
const char *name,
const char *buffer,
const jsmntok_t *tok,
struct tlv_offer_recurrence_base **base)
{
/* Make copy so we can manipulate it */
jsmntok_t t = *tok;
*base = tal(cmd, struct tlv_offer_recurrence_base);
if (json_tok_startswith(buffer, &t, "@")) {
t.start++;
(*base)->start_any_period = false;
} else
(*base)->start_any_period = true;
if (!json_to_u64(buffer, &t, &(*base)->basetime))
return command_fail_badparam(cmd, name, buffer, tok,
"not a valid basetime or @basetime");
return NULL;
}
/* -time+time[%] */
static struct command_result *param_recurrence_paywindow(struct command *cmd,
const char *name,
const char *buffer,
const jsmntok_t *tok,
struct tlv_offer_recurrence_paywindow
**paywindow)
{
jsmntok_t t, before, after;
*paywindow = tal(cmd, struct tlv_offer_recurrence_paywindow);
t = *tok;
if (json_tok_endswith(buffer, &t, "%")) {
(*paywindow)->proportional_amount = true;
t.end--;
} else
(*paywindow)->proportional_amount = false;
if (!json_tok_startswith(buffer, &t, "-"))
return command_fail_badparam(cmd, name, buffer, tok,
"expected -time+time[%]");
t.start++;
if (!split_tok(buffer, &t, '+', &before, &after))
return command_fail_badparam(cmd, name, buffer, tok,
"expected -time+time[%]");
if (!json_to_u32(buffer, &before, &(*paywindow)->seconds_before))
return command_fail_badparam(cmd, name, buffer, &before,
"expected number of seconds");
if (!json_to_u32(buffer, &after, &(*paywindow)->seconds_after))
return command_fail_badparam(cmd, name, buffer, &after,
"expected number of seconds");
return NULL;
}
static struct command_result *param_invoice_payment_hash(struct command *cmd,
const char *name,
const char *buffer,
const jsmntok_t *tok,
struct sha256 **hash)
{
struct tlv_invoice *inv;
char *fail;
inv = invoice_decode(tmpctx, buffer + tok->start, tok->end - tok->start,
plugin_feature_set(cmd->plugin), chainparams,
&fail);
if (!inv)
return command_fail_badparam(cmd, name, buffer, tok,
tal_fmt(cmd,
"Unparsable invoice: %s",
fail));
if (!inv->payment_hash)
return command_fail_badparam(cmd, name, buffer, tok,
"invoice missing payment_hash");
*hash = tal_steal(cmd, inv->payment_hash);
return NULL;
}
struct offer_info {
const struct tlv_offer *offer;
const char *label;
bool *single_use;
};
static struct command_result *create_offer(struct command *cmd,
struct offer_info *offinfo)
{
struct out_req *req;
/* We simply pass this through. */
req = jsonrpc_request_start(cmd->plugin, cmd, "createoffer",
forward_result, forward_error,
offinfo);
json_add_string(req->js, "bolt12",
offer_encode(tmpctx, offinfo->offer));
if (offinfo->label)
json_add_string(req->js, "label", offinfo->label);
json_add_bool(req->js, "single_use", *offinfo->single_use);
return send_outreq(cmd->plugin, req);
}
static struct command_result *currency_done(struct command *cmd,
const char *buf,
const jsmntok_t *result,
struct offer_info *offinfo)
{
/* Fail in this case, by forwarding warnings. */
if (!json_get_member(buf, result, "msat"))
return forward_error(cmd, buf, result, offinfo);
return create_offer(cmd, offinfo);
}
struct command_result *json_offer(struct command *cmd,
const char *buffer,
const jsmntok_t *params)
{
const char *desc, *vendor;
struct tlv_offer *offer;
struct offer_info *offinfo = tal(cmd, struct offer_info);
offinfo->offer = offer = tlv_offer_new(offinfo);
if (!param(cmd, buffer, params,
p_req("amount", param_amount, offer),
p_req("description", param_escaped_string, &desc),
p_opt("vendor", param_escaped_string, &vendor),
p_opt("label", param_escaped_string, &offinfo->label),
p_opt("quantity_min", param_u64, &offer->quantity_min),
p_opt("quantity_max", param_u64, &offer->quantity_max),
p_opt("absolute_expiry", param_u64, &offer->absolute_expiry),
p_opt("recurrence", param_recurrence, &offer->recurrence),
p_opt("recurrence_base",
param_recurrence_base,
&offer->recurrence_base),
p_opt("recurrence_paywindow",
param_recurrence_paywindow,
&offer->recurrence_paywindow),
p_opt("recurrence_limit",
param_number,
&offer->recurrence_limit),
p_opt_def("single_use", param_bool,
&offinfo->single_use, false),
/* FIXME: hints support! */
NULL))
return command_param_failed();
/* 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.
*/
if (!streq(chainparams->network_name, "bitcoin")) {
offer->chains = tal_arr(offer, struct bitcoin_blkid, 1);
offer->chains[0] = chainparams->genesis_blockhash;
}
if (!offer->recurrence) {
if (offer->recurrence_limit)
return command_fail_badparam(cmd, "recurrence_limit",
buffer, params,
"needs recurrence");
if (offer->recurrence_base)
return command_fail_badparam(cmd, "recurrence_base",
buffer, params,
"needs recurrence");
if (offer->recurrence_paywindow)
return command_fail_badparam(cmd, "recurrence_paywindow",
buffer, params,
"needs recurrence");
}
offer->description = tal_dup_arr(offer, char, desc, strlen(desc), 0);
if (vendor) {
offer->vendor
= tal_dup_arr(offer, char, vendor, strlen(vendor), 0);
}
offer->node_id = tal_dup(offer, struct pubkey32, &id);
/* If they specify a different currency, warn if we can't
* convert it! */
if (offer->currency) {
struct out_req *req;
req = jsonrpc_request_start(cmd->plugin, cmd, "currencyconvert",
currency_done, forward_error,
offinfo);
json_add_u32(req->js, "amount", 1);
json_add_stringn(req->js, "currency",
(const char *)offer->currency,
tal_bytelen(offer->currency));
return send_outreq(cmd->plugin, req);
}
return create_offer(cmd, offinfo);
}
struct command_result *json_offerout(struct command *cmd,
const char *buffer,
const jsmntok_t *params)
{
const char *desc, *vendor, *label;
struct tlv_offer *offer;
struct out_req *req;
offer = tlv_offer_new(cmd);
if (!param(cmd, buffer, params,
p_req("amount", param_msat_or_any, offer),
p_req("description", param_escaped_string, &desc),
p_opt("vendor", param_escaped_string, &vendor),
p_opt("label", param_escaped_string, &label),
p_opt("absolute_expiry", param_u64, &offer->absolute_expiry),
p_opt("refund_for", param_invoice_payment_hash, &offer->refund_for),
/* FIXME: hints support! */
NULL))
return command_param_failed();
offer->send_invoice = tal(offer, struct tlv_offer_send_invoice);
/* 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.
*/
if (!streq(chainparams->network_name, "bitcoin")) {
offer->chains = tal_arr(offer, struct bitcoin_blkid, 1);
offer->chains[0] = chainparams->genesis_blockhash;
}
offer->description = tal_dup_arr(offer, char, desc, strlen(desc), 0);
if (vendor)
offer->vendor = tal_dup_arr(offer, char,
vendor, strlen(vendor), 0);
offer->node_id = tal_dup(offer, struct pubkey32, &id);
/* We simply pass this through. */
req = jsonrpc_request_start(cmd->plugin, cmd, "createoffer",
forward_result, forward_error,
offer);
json_add_string(req->js, "bolt12", offer_encode(tmpctx, offer));
if (label)
json_add_string(req->js, "label", label);
json_add_bool(req->js, "single_use", true);
return send_outreq(cmd->plugin, req);
}