Skip to content

Commit 9d02d06

Browse files
committed
Macro-ize InvoiceRequest accessors for reuse
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.
1 parent 66060ca commit 9d02d06

File tree

3 files changed

+31
-27
lines changed

3 files changed

+31
-27
lines changed

lightning/src/offers/invoice_request.rs

+25-21
Original file line numberDiff line numberDiff line change
@@ -441,48 +441,52 @@ pub(super) struct InvoiceRequestContentsWithoutPayerId {
441441
payer_note: Option<String>,
442442
}
443443

444-
impl InvoiceRequest {
444+
macro_rules! invoice_request_accessors { ($self: ident, $contents: expr) => {
445445
/// An unpredictable series of bytes, typically containing information about the derivation of
446446
/// [`payer_id`].
447447
///
448448
/// [`payer_id`]: Self::payer_id
449-
pub fn metadata(&self) -> &[u8] {
450-
self.contents.metadata()
449+
pub fn payer_metadata(&$self) -> &[u8] {
450+
$contents.metadata()
451451
}
452452

453453
/// A chain from [`Offer::chains`] that the offer is valid for.
454-
pub fn chain(&self) -> ChainHash {
455-
self.contents.chain()
454+
pub fn chain(&$self) -> ChainHash {
455+
$contents.chain()
456456
}
457457

458458
/// The amount to pay in msats (i.e., the minimum lightning-payable unit for [`chain`]), which
459459
/// must be greater than or equal to [`Offer::amount`], converted if necessary.
460460
///
461461
/// [`chain`]: Self::chain
462-
pub fn amount_msats(&self) -> Option<u64> {
463-
self.contents.amount_msats()
462+
pub fn amount_msats(&$self) -> Option<u64> {
463+
$contents.amount_msats()
464464
}
465465

466466
/// Features pertaining to requesting an invoice.
467-
pub fn features(&self) -> &InvoiceRequestFeatures {
468-
&self.contents.features()
467+
pub fn invoice_request_features(&$self) -> &InvoiceRequestFeatures {
468+
&$contents.features()
469469
}
470470

471471
/// The quantity of the offer's item conforming to [`Offer::is_valid_quantity`].
472-
pub fn quantity(&self) -> Option<u64> {
473-
self.contents.quantity()
472+
pub fn quantity(&$self) -> Option<u64> {
473+
$contents.quantity()
474474
}
475475

476476
/// A possibly transient pubkey used to sign the invoice request.
477-
pub fn payer_id(&self) -> PublicKey {
478-
self.contents.payer_id()
477+
pub fn payer_id(&$self) -> PublicKey {
478+
$contents.payer_id()
479479
}
480480

481481
/// A payer-provided note which will be seen by the recipient and reflected back in the invoice
482482
/// response.
483-
pub fn payer_note(&self) -> Option<PrintableString> {
484-
self.contents.payer_note()
483+
pub fn payer_note(&$self) -> Option<PrintableString> {
484+
$contents.payer_note()
485485
}
486+
} }
487+
488+
impl InvoiceRequest {
489+
invoice_request_accessors!(self, self.contents);
486490

487491
/// Signature of the invoice request using [`payer_id`].
488492
///
@@ -534,7 +538,7 @@ impl InvoiceRequest {
534538
&self, payment_paths: Vec<(BlindedPayInfo, BlindedPath)>, payment_hash: PaymentHash,
535539
created_at: core::time::Duration
536540
) -> Result<InvoiceBuilder<ExplicitSigningPubkey>, Bolt12SemanticError> {
537-
if self.features().requires_unknown_bits() {
541+
if self.invoice_request_features().requires_unknown_bits() {
538542
return Err(Bolt12SemanticError::UnknownRequiredFeatures);
539543
}
540544

@@ -577,7 +581,7 @@ impl InvoiceRequest {
577581
&self, payment_paths: Vec<(BlindedPayInfo, BlindedPath)>, payment_hash: PaymentHash,
578582
created_at: core::time::Duration, expanded_key: &ExpandedKey, secp_ctx: &Secp256k1<T>
579583
) -> Result<InvoiceBuilder<DerivedSigningPubkey>, Bolt12SemanticError> {
580-
if self.features().requires_unknown_bits() {
584+
if self.invoice_request_features().requires_unknown_bits() {
581585
return Err(Bolt12SemanticError::UnknownRequiredFeatures);
582586
}
583587

@@ -887,10 +891,10 @@ mod tests {
887891
invoice_request.write(&mut buffer).unwrap();
888892

889893
assert_eq!(invoice_request.bytes, buffer.as_slice());
890-
assert_eq!(invoice_request.metadata(), &[1; 32]);
894+
assert_eq!(invoice_request.payer_metadata(), &[1; 32]);
891895
assert_eq!(invoice_request.chain(), ChainHash::using_genesis_block(Network::Bitcoin));
892896
assert_eq!(invoice_request.amount_msats(), None);
893-
assert_eq!(invoice_request.features(), &InvoiceRequestFeatures::empty());
897+
assert_eq!(invoice_request.invoice_request_features(), &InvoiceRequestFeatures::empty());
894898
assert_eq!(invoice_request.quantity(), None);
895899
assert_eq!(invoice_request.payer_id(), payer_pubkey());
896900
assert_eq!(invoice_request.payer_note(), None);
@@ -1291,7 +1295,7 @@ mod tests {
12911295
.build().unwrap()
12921296
.sign(payer_sign).unwrap();
12931297
let (_, _, tlv_stream, _) = invoice_request.as_tlv_stream();
1294-
assert_eq!(invoice_request.features(), &InvoiceRequestFeatures::unknown());
1298+
assert_eq!(invoice_request.invoice_request_features(), &InvoiceRequestFeatures::unknown());
12951299
assert_eq!(tlv_stream.features, Some(&InvoiceRequestFeatures::unknown()));
12961300

12971301
let invoice_request = OfferBuilder::new("foo".into(), recipient_pubkey())
@@ -1303,7 +1307,7 @@ mod tests {
13031307
.build().unwrap()
13041308
.sign(payer_sign).unwrap();
13051309
let (_, _, tlv_stream, _) = invoice_request.as_tlv_stream();
1306-
assert_eq!(invoice_request.features(), &InvoiceRequestFeatures::empty());
1310+
assert_eq!(invoice_request.invoice_request_features(), &InvoiceRequestFeatures::empty());
13071311
assert_eq!(tlv_stream.features, None);
13081312
}
13091313

lightning/src/offers/offer.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -455,16 +455,16 @@ impl Offer {
455455
/// Similar to [`Offer::request_invoice`] except it:
456456
/// - derives the [`InvoiceRequest::payer_id`] such that a different key can be used for each
457457
/// request, and
458-
/// - sets the [`InvoiceRequest::metadata`] when [`InvoiceRequestBuilder::build`] is called such
459-
/// that it can be used by [`Bolt12Invoice::verify`] to determine if the invoice was requested
460-
/// using a base [`ExpandedKey`] from which the payer id was derived.
458+
/// - sets the [`InvoiceRequest::payer_metadata`] when [`InvoiceRequestBuilder::build`] is
459+
/// called such that it can be used by [`Bolt12Invoice::verify`] to determine if the invoice
460+
/// was requested using a base [`ExpandedKey`] from which the payer id was derived.
461461
///
462462
/// Useful to protect the sender's privacy.
463463
///
464464
/// This is not exported to bindings users as builder patterns don't map outside of move semantics.
465465
///
466466
/// [`InvoiceRequest::payer_id`]: crate::offers::invoice_request::InvoiceRequest::payer_id
467-
/// [`InvoiceRequest::metadata`]: crate::offers::invoice_request::InvoiceRequest::metadata
467+
/// [`InvoiceRequest::payer_metadata`]: crate::offers::invoice_request::InvoiceRequest::payer_metadata
468468
/// [`Bolt12Invoice::verify`]: crate::offers::invoice::Bolt12Invoice::verify
469469
/// [`ExpandedKey`]: crate::ln::inbound_payment::ExpandedKey
470470
pub fn request_invoice_deriving_payer_id<'a, 'b, ES: Deref, T: secp256k1::Signing>(

lightning/src/offers/payer.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ use crate::prelude::*;
2222
#[cfg_attr(test, derive(PartialEq))]
2323
pub(super) struct PayerContents(pub Metadata);
2424

25-
/// TLV record type for [`InvoiceRequest::metadata`] and [`Refund::metadata`].
25+
/// TLV record type for [`InvoiceRequest::payer_metadata`] and [`Refund::metadata`].
2626
///
27-
/// [`InvoiceRequest::metadata`]: crate::offers::invoice_request::InvoiceRequest::metadata
27+
/// [`InvoiceRequest::payer_metadata`]: crate::offers::invoice_request::InvoiceRequest::payer_metadata
2828
/// [`Refund::metadata`]: crate::offers::refund::Refund::metadata
2929
pub(super) const PAYER_METADATA_TYPE: u64 = 0;
3030

0 commit comments

Comments
 (0)