Skip to content

Commit 10b25f4

Browse files
committed
Move verify_bolt12_invoice to flow.rs
1 parent bfab3f9 commit 10b25f4

File tree

2 files changed

+47
-32
lines changed

2 files changed

+47
-32
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 18 additions & 22 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, 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<(), ()> {
@@ -2030,17 +2042,17 @@ where
20302042
/// ```
20312043
///
20322044
/// ## BOLT 12 Offers
2033-
///
2045+
///
20342046
/// For more information on creating offers, see [`create_offer_builder`].
20352047
///
20362048
/// For details on initiating payments for offers, see [`pay_for_offer`].
20372049
///
20382050
/// ## BOLT 12 Refunds
2039-
///
2051+
///
20402052
/// For more information on creating refunds, see [`create_refund_builder`].
20412053
///
20422054
/// For requesting refund payments, see [`request_refund_payment`].
2043-
///
2055+
///
20442056
/// # Persistence
20452057
///
20462058
/// Implements [`Writeable`] to write out all channel state to disk. Implies [`peer_disconnected`] for
@@ -9572,27 +9584,11 @@ where
95729584
.collect::<Vec<_>>()
95739585
}
95749586

9575-
fn verify_bolt12_invoice(
9576-
&self, invoice: &Bolt12Invoice, context: Option<&OffersContext>,
9577-
) -> Result<PaymentId, ()> {
9578-
let secp_ctx = &self.secp_ctx;
9579-
let expanded_key = &self.inbound_payment_key;
9580-
9581-
match context {
9582-
None if invoice.is_for_refund_without_paths() => {
9583-
invoice.verify_using_metadata(expanded_key, secp_ctx)
9584-
},
9585-
Some(&OffersContext::OutboundPayment { payment_id, nonce, .. }) => {
9586-
invoice.verify_using_payer_data(payment_id, nonce, expanded_key, secp_ctx)
9587-
},
9588-
_ => Err(()),
9589-
}
9590-
}
9591-
95929587
fn send_payment_for_verified_bolt12_invoice(&self, invoice: &Bolt12Invoice, payment_id: PaymentId) -> Result<(), Bolt12PaymentError> {
95939588
let best_block_height = self.best_block.read().unwrap().height;
9594-
let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
95959589
let features = self.bolt12_invoice_features();
9590+
let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
9591+
95969592
self.pending_outbound_payments
95979593
.send_payment_for_bolt12_invoice(
95989594
invoice, payment_id, &self.router, self.list_usable_channels(), features,

lightning/src/offers/flow.rs

Lines changed: 29 additions & 10 deletions
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,31 @@ 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+
823843
impl<ES: Deref, OMC: Deref, MR: Deref, L: Deref> OffersMessageHandler
824844
for OffersMessageFlow<ES, OMC, MR, L>
825845
where
@@ -1003,11 +1023,10 @@ where
10031023
}
10041024
},
10051025
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-
};
1026+
let payment_id = match self.verify_bolt12_invoice(&invoice, context.as_ref()) {
1027+
Ok(payment_id) => payment_id,
1028+
Err(()) => return None,
1029+
};
10111030

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

0 commit comments

Comments
 (0)