Skip to content

Commit 551ecdf

Browse files
committed
plugins/fetchinvoice: extract recurrence invreq_metadata routine.
We're going to want this for cancelrecurringinvoice. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
1 parent b30e063 commit 551ecdf

File tree

1 file changed

+29
-24
lines changed

1 file changed

+29
-24
lines changed

plugins/fetchinvoice.c

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -818,6 +818,34 @@ static bool payer_key(const u8 *public_tweak, size_t public_tweak_len,
818818
tweakhash.u.u8) == 1;
819819
}
820820

821+
/* BOLT #12:
822+
* - MUST set `invreq_metadata` to an unpredictable series of bytes.
823+
*/
824+
/* Derive metadata (and thus temp key) from offer data and label
825+
* as payer_id must be same for all recurring payments. */
826+
static u8 *recurrence_invreq_metadata(const tal_t *ctx,
827+
const struct tlv_invoice_request *invreq,
828+
const char *rec_label)
829+
{
830+
struct sha256 offer_id, tweak;
831+
u8 *tweak_input;
832+
833+
/* Use "offer_id || label" as tweak input */
834+
invreq_offer_id(invreq, &offer_id);
835+
tweak_input = tal_arr(tmpctx, u8,
836+
sizeof(offer_id) + strlen(rec_label));
837+
memcpy(tweak_input, &offer_id, sizeof(offer_id));
838+
memcpy(tweak_input + sizeof(offer_id),
839+
rec_label,
840+
strlen(rec_label));
841+
842+
bolt12_alias_tweak(&nodealias_base,
843+
tweak_input,
844+
tal_bytelen(tweak_input),
845+
&tweak);
846+
return (u8 *)tal_dup(invreq, struct sha256, &tweak);
847+
}
848+
821849
/* Fetches an invoice for this offer, and makes sure it corresponds. */
822850
struct command_result *json_fetchinvoice(struct command *cmd,
823851
const char *buffer,
@@ -937,9 +965,6 @@ struct command_result *json_fetchinvoice(struct command *cmd,
937965
* - if `offer_recurrence_optional` or `offer_recurrence_compulsory` are present:
938966
*/
939967
if (invreq_recurrence(invreq)) {
940-
struct sha256 offer_id, tweak;
941-
u8 *tweak_input;
942-
943968
/* BOLT-recurrence #12:
944969
* - for the initial request:
945970
*...
@@ -981,28 +1006,8 @@ struct command_result *json_fetchinvoice(struct command *cmd,
9811006
return command_fail(cmd, JSONRPC2_INVALID_PARAMS,
9821007
"needs recurrence_label");
9831008

984-
/* BOLT #12:
985-
* - MUST set `invreq_metadata` to an unpredictable series of
986-
* bytes.
987-
*/
988-
/* Derive metadata (and thus temp key) from offer data and label
989-
* as payer_id must be same for all recurring payments. */
990-
991-
/* Use "offer_id || label" as tweak input */
992-
invreq_offer_id(invreq, &offer_id);
993-
tweak_input = tal_arr(tmpctx, u8,
994-
sizeof(offer_id) + strlen(rec_label));
995-
memcpy(tweak_input, &offer_id, sizeof(offer_id));
996-
memcpy(tweak_input + sizeof(offer_id),
997-
rec_label,
998-
strlen(rec_label));
999-
1000-
bolt12_alias_tweak(&nodealias_base,
1001-
tweak_input,
1002-
tal_bytelen(tweak_input),
1003-
&tweak);
10041009
invreq->invreq_metadata
1005-
= (u8 *)tal_dup(invreq, struct sha256, &tweak);
1010+
= recurrence_invreq_metadata(invreq, invreq, rec_label);
10061011
} else {
10071012
/* BOLT-recurrence #12:
10081013
* - otherwise:

0 commit comments

Comments
 (0)