]> git.bitcoin.ninja Git - rust-lightning/commitdiff
Macro-ize InvoiceRequest accessors for reuse
authorJeffrey Czyz <jkczyz@gmail.com>
Tue, 15 Aug 2023 13:24:40 +0000 (08:24 -0500)
committerJeffrey Czyz <jkczyz@gmail.com>
Tue, 22 Aug 2023 00:14:28 +0000 (19:14 -0500)
Various messages wrap InvoiceRequestContents, which shouldn't be exposed
as it is an implementation detail. Define a macro for InvoiceRequest
accessor methods so that these messages can also define them.

lightning/src/offers/invoice_request.rs
lightning/src/offers/offer.rs
lightning/src/offers/payer.rs

index c3b9f5bdd20386cb45cd26334f341919bf6806cf..e41a699f2e80787fa39a0181432eac8d7c4495f9 100644 (file)
@@ -441,48 +441,52 @@ pub(super) struct InvoiceRequestContentsWithoutPayerId {
        payer_note: Option<String>,
 }
 
-impl InvoiceRequest {
+macro_rules! invoice_request_accessors { ($self: ident, $contents: expr) => {
        /// An unpredictable series of bytes, typically containing information about the derivation of
        /// [`payer_id`].
        ///
        /// [`payer_id`]: Self::payer_id
-       pub fn metadata(&self) -> &[u8] {
-               self.contents.metadata()
+       pub fn payer_metadata(&$self) -> &[u8] {
+               $contents.metadata()
        }
 
        /// A chain from [`Offer::chains`] that the offer is valid for.
-       pub fn chain(&self) -> ChainHash {
-               self.contents.chain()
+       pub fn chain(&$self) -> ChainHash {
+               $contents.chain()
        }
 
        /// The amount to pay in msats (i.e., the minimum lightning-payable unit for [`chain`]), which
        /// must be greater than or equal to [`Offer::amount`], converted if necessary.
        ///
        /// [`chain`]: Self::chain
-       pub fn amount_msats(&self) -> Option<u64> {
-               self.contents.amount_msats()
+       pub fn amount_msats(&$self) -> Option<u64> {
+               $contents.amount_msats()
        }
 
        /// Features pertaining to requesting an invoice.
-       pub fn features(&self) -> &InvoiceRequestFeatures {
-               &self.contents.features()
+       pub fn invoice_request_features(&$self) -> &InvoiceRequestFeatures {
+               &$contents.features()
        }
 
        /// The quantity of the offer's item conforming to [`Offer::is_valid_quantity`].
-       pub fn quantity(&self) -> Option<u64> {
-               self.contents.quantity()
+       pub fn quantity(&$self) -> Option<u64> {
+               $contents.quantity()
        }
 
        /// A possibly transient pubkey used to sign the invoice request.
-       pub fn payer_id(&self) -> PublicKey {
-               self.contents.payer_id()
+       pub fn payer_id(&$self) -> PublicKey {
+               $contents.payer_id()
        }
 
        /// A payer-provided note which will be seen by the recipient and reflected back in the invoice
        /// response.
-       pub fn payer_note(&self) -> Option<PrintableString> {
-               self.contents.payer_note()
+       pub fn payer_note(&$self) -> Option<PrintableString> {
+               $contents.payer_note()
        }
+} }
+
+impl InvoiceRequest {
+       invoice_request_accessors!(self, self.contents);
 
        /// Signature of the invoice request using [`payer_id`].
        ///
@@ -534,7 +538,7 @@ impl InvoiceRequest {
                &self, payment_paths: Vec<(BlindedPayInfo, BlindedPath)>, payment_hash: PaymentHash,
                created_at: core::time::Duration
        ) -> Result<InvoiceBuilder<ExplicitSigningPubkey>, Bolt12SemanticError> {
-               if self.features().requires_unknown_bits() {
+               if self.invoice_request_features().requires_unknown_bits() {
                        return Err(Bolt12SemanticError::UnknownRequiredFeatures);
                }
 
@@ -577,7 +581,7 @@ impl InvoiceRequest {
                &self, payment_paths: Vec<(BlindedPayInfo, BlindedPath)>, payment_hash: PaymentHash,
                created_at: core::time::Duration, expanded_key: &ExpandedKey, secp_ctx: &Secp256k1<T>
        ) -> Result<InvoiceBuilder<DerivedSigningPubkey>, Bolt12SemanticError> {
-               if self.features().requires_unknown_bits() {
+               if self.invoice_request_features().requires_unknown_bits() {
                        return Err(Bolt12SemanticError::UnknownRequiredFeatures);
                }
 
@@ -887,10 +891,10 @@ mod tests {
                invoice_request.write(&mut buffer).unwrap();
 
                assert_eq!(invoice_request.bytes, buffer.as_slice());
-               assert_eq!(invoice_request.metadata(), &[1; 32]);
+               assert_eq!(invoice_request.payer_metadata(), &[1; 32]);
                assert_eq!(invoice_request.chain(), ChainHash::using_genesis_block(Network::Bitcoin));
                assert_eq!(invoice_request.amount_msats(), None);
-               assert_eq!(invoice_request.features(), &InvoiceRequestFeatures::empty());
+               assert_eq!(invoice_request.invoice_request_features(), &InvoiceRequestFeatures::empty());
                assert_eq!(invoice_request.quantity(), None);
                assert_eq!(invoice_request.payer_id(), payer_pubkey());
                assert_eq!(invoice_request.payer_note(), None);
@@ -1291,7 +1295,7 @@ mod tests {
                        .build().unwrap()
                        .sign(payer_sign).unwrap();
                let (_, _, tlv_stream, _) = invoice_request.as_tlv_stream();
-               assert_eq!(invoice_request.features(), &InvoiceRequestFeatures::unknown());
+               assert_eq!(invoice_request.invoice_request_features(), &InvoiceRequestFeatures::unknown());
                assert_eq!(tlv_stream.features, Some(&InvoiceRequestFeatures::unknown()));
 
                let invoice_request = OfferBuilder::new("foo".into(), recipient_pubkey())
@@ -1303,7 +1307,7 @@ mod tests {
                        .build().unwrap()
                        .sign(payer_sign).unwrap();
                let (_, _, tlv_stream, _) = invoice_request.as_tlv_stream();
-               assert_eq!(invoice_request.features(), &InvoiceRequestFeatures::empty());
+               assert_eq!(invoice_request.invoice_request_features(), &InvoiceRequestFeatures::empty());
                assert_eq!(tlv_stream.features, None);
        }
 
index 1044ac9606d5acbe07e736204a97632c08b6758f..f6aa354b9e4f3d6c9efd2aee4ac47a04c8741dc0 100644 (file)
@@ -455,16 +455,16 @@ impl Offer {
        /// Similar to [`Offer::request_invoice`] except it:
        /// - derives the [`InvoiceRequest::payer_id`] such that a different key can be used for each
        ///   request, and
-       /// - sets the [`InvoiceRequest::metadata`] when [`InvoiceRequestBuilder::build`] is called such
-       ///   that it can be used by [`Bolt12Invoice::verify`] to determine if the invoice was requested
-       ///   using a base [`ExpandedKey`] from which the payer id was derived.
+       /// - sets the [`InvoiceRequest::payer_metadata`] when [`InvoiceRequestBuilder::build`] is
+       ///   called such that it can be used by [`Bolt12Invoice::verify`] to determine if the invoice
+       ///   was requested using a base [`ExpandedKey`] from which the payer id was derived.
        ///
        /// Useful to protect the sender's privacy.
        ///
        /// This is not exported to bindings users as builder patterns don't map outside of move semantics.
        ///
        /// [`InvoiceRequest::payer_id`]: crate::offers::invoice_request::InvoiceRequest::payer_id
-       /// [`InvoiceRequest::metadata`]: crate::offers::invoice_request::InvoiceRequest::metadata
+       /// [`InvoiceRequest::payer_metadata`]: crate::offers::invoice_request::InvoiceRequest::payer_metadata
        /// [`Bolt12Invoice::verify`]: crate::offers::invoice::Bolt12Invoice::verify
        /// [`ExpandedKey`]: crate::ln::inbound_payment::ExpandedKey
        pub fn request_invoice_deriving_payer_id<'a, 'b, ES: Deref, T: secp256k1::Signing>(
index bfc02b5dbcb9804f1fd08bdf689f541a12579ed9..b3b2f7a88aee21ec776e0881ad83b682738a6e40 100644 (file)
@@ -22,9 +22,9 @@ use crate::prelude::*;
 #[cfg_attr(test, derive(PartialEq))]
 pub(super) struct PayerContents(pub Metadata);
 
-/// TLV record type for [`InvoiceRequest::metadata`] and [`Refund::metadata`].
+/// TLV record type for [`InvoiceRequest::payer_metadata`] and [`Refund::metadata`].
 ///
-/// [`InvoiceRequest::metadata`]: crate::offers::invoice_request::InvoiceRequest::metadata
+/// [`InvoiceRequest::payer_metadata`]: crate::offers::invoice_request::InvoiceRequest::payer_metadata
 /// [`Refund::metadata`]: crate::offers::refund::Refund::metadata
 pub(super) const PAYER_METADATA_TYPE: u64 = 0;