Skip to content

Commit ccce9d9

Browse files
Merge pull request #3242 from jkczyz/2024-08-payment-id-auth-api
Add `PaymentId` authentication to public API
2 parents 43dcf2f + 5bee20e commit ccce9d9

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use bitcoin::key::constants::SECRET_KEY_SIZE;
2424
use bitcoin::network::Network;
2525

2626
use bitcoin::hashes::Hash;
27+
use bitcoin::hashes::hmac::Hmac;
2728
use bitcoin::hashes::sha256::Hash as Sha256;
2829
use bitcoin::hash_types::{BlockHash, Txid};
2930

@@ -413,6 +414,22 @@ pub struct PaymentId(pub [u8; Self::LENGTH]);
413414
impl PaymentId {
414415
/// Number of bytes in the id.
415416
pub const LENGTH: usize = 32;
417+
418+
/// Constructs an HMAC to include in [`OffersContext::OutboundPayment`] for the payment id
419+
/// along with the given [`Nonce`].
420+
pub fn hmac_for_offer_payment(
421+
&self, nonce: Nonce, expanded_key: &inbound_payment::ExpandedKey,
422+
) -> Hmac<Sha256> {
423+
signer::hmac_for_payment_id(*self, nonce, expanded_key)
424+
}
425+
426+
/// Authenticates the payment id using an HMAC and a [`Nonce`] taken from an
427+
/// [`OffersContext::OutboundPayment`].
428+
pub fn verify(
429+
&self, hmac: Hmac<Sha256>, nonce: Nonce, expanded_key: &inbound_payment::ExpandedKey,
430+
) -> Result<(), ()> {
431+
signer::verify_payment_id(*self, hmac, nonce, expanded_key)
432+
}
416433
}
417434

418435
impl Writeable for PaymentId {
@@ -9024,7 +9041,7 @@ where
90249041
};
90259042
let invoice_request = builder.build_and_sign()?;
90269043

9027-
let hmac = signer::hmac_for_payment_id(payment_id, nonce, expanded_key);
9044+
let hmac = payment_id.hmac_for_offer_payment(nonce, expanded_key);
90289045
let context = OffersContext::OutboundPayment { payment_id, nonce, hmac: Some(hmac) };
90299046
let reply_paths = self.create_blinded_paths(context)
90309047
.map_err(|_| Bolt12SemanticError::MissingPaths)?;
@@ -10900,7 +10917,7 @@ where
1090010917

1090110918
match context {
1090210919
Some(OffersContext::OutboundPayment { payment_id, nonce, hmac: Some(hmac) }) => {
10903-
if signer::verify_payment_id(payment_id, hmac, nonce, expanded_key) {
10920+
if let Ok(()) = payment_id.verify(hmac, nonce, expanded_key) {
1090410921
self.abandon_payment_with_reason(
1090510922
payment_id, PaymentFailureReason::InvoiceRequestRejected,
1090610923
);

lightning/src/offers/signer.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,6 @@ pub(crate) fn hmac_for_payment_id(
410410

411411
pub(crate) fn verify_payment_id(
412412
payment_id: PaymentId, hmac: Hmac<Sha256>, nonce: Nonce, expanded_key: &ExpandedKey,
413-
) -> bool {
414-
hmac_for_payment_id(payment_id, nonce, expanded_key) == hmac
413+
) -> Result<(), ()> {
414+
if hmac_for_payment_id(payment_id, nonce, expanded_key) == hmac { Ok(()) } else { Err(()) }
415415
}

0 commit comments

Comments
 (0)