Skip to content

Commit 4818efe

Browse files
committed
Move send_payment_for_bolt12_invoice to flow.rs
1 parent defec29 commit 4818efe

File tree

2 files changed

+77
-58
lines changed

2 files changed

+77
-58
lines changed

lightning/src/ln/channelmanager.rs

+15-48
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ use bitcoin::secp256k1::Secp256k1;
3333
use bitcoin::{secp256k1, Sequence, Weight};
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, UnauthenticatedReceiveTlvs};
@@ -443,11 +443,15 @@ impl Ord for ClaimableHTLC {
443443
pub trait Verification {
444444
/// Constructs an HMAC to include in [`OffersContext`] for the data along with the given
445445
/// [`Nonce`].
446+
///
447+
/// [`OffersContext`]: crate::blinded_path::message::OffersContext
446448
fn hmac_for_offer_payment(
447449
&self, nonce: Nonce, expanded_key: &inbound_payment::ExpandedKey,
448450
) -> Hmac<Sha256>;
449451

450452
/// Authenticates the data using an HMAC and a [`Nonce`] taken from an [`OffersContext`].
453+
///
454+
/// [`OffersContext`]: crate::blinded_path::message::OffersContext
451455
fn verify_for_offer_payment(
452456
&self, hmac: Hmac<Sha256>, nonce: Nonce, expanded_key: &inbound_payment::ExpandedKey,
453457
) -> Result<(), ()>;
@@ -456,6 +460,8 @@ pub trait Verification {
456460
impl Verification for PaymentHash {
457461
/// Constructs an HMAC to include in [`OffersContext::InboundPayment`] for the payment hash
458462
/// along with the given [`Nonce`].
463+
///
464+
/// [`OffersContext::InboundPayment`]: crate::blinded_path::message::OffersContext::InboundPayment
459465
fn hmac_for_offer_payment(
460466
&self, nonce: Nonce, expanded_key: &inbound_payment::ExpandedKey,
461467
) -> Hmac<Sha256> {
@@ -464,6 +470,8 @@ impl Verification for PaymentHash {
464470

465471
/// Authenticates the payment id using an HMAC and a [`Nonce`] taken from an
466472
/// [`OffersContext::InboundPayment`].
473+
///
474+
/// [`OffersContext::InboundPayment`]: crate::blinded_path::message::OffersContext::InboundPayment
467475
fn verify_for_offer_payment(
468476
&self, hmac: Hmac<Sha256>, nonce: Nonce, expanded_key: &inbound_payment::ExpandedKey,
469477
) -> Result<(), ()> {
@@ -518,6 +526,8 @@ impl PaymentId {
518526
impl Verification for PaymentId {
519527
/// Constructs an HMAC to include in [`OffersContext::OutboundPayment`] for the payment id
520528
/// along with the given [`Nonce`].
529+
///
530+
/// [`OffersContext::OutboundPayment`]: crate::blinded_path::message::OffersContext::OutboundPayment
521531
fn hmac_for_offer_payment(
522532
&self, nonce: Nonce, expanded_key: &inbound_payment::ExpandedKey,
523533
) -> Hmac<Sha256> {
@@ -526,6 +536,8 @@ impl Verification for PaymentId {
526536

527537
/// Authenticates the payment id using an HMAC and a [`Nonce`] taken from an
528538
/// [`OffersContext::OutboundPayment`].
539+
///
540+
/// [`OffersContext::OutboundPayment`]: crate::blinded_path::message::OffersContext::OutboundPayment
529541
fn verify_for_offer_payment(
530542
&self, hmac: Hmac<Sha256>, nonce: Nonce, expanded_key: &inbound_payment::ExpandedKey,
531543
) -> Result<(), ()> {
@@ -4486,35 +4498,6 @@ where
44864498
self.pending_outbound_payments.test_set_payment_metadata(payment_id, new_payment_metadata);
44874499
}
44884500

4489-
/// Pays the [`Bolt12Invoice`] associated with the `payment_id` encoded in its `payer_metadata`.
4490-
///
4491-
/// The invoice's `payer_metadata` is used to authenticate that the invoice was indeed requested
4492-
/// before attempting a payment. [`Bolt12PaymentError::UnexpectedInvoice`] is returned if this
4493-
/// fails or if the encoded `payment_id` is not recognized. The latter may happen once the
4494-
/// payment is no longer tracked because the payment was attempted after:
4495-
/// - an invoice for the `payment_id` was already paid,
4496-
/// - one full [timer tick] has elapsed since initially requesting the invoice when paying an
4497-
/// offer, or
4498-
/// - the refund corresponding to the invoice has already expired.
4499-
///
4500-
/// To retry the payment, request another invoice using a new `payment_id`.
4501-
///
4502-
/// Attempting to pay the same invoice twice while the first payment is still pending will
4503-
/// result in a [`Bolt12PaymentError::DuplicateInvoice`].
4504-
///
4505-
/// Otherwise, either [`Event::PaymentSent`] or [`Event::PaymentFailed`] are used to indicate
4506-
/// whether or not the payment was successful.
4507-
///
4508-
/// [timer tick]: Self::timer_tick_occurred
4509-
pub fn send_payment_for_bolt12_invoice(
4510-
&self, invoice: &Bolt12Invoice, context: Option<&OffersContext>,
4511-
) -> Result<(), Bolt12PaymentError> {
4512-
match self.verify_bolt12_invoice(invoice, context) {
4513-
Ok(payment_id) => self.send_payment_for_verified_bolt12_invoice(invoice, payment_id),
4514-
Err(()) => Err(Bolt12PaymentError::UnexpectedInvoice),
4515-
}
4516-
}
4517-
45184501
#[cfg(async_payments)]
45194502
fn send_payment_for_static_invoice(
45204503
&self, payment_id: PaymentId
@@ -9601,27 +9584,11 @@ where
96019584
.collect::<Vec<_>>()
96029585
}
96039586

9604-
fn verify_bolt12_invoice(
9605-
&self, invoice: &Bolt12Invoice, context: Option<&OffersContext>,
9606-
) -> Result<PaymentId, ()> {
9607-
let secp_ctx = &self.secp_ctx;
9608-
let expanded_key = &self.inbound_payment_key;
9609-
9610-
match context {
9611-
None if invoice.is_for_refund_without_paths() => {
9612-
invoice.verify_using_metadata(expanded_key, secp_ctx)
9613-
},
9614-
Some(&OffersContext::OutboundPayment { payment_id, nonce, .. }) => {
9615-
invoice.verify_using_payer_data(payment_id, nonce, expanded_key, secp_ctx)
9616-
},
9617-
_ => Err(()),
9618-
}
9619-
}
9620-
96219587
fn send_payment_for_verified_bolt12_invoice(&self, invoice: &Bolt12Invoice, payment_id: PaymentId) -> Result<(), Bolt12PaymentError> {
96229588
let best_block_height = self.best_block.read().unwrap().height;
9623-
let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
96249589
let features = self.bolt12_invoice_features();
9590+
let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
9591+
96259592
self.pending_outbound_payments
96269593
.send_payment_for_bolt12_invoice(
96279594
invoice, payment_id, &self.router, self.list_usable_channels(), features,

lightning/src/offers/flow.rs

+62-10
Original file line numberDiff line numberDiff line change
@@ -140,11 +140,6 @@ pub trait OffersMessageCommons {
140140
/// Get the vector of peers that can be used for a blinded path
141141
fn get_peer_for_blinded_path(&self) -> Vec<MessageForwardNode>;
142142

143-
/// Verify bolt12 invoice
144-
fn verify_bolt12_invoice(
145-
&self, invoice: &Bolt12Invoice, context: Option<&OffersContext>,
146-
) -> Result<PaymentId, ()>;
147-
148143
/// Send payment for verified bolt12 invoice
149144
fn send_payment_for_verified_bolt12_invoice(
150145
&self, invoice: &Bolt12Invoice, payment_id: PaymentId,
@@ -820,6 +815,64 @@ where
820815
}
821816
}
822817

818+
impl<ES: Deref, OMC: Deref, MR: Deref, L: Deref> OffersMessageFlow<ES, OMC, MR, L>
819+
where
820+
ES::Target: EntropySource,
821+
OMC::Target: OffersMessageCommons,
822+
MR::Target: MessageRouter,
823+
L::Target: Logger,
824+
{
825+
fn verify_bolt12_invoice(
826+
&self, invoice: &Bolt12Invoice, context: Option<&OffersContext>,
827+
) -> Result<PaymentId, ()> {
828+
let secp_ctx = &self.secp_ctx;
829+
let expanded_key = &self.inbound_payment_key;
830+
831+
match context {
832+
None if invoice.is_for_refund_without_paths() => {
833+
invoice.verify_using_metadata(expanded_key, secp_ctx)
834+
},
835+
Some(&OffersContext::OutboundPayment { payment_id, nonce, .. }) => {
836+
invoice.verify_using_payer_data(payment_id, nonce, expanded_key, secp_ctx)
837+
},
838+
_ => Err(()),
839+
}
840+
}
841+
842+
/// Pays the [`Bolt12Invoice`] associated with the `payment_id` encoded in its `payer_metadata`.
843+
///
844+
/// The invoice's `payer_metadata` is used to authenticate that the invoice was indeed requested
845+
/// before attempting a payment. [`Bolt12PaymentError::UnexpectedInvoice`] is returned if this
846+
/// fails or if the encoded `payment_id` is not recognized. The latter may happen once the
847+
/// payment is no longer tracked because the payment was attempted after:
848+
/// - an invoice for the `payment_id` was already paid,
849+
/// - one full [timer tick] has elapsed since initially requesting the invoice when paying an
850+
/// offer, or
851+
/// - the refund corresponding to the invoice has already expired.
852+
///
853+
/// To retry the payment, request another invoice using a new `payment_id`.
854+
///
855+
/// Attempting to pay the same invoice twice while the first payment is still pending will
856+
/// result in a [`Bolt12PaymentError::DuplicateInvoice`].
857+
///
858+
/// Otherwise, either [`Event::PaymentSent`] or [`Event::PaymentFailed`] are used to indicate
859+
/// whether or not the payment was successful.
860+
///
861+
/// [`Event::PaymentSent`]: crate::events::Event::PaymentSent
862+
/// [`Event::PaymentFailed`]: crate::events::Event::PaymentFailed
863+
/// [timer tick]: crate::ln::channelmanager::ChannelManager::timer_tick_occurred
864+
pub fn send_payment_for_bolt12_invoice(
865+
&self, invoice: &Bolt12Invoice, context: Option<&OffersContext>,
866+
) -> Result<(), Bolt12PaymentError> {
867+
match self.verify_bolt12_invoice(invoice, context) {
868+
Ok(payment_id) => {
869+
self.commons.send_payment_for_verified_bolt12_invoice(invoice, payment_id)
870+
},
871+
Err(()) => Err(Bolt12PaymentError::UnexpectedInvoice),
872+
}
873+
}
874+
}
875+
823876
impl<ES: Deref, OMC: Deref, MR: Deref, L: Deref> OffersMessageHandler
824877
for OffersMessageFlow<ES, OMC, MR, L>
825878
where
@@ -1003,11 +1056,10 @@ where
10031056
}
10041057
},
10051058
OffersMessage::Invoice(invoice) => {
1006-
let payment_id =
1007-
match self.commons.verify_bolt12_invoice(&invoice, context.as_ref()) {
1008-
Ok(payment_id) => payment_id,
1009-
Err(()) => return None,
1010-
};
1059+
let payment_id = match self.verify_bolt12_invoice(&invoice, context.as_ref()) {
1060+
Ok(payment_id) => payment_id,
1061+
Err(()) => return None,
1062+
};
10111063

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

0 commit comments

Comments
 (0)