Skip to content

Commit 5a0c4ba

Browse files
committed
Expose invoice accessors in UnsignedBolt12Invoice
1 parent 558d15f commit 5a0c4ba

File tree

2 files changed

+49
-32
lines changed

2 files changed

+49
-32
lines changed

lightning/src/offers/invoice.rs

Lines changed: 48 additions & 31 deletions
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
@@ -391,11 +392,6 @@ impl UnsignedBolt12Invoice {
391392
Self { bytes, contents, tagged_hash }
392393
}
393394

394-
/// The public key corresponding to the key needed to sign the invoice.
395-
pub fn signing_pubkey(&self) -> PublicKey {
396-
self.contents.fields().signing_pubkey
397-
}
398-
399395
/// Signs the invoice using the given function.
400396
///
401397
/// This is not exported to bindings users as functions aren't currently mapped.
@@ -479,11 +475,11 @@ struct InvoiceFields {
479475
signing_pubkey: PublicKey,
480476
}
481477

482-
impl Bolt12Invoice {
478+
macro_rules! invoice_accessors { ($self: ident, $contents: expr) => {
483479
/// A complete description of the purpose of the originating offer or refund. Intended to be
484480
/// displayed to the user but with the caveat that it has not been verified in any way.
485-
pub fn description(&self) -> PrintableString {
486-
self.contents.description()
481+
pub fn description(&$self) -> PrintableString {
482+
$contents.description()
487483
}
488484

489485
/// Paths to the recipient originating from publicly reachable nodes, including information
@@ -494,52 +490,60 @@ impl Bolt12Invoice {
494490
///
495491
/// This is not exported to bindings users as slices with non-reference types cannot be ABI
496492
/// matched in another language.
497-
pub fn payment_paths(&self) -> &[(BlindedPayInfo, BlindedPath)] {
498-
self.contents.payment_paths()
493+
pub fn payment_paths(&$self) -> &[(BlindedPayInfo, BlindedPath)] {
494+
$contents.payment_paths()
499495
}
500496

501497
/// Duration since the Unix epoch when the invoice was created.
502-
pub fn created_at(&self) -> Duration {
503-
self.contents.created_at()
498+
pub fn created_at(&$self) -> Duration {
499+
$contents.created_at()
504500
}
505501

506502
/// Duration since [`Bolt12Invoice::created_at`] when the invoice has expired and therefore
507503
/// should no longer be paid.
508-
pub fn relative_expiry(&self) -> Duration {
509-
self.contents.relative_expiry()
504+
pub fn relative_expiry(&$self) -> Duration {
505+
$contents.relative_expiry()
510506
}
511507

512508
/// Whether the invoice has expired.
513509
#[cfg(feature = "std")]
514-
pub fn is_expired(&self) -> bool {
515-
self.contents.is_expired()
510+
pub fn is_expired(&$self) -> bool {
511+
$contents.is_expired()
516512
}
517513

518514
/// SHA256 hash of the payment preimage that will be given in return for paying the invoice.
519-
pub fn payment_hash(&self) -> PaymentHash {
520-
self.contents.payment_hash()
515+
pub fn payment_hash(&$self) -> PaymentHash {
516+
$contents.payment_hash()
521517
}
522518

523519
/// The minimum amount required for a successful payment of the invoice.
524-
pub fn amount_msats(&self) -> u64 {
525-
self.contents.amount_msats()
520+
pub fn amount_msats(&$self) -> u64 {
521+
$contents.amount_msats()
526522
}
527523

528524
/// Fallback addresses for paying the invoice on-chain, in order of most-preferred to
529525
/// least-preferred.
530-
pub fn fallbacks(&self) -> Vec<Address> {
531-
self.contents.fallbacks()
526+
pub fn fallbacks(&$self) -> Vec<Address> {
527+
$contents.fallbacks()
532528
}
533529

534530
/// Features pertaining to paying an invoice.
535-
pub fn features(&self) -> &Bolt12InvoiceFeatures {
536-
self.contents.features()
531+
pub fn invoice_features(&$self) -> &Bolt12InvoiceFeatures {
532+
$contents.features()
537533
}
538534

539535
/// The public key corresponding to the key used to sign the invoice.
540-
pub fn signing_pubkey(&self) -> PublicKey {
541-
self.contents.signing_pubkey()
536+
pub fn signing_pubkey(&$self) -> PublicKey {
537+
$contents.signing_pubkey()
542538
}
539+
} }
540+
541+
impl UnsignedBolt12Invoice {
542+
invoice_accessors!(self, self.contents);
543+
}
544+
545+
impl Bolt12Invoice {
546+
invoice_accessors!(self, self.contents);
543547

544548
/// Signature of the invoice verified using [`Bolt12Invoice::signing_pubkey`].
545549
pub fn signature(&self) -> Signature {
@@ -1086,6 +1090,19 @@ mod tests {
10861090
let mut buffer = Vec::new();
10871091
unsigned_invoice.write(&mut buffer).unwrap();
10881092

1093+
assert_eq!(unsigned_invoice.bytes, buffer.as_slice());
1094+
assert_eq!(unsigned_invoice.description(), PrintableString("foo"));
1095+
assert_eq!(unsigned_invoice.payment_paths(), payment_paths.as_slice());
1096+
assert_eq!(unsigned_invoice.created_at(), now);
1097+
assert_eq!(unsigned_invoice.relative_expiry(), DEFAULT_RELATIVE_EXPIRY);
1098+
#[cfg(feature = "std")]
1099+
assert!(!unsigned_invoice.is_expired());
1100+
assert_eq!(unsigned_invoice.payment_hash(), payment_hash);
1101+
assert_eq!(unsigned_invoice.amount_msats(), 1000);
1102+
assert_eq!(unsigned_invoice.fallbacks(), vec![]);
1103+
assert_eq!(unsigned_invoice.invoice_features(), &Bolt12InvoiceFeatures::empty());
1104+
assert_eq!(unsigned_invoice.signing_pubkey(), recipient_pubkey());
1105+
10891106
match UnsignedBolt12Invoice::try_from(buffer) {
10901107
Err(e) => panic!("error parsing unsigned invoice: {:?}", e),
10911108
Ok(parsed) => {
@@ -1109,7 +1126,7 @@ mod tests {
11091126
assert_eq!(invoice.payment_hash(), payment_hash);
11101127
assert_eq!(invoice.amount_msats(), 1000);
11111128
assert_eq!(invoice.fallbacks(), vec![]);
1112-
assert_eq!(invoice.features(), &Bolt12InvoiceFeatures::empty());
1129+
assert_eq!(invoice.invoice_features(), &Bolt12InvoiceFeatures::empty());
11131130
assert_eq!(invoice.signing_pubkey(), recipient_pubkey());
11141131
assert!(
11151132
merkle::verify_signature(
@@ -1192,7 +1209,7 @@ mod tests {
11921209
assert_eq!(invoice.payment_hash(), payment_hash);
11931210
assert_eq!(invoice.amount_msats(), 1000);
11941211
assert_eq!(invoice.fallbacks(), vec![]);
1195-
assert_eq!(invoice.features(), &Bolt12InvoiceFeatures::empty());
1212+
assert_eq!(invoice.invoice_features(), &Bolt12InvoiceFeatures::empty());
11961213
assert_eq!(invoice.signing_pubkey(), recipient_pubkey());
11971214
assert!(
11981215
merkle::verify_signature(
@@ -1540,7 +1557,7 @@ mod tests {
15401557
.build().unwrap()
15411558
.sign(recipient_sign).unwrap();
15421559
let (_, _, _, tlv_stream, _) = invoice.as_tlv_stream();
1543-
assert_eq!(invoice.features(), &features);
1560+
assert_eq!(invoice.invoice_features(), &features);
15441561
assert_eq!(tlv_stream.features, Some(&features));
15451562
}
15461563

@@ -1760,7 +1777,7 @@ mod tests {
17601777
Ok(invoice) => {
17611778
let mut features = Bolt12InvoiceFeatures::empty();
17621779
features.set_basic_mpp_optional();
1763-
assert_eq!(invoice.features(), &features);
1780+
assert_eq!(invoice.invoice_features(), &features);
17641781
},
17651782
Err(e) => panic!("error parsing invoice: {:?}", e),
17661783
}

lightning/src/routing/router.rs

Lines changed: 1 addition & 1 deletion
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)