Skip to content

Commit e1c3863

Browse files
committed
Move send_payment_for_bolt12_invoice to flow.rs
1 parent 0e01bd3 commit e1c3863

File tree

5 files changed

+87
-70
lines changed

5 files changed

+87
-70
lines changed

lightning/src/events/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -857,7 +857,7 @@ pub enum Event {
857857
/// received.
858858
///
859859
/// This event will only be generated if [`UserConfig::manually_handle_bolt12_invoices`] is set.
860-
/// Use [`ChannelManager::send_payment_for_bolt12_invoice`] to pay the invoice or
860+
/// Use [`OffersMessageFlow::send_payment_for_bolt12_invoice`] to pay the invoice or
861861
/// [`ChannelManager::abandon_payment`] to abandon the associated payment. See those docs for
862862
/// further details.
863863
///
@@ -868,7 +868,7 @@ pub enum Event {
868868
/// [`InvoiceRequest`]: crate::offers::invoice_request::InvoiceRequest
869869
/// [`Refund`]: crate::offers::refund::Refund
870870
/// [`UserConfig::manually_handle_bolt12_invoices`]: crate::util::config::UserConfig::manually_handle_bolt12_invoices
871-
/// [`ChannelManager::send_payment_for_bolt12_invoice`]: crate::ln::channelmanager::ChannelManager::send_payment_for_bolt12_invoice
871+
/// [`OffersMessageFlow::send_payment_for_bolt12_invoice`]: crate::offers::flow::OffersMessageFlow::send_payment_for_bolt12_invoice
872872
/// [`ChannelManager::abandon_payment`]: crate::ln::channelmanager::ChannelManager::abandon_payment
873873
InvoiceReceived {
874874
/// The `payment_id` associated with payment for the invoice.

lightning/src/ln/channelmanager.rs

+19-57
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ use bitcoin::secp256k1::Secp256k1;
3333
use bitcoin::{secp256k1, Sequence};
3434

3535
use crate::events::FundingInfo;
36-
use crate::blinded_path::message::{AsyncPaymentsContext, MessageContext, MessageForwardNode, OffersContext};
36+
use crate::blinded_path::message::{AsyncPaymentsContext, MessageContext, MessageForwardNode};
3737
use crate::blinded_path::NodeIdLookUp;
3838
use crate::blinded_path::message::BlindedMessagePath;
3939
use crate::blinded_path::payment::{BlindedPaymentPath, PaymentConstraints, PaymentContext, ReceiveTlvs};
@@ -439,11 +439,15 @@ impl Ord for ClaimableHTLC {
439439
pub trait Verification {
440440
/// Constructs an HMAC to include in [`OffersContext`] for the data along with the given
441441
/// [`Nonce`].
442+
///
443+
/// [`OffersContext`]: crate::blinded_path::message::OffersContext
442444
fn hmac_for_offer_payment(
443445
&self, nonce: Nonce, expanded_key: &inbound_payment::ExpandedKey,
444446
) -> Hmac<Sha256>;
445447

446448
/// Authenticates the data using an HMAC and a [`Nonce`] taken from an [`OffersContext`].
449+
///
450+
/// [`OffersContext`]: crate::blinded_path::message::OffersContext
447451
fn verify_for_offer_payment(
448452
&self, hmac: Hmac<Sha256>, nonce: Nonce, expanded_key: &inbound_payment::ExpandedKey,
449453
) -> Result<(), ()>;
@@ -452,6 +456,8 @@ pub trait Verification {
452456
impl Verification for PaymentHash {
453457
/// Constructs an HMAC to include in [`OffersContext::InboundPayment`] for the payment hash
454458
/// along with the given [`Nonce`].
459+
///
460+
/// [`OffersContext::InboundPayment`]: crate::blinded_path::message::OffersContext::InboundPayment
455461
fn hmac_for_offer_payment(
456462
&self, nonce: Nonce, expanded_key: &inbound_payment::ExpandedKey,
457463
) -> Hmac<Sha256> {
@@ -460,6 +466,8 @@ impl Verification for PaymentHash {
460466

461467
/// Authenticates the payment id using an HMAC and a [`Nonce`] taken from an
462468
/// [`OffersContext::InboundPayment`].
469+
///
470+
/// [`OffersContext::InboundPayment`]: crate::blinded_path::message::OffersContext::InboundPayment
463471
fn verify_for_offer_payment(
464472
&self, hmac: Hmac<Sha256>, nonce: Nonce, expanded_key: &inbound_payment::ExpandedKey,
465473
) -> Result<(), ()> {
@@ -500,6 +508,8 @@ impl PaymentId {
500508
impl Verification for PaymentId {
501509
/// Constructs an HMAC to include in [`OffersContext::OutboundPayment`] for the payment id
502510
/// along with the given [`Nonce`].
511+
///
512+
/// [`OffersContext::OutboundPayment`]: crate::blinded_path::message::OffersContext::OutboundPayment
503513
fn hmac_for_offer_payment(
504514
&self, nonce: Nonce, expanded_key: &inbound_payment::ExpandedKey,
505515
) -> Hmac<Sha256> {
@@ -508,6 +518,8 @@ impl Verification for PaymentId {
508518

509519
/// Authenticates the payment id using an HMAC and a [`Nonce`] taken from an
510520
/// [`OffersContext::OutboundPayment`].
521+
///
522+
/// [`OffersContext::OutboundPayment`]: crate::blinded_path::message::OffersContext::OutboundPayment
511523
fn verify_for_offer_payment(
512524
&self, hmac: Hmac<Sha256>, nonce: Nonce, expanded_key: &inbound_payment::ExpandedKey,
513525
) -> Result<(), ()> {
@@ -1997,11 +2009,11 @@ where
19972009
/// For details on initiating payments for offers, see [`pay_for_offer`].
19982010
///
19992011
/// ## BOLT 12 Refunds
2000-
///
2012+
///
20012013
/// For more information on creating refunds, see [`create_refund_builder`].
20022014
///
20032015
/// For requesting refund payments, see [`request_refund_payment`].
2004-
///
2016+
///
20052017
/// # Persistence
20062018
///
20072019
/// Implements [`Writeable`] to write out all channel state to disk. Implies [`peer_disconnected`] for
@@ -2572,7 +2584,7 @@ pub enum RecentPaymentDetails {
25722584
AwaitingInvoice {
25732585
/// A user-provided identifier in [`OffersMessageFlow::pay_for_offer`] used to uniquely identify a
25742586
/// payment and ensure idempotency in LDK.
2575-
///
2587+
///
25762588
/// [`OffersMessageFlow::pay_for_offer`]: crate::offers::flow::OffersMessageFlow::pay_for_offer
25772589
payment_id: PaymentId,
25782590
},
@@ -4390,35 +4402,6 @@ where
43904402
self.pending_outbound_payments.test_set_payment_metadata(payment_id, new_payment_metadata);
43914403
}
43924404

4393-
/// Pays the [`Bolt12Invoice`] associated with the `payment_id` encoded in its `payer_metadata`.
4394-
///
4395-
/// The invoice's `payer_metadata` is used to authenticate that the invoice was indeed requested
4396-
/// before attempting a payment. [`Bolt12PaymentError::UnexpectedInvoice`] is returned if this
4397-
/// fails or if the encoded `payment_id` is not recognized. The latter may happen once the
4398-
/// payment is no longer tracked because the payment was attempted after:
4399-
/// - an invoice for the `payment_id` was already paid,
4400-
/// - one full [timer tick] has elapsed since initially requesting the invoice when paying an
4401-
/// offer, or
4402-
/// - the refund corresponding to the invoice has already expired.
4403-
///
4404-
/// To retry the payment, request another invoice using a new `payment_id`.
4405-
///
4406-
/// Attempting to pay the same invoice twice while the first payment is still pending will
4407-
/// result in a [`Bolt12PaymentError::DuplicateInvoice`].
4408-
///
4409-
/// Otherwise, either [`Event::PaymentSent`] or [`Event::PaymentFailed`] are used to indicate
4410-
/// whether or not the payment was successful.
4411-
///
4412-
/// [timer tick]: Self::timer_tick_occurred
4413-
pub fn send_payment_for_bolt12_invoice(
4414-
&self, invoice: &Bolt12Invoice, context: Option<&OffersContext>,
4415-
) -> Result<(), Bolt12PaymentError> {
4416-
match self.verify_bolt12_invoice(invoice, context) {
4417-
Ok(payment_id) => self.send_payment_for_verified_bolt12_invoice(invoice, payment_id),
4418-
Err(()) => Err(Bolt12PaymentError::UnexpectedInvoice),
4419-
}
4420-
}
4421-
44224405
#[cfg(async_payments)]
44234406
fn initiate_async_payment(
44244407
&self, invoice: &StaticInvoice, payment_id: PaymentId
@@ -9217,15 +9200,10 @@ pub trait OffersMessageCommons {
92179200
/// Get the vector of peers that can be used for a blinded path
92189201
fn get_peer_for_blinded_path(&self) -> Vec<MessageForwardNode>;
92199202

9220-
/// Verify bolt12 invoice
9221-
fn verify_bolt12_invoice(
9222-
&self, invoice: &Bolt12Invoice, context: Option<&OffersContext>,
9223-
) -> Result<PaymentId, ()>;
9224-
92259203
/// Gets the current configuration applied to all new channels.
92269204
fn get_current_default_configuration(&self) -> &UserConfig;
92279205

9228-
/// Send payment for verified bolt12 invoice
9206+
/// Send Payment for verified BOLT12 Invoice
92299207
fn send_payment_for_verified_bolt12_invoice(&self, invoice: &Bolt12Invoice, payment_id: PaymentId) -> Result<(), Bolt12PaymentError>;
92309208

92319209
/// Add new pending event
@@ -9347,31 +9325,15 @@ where
93479325
.collect::<Vec<_>>()
93489326
}
93499327

9350-
fn verify_bolt12_invoice(
9351-
&self, invoice: &Bolt12Invoice, context: Option<&OffersContext>,
9352-
) -> Result<PaymentId, ()> {
9353-
let secp_ctx = &self.secp_ctx;
9354-
let expanded_key = &self.inbound_payment_key;
9355-
9356-
match context {
9357-
None if invoice.is_for_refund_without_paths() => {
9358-
invoice.verify_using_metadata(expanded_key, secp_ctx)
9359-
},
9360-
Some(&OffersContext::OutboundPayment { payment_id, nonce, .. }) => {
9361-
invoice.verify_using_payer_data(payment_id, nonce, expanded_key, secp_ctx)
9362-
},
9363-
_ => Err(()),
9364-
}
9365-
}
9366-
93679328
fn get_current_default_configuration(&self) -> &UserConfig {
93689329
&self.default_configuration
93699330
}
93709331

93719332
fn send_payment_for_verified_bolt12_invoice(&self, invoice: &Bolt12Invoice, payment_id: PaymentId) -> Result<(), Bolt12PaymentError> {
93729333
let best_block_height = self.best_block.read().unwrap().height;
9373-
let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
93749334
let features = self.bolt12_invoice_features();
9335+
let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
9336+
93759337
self.pending_outbound_payments
93769338
.send_payment_for_bolt12_invoice(
93779339
invoice, payment_id, &self.router, self.list_usable_channels(), features,

lightning/src/ln/offers_tests.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1201,9 +1201,9 @@ fn pays_bolt12_invoice_asynchronously() {
12011201
assert_eq!(path.introduction_node(), &IntroductionNode::NodeId(alice_id));
12021202
}
12031203

1204-
assert!(bob.node.send_payment_for_bolt12_invoice(&invoice, context.as_ref()).is_ok());
1204+
assert!(bob.offers_handler.send_payment_for_bolt12_invoice(&invoice, context.as_ref()).is_ok());
12051205
assert_eq!(
1206-
bob.node.send_payment_for_bolt12_invoice(&invoice, context.as_ref()),
1206+
bob.offers_handler.send_payment_for_bolt12_invoice(&invoice, context.as_ref()),
12071207
Err(Bolt12PaymentError::DuplicateInvoice),
12081208
);
12091209

@@ -1214,7 +1214,7 @@ fn pays_bolt12_invoice_asynchronously() {
12141214
expect_recent_payment!(bob, RecentPaymentDetails::Fulfilled, payment_id);
12151215

12161216
assert_eq!(
1217-
bob.node.send_payment_for_bolt12_invoice(&invoice, context.as_ref()),
1217+
bob.offers_handler.send_payment_for_bolt12_invoice(&invoice, context.as_ref()),
12181218
Err(Bolt12PaymentError::DuplicateInvoice),
12191219
);
12201220

@@ -1223,7 +1223,7 @@ fn pays_bolt12_invoice_asynchronously() {
12231223
}
12241224

12251225
assert_eq!(
1226-
bob.node.send_payment_for_bolt12_invoice(&invoice, context.as_ref()),
1226+
bob.offers_handler.send_payment_for_bolt12_invoice(&invoice, context.as_ref()),
12271227
Err(Bolt12PaymentError::UnexpectedInvoice),
12281228
);
12291229
}

lightning/src/offers/flow.rs

+60-5
Original file line numberDiff line numberDiff line change
@@ -648,6 +648,62 @@ where
648648
}
649649
}
650650

651+
impl<ES: Deref, OMC: Deref, MR: Deref, L: Deref> OffersMessageFlow<ES, OMC, MR, L>
652+
where
653+
ES::Target: EntropySource,
654+
OMC::Target: OffersMessageCommons,
655+
MR::Target: MessageRouter,
656+
L::Target: Logger,
657+
{
658+
fn verify_bolt12_invoice(
659+
&self, invoice: &Bolt12Invoice, context: Option<&OffersContext>,
660+
) -> Result<PaymentId, ()> {
661+
let secp_ctx = &self.secp_ctx;
662+
let expanded_key = &self.commons.get_expanded_key();
663+
664+
match context {
665+
None if invoice.is_for_refund_without_paths() => {
666+
invoice.verify_using_metadata(expanded_key, secp_ctx)
667+
},
668+
Some(&OffersContext::OutboundPayment { payment_id, nonce, .. }) => {
669+
invoice.verify_using_payer_data(payment_id, nonce, expanded_key, secp_ctx)
670+
},
671+
_ => Err(()),
672+
}
673+
}
674+
675+
/// Pays the [`Bolt12Invoice`] associated with the `payment_id` encoded in its `payer_metadata`.
676+
///
677+
/// The invoice's `payer_metadata` is used to authenticate that the invoice was indeed requested
678+
/// before attempting a payment. [`Bolt12PaymentError::UnexpectedInvoice`] is returned if this
679+
/// fails or if the encoded `payment_id` is not recognized. The latter may happen once the
680+
/// payment is no longer tracked because the payment was attempted after:
681+
/// - an invoice for the `payment_id` was already paid,
682+
/// - one full [timer tick] has elapsed since initially requesting the invoice when paying an
683+
/// offer, or
684+
/// - the refund corresponding to the invoice has already expired.
685+
///
686+
/// To retry the payment, request another invoice using a new `payment_id`.
687+
///
688+
/// Attempting to pay the same invoice twice while the first payment is still pending will
689+
/// result in a [`Bolt12PaymentError::DuplicateInvoice`].
690+
///
691+
/// Otherwise, either [`Event::PaymentSent`] or [`Event::PaymentFailed`] are used to indicate
692+
/// whether or not the payment was successful.
693+
///
694+
/// [timer tick]: crate::ln::channelmanager::ChannelManager::timer_tick_occurred
695+
pub fn send_payment_for_bolt12_invoice(
696+
&self, invoice: &Bolt12Invoice, context: Option<&OffersContext>,
697+
) -> Result<(), Bolt12PaymentError> {
698+
match self.verify_bolt12_invoice(invoice, context) {
699+
Ok(payment_id) => {
700+
self.commons.send_payment_for_verified_bolt12_invoice(invoice, payment_id)
701+
},
702+
Err(()) => Err(Bolt12PaymentError::UnexpectedInvoice),
703+
}
704+
}
705+
}
706+
651707
impl<ES: Deref, OMC: Deref, MR: Deref, L: Deref> OffersMessageHandler
652708
for OffersMessageFlow<ES, OMC, MR, L>
653709
where
@@ -831,11 +887,10 @@ where
831887
}
832888
},
833889
OffersMessage::Invoice(invoice) => {
834-
let payment_id =
835-
match self.commons.verify_bolt12_invoice(&invoice, context.as_ref()) {
836-
Ok(payment_id) => payment_id,
837-
Err(()) => return None,
838-
};
890+
let payment_id = match self.verify_bolt12_invoice(&invoice, context.as_ref()) {
891+
Ok(payment_id) => payment_id,
892+
Err(()) => return None,
893+
};
839894

840895
let logger =
841896
WithContext::from(&self.logger, None, None, Some(invoice.payment_hash()));

lightning/src/util/config.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -859,14 +859,14 @@ pub struct UserConfig {
859859
///
860860
/// When set to `true`, [`Event::InvoiceReceived`] will be generated for each received
861861
/// [`Bolt12Invoice`] instead of being automatically paid after verification. Use
862-
/// [`ChannelManager::send_payment_for_bolt12_invoice`] to pay the invoice or
862+
/// [`OffersMessageFlow::send_payment_for_bolt12_invoice`] to pay the invoice or
863863
/// [`ChannelManager::abandon_payment`] to abandon the associated payment.
864864
///
865865
/// Default value: `false`
866866
///
867867
/// [`Bolt12Invoice`]: crate::offers::invoice::Bolt12Invoice
868868
/// [`Event::InvoiceReceived`]: crate::events::Event::InvoiceReceived
869-
/// [`ChannelManager::send_payment_for_bolt12_invoice`]: crate::ln::channelmanager::ChannelManager::send_payment_for_bolt12_invoice
869+
/// [`OffersMessageFlow::send_payment_for_bolt12_invoice`]: crate::offers::flow::OffersMessageFlow::send_payment_for_bolt12_invoice
870870
/// [`ChannelManager::abandon_payment`]: crate::ln::channelmanager::ChannelManager::abandon_payment
871871
pub manually_handle_bolt12_invoices: bool,
872872
}

0 commit comments

Comments
 (0)