Skip to content

Commit 3ba8ba5

Browse files
committed
Move send_payment_for_bolt12_invoice to flow.rs
1 parent 7555bb8 commit 3ba8ba5

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, ReceiveTlvs};
@@ -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<(), ()> {
@@ -504,6 +512,8 @@ impl PaymentId {
504512
impl Verification for PaymentId {
505513
/// Constructs an HMAC to include in [`OffersContext::OutboundPayment`] for the payment id
506514
/// along with the given [`Nonce`].
515+
///
516+
/// [`OffersContext::OutboundPayment`]: crate::blinded_path::message::OffersContext::OutboundPayment
507517
fn hmac_for_offer_payment(
508518
&self, nonce: Nonce, expanded_key: &inbound_payment::ExpandedKey,
509519
) -> Hmac<Sha256> {
@@ -512,6 +522,8 @@ impl Verification for PaymentId {
512522

513523
/// Authenticates the payment id using an HMAC and a [`Nonce`] taken from an
514524
/// [`OffersContext::OutboundPayment`].
525+
///
526+
/// [`OffersContext::OutboundPayment`]: crate::blinded_path::message::OffersContext::OutboundPayment
515527
fn verify_for_offer_payment(
516528
&self, hmac: Hmac<Sha256>, nonce: Nonce, expanded_key: &inbound_payment::ExpandedKey,
517529
) -> Result<(), ()> {
@@ -4470,35 +4482,6 @@ where
44704482
self.pending_outbound_payments.test_set_payment_metadata(payment_id, new_payment_metadata);
44714483
}
44724484

4473-
/// Pays the [`Bolt12Invoice`] associated with the `payment_id` encoded in its `payer_metadata`.
4474-
///
4475-
/// The invoice's `payer_metadata` is used to authenticate that the invoice was indeed requested
4476-
/// before attempting a payment. [`Bolt12PaymentError::UnexpectedInvoice`] is returned if this
4477-
/// fails or if the encoded `payment_id` is not recognized. The latter may happen once the
4478-
/// payment is no longer tracked because the payment was attempted after:
4479-
/// - an invoice for the `payment_id` was already paid,
4480-
/// - one full [timer tick] has elapsed since initially requesting the invoice when paying an
4481-
/// offer, or
4482-
/// - the refund corresponding to the invoice has already expired.
4483-
///
4484-
/// To retry the payment, request another invoice using a new `payment_id`.
4485-
///
4486-
/// Attempting to pay the same invoice twice while the first payment is still pending will
4487-
/// result in a [`Bolt12PaymentError::DuplicateInvoice`].
4488-
///
4489-
/// Otherwise, either [`Event::PaymentSent`] or [`Event::PaymentFailed`] are used to indicate
4490-
/// whether or not the payment was successful.
4491-
///
4492-
/// [timer tick]: Self::timer_tick_occurred
4493-
pub fn send_payment_for_bolt12_invoice(
4494-
&self, invoice: &Bolt12Invoice, context: Option<&OffersContext>,
4495-
) -> Result<(), Bolt12PaymentError> {
4496-
match self.verify_bolt12_invoice(invoice, context) {
4497-
Ok(payment_id) => self.send_payment_for_verified_bolt12_invoice(invoice, payment_id),
4498-
Err(()) => Err(Bolt12PaymentError::UnexpectedInvoice),
4499-
}
4500-
}
4501-
45024485
#[cfg(async_payments)]
45034486
fn send_payment_for_static_invoice(
45044487
&self, payment_id: PaymentId
@@ -9550,27 +9533,11 @@ where
95509533
.collect::<Vec<_>>()
95519534
}
95529535

9553-
fn verify_bolt12_invoice(
9554-
&self, invoice: &Bolt12Invoice, context: Option<&OffersContext>,
9555-
) -> Result<PaymentId, ()> {
9556-
let secp_ctx = &self.secp_ctx;
9557-
let expanded_key = &self.inbound_payment_key;
9558-
9559-
match context {
9560-
None if invoice.is_for_refund_without_paths() => {
9561-
invoice.verify_using_metadata(expanded_key, secp_ctx)
9562-
},
9563-
Some(&OffersContext::OutboundPayment { payment_id, nonce, .. }) => {
9564-
invoice.verify_using_payer_data(payment_id, nonce, expanded_key, secp_ctx)
9565-
},
9566-
_ => Err(()),
9567-
}
9568-
}
9569-
95709536
fn send_payment_for_verified_bolt12_invoice(&self, invoice: &Bolt12Invoice, payment_id: PaymentId) -> Result<(), Bolt12PaymentError> {
95719537
let best_block_height = self.best_block.read().unwrap().height;
9572-
let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
95739538
let features = self.bolt12_invoice_features();
9539+
let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
9540+
95749541
self.pending_outbound_payments
95759542
.send_payment_for_bolt12_invoice(
95769543
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,
@@ -810,6 +805,64 @@ where
810805
}
811806
}
812807

808+
impl<ES: Deref, OMC: Deref, MR: Deref, L: Deref> OffersMessageFlow<ES, OMC, MR, L>
809+
where
810+
ES::Target: EntropySource,
811+
OMC::Target: OffersMessageCommons,
812+
MR::Target: MessageRouter,
813+
L::Target: Logger,
814+
{
815+
fn verify_bolt12_invoice(
816+
&self, invoice: &Bolt12Invoice, context: Option<&OffersContext>,
817+
) -> Result<PaymentId, ()> {
818+
let secp_ctx = &self.secp_ctx;
819+
let expanded_key = &self.inbound_payment_key;
820+
821+
match context {
822+
None if invoice.is_for_refund_without_paths() => {
823+
invoice.verify_using_metadata(expanded_key, secp_ctx)
824+
},
825+
Some(&OffersContext::OutboundPayment { payment_id, nonce, .. }) => {
826+
invoice.verify_using_payer_data(payment_id, nonce, expanded_key, secp_ctx)
827+
},
828+
_ => Err(()),
829+
}
830+
}
831+
832+
/// Pays the [`Bolt12Invoice`] associated with the `payment_id` encoded in its `payer_metadata`.
833+
///
834+
/// The invoice's `payer_metadata` is used to authenticate that the invoice was indeed requested
835+
/// before attempting a payment. [`Bolt12PaymentError::UnexpectedInvoice`] is returned if this
836+
/// fails or if the encoded `payment_id` is not recognized. The latter may happen once the
837+
/// payment is no longer tracked because the payment was attempted after:
838+
/// - an invoice for the `payment_id` was already paid,
839+
/// - one full [timer tick] has elapsed since initially requesting the invoice when paying an
840+
/// offer, or
841+
/// - the refund corresponding to the invoice has already expired.
842+
///
843+
/// To retry the payment, request another invoice using a new `payment_id`.
844+
///
845+
/// Attempting to pay the same invoice twice while the first payment is still pending will
846+
/// result in a [`Bolt12PaymentError::DuplicateInvoice`].
847+
///
848+
/// Otherwise, either [`Event::PaymentSent`] or [`Event::PaymentFailed`] are used to indicate
849+
/// whether or not the payment was successful.
850+
///
851+
/// [`Event::PaymentSent`]: crate::events::Event::PaymentSent
852+
/// [`Event::PaymentFailed`]: crate::events::Event::PaymentFailed
853+
/// [timer tick]: crate::ln::channelmanager::ChannelManager::timer_tick_occurred
854+
pub fn send_payment_for_bolt12_invoice(
855+
&self, invoice: &Bolt12Invoice, context: Option<&OffersContext>,
856+
) -> Result<(), Bolt12PaymentError> {
857+
match self.verify_bolt12_invoice(invoice, context) {
858+
Ok(payment_id) => {
859+
self.commons.send_payment_for_verified_bolt12_invoice(invoice, payment_id)
860+
},
861+
Err(()) => Err(Bolt12PaymentError::UnexpectedInvoice),
862+
}
863+
}
864+
}
865+
813866
impl<ES: Deref, OMC: Deref, MR: Deref, L: Deref> OffersMessageHandler
814867
for OffersMessageFlow<ES, OMC, MR, L>
815868
where
@@ -993,11 +1046,10 @@ where
9931046
}
9941047
},
9951048
OffersMessage::Invoice(invoice) => {
996-
let payment_id =
997-
match self.commons.verify_bolt12_invoice(&invoice, context.as_ref()) {
998-
Ok(payment_id) => payment_id,
999-
Err(()) => return None,
1000-
};
1049+
let payment_id = match self.verify_bolt12_invoice(&invoice, context.as_ref()) {
1050+
Ok(payment_id) => payment_id,
1051+
Err(()) => return None,
1052+
};
10011053

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

0 commit comments

Comments
 (0)