Skip to content

Commit cd9431f

Browse files
committed
Invoice request message interface and data format
Define an interface for BOLT 12 `invoice_request` messages. The underlying format consists of the original bytes and the parsed contents. The bytes are later needed when constructing an `invoice` message. This is because it must mirror all the `offer` and `invoice_request` TLV records, including unknown ones, which aren't represented in the contents. The contents will be used in `invoice` messages to avoid duplication. Some fields while required in a typical user-pays-merchant flow may not be necessary in the merchant-pays-user flow (e.g., refund, ATM).
1 parent c15b93d commit cd9431f

File tree

4 files changed

+132
-6
lines changed

4 files changed

+132
-6
lines changed
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
// This file is Copyright its original authors, visible in version control
2+
// history.
3+
//
4+
// This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
5+
// or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
7+
// You may not use this file except in accordance with one or both of these
8+
// licenses.
9+
10+
//! Data structures and encoding for `invoice_request` messages.
11+
12+
use bitcoin::blockdata::constants::ChainHash;
13+
use bitcoin::secp256k1::PublicKey;
14+
use bitcoin::secp256k1::schnorr::Signature;
15+
use crate::ln::features::OfferFeatures;
16+
use crate::offers::offer::OfferContents;
17+
use crate::offers::payer::PayerContents;
18+
19+
use crate::prelude::*;
20+
21+
/// An `InvoiceRequest` is a request for an `Invoice` formulated from an [`Offer`].
22+
///
23+
/// An offer may provided choices such as quantity, amount, chain, features, etc. An invoice request
24+
/// specifies these such that the recipient can send an invoice for payment.
25+
///
26+
/// [`Offer`]: crate::offers::offer::Offer
27+
#[derive(Clone, Debug)]
28+
pub struct InvoiceRequest {
29+
bytes: Vec<u8>,
30+
contents: InvoiceRequestContents,
31+
signature: Option<Signature>,
32+
}
33+
34+
/// The contents of an [`InvoiceRequest`], which may be shared with an `Invoice`.
35+
#[derive(Clone, Debug)]
36+
pub(crate) struct InvoiceRequestContents {
37+
payer: PayerContents,
38+
offer: OfferContents,
39+
chain: Option<ChainHash>,
40+
amount_msats: Option<u64>,
41+
features: Option<OfferFeatures>,
42+
quantity: Option<u64>,
43+
payer_id: PublicKey,
44+
payer_note: Option<String>,
45+
}
46+
47+
impl InvoiceRequest {
48+
/// An unpredictable series of bytes, typically containing information about the derivation of
49+
/// [`payer_id`].
50+
///
51+
/// [`payer_id`]: Self::payer_id
52+
pub fn metadata(&self) -> Option<&Vec<u8>> {
53+
self.contents.payer.0.as_ref()
54+
}
55+
56+
/// A chain from [`Offer::chains`] that the offer is valid for.
57+
///
58+
/// [`Offer::chains`]: crate::offers::offer::Offer::chains
59+
pub fn chain(&self) -> ChainHash {
60+
self.contents.chain.unwrap_or_else(|| self.contents.offer.implied_chain())
61+
}
62+
63+
/// The amount to pay in msats (i.e., the minimum lightning-payable unit for [`chain`]), which
64+
/// must be greater than or equal to [`Offer::amount`], converted if necessary.
65+
///
66+
/// [`chain`]: Self::chain
67+
/// [`Offer::amount`]: crate::offers::offer::Offer::amount
68+
pub fn amount_msats(&self) -> Option<u64> {
69+
self.contents.amount_msats
70+
}
71+
72+
/// Features for paying the invoice.
73+
pub fn features(&self) -> Option<&OfferFeatures> {
74+
self.contents.features.as_ref()
75+
}
76+
77+
/// The quantity of the offer's item conforming to [`Offer::supported_quantity`].
78+
///
79+
/// [`Offer::supported_quantity`]: crate::offers::offer::Offer::supported_quantity
80+
pub fn quantity(&self) -> Option<u64> {
81+
self.contents.quantity
82+
}
83+
84+
/// A transient pubkey used to sign the invoice request.
85+
pub fn payer_id(&self) -> PublicKey {
86+
self.contents.payer_id
87+
}
88+
89+
/// Payer provided note to include in the invoice.
90+
pub fn payer_note(&self) -> Option<&String> {
91+
self.contents.payer_note.as_ref()
92+
}
93+
94+
/// Signature of the invoice request using [`payer_id`].
95+
///
96+
/// [`payer_id`]: Self::payer_id
97+
pub fn signature(&self) -> Option<Signature> {
98+
self.signature
99+
}
100+
}

lightning/src/offers/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,7 @@
1212
//!
1313
//! Offers are a flexible protocol for Lightning payments.
1414
15+
pub mod invoice_request;
1516
pub mod offer;
1617
pub mod parse;
18+
mod payer;

lightning/src/offers/offer.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -223,14 +223,16 @@ impl OfferBuilder {
223223

224224
/// An `Offer` is a potentially long-lived proposal for payment of a good or service.
225225
///
226-
/// An offer is a precursor to an `InvoiceRequest`. A merchant publishes an offer from which a
226+
/// An offer is a precursor to an [`InvoiceRequest`]. A merchant publishes an offer from which a
227227
/// customer may request an `Invoice` for a specific quantity and using an amount sufficient to
228228
/// cover that quantity (i.e., at least `quantity * amount`). See [`Offer::amount`].
229229
///
230230
/// Offers may be denominated in currency other than bitcoin but are ultimately paid using the
231231
/// latter.
232232
///
233233
/// Through the use of [`BlindedPath`]s, offers provide recipient privacy.
234+
///
235+
/// [`InvoiceRequest`]: crate::offers::invoice_request::InvoiceRequest
234236
#[derive(Clone, Debug)]
235237
pub struct Offer {
236238
// The serialized offer. Needed when creating an `InvoiceRequest` if the offer contains unknown
@@ -239,7 +241,9 @@ pub struct Offer {
239241
contents: OfferContents,
240242
}
241243

242-
/// The contents of an [`Offer`], which may be shared with an `InvoiceRequest` or an `Invoice`.
244+
/// The contents of an [`Offer`], which may be shared with an [`InvoiceRequest`] or an `Invoice`.
245+
///
246+
/// [`InvoiceRequest`]: crate::offers::invoice_request::InvoiceRequest
243247
#[derive(Clone, Debug)]
244248
pub(crate) struct OfferContents {
245249
chains: Option<Vec<ChainHash>>,
@@ -262,10 +266,7 @@ impl Offer {
262266
/// Payments must be denominated in units of the minimal lightning-payable unit (e.g., msats)
263267
/// for the selected chain.
264268
pub fn chains(&self) -> Vec<ChainHash> {
265-
self.contents.chains
266-
.as_ref()
267-
.cloned()
268-
.unwrap_or_else(|| vec![self.contents.implied_chain()])
269+
self.contents.chains()
269270
}
270271

271272
// TODO: Link to corresponding method in `InvoiceRequest`.
@@ -345,6 +346,10 @@ impl AsRef<[u8]> for Offer {
345346
}
346347

347348
impl OfferContents {
349+
pub fn chains(&self) -> Vec<ChainHash> {
350+
self.chains.as_ref().cloned().unwrap_or_else(|| vec![self.implied_chain()])
351+
}
352+
348353
pub fn implied_chain(&self) -> ChainHash {
349354
ChainHash::using_genesis_block(Network::Bitcoin)
350355
}

lightning/src/offers/payer.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// This file is Copyright its original authors, visible in version control
2+
// history.
3+
//
4+
// This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
5+
// or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
7+
// You may not use this file except in accordance with one or both of these
8+
// licenses.
9+
10+
//! Data structures and encoding for `invoice_request_metadata` records.
11+
12+
use crate::prelude::*;
13+
14+
/// An unpredictable sequence of bytes typically containing information needed to derive
15+
/// [`InvoiceRequestContents::payer_id`].
16+
///
17+
/// [`InvoiceRequestContents::payer_id`]: invoice_request::InvoiceRequestContents::payer_id
18+
#[derive(Clone, Debug)]
19+
pub(crate) struct PayerContents(pub Option<Vec<u8>>);

0 commit comments

Comments
 (0)