/// Sets the [`Offer::amount`] as an [`Amount::Bitcoin`].
///
/// Successive calls to this method will override the previous setting.
- pub fn amount_msats(mut self, amount_msats: u64) -> Self {
+ pub fn amount_msats(self, amount_msats: u64) -> Self {
self.amount(Amount::Bitcoin { amount_msats })
}
/// An `Offer` is a potentially long-lived proposal for payment of a good or service.
///
-/// An offer is a precursor to an `InvoiceRequest`. A merchant publishes an offer from which a
+/// An offer is a precursor to an [`InvoiceRequest`]. A merchant publishes an offer from which a
/// customer may request an `Invoice` for a specific quantity and using an amount sufficient to
/// cover that quantity (i.e., at least `quantity * amount`). See [`Offer::amount`].
///
/// latter.
///
/// Through the use of [`BlindedPath`]s, offers provide recipient privacy.
+///
+/// [`InvoiceRequest`]: crate::offers::invoice_request::InvoiceRequest
#[derive(Clone, Debug)]
pub struct Offer {
// The serialized offer. Needed when creating an `InvoiceRequest` if the offer contains unknown
contents: OfferContents,
}
-/// The contents of an [`Offer`], which may be shared with an `InvoiceRequest` or an `Invoice`.
+/// The contents of an [`Offer`], which may be shared with an [`InvoiceRequest`] or an `Invoice`.
+///
+/// [`InvoiceRequest`]: crate::offers::invoice_request::InvoiceRequest
#[derive(Clone, Debug)]
-pub(crate) struct OfferContents {
+pub(super) struct OfferContents {
chains: Option<Vec<ChainHash>>,
metadata: Option<Vec<u8>>,
amount: Option<Amount>,
/// Payments must be denominated in units of the minimal lightning-payable unit (e.g., msats)
/// for the selected chain.
pub fn chains(&self) -> Vec<ChainHash> {
- self.contents.chains
- .as_ref()
- .cloned()
- .unwrap_or_else(|| vec![self.contents.implied_chain()])
+ self.contents.chains()
+ }
+
+ /// Returns whether the given chain is supported by the offer.
+ pub fn supports_chain(&self, chain: ChainHash) -> bool {
+ self.contents.supports_chain(chain)
}
// TODO: Link to corresponding method in `InvoiceRequest`.
/// The minimum amount required for a successful payment of a single item.
pub fn amount(&self) -> Option<&Amount> {
- self.contents.amount.as_ref()
+ self.contents.amount()
}
/// A complete description of the purpose of the payment. Intended to be displayed to the user
self.contents.supported_quantity()
}
+ /// Returns whether the given quantity is valid for the offer.
+ pub fn is_valid_quantity(&self, quantity: u64) -> bool {
+ self.contents.is_valid_quantity(quantity)
+ }
+
+ /// Returns whether a quantity is expected in an [`InvoiceRequest`] for the offer.
+ ///
+ /// [`InvoiceRequest`]: crate::offers::invoice_request::InvoiceRequest
+ pub fn expects_quantity(&self) -> bool {
+ self.contents.expects_quantity()
+ }
+
/// The public key used by the recipient to sign invoices.
pub fn signing_pubkey(&self) -> PublicKey {
self.contents.signing_pubkey.unwrap()
}
impl OfferContents {
+ pub fn chains(&self) -> Vec<ChainHash> {
+ self.chains.as_ref().cloned().unwrap_or_else(|| vec![self.implied_chain()])
+ }
+
pub fn implied_chain(&self) -> ChainHash {
ChainHash::using_genesis_block(Network::Bitcoin)
}
+ pub fn supports_chain(&self, chain: ChainHash) -> bool {
+ self.chains().contains(&chain)
+ }
+
+ pub fn amount(&self) -> Option<&Amount> {
+ self.amount.as_ref()
+ }
+
+ pub fn amount_msats(&self) -> u64 {
+ match self.amount() {
+ None => 0,
+ Some(&Amount::Bitcoin { amount_msats }) => amount_msats,
+ Some(&Amount::Currency { .. }) => unreachable!(),
+ }
+ }
+
+ pub fn expected_invoice_amount_msats(&self, quantity: u64) -> u64 {
+ self.amount_msats() * quantity
+ }
+
pub fn supported_quantity(&self) -> Quantity {
self.supported_quantity
}
+ pub fn is_valid_quantity(&self, quantity: u64) -> bool {
+ match self.supported_quantity {
+ Quantity::Bounded(n) => {
+ let n = n.get();
+ if n == 1 { false }
+ else { quantity > 0 && quantity <= n }
+ },
+ Quantity::Unbounded => quantity > 0,
+ }
+ }
+
+ pub fn expects_quantity(&self) -> bool {
+ match self.supported_quantity {
+ Quantity::Bounded(n) => n.get() != 1,
+ Quantity::Unbounded => true,
+ }
+ }
+
fn as_tlv_stream(&self) -> OfferTlvStreamRef {
let (currency, amount) = match &self.amount {
None => (None, None),
assert_eq!(offer.bytes, buffer.as_slice());
assert_eq!(offer.chains(), vec![ChainHash::using_genesis_block(Network::Bitcoin)]);
+ assert!(offer.supports_chain(ChainHash::using_genesis_block(Network::Bitcoin)));
assert_eq!(offer.metadata(), None);
assert_eq!(offer.amount(), None);
assert_eq!(offer.description(), PrintableString("foo"));
.chain(Network::Bitcoin)
.build()
.unwrap();
+ assert!(offer.supports_chain(mainnet));
assert_eq!(offer.chains(), vec![mainnet]);
assert_eq!(offer.as_tlv_stream().chains, None);
.chain(Network::Testnet)
.build()
.unwrap();
+ assert!(offer.supports_chain(testnet));
assert_eq!(offer.chains(), vec![testnet]);
assert_eq!(offer.as_tlv_stream().chains, Some(&vec![testnet]));
.chain(Network::Testnet)
.build()
.unwrap();
+ assert!(offer.supports_chain(testnet));
assert_eq!(offer.chains(), vec![testnet]);
assert_eq!(offer.as_tlv_stream().chains, Some(&vec![testnet]));
.chain(Network::Testnet)
.build()
.unwrap();
+ assert!(offer.supports_chain(mainnet));
+ assert!(offer.supports_chain(testnet));
assert_eq!(offer.chains(), vec![mainnet, testnet]);
assert_eq!(offer.as_tlv_stream().chains, Some(&vec![mainnet, testnet]));
}