Skip to content

Commit aba273a

Browse files
committed
Add PaymentId authentication to public API
When receiving an InvoiceError message, it should be authenticated before using it to abandon the payment. Add methods to PaymentId's public API for constructing and verifying an HMAC for use in OffersContext::OutboundPayment. This allows other implementations of OffersMessageHandler to construct the HMAC and authenticate the message.
1 parent fa6c058 commit aba273a

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

lightning/src/ln/channelmanager.rs

+20-2
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,23 @@ 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+
419+
/// Constructs an HMAC to include in [`OffersContext::OutboundPayment`] for the payment id
420+
/// along with the given [`Nonce`].
421+
pub fn hmac_for_offer(
422+
&self, nonce: Nonce, expanded_key: &inbound_payment::ExpandedKey,
423+
) -> Hmac<Sha256> {
424+
signer::hmac_for_payment_id(*self, nonce, expanded_key)
425+
}
426+
427+
/// Authenticates the payment id using an HMAC and a [`Nonce`] taken from an
428+
/// [`OffersContext::OutboundPayment`].
429+
pub fn verify(
430+
&self, hmac: Hmac<Sha256>, nonce: Nonce, expanded_key: &inbound_payment::ExpandedKey,
431+
) -> Result<(), ()> {
432+
signer::verify_payment_id(*self, hmac, nonce, expanded_key)
433+
}
416434
}
417435

418436
impl Writeable for PaymentId {
@@ -9024,7 +9042,7 @@ where
90249042
};
90259043
let invoice_request = builder.build_and_sign()?;
90269044

9027-
let hmac = signer::hmac_for_payment_id(payment_id, nonce, expanded_key);
9045+
let hmac = payment_id.hmac_for_offer(nonce, expanded_key);
90289046
let context = OffersContext::OutboundPayment { payment_id, nonce, hmac: Some(hmac) };
90299047
let reply_paths = self.create_blinded_paths(context)
90309048
.map_err(|_| Bolt12SemanticError::MissingPaths)?;
@@ -10900,7 +10918,7 @@ where
1090010918

1090110919
match context {
1090210920
Some(OffersContext::OutboundPayment { payment_id, nonce, hmac: Some(hmac) }) => {
10903-
if let Ok(()) = signer::verify_payment_id(payment_id, hmac, nonce, expanded_key) {
10921+
if let Ok(()) = payment_id.verify(hmac, nonce, expanded_key) {
1090410922
self.abandon_payment_with_reason(
1090510923
payment_id, PaymentFailureReason::InvoiceRequestRejected,
1090610924
);

0 commit comments

Comments
 (0)