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 <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell 2022-11-09 13:02:00 +10:30 committed by Christian Decker
parent aa73878831
commit 6e755d6fe8
2 changed files with 54 additions and 0 deletions

View File

@ -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);
}

View File

@ -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 */