Skip to content

Commit 47be9fe

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 e2ee325 commit 47be9fe

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};
@@ -9192,8 +9192,10 @@ where
91929192
let builder: InvoiceBuilder<DerivedSigningPubkey> = builder.into();
91939193
let invoice = builder.allow_mpp().build_and_sign(secp_ctx)?;
91949194

9195+
let nonce = Nonce::from_entropy_source(entropy);
9196+
let hmac = hmac_for_payment_hash(invoice.payment_hash(), nonce, expanded_key);
91959197
let context = OffersContext::InboundPayment {
9196-
payment_hash: invoice.payment_hash(),
9198+
payment_hash: invoice.payment_hash(), nonce, hmac
91979199
};
91989200
let reply_paths = self.create_blinded_paths(context)
91999201
.map_err(|_| Bolt12SemanticError::MissingPaths)?;
@@ -10953,7 +10955,12 @@ where
1095310955
},
1095410956
OffersMessage::InvoiceError(invoice_error) => {
1095510957
let payment_hash = match context {
10956-
Some(OffersContext::InboundPayment { payment_hash }) => Some(payment_hash),
10958+
Some(OffersContext::InboundPayment { payment_hash, nonce, hmac }) => {
10959+
match signer::verify_payment_hash(payment_hash, hmac, nonce, expanded_key) {
10960+
Ok(_) => Some(payment_hash),
10961+
Err(_) => None,
10962+
}
10963+
},
1095710964
_ => None,
1095810965
};
1095910966

0 commit comments

Comments
 (0)