Skip to content
This repository was archived by the owner on Jan 6, 2025. It is now read-only.

Commit 795a097

Browse files
add valid_until check into is_valid_opening_fee_params
1 parent cc4f009 commit 795a097

File tree

3 files changed

+31
-6
lines changed

3 files changed

+31
-6
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@ lightning-invoice = "0.25.0"
1313

1414
bitcoin = "0.29.0"
1515

16-
chrono = { version = "0.4.31", default-features = false, features = ["std", "serde"] }
16+
chrono = { version = "0.4.31", default-features = false, features = ["std", "serde", "clock"] }
1717
serde = { version = "1.0", default-features = false, features = ["derive", "alloc"] }
1818
serde_json = "1.0"

src/jit_channel/msgs.rs

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ mod tests {
229229
let min_fee_msat = 100;
230230
let proportional = 21;
231231
let valid_until: chrono::DateTime<Utc> =
232-
chrono::DateTime::parse_from_rfc3339("2023-05-20T08:30:45Z").unwrap().into();
232+
chrono::DateTime::parse_from_rfc3339("2035-05-20T08:30:45Z").unwrap().into();
233233
let min_lifetime = 144;
234234
let max_client_to_self_delay = 128;
235235

@@ -258,7 +258,7 @@ mod tests {
258258
fn changing_single_field_produced_invalid_params() {
259259
let min_fee_msat = 100;
260260
let proportional = 21;
261-
let valid_until = chrono::DateTime::parse_from_rfc3339("2023-05-20T08:30:45Z").unwrap();
261+
let valid_until = chrono::DateTime::parse_from_rfc3339("2035-05-20T08:30:45Z").unwrap();
262262
let min_lifetime = 144;
263263
let max_client_to_self_delay = 128;
264264

@@ -281,7 +281,7 @@ mod tests {
281281
fn wrong_secret_produced_invalid_params() {
282282
let min_fee_msat = 100;
283283
let proportional = 21;
284-
let valid_until = chrono::DateTime::parse_from_rfc3339("2023-05-20T08:30:45Z").unwrap();
284+
let valid_until = chrono::DateTime::parse_from_rfc3339("2035-05-20T08:30:45Z").unwrap();
285285
let min_lifetime = 144;
286286
let max_client_to_self_delay = 128;
287287

@@ -297,6 +297,28 @@ mod tests {
297297
let other_secret = [2u8; 32];
298298

299299
let opening_fee_params = raw.into_opening_fee_params(&promise_secret);
300-
assert!(is_valid_opening_fee_params(&opening_fee_params, &promise_secret));
300+
assert!(!is_valid_opening_fee_params(&opening_fee_params, &other_secret));
301+
}
302+
303+
#[test]
304+
fn expired_params_produces_invalid_params() {
305+
let min_fee_msat = 100;
306+
let proportional = 21;
307+
let valid_until = chrono::DateTime::parse_from_rfc3339("2023-05-20T08:30:45Z").unwrap();
308+
let min_lifetime = 144;
309+
let max_client_to_self_delay = 128;
310+
311+
let raw = RawOpeningFeeParams {
312+
min_fee_msat,
313+
proportional,
314+
valid_until: valid_until.into(),
315+
min_lifetime,
316+
max_client_to_self_delay,
317+
};
318+
319+
let promise_secret = [1u8; 32];
320+
321+
let opening_fee_params = raw.into_opening_fee_params(&promise_secret);
322+
assert!(!is_valid_opening_fee_params(&opening_fee_params, &promise_secret));
301323
}
302324
}

src/jit_channel/utils.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,13 @@ use crate::jit_channel::msgs::OpeningFeeParams;
66
use crate::utils;
77

88
/// Determines if the given parameters are valid given the secret used to generate the promise.
9-
// TODO: add validation check that valid_until >= now()
109
pub fn is_valid_opening_fee_params(
1110
fee_params: &OpeningFeeParams, promise_secret: &[u8; 32],
1211
) -> bool {
12+
if chrono::Utc::now() > fee_params.valid_until {
13+
return false;
14+
}
15+
1316
let mut hmac = HmacEngine::<Sha256>::new(promise_secret);
1417
hmac.input(&fee_params.min_fee_msat.to_be_bytes());
1518
hmac.input(&fee_params.proportional.to_be_bytes());

0 commit comments

Comments
 (0)