Skip to content

Commit 9513673

Browse files
committed
Add HMAC, and nonce to OffersContext::InboundPayment
Introduce HMAC and nonce calculation when sending Invoice with reply path, so that if we receive InvoiceError back for the corresponding Invoice we can verify the payment hash before logging it.
1 parent 669d7b3 commit 9513673

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

lightning/src/blinded_path/message.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,20 @@ pub enum OffersContext {
347347
///
348348
/// [`Bolt12Invoice::payment_hash`]: crate::offers::invoice::Bolt12Invoice::payment_hash
349349
payment_hash: PaymentHash,
350+
351+
/// A nonce used for authenticating that a [`Bolt12Invoice`] is for a valid [`Refund`] or
352+
/// [`InvoiceRequest`] and for deriving their signing keys.
353+
///
354+
/// [`Bolt12Invoice`]: crate::offers::invoice::Bolt12Invoice
355+
/// [`Refund`]: crate::offers::refund::Refund
356+
/// [`InvoiceRequest`]: crate::offers::invoice_request::InvoiceRequest
357+
nonce: Nonce,
358+
359+
/// Authentication code for the [`PaymentId`], which should be checked when the context is
360+
/// used with an [`InvoiceError`].
361+
///
362+
/// [`InvoiceError`]: crate::offers::invoice_error::InvoiceError
363+
hmac: Hmac<Sha256>,
350364
},
351365
}
352366

@@ -366,6 +380,8 @@ impl_writeable_tlv_based_enum!(OffersContext,
366380
},
367381
(2, InboundPayment) => {
368382
(0, payment_hash, required),
383+
(1, nonce, required),
384+
(2, hmac, required)
369385
},
370386
);
371387

lightning/src/ln/channelmanager.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ use crate::offers::nonce::Nonce;
7070
use crate::offers::offer::{Offer, OfferBuilder};
7171
use crate::offers::parse::Bolt12SemanticError;
7272
use crate::offers::refund::{Refund, RefundBuilder};
73-
use crate::offers::signer;
73+
use crate::offers::signer::{self, hmac_for_payment_hash};
7474
use crate::onion_message::async_payments::{AsyncPaymentsMessage, HeldHtlcAvailable, ReleaseHeldHtlc, AsyncPaymentsMessageHandler};
7575
use crate::onion_message::messenger::{Destination, MessageRouter, Responder, ResponseInstruction, MessageSendInstructions};
7676
use crate::onion_message::offers::{OffersMessage, OffersMessageHandler};
@@ -9226,8 +9226,10 @@ where
92269226
let builder: InvoiceBuilder<DerivedSigningPubkey> = builder.into();
92279227
let invoice = builder.allow_mpp().build_and_sign(secp_ctx)?;
92289228

9229+
let nonce = Nonce::from_entropy_source(entropy);
9230+
let hmac = hmac_for_payment_hash(invoice.payment_hash(), nonce, expanded_key);
92299231
let context = OffersContext::InboundPayment {
9230-
payment_hash: invoice.payment_hash(),
9232+
payment_hash: invoice.payment_hash(), nonce, hmac
92319233
};
92329234
let reply_paths = self.create_blinded_paths(context)
92339235
.map_err(|_| Bolt12SemanticError::MissingPaths)?;
@@ -10987,7 +10989,12 @@ where
1098710989
},
1098810990
OffersMessage::InvoiceError(invoice_error) => {
1098910991
let payment_hash = match context {
10990-
Some(OffersContext::InboundPayment { payment_hash }) => Some(payment_hash),
10992+
Some(OffersContext::InboundPayment { payment_hash, nonce, hmac }) => {
10993+
match signer::verify_payment_hash(payment_hash, hmac, nonce, expanded_key) {
10994+
Ok(_) => Some(payment_hash),
10995+
Err(_) => None,
10996+
}
10997+
},
1099110998
_ => None,
1099210999
};
1099311000

0 commit comments

Comments
 (0)