|
| 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 | +} |
0 commit comments