Skip to content

Commit 8cd50d8

Browse files
committed
fixup! Add new payment type and metadata bytes
Return delta from verify and remove helper
1 parent 60333c8 commit 8cd50d8

File tree

2 files changed

+8
-28
lines changed

2 files changed

+8
-28
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3182,16 +3182,15 @@ where
31823182
match claimable_htlc.onion_payload {
31833183
OnionPayload::Invoice { .. } => {
31843184
let payment_data = payment_data.unwrap();
3185-
let payment_preimage = match inbound_payment::verify(payment_hash, &payment_data, self.highest_seen_timestamp.load(Ordering::Acquire) as u64, &self.inbound_payment_key, &self.logger) {
3186-
Ok(payment_preimage) => payment_preimage,
3185+
let (payment_preimage, min_final_cltv_expiry_delta) = match inbound_payment::verify(payment_hash, &payment_data, self.highest_seen_timestamp.load(Ordering::Acquire) as u64, &self.inbound_payment_key, &self.logger) {
3186+
Ok(result) => result,
31873187
Err(()) => {
31883188
log_trace!(self.logger, "Failing new HTLC with payment_hash {} as payment verification failed", log_bytes!(payment_hash.0));
31893189
fail_htlc!(claimable_htlc, payment_hash);
31903190
continue
31913191
}
31923192
};
3193-
if let Some(min_final_cltv_expiry_delta) = inbound_payment::get_custom_min_final_cltv_expiry_delta(
3194-
payment_data.payment_secret, &self.inbound_payment_key) {
3193+
if let Some(min_final_cltv_expiry_delta) = min_final_cltv_expiry_delta {
31953194
let expected_min_expiry_height = (self.current_best_block().height() + min_final_cltv_expiry_delta as u32) as u64;
31963195
if (cltv_expiry as u64) < expected_min_expiry_height {
31973196
log_trace!(self.logger, "Failing new HTLC with payment_hash {} as its CLTV expiry was too soon (had {}, earliest expected {})",
@@ -7393,7 +7392,7 @@ where
73937392
payment_preimage: match pending_inbound_payments.get(&payment_hash) {
73947393
Some(inbound_payment) => inbound_payment.payment_preimage,
73957394
None => match inbound_payment::verify(payment_hash, &hop_data, 0, &expanded_inbound_key, &args.logger) {
7396-
Ok(payment_preimage) => payment_preimage,
7395+
Ok((payment_preimage, _)) => payment_preimage,
73977396
Err(()) => {
73987397
log_error!(args.logger, "Failed to read claimable payment data for HTLC with payment hash {} - was not a pending inbound payment and didn't match our payment key", log_bytes!(payment_hash.0));
73997398
return Err(DecodeError::InvalidValue);

lightning/src/ln/inbound_payment.rs

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ use bitcoin::hashes::cmp::fixed_time_eq;
1515
use bitcoin::hashes::hmac::{Hmac, HmacEngine};
1616
use bitcoin::hashes::sha256::Hash as Sha256;
1717
use crate::chain::keysinterface::{KeyMaterial, EntropySource};
18-
use crate::ln::channelmanager::MIN_FINAL_CLTV_EXPIRY_DELTA;
1918
use crate::ln::{PaymentHash, PaymentPreimage, PaymentSecret};
2019
use crate::ln::msgs;
2120
use crate::ln::msgs::MAX_VALUE_MSAT;
@@ -252,7 +251,9 @@ fn construct_payment_secret(iv_bytes: &[u8; IV_LEN], metadata_bytes: &[u8; METAD
252251
/// [`NodeSigner::get_inbound_payment_key_material`]: crate::chain::keysinterface::NodeSigner::get_inbound_payment_key_material
253252
/// [`create_inbound_payment`]: crate::ln::channelmanager::ChannelManager::create_inbound_payment
254253
/// [`create_inbound_payment_for_hash`]: crate::ln::channelmanager::ChannelManager::create_inbound_payment_for_hash
255-
pub(super) fn verify<L: Deref>(payment_hash: PaymentHash, payment_data: &msgs::FinalOnionHopData, highest_seen_timestamp: u64, keys: &ExpandedKey, logger: &L) -> Result<Option<PaymentPreimage>, ()>
254+
pub(super) fn verify<L: Deref>(payment_hash: PaymentHash, payment_data: &msgs::FinalOnionHopData,
255+
highest_seen_timestamp: u64, keys: &ExpandedKey, logger: &L) -> Result<
256+
(Option<PaymentPreimage>, Option<u16>), ()>
256257
where L::Target: Logger
257258
{
258259
let (iv_bytes, metadata_bytes) = decrypt_metadata(payment_data.payment_secret, keys);
@@ -317,27 +318,7 @@ pub(super) fn verify<L: Deref>(payment_hash: PaymentHash, payment_data: &msgs::F
317318
return Err(())
318319
}
319320

320-
if let Some(min_final_cltv_expiry_delta) = min_final_cltv_expiry_delta {
321-
if min_final_cltv_expiry_delta.saturating_add(3) < MIN_FINAL_CLTV_EXPIRY_DELTA {
322-
log_trace!(logger, "Failing HTLC with payment_hash {} due to min_final_cltv_expiry_delta {} being less
323-
than our absolute minimum of {}", log_bytes!(payment_hash.0), min_final_cltv_expiry_delta,
324-
MIN_FINAL_CLTV_EXPIRY_DELTA);
325-
return Err(())
326-
}
327-
}
328-
329-
Ok(payment_preimage)
330-
}
331-
332-
pub(super) fn get_custom_min_final_cltv_expiry_delta(payment_secret: PaymentSecret, keys: &ExpandedKey) -> Option<u16> {
333-
let (_, metadata_bytes) = decrypt_metadata(payment_secret, keys);
334-
335-
match Method::from_bits((metadata_bytes[0] & 0b1110_0000) >> METHOD_TYPE_OFFSET) {
336-
Ok(Method::UserPaymentHashCustomFinalCltv) | Ok(Method::LdkPaymentHashCustomFinalCltv) => {
337-
Some(min_final_cltv_expiry_delta_from_metadata(metadata_bytes))
338-
},
339-
_ => None,
340-
}
321+
Ok((payment_preimage, min_final_cltv_expiry_delta))
341322
}
342323

343324
pub(super) fn get_payment_preimage(payment_hash: PaymentHash, payment_secret: PaymentSecret, keys: &ExpandedKey) -> Result<PaymentPreimage, APIError> {

0 commit comments

Comments
 (0)