Skip to content

Commit 4e7c2fc

Browse files
committed
plugins/fetchinvoice: extract bip353 parsing to param helper.
We're going to want this for cancelrecurringinvoice. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
1 parent 551ecdf commit 4e7c2fc

File tree

1 file changed

+38
-33
lines changed

1 file changed

+38
-33
lines changed

plugins/fetchinvoice.c

Lines changed: 38 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -846,6 +846,41 @@ static u8 *recurrence_invreq_metadata(const tal_t *ctx,
846846
return (u8 *)tal_dup(invreq, struct sha256, &tweak);
847847
}
848848

849+
static struct command_result *param_bip353(struct command *cmd, const char *name,
850+
const char *buffer, const jsmntok_t *tok,
851+
struct bip_353_name **bip353)
852+
{
853+
char *str, *at;
854+
855+
/* BOLT #12:
856+
* - if it received the offer from which it constructed this
857+
* `invoice_request` using BIP 353 resolution:
858+
* - MUST include `invreq_bip_353_name` with,
859+
* - `name` set to the post-₿, pre-@ part of the BIP 353 HRN,
860+
* - `domain` set to the post-@ part of the BIP 353 HRN.
861+
*/
862+
str = json_strdup(tmpctx, buffer, tok);
863+
if (!utf8_check(str, strlen(str)))
864+
return command_fail_badparam(cmd, name, buffer, tok, "invalid UTF-8");
865+
866+
at = strchr(str, '@');
867+
if (!at)
868+
return command_fail_badparam(cmd, name, buffer, tok, "missing @");
869+
870+
/* Strip ₿ if present (0xE2 0x82 0xBF) */
871+
if (strstarts(str, "₿"))
872+
str += strlen("₿");
873+
874+
*bip353 = tal(cmd, struct bip_353_name);
875+
/* Not nul-terminated! */
876+
(*bip353)->name
877+
= tal_dup_arr(*bip353, u8, (const u8 *)str, at - str, 0);
878+
(*bip353)->domain
879+
= tal_dup_arr(*bip353, u8, (const u8 *)at + 1, strlen(at + 1), 0);
880+
881+
return NULL;
882+
}
883+
849884
/* Fetches an invoice for this offer, and makes sure it corresponds. */
850885
struct command_result *json_fetchinvoice(struct command *cmd,
851886
const char *buffer,
@@ -857,7 +892,7 @@ struct command_result *json_fetchinvoice(struct command *cmd,
857892
struct out_req *req;
858893
struct tlv_invoice_request *invreq;
859894
struct sent *sent = tal(cmd, struct sent);
860-
const char *bip353;
895+
struct bip_353_name *bip353;
861896
u32 *timeout;
862897
u64 *quantity;
863898
u32 *recurrence_counter, *recurrence_start;
@@ -872,7 +907,7 @@ struct command_result *json_fetchinvoice(struct command *cmd,
872907
p_opt_def("timeout", param_number, &timeout, 60),
873908
p_opt("payer_note", param_string, &payer_note),
874909
p_opt("payer_metadata", param_bin_from_hex, &payer_metadata),
875-
p_opt("bip353", param_string, &bip353),
910+
p_opt("bip353", param_bip353, &bip353),
876911
p_opt("dev_path_use_scidd", param_dev_scidd, &sent->dev_path_use_scidd),
877912
p_opt("dev_reply_path", param_dev_reply_path, &sent->dev_reply_path),
878913
NULL))
@@ -911,6 +946,7 @@ struct command_result *json_fetchinvoice(struct command *cmd,
911946
invreq->invreq_recurrence_counter = tal_steal(invreq, recurrence_counter);
912947
invreq->invreq_recurrence_start = tal_steal(invreq, recurrence_start);
913948
invreq->invreq_quantity = tal_steal(invreq, quantity);
949+
invreq->invreq_bip_353_name = tal_steal(invreq, bip353);
914950

915951
/* BOLT-recurrence #12:
916952
* - if `offer_amount` is not present:
@@ -1039,37 +1075,6 @@ struct command_result *json_fetchinvoice(struct command *cmd,
10391075
}
10401076
}
10411077

1042-
/* BOLT #12:
1043-
* - if it received the offer from which it constructed this
1044-
* `invoice_request` using BIP 353 resolution:
1045-
* - MUST include `invreq_bip_353_name` with,
1046-
* - `name` set to the post-₿, pre-@ part of the BIP 353 HRN,
1047-
* - `domain` set to the post-@ part of the BIP 353 HRN.
1048-
*/
1049-
if (bip353) {
1050-
char *at;
1051-
if (!utf8_check(bip353, strlen(bip353)))
1052-
return command_fail(cmd, JSONRPC2_INVALID_PARAMS,
1053-
"invalid UTF-8 for bip353");
1054-
at = strchr(bip353, '@');
1055-
if (!at)
1056-
return command_fail(cmd, JSONRPC2_INVALID_PARAMS,
1057-
"missing @ for bip353");
1058-
1059-
/* Strip ₿ if present (0xE2 0x82 0xBF) */
1060-
if (strstarts(bip353, "₿"))
1061-
bip353 += strlen("₿");
1062-
invreq->invreq_bip_353_name
1063-
= tal(invreq, struct bip_353_name);
1064-
/* Not nul-terminated! */
1065-
invreq->invreq_bip_353_name->name
1066-
= tal_dup_arr(invreq->invreq_bip_353_name, u8,
1067-
(const u8 *)bip353, at - bip353, 0);
1068-
invreq->invreq_bip_353_name->domain
1069-
= tal_dup_arr(invreq->invreq_bip_353_name, u8,
1070-
(const u8 *)at + 1, strlen(at + 1), 0);
1071-
}
1072-
10731078
/* We derive transient payer_id from invreq_metadata */
10741079
invreq->invreq_payer_id = tal(invreq, struct pubkey);
10751080
if (!payer_key(invreq->invreq_metadata,

0 commit comments

Comments
 (0)