From 6e755d6fe860277233312f44577006be3e5b5142 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Wed, 9 Nov 2022 13:02:00 +1030 Subject: [PATCH] common/bolt12: code to initialize invreqs from offers, invs from invreqs. This is an important part of the coming spec: we mirror all fields, known and unknown. Signed-off-by: Rusty Russell --- common/bolt12.c | 42 ++++++++++++++++++++++++++++++++++++++++++ common/bolt12.h | 12 ++++++++++++ 2 files changed, 54 insertions(+) diff --git a/common/bolt12.c b/common/bolt12.c index be1eecd4d..a43fcee83 100644 --- a/common/bolt12.c +++ b/common/bolt12.c @@ -501,3 +501,45 @@ void invoice_offer_id(const struct tlv_invoice *invoice, struct sha256 *id) towire_tlv_invoice(&wire, invoice); calc_offer(wire, id); } + +/* BOLT-offers #12: + * ## Requirements for Invoice Requests + * + * The writer: + * - if it is responding to an offer: + * - MUST copy all fields from the offer (including unknown fields). + */ +struct tlv_invoice_request *invoice_request_for_offer(const tal_t *ctx, + const struct tlv_offer *offer) +{ + const u8 *cursor; + size_t max; + u8 *wire = tal_arr(tmpctx, u8, 0); + towire_tlv_offer(&wire, offer); + + cursor = wire; + max = tal_bytelen(wire); + return fromwire_tlv_invoice_request(ctx, &cursor, &max); +} + +/** + * Prepare a new invoice based on an invoice_request. + */ +struct tlv_invoice *invoice_for_invreq(const tal_t *ctx, + const struct tlv_invoice_request *invreq) +{ + const u8 *cursor; + size_t start, len; + u8 *wire = tal_arr(tmpctx, u8, 0); + towire_tlv_invoice_request(&wire, invreq); + + /* BOLT-offers #12: + * A writer of an invoice: + * - MUST copy all non-signature fields from the invreq (including + * unknown fields). + */ + len = tlv_span(wire, 0, 159, &start); + cursor = wire + start; + return fromwire_tlv_invoice(ctx, &cursor, &len); +} + diff --git a/common/bolt12.h b/common/bolt12.h index a337a85e0..6a732644b 100644 --- a/common/bolt12.h +++ b/common/bolt12.h @@ -137,4 +137,16 @@ void offer_offer_id(const struct tlv_offer *offer, struct sha256 *id); void invreq_offer_id(const struct tlv_invoice_request *invreq, struct sha256 *id); void invoice_offer_id(const struct tlv_invoice *invoice, struct sha256 *id); +/** + * Prepare a new invoice_request based on an offer. + */ +struct tlv_invoice_request *invoice_request_for_offer(const tal_t *ctx, + const struct tlv_offer *offer); + +/** + * Prepare a new invoice based on an invoice_request. + */ +struct tlv_invoice *invoice_for_invreq(const tal_t *ctx, + const struct tlv_invoice_request *invreq); + #endif /* LIGHTNING_COMMON_BOLT12_H */