Skip to content

Commit 0c52ea2

Browse files
committed
Use BOLT 12 PaymentPurpose variants
When constructing a PaymentPurpose in ChannelManager, use the PaymentContext from OnionPayload to determine which variant to construct, including those for BOLT 12 payments.
1 parent 54ca54d commit 0c52ea2

File tree

3 files changed

+57
-46
lines changed

3 files changed

+57
-46
lines changed

lightning/src/events/mod.rs

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,34 @@ impl PaymentPurpose {
135135
PaymentPurpose::SpontaneousPayment(..) => true,
136136
}
137137
}
138+
139+
pub(crate) fn from_parts(
140+
payment_preimage: Option<PaymentPreimage>, payment_secret: PaymentSecret,
141+
payment_context: Option<PaymentContext>,
142+
) -> Self {
143+
match payment_context {
144+
Some(PaymentContext::Unknown(_)) | None => {
145+
PaymentPurpose::Bolt11InvoicePayment {
146+
payment_preimage,
147+
payment_secret,
148+
}
149+
},
150+
Some(PaymentContext::Bolt12Offer(context)) => {
151+
PaymentPurpose::Bolt12OfferPayment {
152+
payment_preimage,
153+
payment_secret,
154+
payment_context: context,
155+
}
156+
},
157+
Some(PaymentContext::Bolt12Refund(context)) => {
158+
PaymentPurpose::Bolt12RefundPayment {
159+
payment_preimage,
160+
payment_secret,
161+
payment_context: context,
162+
}
163+
},
164+
}
165+
}
138166
}
139167

140168
impl_writeable_tlv_based_enum!(PaymentPurpose,
@@ -1408,28 +1436,7 @@ impl MaybeReadable for Event {
14081436
(11, payment_context, option),
14091437
});
14101438
let purpose = match payment_secret {
1411-
Some(secret) => match payment_context {
1412-
Some(PaymentContext::Unknown(_)) | None => {
1413-
PaymentPurpose::Bolt11InvoicePayment {
1414-
payment_preimage,
1415-
payment_secret: secret,
1416-
}
1417-
},
1418-
Some(PaymentContext::Bolt12Offer(context)) => {
1419-
PaymentPurpose::Bolt12OfferPayment {
1420-
payment_preimage,
1421-
payment_secret: secret,
1422-
payment_context: context,
1423-
}
1424-
},
1425-
Some(PaymentContext::Bolt12Refund(context)) => {
1426-
PaymentPurpose::Bolt12RefundPayment {
1427-
payment_preimage,
1428-
payment_secret: secret,
1429-
payment_context: context,
1430-
}
1431-
},
1432-
},
1439+
Some(secret) => PaymentPurpose::from_parts(payment_preimage, secret, payment_context),
14331440
None if payment_preimage.is_some() => PaymentPurpose::SpontaneousPayment(payment_preimage.unwrap()),
14341441
None => return Err(msgs::DecodeError::InvalidValue),
14351442
};

lightning/src/ln/channelmanager.rs

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1566,11 +1566,11 @@ where
15661566
/// // On the event processing thread
15671567
/// channel_manager.process_pending_events(&|event| match event {
15681568
/// Event::PaymentClaimable { payment_hash, purpose, .. } => match purpose {
1569-
/// PaymentPurpose::Bolt11InvoicePayment { payment_preimage: Some(payment_preimage), .. } => {
1569+
/// PaymentPurpose::Bolt12OfferPayment { payment_preimage: Some(payment_preimage), .. } => {
15701570
/// println!("Claiming payment {}", payment_hash);
15711571
/// channel_manager.claim_funds(payment_preimage);
15721572
/// },
1573-
/// PaymentPurpose::Bolt11InvoicePayment { payment_preimage: None, .. } => {
1573+
/// PaymentPurpose::Bolt12OfferPayment { payment_preimage: None, .. } => {
15741574
/// println!("Unknown payment hash: {}", payment_hash);
15751575
/// },
15761576
/// // ...
@@ -1718,12 +1718,12 @@ where
17181718
/// // On the event processing thread
17191719
/// channel_manager.process_pending_events(&|event| match event {
17201720
/// Event::PaymentClaimable { payment_hash, purpose, .. } => match purpose {
1721-
/// PaymentPurpose::Bolt11InvoicePayment { payment_preimage: Some(payment_preimage), .. } => {
1721+
/// PaymentPurpose::Bolt12RefundPayment { payment_preimage: Some(payment_preimage), .. } => {
17221722
/// assert_eq!(payment_hash, known_payment_hash);
17231723
/// println!("Claiming payment {}", payment_hash);
17241724
/// channel_manager.claim_funds(payment_preimage);
17251725
/// },
1726-
/// PaymentPurpose::Bolt11InvoicePayment { payment_preimage: None, .. } => {
1726+
/// PaymentPurpose::Bolt12RefundPayment { payment_preimage: None, .. } => {
17271727
/// println!("Unknown payment hash: {}", payment_hash);
17281728
/// },
17291729
/// // ...
@@ -5530,7 +5530,7 @@ where
55305530
match payment_secrets.entry(payment_hash) {
55315531
hash_map::Entry::Vacant(_) => {
55325532
match claimable_htlc.onion_payload {
5533-
OnionPayload::Invoice { .. } => {
5533+
OnionPayload::Invoice { ref payment_context, .. } => {
55345534
let payment_data = payment_data.unwrap();
55355535
let (payment_preimage, min_final_cltv_expiry_delta) = match inbound_payment::verify(payment_hash, &payment_data, self.highest_seen_timestamp.load(Ordering::Acquire) as u64, &self.inbound_payment_key, &self.logger) {
55365536
Ok(result) => result,
@@ -5547,10 +5547,11 @@ where
55475547
fail_htlc!(claimable_htlc, payment_hash);
55485548
}
55495549
}
5550-
let purpose = events::PaymentPurpose::Bolt11InvoicePayment {
5551-
payment_preimage: payment_preimage.clone(),
5552-
payment_secret: payment_data.payment_secret,
5553-
};
5550+
let purpose = events::PaymentPurpose::from_parts(
5551+
payment_preimage.clone(),
5552+
payment_data.payment_secret,
5553+
payment_context.clone(),
5554+
);
55545555
check_total_value!(purpose);
55555556
},
55565557
OnionPayload::Spontaneous(preimage) => {
@@ -5560,10 +5561,13 @@ where
55605561
}
55615562
},
55625563
hash_map::Entry::Occupied(inbound_payment) => {
5563-
if let OnionPayload::Spontaneous(_) = claimable_htlc.onion_payload {
5564-
log_trace!(self.logger, "Failing new keysend HTLC with payment_hash {} because we already have an inbound payment with the same payment hash", &payment_hash);
5565-
fail_htlc!(claimable_htlc, payment_hash);
5566-
}
5564+
let payment_context = match claimable_htlc.onion_payload {
5565+
OnionPayload::Spontaneous(_) => {
5566+
log_trace!(self.logger, "Failing new keysend HTLC with payment_hash {} because we already have an inbound payment with the same payment hash", &payment_hash);
5567+
fail_htlc!(claimable_htlc, payment_hash);
5568+
},
5569+
OnionPayload::Invoice { ref payment_context, .. } => payment_context,
5570+
};
55675571
let payment_data = payment_data.unwrap();
55685572
if inbound_payment.get().payment_secret != payment_data.payment_secret {
55695573
log_trace!(self.logger, "Failing new HTLC with payment_hash {} as it didn't match our expected payment secret.", &payment_hash);
@@ -5573,10 +5577,11 @@ where
55735577
&payment_hash, payment_data.total_msat, inbound_payment.get().min_value_msat.unwrap());
55745578
fail_htlc!(claimable_htlc, payment_hash);
55755579
} else {
5576-
let purpose = events::PaymentPurpose::Bolt11InvoicePayment {
5577-
payment_preimage: inbound_payment.get().payment_preimage,
5578-
payment_secret: payment_data.payment_secret,
5579-
};
5580+
let purpose = events::PaymentPurpose::from_parts(
5581+
inbound_payment.get().payment_preimage,
5582+
payment_data.payment_secret,
5583+
payment_context.clone(),
5584+
);
55805585
let payment_claimable_generated = check_total_value!(purpose);
55815586
if payment_claimable_generated {
55825587
inbound_payment.remove_entry();

lightning/src/ln/offers_tests.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
use bitcoin::network::constants::Network;
4444
use core::time::Duration;
4545
use crate::blinded_path::{BlindedPath, IntroductionNode};
46-
use crate::events::{Event, MessageSendEventsProvider, PaymentPurpose};
46+
use crate::events::{Event, MessageSendEventsProvider};
4747
use crate::ln::channelmanager::{PaymentId, RecentPaymentDetails, Retry, self};
4848
use crate::ln::functional_test_utils::*;
4949
use crate::ln::msgs::{ChannelMessageHandler, Init, NodeAnnouncement, OnionMessage, OnionMessageHandler, RoutingMessageHandler, SocketAddress, UnsignedGossipMessage, UnsignedNodeAnnouncement};
@@ -154,12 +154,11 @@ fn route_bolt12_payment<'a, 'b, 'c>(
154154
fn claim_bolt12_payment<'a, 'b, 'c>(node: &Node<'a, 'b, 'c>, path: &[&Node<'a, 'b, 'c>]) {
155155
let recipient = &path[path.len() - 1];
156156
match get_event!(recipient, Event::PaymentClaimable) {
157-
Event::PaymentClaimable {
158-
purpose: PaymentPurpose::Bolt11InvoicePayment {
159-
payment_preimage: Some(payment_preimage), ..
160-
}, ..
161-
} => claim_payment(node, path, payment_preimage),
162-
_ => panic!(),
157+
Event::PaymentClaimable { purpose, .. } => match purpose.preimage() {
158+
Some(payment_preimage) => claim_payment(node, path, payment_preimage),
159+
None => panic!("No preimage in Event::PaymentClaimable"),
160+
},
161+
_ => panic!("No Event::PaymentClaimable"),
163162
};
164163
}
165164

0 commit comments

Comments
 (0)