Skip to content

Commit

Permalink
Move verify_bolt12_invoice to flow.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
shaavan committed Dec 17, 2024
1 parent bfab3f9 commit 10b25f4
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 32 deletions.
40 changes: 18 additions & 22 deletions lightning/src/ln/channelmanager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ use bitcoin::secp256k1::Secp256k1;
use bitcoin::{secp256k1, Sequence, Weight};

use crate::events::FundingInfo;
use crate::blinded_path::message::{AsyncPaymentsContext, MessageContext, MessageForwardNode, OffersContext};
use crate::blinded_path::message::{AsyncPaymentsContext, MessageContext, MessageForwardNode};
use crate::blinded_path::NodeIdLookUp;
use crate::blinded_path::message::BlindedMessagePath;
use crate::blinded_path::payment::{BlindedPaymentPath, PaymentConstraints, PaymentContext, UnauthenticatedReceiveTlvs};
Expand Down Expand Up @@ -443,11 +443,15 @@ impl Ord for ClaimableHTLC {
pub trait Verification {
/// Constructs an HMAC to include in [`OffersContext`] for the data along with the given
/// [`Nonce`].
///
/// [`OffersContext`]: crate::blinded_path::message::OffersContext
fn hmac_for_offer_payment(
&self, nonce: Nonce, expanded_key: &inbound_payment::ExpandedKey,
) -> Hmac<Sha256>;

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

/// Authenticates the payment id using an HMAC and a [`Nonce`] taken from an
/// [`OffersContext::InboundPayment`].
///
/// [`OffersContext::InboundPayment`]: crate::blinded_path::message::OffersContext::InboundPayment
fn verify_for_offer_payment(
&self, hmac: Hmac<Sha256>, nonce: Nonce, expanded_key: &inbound_payment::ExpandedKey,
) -> Result<(), ()> {
Expand Down Expand Up @@ -518,6 +526,8 @@ impl PaymentId {
impl Verification for PaymentId {
/// Constructs an HMAC to include in [`OffersContext::OutboundPayment`] for the payment id
/// along with the given [`Nonce`].
///
/// [`OffersContext::OutboundPayment`]: crate::blinded_path::message::OffersContext::OutboundPayment
fn hmac_for_offer_payment(
&self, nonce: Nonce, expanded_key: &inbound_payment::ExpandedKey,
) -> Hmac<Sha256> {
Expand All @@ -526,6 +536,8 @@ impl Verification for PaymentId {

/// Authenticates the payment id using an HMAC and a [`Nonce`] taken from an
/// [`OffersContext::OutboundPayment`].
///
/// [`OffersContext::OutboundPayment`]: crate::blinded_path::message::OffersContext::OutboundPayment
fn verify_for_offer_payment(
&self, hmac: Hmac<Sha256>, nonce: Nonce, expanded_key: &inbound_payment::ExpandedKey,
) -> Result<(), ()> {
Expand Down Expand Up @@ -2030,17 +2042,17 @@ where
/// ```
///
/// ## BOLT 12 Offers
///
///
/// For more information on creating offers, see [`create_offer_builder`].
///
/// For details on initiating payments for offers, see [`pay_for_offer`].
///
/// ## BOLT 12 Refunds
///
///
/// For more information on creating refunds, see [`create_refund_builder`].
///
/// For requesting refund payments, see [`request_refund_payment`].
///
///
/// # Persistence
///
/// Implements [`Writeable`] to write out all channel state to disk. Implies [`peer_disconnected`] for
Expand Down Expand Up @@ -9572,27 +9584,11 @@ where
.collect::<Vec<_>>()
}

fn verify_bolt12_invoice(
&self, invoice: &Bolt12Invoice, context: Option<&OffersContext>,
) -> Result<PaymentId, ()> {
let secp_ctx = &self.secp_ctx;
let expanded_key = &self.inbound_payment_key;

match context {
None if invoice.is_for_refund_without_paths() => {
invoice.verify_using_metadata(expanded_key, secp_ctx)
},
Some(&OffersContext::OutboundPayment { payment_id, nonce, .. }) => {
invoice.verify_using_payer_data(payment_id, nonce, expanded_key, secp_ctx)
},
_ => Err(()),
}
}

fn send_payment_for_verified_bolt12_invoice(&self, invoice: &Bolt12Invoice, payment_id: PaymentId) -> Result<(), Bolt12PaymentError> {
let best_block_height = self.best_block.read().unwrap().height;
let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
let features = self.bolt12_invoice_features();
let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);

self.pending_outbound_payments
.send_payment_for_bolt12_invoice(
invoice, payment_id, &self.router, self.list_usable_channels(), features,
Expand Down
39 changes: 29 additions & 10 deletions lightning/src/offers/flow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,6 @@ pub trait OffersMessageCommons {
/// Get the vector of peers that can be used for a blinded path
fn get_peer_for_blinded_path(&self) -> Vec<MessageForwardNode>;

/// Verify bolt12 invoice
fn verify_bolt12_invoice(
&self, invoice: &Bolt12Invoice, context: Option<&OffersContext>,
) -> Result<PaymentId, ()>;

/// Send payment for verified bolt12 invoice
fn send_payment_for_verified_bolt12_invoice(
&self, invoice: &Bolt12Invoice, payment_id: PaymentId,
Expand Down Expand Up @@ -820,6 +815,31 @@ where
}
}

impl<ES: Deref, OMC: Deref, MR: Deref, L: Deref> OffersMessageFlow<ES, OMC, MR, L>
where
ES::Target: EntropySource,
OMC::Target: OffersMessageCommons,
MR::Target: MessageRouter,
L::Target: Logger,
{
fn verify_bolt12_invoice(
&self, invoice: &Bolt12Invoice, context: Option<&OffersContext>,
) -> Result<PaymentId, ()> {
let secp_ctx = &self.secp_ctx;
let expanded_key = &self.inbound_payment_key;

match context {
None if invoice.is_for_refund_without_paths() => {
invoice.verify_using_metadata(expanded_key, secp_ctx)
},
Some(&OffersContext::OutboundPayment { payment_id, nonce, .. }) => {
invoice.verify_using_payer_data(payment_id, nonce, expanded_key, secp_ctx)
},
_ => Err(()),
}
}
}

impl<ES: Deref, OMC: Deref, MR: Deref, L: Deref> OffersMessageHandler
for OffersMessageFlow<ES, OMC, MR, L>
where
Expand Down Expand Up @@ -1003,11 +1023,10 @@ where
}
},
OffersMessage::Invoice(invoice) => {
let payment_id =
match self.commons.verify_bolt12_invoice(&invoice, context.as_ref()) {
Ok(payment_id) => payment_id,
Err(()) => return None,
};
let payment_id = match self.verify_bolt12_invoice(&invoice, context.as_ref()) {
Ok(payment_id) => payment_id,
Err(()) => return None,
};

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

0 comments on commit 10b25f4

Please sign in to comment.