diff --git a/common/bolt12.c b/common/bolt12.c index 30629c90d..07ef925db 100644 --- a/common/bolt12.c +++ b/common/bolt12.c @@ -493,6 +493,36 @@ void invoice_offer_id(const struct tlv_invoice *invoice, struct sha256 *id) calc_offer(wire, id); } +static void calc_invreq(const u8 *tlvstream, struct sha256 *id) +{ + size_t start, len; + + /* BOLT-offers #12: + * - if the invoice is a response to an `invoice_request`: + * - MUST reject the invoice if all fields less than type 160 + * do not exactly match the `invoice_request`. + */ + len = tlv_span(tlvstream, 0, 159, &start); + sha256(id, tlvstream + start, len); +} + +void invreq_invreq_id(const struct tlv_invoice_request *invreq, struct sha256 *id) +{ + u8 *wire = tal_arr(tmpctx, u8, 0); + + towire_tlv_invoice_request(&wire, invreq); + calc_invreq(wire, id); +} + +void invoice_invreq_id(const struct tlv_invoice *invoice, struct sha256 *id) +{ + u8 *wire = tal_arr(tmpctx, u8, 0); + + towire_tlv_invoice(&wire, invoice); + calc_invreq(wire, id); +} + + /* BOLT-offers #12: * ## Requirements for Invoice Requests * diff --git a/common/bolt12.h b/common/bolt12.h index cce31a1b6..879a09279 100644 --- a/common/bolt12.h +++ b/common/bolt12.h @@ -143,6 +143,11 @@ 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); +/* Get invreq_id: this is used to match incoming invoices to invoice_requests + * we publish. */ +void invreq_invreq_id(const struct tlv_invoice_request *invreq, struct sha256 *id); +void invoice_invreq_id(const struct tlv_invoice *invoice, struct sha256 *id); + /** * Prepare a new invoice_request based on an offer. */