Skip to content

Commit c6c9c29

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 c6c9c29

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

lightning/src/ln/channelmanager.rs

+19-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,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(
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(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 let Ok(()) = 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
);

0 commit comments

Comments
 (0)