Skip to content

Commit 35c408f

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 3a0d0ff commit 35c408f

File tree

3 files changed

+21
-9
lines changed

3 files changed

+21
-9
lines changed

lightning/src/events/mod.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,9 @@ pub enum Event {
584584
payment_id: PaymentId,
585585
},
586586
/// Indicates that a [`Bolt12Invoice`] was generated in response to an [`InvoiceRequest`] and is
587-
/// being prepared to be sent via an [`OnionMessage`].
587+
/// being prepared to be sent via an [`OnionMessage`]. The event is provided only for invoices
588+
/// corresponding to an [`Offer`], not for a [`Refund`]. For the latter, the invoice is returned
589+
/// by [`ChannelManager::request_refund_payment`].
588590
///
589591
/// Note that this doesn't necessarily mean that the invoice was sent and -- once sent -- it may
590592
/// never reach its destination because of the unreliable nature of onion messages. Any of the
@@ -606,10 +608,12 @@ pub enum Event {
606608
///
607609
/// [`InvoiceRequest`]: crate::offers::invoice_request::InvoiceRequest
608610
/// [`OnionMessage`]: crate::ln::msgs::OnionMessage
611+
/// [`Offer`]: crate::offers::offer::Offer
612+
/// [`Refund`]: crate::offers::refund::Refund
613+
/// [`ChannelManager::request_refund_payment`]: crate::ln::channelmanager::ChannelManager::request_refund_payment
609614
/// [`PeerManager`]: crate::ln::peer_handler::PeerManager
610615
/// [`MessageRouter`]: crate::onion_message::messenger::MessageRouter
611616
/// [`OnionMessagePath`]: crate::onion_message::messenger::OnionMessagePath
612-
/// [`Offer`]: crate::offers::offer::Offer
613617
InvoiceGenerated {
614618
/// An invoice that was generated in response to an [`InvoiceRequest`].
615619
///

lightning/src/ln/channelmanager.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7906,7 +7906,7 @@ where
79067906
///
79077907
/// The resulting invoice uses a [`PaymentHash`] recognized by the [`ChannelManager`] and a
79087908
/// [`BlindedPath`] containing the [`PaymentSecret`] needed to reconstruct the corresponding
7909-
/// [`PaymentPreimage`].
7909+
/// [`PaymentPreimage`]. It is returned purely for informational purposes.
79107910
///
79117911
/// # Limitations
79127912
///
@@ -7921,7 +7921,9 @@ where
79217921
/// path for the invoice.
79227922
///
79237923
/// [`Bolt12Invoice`]: crate::offers::invoice::Bolt12Invoice
7924-
pub fn request_refund_payment(&self, refund: &Refund) -> Result<(), Bolt12SemanticError> {
7924+
pub fn request_refund_payment(
7925+
&self, refund: &Refund
7926+
) -> Result<Bolt12Invoice, Bolt12SemanticError> {
79257927
let expanded_key = &self.inbound_payment_key;
79267928
let entropy = &*self.entropy_source;
79277929
let secp_ctx = &self.secp_ctx;
@@ -7954,7 +7956,7 @@ where
79547956
let mut pending_offers_messages = self.pending_offers_messages.lock().unwrap();
79557957
if refund.paths().is_empty() {
79567958
let message = new_pending_onion_message(
7957-
OffersMessage::Invoice(invoice),
7959+
OffersMessage::Invoice(invoice.clone()),
79587960
Destination::Node(refund.payer_id()),
79597961
Some(reply_path),
79607962
);
@@ -7970,7 +7972,7 @@ where
79707972
}
79717973
}
79727974

7973-
Ok(())
7975+
Ok(invoice)
79747976
},
79757977
Err(()) => Err(Bolt12SemanticError::InvalidAmount),
79767978
}

lightning/src/ln/offers_tests.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,7 @@ fn creates_and_pays_for_refund_using_two_hop_blinded_path() {
492492
}
493493
expect_recent_payment!(david, RecentPaymentDetails::AwaitingInvoice, payment_id);
494494

495-
alice.node.request_refund_payment(&refund).unwrap();
495+
let expected_invoice = alice.node.request_refund_payment(&refund).unwrap();
496496

497497
connect_peers(alice, charlie);
498498

@@ -503,6 +503,8 @@ fn creates_and_pays_for_refund_using_two_hop_blinded_path() {
503503
david.onion_messenger.handle_onion_message(&charlie_id, &onion_message);
504504

505505
let invoice = extract_invoice(david, &onion_message);
506+
assert_eq!(invoice, expected_invoice);
507+
506508
assert_eq!(invoice.amount_msats(), 10_000_000);
507509
assert_ne!(invoice.signing_pubkey(), alice_id);
508510
assert!(!invoice.payment_paths().is_empty());
@@ -610,12 +612,14 @@ fn creates_and_pays_for_refund_using_one_hop_blinded_path() {
610612
}
611613
expect_recent_payment!(bob, RecentPaymentDetails::AwaitingInvoice, payment_id);
612614

613-
alice.node.request_refund_payment(&refund).unwrap();
615+
let expected_invoice = alice.node.request_refund_payment(&refund).unwrap();
614616

615617
let onion_message = alice.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
616618
bob.onion_messenger.handle_onion_message(&alice_id, &onion_message);
617619

618620
let invoice = extract_invoice(bob, &onion_message);
621+
assert_eq!(invoice, expected_invoice);
622+
619623
assert_eq!(invoice.amount_msats(), 10_000_000);
620624
assert_ne!(invoice.signing_pubkey(), alice_id);
621625
assert!(!invoice.payment_paths().is_empty());
@@ -704,12 +708,14 @@ fn pays_for_refund_without_blinded_paths() {
704708
assert!(refund.paths().is_empty());
705709
expect_recent_payment!(bob, RecentPaymentDetails::AwaitingInvoice, payment_id);
706710

707-
alice.node.request_refund_payment(&refund).unwrap();
711+
let expected_invoice = alice.node.request_refund_payment(&refund).unwrap();
708712

709713
let onion_message = alice.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
710714
bob.onion_messenger.handle_onion_message(&alice_id, &onion_message);
711715

712716
let invoice = extract_invoice(bob, &onion_message);
717+
assert_eq!(invoice, expected_invoice);
718+
713719
route_bolt12_payment(bob, &[alice], &invoice);
714720
expect_recent_payment!(bob, RecentPaymentDetails::Pending, payment_id);
715721

0 commit comments

Comments
 (0)