Skip to content

Commit 12c3a24

Browse files
committed
Return the invoice when requesting a refund
When sending an invoice for a refund, information from the invoice may be useful for caller. For instance, the payment_hash can be used to track whether the refund was paid. Return the invoice to facilitate this use case.
1 parent 01814dc commit 12c3a24

File tree

2 files changed

+25
-11
lines changed

2 files changed

+25
-11
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1694,15 +1694,20 @@ where
16941694
/// #
16951695
/// # fn example<T: AChannelManager>(channel_manager: T, refund: &Refund) {
16961696
/// # let channel_manager = channel_manager.get_cm();
1697-
/// match channel_manager.request_refund_payment(refund) {
1698-
/// Ok(()) => println!("Requesting payment for refund"),
1699-
/// Err(e) => println!("Unable to request payment for refund: {:?}", e),
1700-
/// }
1697+
/// let known_payment_hash = match channel_manager.request_refund_payment(refund) {
1698+
/// Ok(invoice) => {
1699+
/// let payment_hash = invoice.payment_hash();
1700+
/// println!("Requesting refund payment {}", payment_hash);
1701+
/// payment_hash
1702+
/// },
1703+
/// Err(e) => panic!("Unable to request payment for refund: {:?}", e),
1704+
/// };
17011705
///
17021706
/// // On the event processing thread
17031707
/// channel_manager.process_pending_events(&|event| match event {
17041708
/// Event::PaymentClaimable { payment_hash, purpose, .. } => match purpose {
17051709
/// PaymentPurpose::InvoicePayment { payment_preimage: Some(payment_preimage), .. } => {
1710+
/// assert_eq!(payment_hash, known_payment_hash);
17061711
/// println!("Claiming payment {}", payment_hash);
17071712
/// channel_manager.claim_funds(payment_preimage);
17081713
/// },
@@ -1713,6 +1718,7 @@ where
17131718
/// # _ => {},
17141719
/// },
17151720
/// Event::PaymentClaimed { payment_hash, amount_msat, .. } => {
1721+
/// assert_eq!(payment_hash, known_payment_hash);
17161722
/// println!("Claimed {} msats", amount_msat);
17171723
/// },
17181724
/// // ...
@@ -8774,7 +8780,7 @@ where
87748780
///
87758781
/// The resulting invoice uses a [`PaymentHash`] recognized by the [`ChannelManager`] and a
87768782
/// [`BlindedPath`] containing the [`PaymentSecret`] needed to reconstruct the corresponding
8777-
/// [`PaymentPreimage`].
8783+
/// [`PaymentPreimage`]. It is returned purely for informational purposes.
87788784
///
87798785
/// # Limitations
87808786
///
@@ -8791,7 +8797,9 @@ where
87918797
/// the invoice.
87928798
///
87938799
/// [`Bolt12Invoice`]: crate::offers::invoice::Bolt12Invoice
8794-
pub fn request_refund_payment(&self, refund: &Refund) -> Result<(), Bolt12SemanticError> {
8800+
pub fn request_refund_payment(
8801+
&self, refund: &Refund
8802+
) -> Result<Bolt12Invoice, Bolt12SemanticError> {
87958803
let expanded_key = &self.inbound_payment_key;
87968804
let entropy = &*self.entropy_source;
87978805
let secp_ctx = &self.secp_ctx;
@@ -8830,7 +8838,7 @@ where
88308838
let mut pending_offers_messages = self.pending_offers_messages.lock().unwrap();
88318839
if refund.paths().is_empty() {
88328840
let message = new_pending_onion_message(
8833-
OffersMessage::Invoice(invoice),
8841+
OffersMessage::Invoice(invoice.clone()),
88348842
Destination::Node(refund.payer_id()),
88358843
Some(reply_path),
88368844
);
@@ -8846,7 +8854,7 @@ where
88468854
}
88478855
}
88488856

8849-
Ok(())
8857+
Ok(invoice)
88508858
},
88518859
Err(()) => Err(Bolt12SemanticError::InvalidAmount),
88528860
}

lightning/src/ln/offers_tests.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,7 @@ fn creates_and_pays_for_refund_using_two_hop_blinded_path() {
473473
}
474474
expect_recent_payment!(david, RecentPaymentDetails::AwaitingInvoice, payment_id);
475475

476-
alice.node.request_refund_payment(&refund).unwrap();
476+
let expected_invoice = alice.node.request_refund_payment(&refund).unwrap();
477477

478478
connect_peers(alice, charlie);
479479

@@ -484,6 +484,8 @@ fn creates_and_pays_for_refund_using_two_hop_blinded_path() {
484484
david.onion_messenger.handle_onion_message(&charlie_id, &onion_message);
485485

486486
let invoice = extract_invoice(david, &onion_message);
487+
assert_eq!(invoice, expected_invoice);
488+
487489
assert_eq!(invoice.amount_msats(), 10_000_000);
488490
assert_ne!(invoice.signing_pubkey(), alice_id);
489491
assert!(!invoice.payment_paths().is_empty());
@@ -589,12 +591,14 @@ fn creates_and_pays_for_refund_using_one_hop_blinded_path() {
589591
}
590592
expect_recent_payment!(bob, RecentPaymentDetails::AwaitingInvoice, payment_id);
591593

592-
alice.node.request_refund_payment(&refund).unwrap();
594+
let expected_invoice = alice.node.request_refund_payment(&refund).unwrap();
593595

594596
let onion_message = alice.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
595597
bob.onion_messenger.handle_onion_message(&alice_id, &onion_message);
596598

597599
let invoice = extract_invoice(bob, &onion_message);
600+
assert_eq!(invoice, expected_invoice);
601+
598602
assert_eq!(invoice.amount_msats(), 10_000_000);
599603
assert_ne!(invoice.signing_pubkey(), alice_id);
600604
assert!(!invoice.payment_paths().is_empty());
@@ -681,12 +685,14 @@ fn pays_for_refund_without_blinded_paths() {
681685
assert!(refund.paths().is_empty());
682686
expect_recent_payment!(bob, RecentPaymentDetails::AwaitingInvoice, payment_id);
683687

684-
alice.node.request_refund_payment(&refund).unwrap();
688+
let expected_invoice = alice.node.request_refund_payment(&refund).unwrap();
685689

686690
let onion_message = alice.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
687691
bob.onion_messenger.handle_onion_message(&alice_id, &onion_message);
688692

689693
let invoice = extract_invoice(bob, &onion_message);
694+
assert_eq!(invoice, expected_invoice);
695+
690696
route_bolt12_payment(bob, &[alice], &invoice);
691697
expect_recent_payment!(bob, RecentPaymentDetails::Pending, payment_id);
692698

0 commit comments

Comments
 (0)