MCPcopy Create free account
hub / github.com/ElementsProject/lightning / param_bolt12_invstring

Function param_bolt12_invstring

plugins/offers_proof.c:412–469  ·  view source on GitHub ↗

param_ callback for `invstring`: canonicalizes and decodes offer/BIP353. */

Source from the content-addressed store, hash-verified

410
411/* param_ callback for `invstring`: canonicalizes and decodes offer/BIP353. */
412static struct command_result *param_bolt12_invstring(struct command *cmd,
413 const char *name,
414 const char *buf,
415 const jsmntok_t *tok,
416 struct bolt12_invinfo **invinfo)
417{
418 const char *invstring = to_canonical_invstr(cmd, json_strdup(tmpctx, buf, tok));
419 const char *fail;
420
421 *invinfo = tal(cmd, struct bolt12_invinfo);
422 (*invinfo)->invstring = invstring;
423
424 if (strstarts(invstring, "lni1")) {
425 (*invinfo)->type = INVTYPE_INVOICE;
426 } else if (strstarts(invstring, "lno1")) {
427 struct tlv_offer *offer;
428 offer = offer_decode(tmpctx, invstring, strlen(invstring),
429 NULL, chainparams, &fail);
430 if (!offer)
431 return command_fail_badparam(cmd, name, buf, tok,
432 tal_fmt(tmpctx, "Invalid offer: %s", fail));
433 offer_offer_id(offer, &(*invinfo)->offer_id);
434 (*invinfo)->type = INVTYPE_OFFER;
435 } else if (strchr(invstring, '@')) {
436 /* BOLT #12:
437 * - if it received the offer from which it constructed this
438 * `invoice_request` using BIP 353 resolution:
439 * - MUST include `invreq_bip_353_name` with,
440 * - `name` set to the post-₿, pre-@ part of the BIP 353 HRN,
441 * - `domain` set to the post-@ part of the BIP 353 HRN.
442 */
443 char *str = json_strdup(tmpctx, buf, tok);
444 char *at;
445
446 if (!utf8_check(str, strlen(str)))
447 return command_fail_badparam(cmd, name, buf, tok,
448 "Invalid UTF-8");
449 /* Strip ₿ if present (0xE2 0x82 0xBF) */
450 if (strstarts(str, "₿"))
451 str += strlen("₿");
452 at = strchr(str, '@');
453 if (!at)
454 return command_fail_badparam(cmd, name, buf, tok,
455 "Missing @ in BIP353 address");
456 (*invinfo)->bip353.name
457 = tal_dup_arr(*invinfo, u8, (const u8 *)str,
458 at - str, 0);
459 (*invinfo)->bip353.domain
460 = tal_dup_arr(*invinfo, u8, (const u8 *)(at + 1),
461 strlen(at + 1), 0);
462 (*invinfo)->type = INVTYPE_BIP353;
463 } else {
464 return command_fail_badparam(cmd, name, buf, tok,
465 "Expected bolt12 invoice (lni1...), "
466 "offer (lno1...), or user@domain");
467 }
468 return NULL;
469}

Callers

nothing calls this directly

Calls 6

offer_decodeFunction · 0.85
offer_offer_idFunction · 0.85
to_canonical_invstrFunction · 0.50
json_strdupFunction · 0.50
command_fail_badparamFunction · 0.50
utf8_checkFunction · 0.50

Tested by

no test coverage detected