Skip to content

Commit

Permalink
Rename PaymentId hmac creation/verification methods for offers.
Browse files Browse the repository at this point in the history
We want to specify that these methods are only to be used in an outbound offers
payment context, because we'll be adding similar methods for the outbound async
payments context in upcoming commits.
  • Loading branch information
valentinewallace committed Sep 5, 2024
1 parent b8c3674 commit 9a3e1bc
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
10 changes: 5 additions & 5 deletions lightning/src/ln/channelmanager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -427,15 +427,15 @@ impl PaymentId {
pub fn hmac_for_offer_payment(
&self, nonce: Nonce, expanded_key: &inbound_payment::ExpandedKey,
) -> Hmac<Sha256> {
signer::hmac_for_payment_id(*self, nonce, expanded_key)
signer::hmac_for_offer_payment_id(*self, nonce, expanded_key)
}

/// Authenticates the payment id using an HMAC and a [`Nonce`] taken from an
/// [`OffersContext::OutboundPayment`].
pub fn verify(
pub fn verify_for_offer_payment(
&self, hmac: Hmac<Sha256>, nonce: Nonce, expanded_key: &inbound_payment::ExpandedKey,
) -> Result<(), ()> {
signer::verify_payment_id(*self, hmac, nonce, expanded_key)
signer::verify_offer_payment_id(*self, hmac, nonce, expanded_key)
}
}

Expand Down Expand Up @@ -11052,7 +11052,7 @@ where
OffersMessage::StaticInvoice(invoice) => {
let payment_id = match context {
Some(OffersContext::OutboundPayment { payment_id, nonce, hmac: Some(hmac) }) => {
if payment_id.verify(hmac, nonce, expanded_key).is_err() {
if payment_id.verify_for_offer_payment(hmac, nonce, expanded_key).is_err() {
return None
}
payment_id
Expand All @@ -11073,7 +11073,7 @@ where

match context {
Some(OffersContext::OutboundPayment { payment_id, nonce, hmac: Some(hmac) }) => {
if let Ok(()) = payment_id.verify(hmac, nonce, expanded_key) {
if let Ok(()) = payment_id.verify_for_offer_payment(hmac, nonce, expanded_key) {
self.abandon_payment_with_reason(
payment_id, PaymentFailureReason::InvoiceRequestRejected,
);
Expand Down
10 changes: 5 additions & 5 deletions lightning/src/offers/signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const WITHOUT_ENCRYPTED_PAYMENT_ID_HMAC_INPUT: &[u8; 16] = &[3; 16];
const WITH_ENCRYPTED_PAYMENT_ID_HMAC_INPUT: &[u8; 16] = &[4; 16];

// HMAC input for a `PaymentId`. The HMAC is used in `OffersContext::OutboundPayment`.
const PAYMENT_ID_HMAC_INPUT: &[u8; 16] = &[5; 16];
const OFFER_PAYMENT_ID_HMAC_INPUT: &[u8; 16] = &[5; 16];

/// Message metadata which possibly is derived from [`MetadataMaterial`] such that it can be
/// verified.
Expand Down Expand Up @@ -395,21 +395,21 @@ fn hmac_for_message<'a>(
Ok(hmac)
}

pub(crate) fn hmac_for_payment_id(
pub(crate) fn hmac_for_offer_payment_id(
payment_id: PaymentId, nonce: Nonce, expanded_key: &ExpandedKey,
) -> Hmac<Sha256> {
const IV_BYTES: &[u8; IV_LEN] = b"LDK Payment ID ~";
let mut hmac = expanded_key.hmac_for_offer();
hmac.input(IV_BYTES);
hmac.input(&nonce.0);
hmac.input(PAYMENT_ID_HMAC_INPUT);
hmac.input(OFFER_PAYMENT_ID_HMAC_INPUT);
hmac.input(&payment_id.0);

Hmac::from_engine(hmac)
}

pub(crate) fn verify_payment_id(
pub(crate) fn verify_offer_payment_id(
payment_id: PaymentId, hmac: Hmac<Sha256>, nonce: Nonce, expanded_key: &ExpandedKey,
) -> Result<(), ()> {
if hmac_for_payment_id(payment_id, nonce, expanded_key) == hmac { Ok(()) } else { Err(()) }
if hmac_for_offer_payment_id(payment_id, nonce, expanded_key) == hmac { Ok(()) } else { Err(()) }
}

0 comments on commit 9a3e1bc

Please sign in to comment.