Skip to content

Commit 652c45d

Browse files
committed
f dry up payment method matching
1 parent fbdc6fc commit 652c45d

File tree

1 file changed

+12
-28
lines changed

1 file changed

+12
-28
lines changed

lightning/src/ln/inbound_payment.rs

Lines changed: 12 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -269,8 +269,9 @@ pub(super) fn verify<L: Deref>(payment_hash: PaymentHash, payment_data: &msgs::F
269269

270270
// Make sure to check the HMAC before doing the other checks below, to mitigate timing attacks.
271271
let mut payment_preimage = None;
272+
272273
match payment_type_res {
273-
Ok(Method::UserPaymentHash) => {
274+
Ok(Method::UserPaymentHash) | Ok(Method::UserPaymentHashCustomFinalCltv) => {
274275
let mut hmac = HmacEngine::<Sha256>::new(&keys.user_pmt_hash_key);
275276
hmac.input(&metadata_bytes[..]);
276277
hmac.input(&payment_hash.0);
@@ -279,7 +280,7 @@ pub(super) fn verify<L: Deref>(payment_hash: PaymentHash, payment_data: &msgs::F
279280
return Err(())
280281
}
281282
},
282-
Ok(Method::LdkPaymentHash) => {
283+
Ok(Method::LdkPaymentHash) | Ok(Method::LdkPaymentHashCustomFinalCltv) => {
283284
match derive_ldk_payment_preimage(payment_hash, &iv_bytes, &metadata_bytes, keys) {
284285
Ok(preimage) => payment_preimage = Some(preimage),
285286
Err(bad_preimage_bytes) => {
@@ -288,40 +289,23 @@ pub(super) fn verify<L: Deref>(payment_hash: PaymentHash, payment_data: &msgs::F
288289
}
289290
}
290291
},
291-
Ok(Method::UserPaymentHashCustomFinalCltv) => {
292-
let mut hmac = HmacEngine::<Sha256>::new(&keys.user_pmt_hash_key);
293-
hmac.input(&metadata_bytes[..]);
294-
hmac.input(&payment_hash.0);
295-
if !fixed_time_eq(&iv_bytes, &Hmac::from_engine(hmac).into_inner().split_at_mut(IV_LEN).0) {
296-
log_trace!(logger, "Failing HTLC with user-generated payment_hash {}: unexpected payment_secret", log_bytes!(payment_hash.0));
297-
return Err(())
298-
}
292+
Err(unknown_bits) => {
293+
log_trace!(logger, "Failing HTLC with payment hash {} due to unknown payment type {}", log_bytes!(payment_hash.0), unknown_bits);
294+
return Err(());
295+
}
296+
}
299297

298+
// Match again to check for custom `min_final_cltv_expiry_delta`.
299+
match payment_type_res {
300+
Ok(Method::UserPaymentHashCustomFinalCltv) | Ok(Method::LdkPaymentHashCustomFinalCltv) => {
300301
min_final_cltv_expiry_delta = Some(min_final_cltv_expiry_delta_from_metadata(metadata_bytes));
301302
// Zero out bits reserved for first 4 bits of `min_final_cltv_expiry_delta`, leaving one bit
302303
// which is the most significant bit of `min_amt_msat`.
303304
amt_msat_bytes[0] &= 0b0000_0001;
304305
// Zero out bits reserved for the last 8 bits of `min_final_cltv_expiry_delta`.
305306
expiry_bytes[0] &= 0;
306-
},
307-
Ok(Method::LdkPaymentHashCustomFinalCltv) => {
308-
match derive_ldk_payment_preimage(payment_hash, &iv_bytes, &metadata_bytes, keys) {
309-
Ok(preimage) => payment_preimage = Some(preimage),
310-
Err(bad_preimage_bytes) => {
311-
log_trace!(logger, "Failing HTLC with payment_hash {} due to mismatching preimage {}", log_bytes!(payment_hash.0), log_bytes!(bad_preimage_bytes));
312-
return Err(())
313-
}
314-
}
315-
min_final_cltv_expiry_delta = Some(min_final_cltv_expiry_delta_from_metadata(metadata_bytes));
316-
// Zero out bits reserved for first 4 bits of `min_final_cltv_expiry_delta`.
317-
amt_msat_bytes[0] &= 0b1110_0001;
318-
// Zero out bits reserved for the last 8 bits of `min_final_cltv_expiry_delta`.
319-
expiry_bytes[0] &= 0;
320-
},
321-
Err(unknown_bits) => {
322-
log_trace!(logger, "Failing HTLC with payment hash {} due to unknown payment type {}", log_bytes!(payment_hash.0), unknown_bits);
323-
return Err(());
324307
}
308+
_ => {}
325309
}
326310

327311
let min_amt_msat: u64 = u64::from_be_bytes(amt_msat_bytes.into());

0 commit comments

Comments
 (0)