Skip to content

Commit 8f5dc5e

Browse files
add the bolt12 invoice to the PaymentSend event
To enable proof of payment, we need to share the bolt12 invoice with the library user. This is already possible if we `manually_handle_bolt12_invoices`, but this approach requires a significant amount of work from the user. This commit adds the bolt12 invoice to the PaymentSend event when the payment is completed. This allows the user to always have the option to perform proof of payment. Link: lightningdevkit#3344 Signed-off-by: Vincenzo Palazzo <[email protected]>
1 parent 4d83694 commit 8f5dc5e

File tree

4 files changed

+27
-17
lines changed

4 files changed

+27
-17
lines changed

lightning/src/events/mod.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -939,6 +939,8 @@ pub enum Event {
939939
///
940940
/// [`Route::get_total_fees`]: crate::routing::router::Route::get_total_fees
941941
fee_paid_msat: Option<u64>,
942+
/// The bolt12 invoice that was paid. `None` if the payment was a non-bolt12 payment.
943+
bolt12_invoice: Option<Bolt12Invoice>,
942944
},
943945
/// Indicates an outbound payment failed. Individual [`Event::PaymentPathFailed`] events
944946
/// provide failure information for each path attempt in the payment, including retries.
@@ -1190,12 +1192,12 @@ pub enum Event {
11901192
/// events generated or serialized by versions prior to 0.0.122.
11911193
next_user_channel_id: Option<u128>,
11921194
/// The node id of the previous node.
1193-
///
1195+
///
11941196
/// This is only `None` for HTLCs received prior to 0.1 or for events serialized by
11951197
/// versions prior to 0.1
11961198
prev_node_id: Option<PublicKey>,
11971199
/// The node id of the next node.
1198-
///
1200+
///
11991201
/// This is only `None` for HTLCs received prior to 0.1 or for events serialized by
12001202
/// versions prior to 0.1
12011203
next_node_id: Option<PublicKey>,
@@ -1546,13 +1548,14 @@ impl Writeable for Event {
15461548
(13, payment_id, option),
15471549
});
15481550
},
1549-
&Event::PaymentSent { ref payment_id, ref payment_preimage, ref payment_hash, ref fee_paid_msat } => {
1551+
&Event::PaymentSent { ref payment_id, ref payment_preimage, ref payment_hash, ref fee_paid_msat, ref bolt12_invoice } => {
15501552
2u8.write(writer)?;
15511553
write_tlv_fields!(writer, {
15521554
(0, payment_preimage, required),
15531555
(1, payment_hash, required),
15541556
(3, payment_id, option),
15551557
(5, fee_paid_msat, option),
1558+
(7, bolt12_invoice, option),
15561559
});
15571560
},
15581561
&Event::PaymentPathFailed {
@@ -1886,11 +1889,13 @@ impl MaybeReadable for Event {
18861889
let mut payment_hash = None;
18871890
let mut payment_id = None;
18881891
let mut fee_paid_msat = None;
1892+
let mut bolt12_invoice = None;
18891893
read_tlv_fields!(reader, {
18901894
(0, payment_preimage, required),
18911895
(1, payment_hash, option),
18921896
(3, payment_id, option),
18931897
(5, fee_paid_msat, option),
1898+
(7, bolt12_invoice, option),
18941899
});
18951900
if payment_hash.is_none() {
18961901
payment_hash = Some(PaymentHash(Sha256::hash(&payment_preimage.0[..]).to_byte_array()));
@@ -1900,6 +1905,7 @@ impl MaybeReadable for Event {
19001905
payment_preimage,
19011906
payment_hash: payment_hash.unwrap(),
19021907
fee_paid_msat,
1908+
bolt12_invoice,
19031909
}))
19041910
};
19051911
f()

lightning/src/ln/functional_test_utils.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ use crate::util::test_channel_signer::SignerOp;
3737
use crate::util::test_utils;
3838
use crate::util::test_utils::{TestChainMonitor, TestScorer, TestKeysInterface};
3939
use crate::util::ser::{ReadableArgs, Writeable};
40+
use crate::offers::invoice::Bolt12Invoice;
4041

4142
use bitcoin::WPubkeyHash;
4243
use bitcoin::amount::Amount;
@@ -3093,10 +3094,11 @@ pub fn claim_payment_along_route(args: ClaimAlongRouteArgs) {
30933094
}
30943095
}
30953096

3096-
pub fn claim_payment<'a, 'b, 'c>(origin_node: &Node<'a, 'b, 'c>, expected_route: &[&Node<'a, 'b, 'c>], our_payment_preimage: PaymentPreimage) {
3097+
pub fn claim_payment<'a, 'b, 'c>(origin_node: &Node<'a, 'b, 'c>, expected_route: &[&Node<'a, 'b, 'c>], our_payment_preimage: PaymentPreimage) -> Option<Bolt12Invoice> {
30973098
claim_payment_along_route(
30983099
ClaimAlongRouteArgs::new(origin_node, &[expected_route], our_payment_preimage)
30993100
);
3101+
None
31003102
}
31013103

31023104
pub const TEST_FINAL_CLTV: u32 = 70;

lightning/src/ln/offers_tests.rs

+14-13
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ fn route_bolt12_payment<'a, 'b, 'c>(
168168
}
169169

170170
fn claim_bolt12_payment<'a, 'b, 'c>(
171-
node: &Node<'a, 'b, 'c>, path: &[&Node<'a, 'b, 'c>], expected_payment_context: PaymentContext
171+
node: &Node<'a, 'b, 'c>, path: &[&Node<'a, 'b, 'c>], expected_payment_context: PaymentContext, invoice: &Bolt12Invoice
172172
) {
173173
let recipient = &path[path.len() - 1];
174174
let payment_purpose = match get_event!(recipient, Event::PaymentClaimable) {
@@ -188,7 +188,9 @@ fn claim_bolt12_payment<'a, 'b, 'c>(
188188
},
189189
_ => panic!("Unexpected payment purpose: {:?}", payment_purpose),
190190
}
191-
claim_payment(node, path, payment_preimage);
191+
if let Some(inv) = claim_payment(node, path, payment_preimage) {
192+
assert_eq!(inv, invoice.to_owned());
193+
};
192194
}
193195

194196
fn extract_offer_nonce<'a, 'b, 'c>(node: &Node<'a, 'b, 'c>, message: &OnionMessage) -> Nonce {
@@ -597,7 +599,7 @@ fn creates_and_pays_for_offer_using_two_hop_blinded_path() {
597599
route_bolt12_payment(david, &[charlie, bob, alice], &invoice);
598600
expect_recent_payment!(david, RecentPaymentDetails::Pending, payment_id);
599601

600-
claim_bolt12_payment(david, &[charlie, bob, alice], payment_context);
602+
claim_bolt12_payment(david, &[charlie, bob, alice], payment_context, &invoice);
601603
expect_recent_payment!(david, RecentPaymentDetails::Fulfilled, payment_id);
602604
}
603605

@@ -680,7 +682,7 @@ fn creates_and_pays_for_refund_using_two_hop_blinded_path() {
680682
route_bolt12_payment(david, &[charlie, bob, alice], &invoice);
681683
expect_recent_payment!(david, RecentPaymentDetails::Pending, payment_id);
682684

683-
claim_bolt12_payment(david, &[charlie, bob, alice], payment_context);
685+
claim_bolt12_payment(david, &[charlie, bob, alice], payment_context, &invoice);
684686
expect_recent_payment!(david, RecentPaymentDetails::Fulfilled, payment_id);
685687
}
686688

@@ -747,7 +749,7 @@ fn creates_and_pays_for_offer_using_one_hop_blinded_path() {
747749
route_bolt12_payment(bob, &[alice], &invoice);
748750
expect_recent_payment!(bob, RecentPaymentDetails::Pending, payment_id);
749751

750-
claim_bolt12_payment(bob, &[alice], payment_context);
752+
claim_bolt12_payment(bob, &[alice], payment_context, &invoice);
751753
expect_recent_payment!(bob, RecentPaymentDetails::Fulfilled, payment_id);
752754
}
753755

@@ -803,7 +805,7 @@ fn creates_and_pays_for_refund_using_one_hop_blinded_path() {
803805
route_bolt12_payment(bob, &[alice], &invoice);
804806
expect_recent_payment!(bob, RecentPaymentDetails::Pending, payment_id);
805807

806-
claim_bolt12_payment(bob, &[alice], payment_context);
808+
claim_bolt12_payment(bob, &[alice], payment_context, &invoice);
807809
expect_recent_payment!(bob, RecentPaymentDetails::Fulfilled, payment_id);
808810
}
809811

@@ -857,7 +859,7 @@ fn pays_for_offer_without_blinded_paths() {
857859
route_bolt12_payment(bob, &[alice], &invoice);
858860
expect_recent_payment!(bob, RecentPaymentDetails::Pending, payment_id);
859861

860-
claim_bolt12_payment(bob, &[alice], payment_context);
862+
claim_bolt12_payment(bob, &[alice], payment_context, &invoice);
861863
expect_recent_payment!(bob, RecentPaymentDetails::Fulfilled, payment_id);
862864
}
863865

@@ -900,7 +902,7 @@ fn pays_for_refund_without_blinded_paths() {
900902
route_bolt12_payment(bob, &[alice], &invoice);
901903
expect_recent_payment!(bob, RecentPaymentDetails::Pending, payment_id);
902904

903-
claim_bolt12_payment(bob, &[alice], payment_context);
905+
claim_bolt12_payment(bob, &[alice], payment_context, &invoice);
904906
expect_recent_payment!(bob, RecentPaymentDetails::Fulfilled, payment_id);
905907
}
906908

@@ -1138,7 +1140,7 @@ fn creates_and_pays_for_offer_with_retry() {
11381140
}
11391141
route_bolt12_payment(bob, &[alice], &invoice);
11401142
expect_recent_payment!(bob, RecentPaymentDetails::Pending, payment_id);
1141-
claim_bolt12_payment(bob, &[alice], payment_context);
1143+
claim_bolt12_payment(bob, &[alice], payment_context, &invoice);
11421144
expect_recent_payment!(bob, RecentPaymentDetails::Fulfilled, payment_id);
11431145
}
11441146

@@ -1209,7 +1211,7 @@ fn pays_bolt12_invoice_asynchronously() {
12091211
route_bolt12_payment(bob, &[alice], &invoice);
12101212
expect_recent_payment!(bob, RecentPaymentDetails::Pending, payment_id);
12111213

1212-
claim_bolt12_payment(bob, &[alice], payment_context);
1214+
claim_bolt12_payment(bob, &[alice], payment_context, &invoice);
12131215
expect_recent_payment!(bob, RecentPaymentDetails::Fulfilled, payment_id);
12141216

12151217
assert_eq!(
@@ -1289,7 +1291,7 @@ fn creates_offer_with_blinded_path_using_unannounced_introduction_node() {
12891291
route_bolt12_payment(bob, &[alice], &invoice);
12901292
expect_recent_payment!(bob, RecentPaymentDetails::Pending, payment_id);
12911293

1292-
claim_bolt12_payment(bob, &[alice], payment_context);
1294+
claim_bolt12_payment(bob, &[alice], payment_context, &invoice);
12931295
expect_recent_payment!(bob, RecentPaymentDetails::Fulfilled, payment_id);
12941296
}
12951297

@@ -2145,7 +2147,7 @@ fn fails_paying_invoice_more_than_once() {
21452147
assert!(david.node.get_and_clear_pending_msg_events().is_empty());
21462148

21472149
// Complete paying the first invoice
2148-
claim_bolt12_payment(david, &[charlie, bob, alice], payment_context);
2150+
claim_bolt12_payment(david, &[charlie, bob, alice], payment_context, &invoice1);
21492151
expect_recent_payment!(david, RecentPaymentDetails::Fulfilled, payment_id);
21502152
}
21512153

@@ -2405,4 +2407,3 @@ fn no_double_pay_with_stale_channelmanager() {
24052407
// generated in response to the duplicate invoice.
24062408
assert!(nodes[0].node.get_and_clear_pending_events().is_empty());
24072409
}
2408-

lightning/src/ln/outbound_payment.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1947,6 +1947,7 @@ impl OutboundPayments {
19471947
payment_preimage,
19481948
payment_hash,
19491949
fee_paid_msat,
1950+
bolt12_invoice: payment.get().bolt12_invoice().cloned(),
19501951
}, Some(ev_completion_action.clone())));
19511952
payment.get_mut().mark_fulfilled();
19521953
}

0 commit comments

Comments
 (0)