Skip to content

Commit d5c244a

Browse files
committed
Move send_payment_for_bolt12_invoice to flow.rs
1 parent e15d10a commit d5c244a

File tree

5 files changed

+84
-67
lines changed

5 files changed

+84
-67
lines changed

lightning/src/events/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -871,7 +871,7 @@ pub enum Event {
871871
/// received.
872872
///
873873
/// This event will only be generated if [`UserConfig::manually_handle_bolt12_invoices`] is set.
874-
/// Use [`ChannelManager::send_payment_for_bolt12_invoice`] to pay the invoice or
874+
/// Use [`OffersMessageFlow::send_payment_for_bolt12_invoice`] to pay the invoice or
875875
/// [`ChannelManager::abandon_payment`] to abandon the associated payment. See those docs for
876876
/// further details.
877877
///
@@ -882,7 +882,7 @@ pub enum Event {
882882
/// [`InvoiceRequest`]: crate::offers::invoice_request::InvoiceRequest
883883
/// [`Refund`]: crate::offers::refund::Refund
884884
/// [`UserConfig::manually_handle_bolt12_invoices`]: crate::util::config::UserConfig::manually_handle_bolt12_invoices
885-
/// [`ChannelManager::send_payment_for_bolt12_invoice`]: crate::ln::channelmanager::ChannelManager::send_payment_for_bolt12_invoice
885+
/// [`OffersMessageFlow::send_payment_for_bolt12_invoice`]: crate::offers::flow::OffersMessageFlow::send_payment_for_bolt12_invoice
886886
/// [`ChannelManager::abandon_payment`]: crate::ln::channelmanager::ChannelManager::abandon_payment
887887
InvoiceReceived {
888888
/// The `payment_id` associated with payment for the invoice.

lightning/src/ln/channelmanager.rs

+16-54
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};
@@ -438,11 +438,15 @@ impl Ord for ClaimableHTLC {
438438
pub trait Verification {
439439
/// Constructs an HMAC to include in [`OffersContext`] for the data along with the given
440440
/// [`Nonce`].
441+
///
442+
/// [`OffersContext`]: crate::blinded_path::message::OffersContext
441443
fn hmac_for_offer_payment(
442444
&self, nonce: Nonce, expanded_key: &inbound_payment::ExpandedKey,
443445
) -> Hmac<Sha256>;
444446

445447
/// Authenticates the data using an HMAC and a [`Nonce`] taken from an [`OffersContext`].
448+
///
449+
/// [`OffersContext`]: crate::blinded_path::message::OffersContext
446450
fn verify_for_offer_payment(
447451
&self, hmac: Hmac<Sha256>, nonce: Nonce, expanded_key: &inbound_payment::ExpandedKey,
448452
) -> Result<(), ()>;
@@ -451,6 +455,8 @@ pub trait Verification {
451455
impl Verification for PaymentHash {
452456
/// Constructs an HMAC to include in [`OffersContext::InboundPayment`] for the payment hash
453457
/// along with the given [`Nonce`].
458+
///
459+
/// [`OffersContext::InboundPayment`]: crate::blinded_path::message::OffersContext::InboundPayment
454460
fn hmac_for_offer_payment(
455461
&self, nonce: Nonce, expanded_key: &inbound_payment::ExpandedKey,
456462
) -> Hmac<Sha256> {
@@ -459,6 +465,8 @@ impl Verification for PaymentHash {
459465

460466
/// Authenticates the payment id using an HMAC and a [`Nonce`] taken from an
461467
/// [`OffersContext::InboundPayment`].
468+
///
469+
/// [`OffersContext::InboundPayment`]: crate::blinded_path::message::OffersContext::InboundPayment
462470
fn verify_for_offer_payment(
463471
&self, hmac: Hmac<Sha256>, nonce: Nonce, expanded_key: &inbound_payment::ExpandedKey,
464472
) -> Result<(), ()> {
@@ -499,6 +507,8 @@ impl PaymentId {
499507
impl Verification for PaymentId {
500508
/// Constructs an HMAC to include in [`OffersContext::OutboundPayment`] for the payment id
501509
/// along with the given [`Nonce`].
510+
///
511+
/// [`OffersContext::OutboundPayment`]: crate::blinded_path::message::OffersContext::OutboundPayment
502512
fn hmac_for_offer_payment(
503513
&self, nonce: Nonce, expanded_key: &inbound_payment::ExpandedKey,
504514
) -> Hmac<Sha256> {
@@ -507,6 +517,8 @@ impl Verification for PaymentId {
507517

508518
/// Authenticates the payment id using an HMAC and a [`Nonce`] taken from an
509519
/// [`OffersContext::OutboundPayment`].
520+
///
521+
/// [`OffersContext::OutboundPayment`]: crate::blinded_path::message::OffersContext::OutboundPayment
510522
fn verify_for_offer_payment(
511523
&self, hmac: Hmac<Sha256>, nonce: Nonce, expanded_key: &inbound_payment::ExpandedKey,
512524
) -> Result<(), ()> {
@@ -4376,35 +4388,6 @@ where
43764388
self.pending_outbound_payments.test_set_payment_metadata(payment_id, new_payment_metadata);
43774389
}
43784390

4379-
/// Pays the [`Bolt12Invoice`] associated with the `payment_id` encoded in its `payer_metadata`.
4380-
///
4381-
/// The invoice's `payer_metadata` is used to authenticate that the invoice was indeed requested
4382-
/// before attempting a payment. [`Bolt12PaymentError::UnexpectedInvoice`] is returned if this
4383-
/// fails or if the encoded `payment_id` is not recognized. The latter may happen once the
4384-
/// payment is no longer tracked because the payment was attempted after:
4385-
/// - an invoice for the `payment_id` was already paid,
4386-
/// - one full [timer tick] has elapsed since initially requesting the invoice when paying an
4387-
/// offer, or
4388-
/// - the refund corresponding to the invoice has already expired.
4389-
///
4390-
/// To retry the payment, request another invoice using a new `payment_id`.
4391-
///
4392-
/// Attempting to pay the same invoice twice while the first payment is still pending will
4393-
/// result in a [`Bolt12PaymentError::DuplicateInvoice`].
4394-
///
4395-
/// Otherwise, either [`Event::PaymentSent`] or [`Event::PaymentFailed`] are used to indicate
4396-
/// whether or not the payment was successful.
4397-
///
4398-
/// [timer tick]: Self::timer_tick_occurred
4399-
pub fn send_payment_for_bolt12_invoice(
4400-
&self, invoice: &Bolt12Invoice, context: Option<&OffersContext>,
4401-
) -> Result<(), Bolt12PaymentError> {
4402-
match self.verify_bolt12_invoice(invoice, context) {
4403-
Ok(payment_id) => self.send_payment_for_verified_bolt12_invoice(invoice, payment_id),
4404-
Err(()) => Err(Bolt12PaymentError::UnexpectedInvoice),
4405-
}
4406-
}
4407-
44084391
#[cfg(async_payments)]
44094392
fn initiate_async_payment(
44104393
&self, invoice: &StaticInvoice, payment_id: PaymentId
@@ -9528,15 +9511,10 @@ pub trait OffersMessageCommons {
95289511
/// Get the vector of peers that can be used for a blinded path
95299512
fn get_peer_for_blinded_path(&self) -> Vec<MessageForwardNode>;
95309513

9531-
/// Verify bolt12 invoice
9532-
fn verify_bolt12_invoice(
9533-
&self, invoice: &Bolt12Invoice, context: Option<&OffersContext>,
9534-
) -> Result<PaymentId, ()>;
9535-
95369514
/// Gets the current configuration applied to all new channels.
95379515
fn get_current_default_configuration(&self) -> &UserConfig;
95389516

9539-
/// Send payment for verified bolt12 invoice
9517+
/// Send Payment for verified BOLT12 Invoice
95409518
fn send_payment_for_verified_bolt12_invoice(&self, invoice: &Bolt12Invoice, payment_id: PaymentId) -> Result<(), Bolt12PaymentError>;
95419519

95429520
/// Add new pending event
@@ -9658,31 +9636,15 @@ where
96589636
.collect::<Vec<_>>()
96599637
}
96609638

9661-
fn verify_bolt12_invoice(
9662-
&self, invoice: &Bolt12Invoice, context: Option<&OffersContext>,
9663-
) -> Result<PaymentId, ()> {
9664-
let secp_ctx = &self.secp_ctx;
9665-
let expanded_key = &self.inbound_payment_key;
9666-
9667-
match context {
9668-
None if invoice.is_for_refund_without_paths() => {
9669-
invoice.verify_using_metadata(expanded_key, secp_ctx)
9670-
},
9671-
Some(&OffersContext::OutboundPayment { payment_id, nonce, .. }) => {
9672-
invoice.verify_using_payer_data(payment_id, nonce, expanded_key, secp_ctx)
9673-
},
9674-
_ => Err(()),
9675-
}
9676-
}
9677-
96789639
fn get_current_default_configuration(&self) -> &UserConfig {
96799640
&self.default_configuration
96809641
}
96819642

96829643
fn send_payment_for_verified_bolt12_invoice(&self, invoice: &Bolt12Invoice, payment_id: PaymentId) -> Result<(), Bolt12PaymentError> {
96839644
let best_block_height = self.best_block.read().unwrap().height;
9684-
let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
96859645
let features = self.bolt12_invoice_features();
9646+
let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
9647+
96869648
self.pending_outbound_payments
96879649
.send_payment_for_bolt12_invoice(
96889650
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
@@ -650,6 +650,62 @@ where
650650
}
651651
}
652652

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

842897
let logger =
843898
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)