Skip to content

Commit 57e62da

Browse files
committed
Expose invoice accessors in UnsignedBolt12Invoice
1 parent bde9823 commit 57e62da

File tree

2 files changed

+49
-32
lines changed

2 files changed

+49
-32
lines changed

lightning/src/offers/invoice.rs

+48-31
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,8 @@ impl<'a, S: SigningPubkeyStrategy> InvoiceBuilder<'a, S> {
320320
self
321321
}
322322

323-
/// Sets [`Bolt12Invoice::features`] to indicate MPP may be used. Otherwise, MPP is disallowed.
323+
/// Sets [`Bolt12Invoice::invoice_features`] to indicate MPP may be used. Otherwise, MPP is
324+
/// disallowed.
324325
pub fn allow_mpp(mut self) -> Self {
325326
self.invoice.fields_mut().features.set_basic_mpp_optional();
326327
self
@@ -396,11 +397,6 @@ impl UnsignedBolt12Invoice {
396397
Self { bytes, contents, tagged_hash }
397398
}
398399

399-
/// The public key corresponding to the key needed to sign the invoice.
400-
pub fn signing_pubkey(&self) -> PublicKey {
401-
self.contents.fields().signing_pubkey
402-
}
403-
404400
/// Signs the [`TaggedHash`] of the invoice using the given function.
405401
///
406402
/// Note: The hash computation may have included unknown, odd TLV records.
@@ -485,11 +481,11 @@ struct InvoiceFields {
485481
signing_pubkey: PublicKey,
486482
}
487483

488-
impl Bolt12Invoice {
484+
macro_rules! invoice_accessors { ($self: ident, $contents: expr) => {
489485
/// A complete description of the purpose of the originating offer or refund. Intended to be
490486
/// displayed to the user but with the caveat that it has not been verified in any way.
491-
pub fn description(&self) -> PrintableString {
492-
self.contents.description()
487+
pub fn description(&$self) -> PrintableString {
488+
$contents.description()
493489
}
494490

495491
/// Paths to the recipient originating from publicly reachable nodes, including information
@@ -500,52 +496,60 @@ impl Bolt12Invoice {
500496
///
501497
/// This is not exported to bindings users as slices with non-reference types cannot be ABI
502498
/// matched in another language.
503-
pub fn payment_paths(&self) -> &[(BlindedPayInfo, BlindedPath)] {
504-
self.contents.payment_paths()
499+
pub fn payment_paths(&$self) -> &[(BlindedPayInfo, BlindedPath)] {
500+
$contents.payment_paths()
505501
}
506502

507503
/// Duration since the Unix epoch when the invoice was created.
508-
pub fn created_at(&self) -> Duration {
509-
self.contents.created_at()
504+
pub fn created_at(&$self) -> Duration {
505+
$contents.created_at()
510506
}
511507

512508
/// Duration since [`Bolt12Invoice::created_at`] when the invoice has expired and therefore
513509
/// should no longer be paid.
514-
pub fn relative_expiry(&self) -> Duration {
515-
self.contents.relative_expiry()
510+
pub fn relative_expiry(&$self) -> Duration {
511+
$contents.relative_expiry()
516512
}
517513

518514
/// Whether the invoice has expired.
519515
#[cfg(feature = "std")]
520-
pub fn is_expired(&self) -> bool {
521-
self.contents.is_expired()
516+
pub fn is_expired(&$self) -> bool {
517+
$contents.is_expired()
522518
}
523519

524520
/// SHA256 hash of the payment preimage that will be given in return for paying the invoice.
525-
pub fn payment_hash(&self) -> PaymentHash {
526-
self.contents.payment_hash()
521+
pub fn payment_hash(&$self) -> PaymentHash {
522+
$contents.payment_hash()
527523
}
528524

529525
/// The minimum amount required for a successful payment of the invoice.
530-
pub fn amount_msats(&self) -> u64 {
531-
self.contents.amount_msats()
526+
pub fn amount_msats(&$self) -> u64 {
527+
$contents.amount_msats()
532528
}
533529

534530
/// Fallback addresses for paying the invoice on-chain, in order of most-preferred to
535531
/// least-preferred.
536-
pub fn fallbacks(&self) -> Vec<Address> {
537-
self.contents.fallbacks()
532+
pub fn fallbacks(&$self) -> Vec<Address> {
533+
$contents.fallbacks()
538534
}
539535

540536
/// Features pertaining to paying an invoice.
541-
pub fn features(&self) -> &Bolt12InvoiceFeatures {
542-
self.contents.features()
537+
pub fn invoice_features(&$self) -> &Bolt12InvoiceFeatures {
538+
$contents.features()
543539
}
544540

545541
/// The public key corresponding to the key used to sign the invoice.
546-
pub fn signing_pubkey(&self) -> PublicKey {
547-
self.contents.signing_pubkey()
542+
pub fn signing_pubkey(&$self) -> PublicKey {
543+
$contents.signing_pubkey()
548544
}
545+
} }
546+
547+
impl UnsignedBolt12Invoice {
548+
invoice_accessors!(self, self.contents);
549+
}
550+
551+
impl Bolt12Invoice {
552+
invoice_accessors!(self, self.contents);
549553

550554
/// Signature of the invoice verified using [`Bolt12Invoice::signing_pubkey`].
551555
pub fn signature(&self) -> Signature {
@@ -1092,6 +1096,19 @@ mod tests {
10921096
let mut buffer = Vec::new();
10931097
unsigned_invoice.write(&mut buffer).unwrap();
10941098

1099+
assert_eq!(unsigned_invoice.bytes, buffer.as_slice());
1100+
assert_eq!(unsigned_invoice.description(), PrintableString("foo"));
1101+
assert_eq!(unsigned_invoice.payment_paths(), payment_paths.as_slice());
1102+
assert_eq!(unsigned_invoice.created_at(), now);
1103+
assert_eq!(unsigned_invoice.relative_expiry(), DEFAULT_RELATIVE_EXPIRY);
1104+
#[cfg(feature = "std")]
1105+
assert!(!unsigned_invoice.is_expired());
1106+
assert_eq!(unsigned_invoice.payment_hash(), payment_hash);
1107+
assert_eq!(unsigned_invoice.amount_msats(), 1000);
1108+
assert_eq!(unsigned_invoice.fallbacks(), vec![]);
1109+
assert_eq!(unsigned_invoice.invoice_features(), &Bolt12InvoiceFeatures::empty());
1110+
assert_eq!(unsigned_invoice.signing_pubkey(), recipient_pubkey());
1111+
10951112
match UnsignedBolt12Invoice::try_from(buffer) {
10961113
Err(e) => panic!("error parsing unsigned invoice: {:?}", e),
10971114
Ok(parsed) => {
@@ -1115,7 +1132,7 @@ mod tests {
11151132
assert_eq!(invoice.payment_hash(), payment_hash);
11161133
assert_eq!(invoice.amount_msats(), 1000);
11171134
assert_eq!(invoice.fallbacks(), vec![]);
1118-
assert_eq!(invoice.features(), &Bolt12InvoiceFeatures::empty());
1135+
assert_eq!(invoice.invoice_features(), &Bolt12InvoiceFeatures::empty());
11191136
assert_eq!(invoice.signing_pubkey(), recipient_pubkey());
11201137
assert!(
11211138
merkle::verify_signature(
@@ -1198,7 +1215,7 @@ mod tests {
11981215
assert_eq!(invoice.payment_hash(), payment_hash);
11991216
assert_eq!(invoice.amount_msats(), 1000);
12001217
assert_eq!(invoice.fallbacks(), vec![]);
1201-
assert_eq!(invoice.features(), &Bolt12InvoiceFeatures::empty());
1218+
assert_eq!(invoice.invoice_features(), &Bolt12InvoiceFeatures::empty());
12021219
assert_eq!(invoice.signing_pubkey(), recipient_pubkey());
12031220
assert!(
12041221
merkle::verify_signature(
@@ -1546,7 +1563,7 @@ mod tests {
15461563
.build().unwrap()
15471564
.sign(recipient_sign).unwrap();
15481565
let (_, _, _, tlv_stream, _) = invoice.as_tlv_stream();
1549-
assert_eq!(invoice.features(), &features);
1566+
assert_eq!(invoice.invoice_features(), &features);
15501567
assert_eq!(tlv_stream.features, Some(&features));
15511568
}
15521569

@@ -1766,7 +1783,7 @@ mod tests {
17661783
Ok(invoice) => {
17671784
let mut features = Bolt12InvoiceFeatures::empty();
17681785
features.set_basic_mpp_optional();
1769-
assert_eq!(invoice.features(), &features);
1786+
assert_eq!(invoice.invoice_features(), &features);
17701787
},
17711788
Err(e) => panic!("error parsing invoice: {:?}", e),
17721789
}

lightning/src/routing/router.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -652,7 +652,7 @@ impl PaymentParameters {
652652
/// [`PaymentParameters::expiry_time`].
653653
pub fn from_bolt12_invoice(invoice: &Bolt12Invoice) -> Self {
654654
Self::blinded(invoice.payment_paths().to_vec())
655-
.with_bolt12_features(invoice.features().clone()).unwrap()
655+
.with_bolt12_features(invoice.invoice_features().clone()).unwrap()
656656
.with_expiry_time(invoice.created_at().as_secs().saturating_add(invoice.relative_expiry().as_secs()))
657657
}
658658

0 commit comments

Comments
 (0)