Skip to content

Commit c07e7f6

Browse files
committed
Move send_payment_for_bolt12_invoice to flow.rs
1 parent 5c7e348 commit c07e7f6

File tree

2 files changed

+77
-58
lines changed

2 files changed

+77
-58
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 15 additions & 48 deletions
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};
@@ -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<(), ()> {
@@ -4382,35 +4394,6 @@ where
43824394
self.pending_outbound_payments.test_set_payment_metadata(payment_id, new_payment_metadata);
43834395
}
43844396

4385-
/// Pays the [`Bolt12Invoice`] associated with the `payment_id` encoded in its `payer_metadata`.
4386-
///
4387-
/// The invoice's `payer_metadata` is used to authenticate that the invoice was indeed requested
4388-
/// before attempting a payment. [`Bolt12PaymentError::UnexpectedInvoice`] is returned if this
4389-
/// fails or if the encoded `payment_id` is not recognized. The latter may happen once the
4390-
/// payment is no longer tracked because the payment was attempted after:
4391-
/// - an invoice for the `payment_id` was already paid,
4392-
/// - one full [timer tick] has elapsed since initially requesting the invoice when paying an
4393-
/// offer, or
4394-
/// - the refund corresponding to the invoice has already expired.
4395-
///
4396-
/// To retry the payment, request another invoice using a new `payment_id`.
4397-
///
4398-
/// Attempting to pay the same invoice twice while the first payment is still pending will
4399-
/// result in a [`Bolt12PaymentError::DuplicateInvoice`].
4400-
///
4401-
/// Otherwise, either [`Event::PaymentSent`] or [`Event::PaymentFailed`] are used to indicate
4402-
/// whether or not the payment was successful.
4403-
///
4404-
/// [timer tick]: Self::timer_tick_occurred
4405-
pub fn send_payment_for_bolt12_invoice(
4406-
&self, invoice: &Bolt12Invoice, context: Option<&OffersContext>,
4407-
) -> Result<(), Bolt12PaymentError> {
4408-
match self.verify_bolt12_invoice(invoice, context) {
4409-
Ok(payment_id) => self.send_payment_for_verified_bolt12_invoice(invoice, payment_id),
4410-
Err(()) => Err(Bolt12PaymentError::UnexpectedInvoice),
4411-
}
4412-
}
4413-
44144397
#[cfg(async_payments)]
44154398
fn initiate_async_payment(
44164399
&self, invoice: &StaticInvoice, payment_id: PaymentId
@@ -9537,27 +9520,11 @@ where
95379520
.collect::<Vec<_>>()
95389521
}
95399522

9540-
fn verify_bolt12_invoice(
9541-
&self, invoice: &Bolt12Invoice, context: Option<&OffersContext>,
9542-
) -> Result<PaymentId, ()> {
9543-
let secp_ctx = &self.secp_ctx;
9544-
let expanded_key = &self.inbound_payment_key;
9545-
9546-
match context {
9547-
None if invoice.is_for_refund_without_paths() => {
9548-
invoice.verify_using_metadata(expanded_key, secp_ctx)
9549-
},
9550-
Some(&OffersContext::OutboundPayment { payment_id, nonce, .. }) => {
9551-
invoice.verify_using_payer_data(payment_id, nonce, expanded_key, secp_ctx)
9552-
},
9553-
_ => Err(()),
9554-
}
9555-
}
9556-
95579523
fn send_payment_for_verified_bolt12_invoice(&self, invoice: &Bolt12Invoice, payment_id: PaymentId) -> Result<(), Bolt12PaymentError> {
95589524
let best_block_height = self.best_block.read().unwrap().height;
9559-
let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
95609525
let features = self.bolt12_invoice_features();
9526+
let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
9527+
95619528
self.pending_outbound_payments
95629529
.send_payment_for_bolt12_invoice(
95639530
invoice, payment_id, &self.router, self.list_usable_channels(), features,

lightning/src/offers/flow.rs

Lines changed: 62 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -134,11 +134,6 @@ pub trait OffersMessageCommons {
134134
/// Get the vector of peers that can be used for a blinded path
135135
fn get_peer_for_blinded_path(&self) -> Vec<MessageForwardNode>;
136136

137-
/// Verify bolt12 invoice
138-
fn verify_bolt12_invoice(
139-
&self, invoice: &Bolt12Invoice, context: Option<&OffersContext>,
140-
) -> Result<PaymentId, ()>;
141-
142137
/// Send payment for verified bolt12 invoice
143138
fn send_payment_for_verified_bolt12_invoice(
144139
&self, invoice: &Bolt12Invoice, payment_id: PaymentId,
@@ -794,6 +789,64 @@ where
794789
}
795790
}
796791

792+
impl<ES: Deref, OMC: Deref, MR: Deref, L: Deref> OffersMessageFlow<ES, OMC, MR, L>
793+
where
794+
ES::Target: EntropySource,
795+
OMC::Target: OffersMessageCommons,
796+
MR::Target: MessageRouter,
797+
L::Target: Logger,
798+
{
799+
fn verify_bolt12_invoice(
800+
&self, invoice: &Bolt12Invoice, context: Option<&OffersContext>,
801+
) -> Result<PaymentId, ()> {
802+
let secp_ctx = &self.secp_ctx;
803+
let expanded_key = &self.inbound_payment_key;
804+
805+
match context {
806+
None if invoice.is_for_refund_without_paths() => {
807+
invoice.verify_using_metadata(expanded_key, secp_ctx)
808+
},
809+
Some(&OffersContext::OutboundPayment { payment_id, nonce, .. }) => {
810+
invoice.verify_using_payer_data(payment_id, nonce, expanded_key, secp_ctx)
811+
},
812+
_ => Err(()),
813+
}
814+
}
815+
816+
/// Pays the [`Bolt12Invoice`] associated with the `payment_id` encoded in its `payer_metadata`.
817+
///
818+
/// The invoice's `payer_metadata` is used to authenticate that the invoice was indeed requested
819+
/// before attempting a payment. [`Bolt12PaymentError::UnexpectedInvoice`] is returned if this
820+
/// fails or if the encoded `payment_id` is not recognized. The latter may happen once the
821+
/// payment is no longer tracked because the payment was attempted after:
822+
/// - an invoice for the `payment_id` was already paid,
823+
/// - one full [timer tick] has elapsed since initially requesting the invoice when paying an
824+
/// offer, or
825+
/// - the refund corresponding to the invoice has already expired.
826+
///
827+
/// To retry the payment, request another invoice using a new `payment_id`.
828+
///
829+
/// Attempting to pay the same invoice twice while the first payment is still pending will
830+
/// result in a [`Bolt12PaymentError::DuplicateInvoice`].
831+
///
832+
/// Otherwise, either [`Event::PaymentSent`] or [`Event::PaymentFailed`] are used to indicate
833+
/// whether or not the payment was successful.
834+
///
835+
/// [`Event::PaymentSent`]: crate::events::Event::PaymentSent
836+
/// [`Event::PaymentFailed`]: crate::events::Event::PaymentFailed
837+
/// [timer tick]: crate::ln::channelmanager::ChannelManager::timer_tick_occurred
838+
pub fn send_payment_for_bolt12_invoice(
839+
&self, invoice: &Bolt12Invoice, context: Option<&OffersContext>,
840+
) -> Result<(), Bolt12PaymentError> {
841+
match self.verify_bolt12_invoice(invoice, context) {
842+
Ok(payment_id) => {
843+
self.commons.send_payment_for_verified_bolt12_invoice(invoice, payment_id)
844+
},
845+
Err(()) => Err(Bolt12PaymentError::UnexpectedInvoice),
846+
}
847+
}
848+
}
849+
797850
impl<ES: Deref, OMC: Deref, MR: Deref, L: Deref> OffersMessageHandler
798851
for OffersMessageFlow<ES, OMC, MR, L>
799852
where
@@ -977,11 +1030,10 @@ where
9771030
}
9781031
},
9791032
OffersMessage::Invoice(invoice) => {
980-
let payment_id =
981-
match self.commons.verify_bolt12_invoice(&invoice, context.as_ref()) {
982-
Ok(payment_id) => payment_id,
983-
Err(()) => return None,
984-
};
1033+
let payment_id = match self.verify_bolt12_invoice(&invoice, context.as_ref()) {
1034+
Ok(payment_id) => payment_id,
1035+
Err(()) => return None,
1036+
};
9851037

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

0 commit comments

Comments
 (0)