Skip to content

Commit 6bb77be

Browse files
Implement comments. Rebase needed
1 parent b70e9fb commit 6bb77be

File tree

6 files changed

+151
-220
lines changed

6 files changed

+151
-220
lines changed

lightning-liquidity/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,4 @@ check-cfg = [
4747
"cfg(c_bindings)",
4848
"cfg(backtrace)",
4949
"cfg(ldk_bench)",
50-
"cfg(time)",
5150
]

lightning-liquidity/src/lsps0/ser.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -238,14 +238,15 @@ impl LSPSDateTime {
238238
now_seconds_since_epoch > datetime_seconds_since_epoch
239239
}
240240

241-
/// Returns the number of seconds since the Unix epoch.
242-
pub fn timestamp(&self) -> u64 {
243-
self.0.timestamp() as u64
241+
/// Returns the time in seconds since the unix epoch.
242+
pub fn abs_diff(&self, other: &Self) -> u64 {
243+
self.0.timestamp().abs_diff(other.0.timestamp())
244244
}
245+
}
245246

246-
/// Returns the number of seconds since the Unix epoch.
247-
pub fn new_from_duration(duration: Duration) -> Self {
248-
Self(DateTime::from_timestamp(duration.as_secs() as i64, duration.subsec_nanos()).unwrap())
247+
impl From<Duration> for LSPSDateTime {
248+
fn from(duration: Duration) -> Self {
249+
Self(DateTime::UNIX_EPOCH + duration)
249250
}
250251
}
251252

lightning-liquidity/src/lsps5/client.rs

Lines changed: 46 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ where
217217

218218
peer_state_lock.cleanup_expired_responses(
219219
self.config.response_max_age_secs,
220-
self.time_provider.clone(),
220+
Arc::clone(&self.time_provider),
221221
);
222222

223223
Ok(f(&mut *peer_state_lock))
@@ -240,25 +240,15 @@ where
240240
pub fn set_webhook(
241241
&self, counterparty_node_id: PublicKey, app_name: String, webhook_url: String,
242242
) -> Result<LSPSRequestId, LightningError> {
243-
let app_name = match LSPS5AppName::new(app_name) {
244-
Ok(app_name) => app_name,
245-
Err(e) => {
246-
return Err(LightningError {
247-
err: e.message,
248-
action: ErrorAction::IgnoreAndLog(Level::Error),
249-
})
250-
},
251-
};
243+
let app_name = LSPS5AppName::new(app_name).map_err(|e| LightningError {
244+
err: e.message,
245+
action: ErrorAction::IgnoreAndLog(Level::Error),
246+
})?;
252247

253-
let lsps_webhook_url = match LSPS5WebhookUrl::new(webhook_url) {
254-
Ok(webhook) => webhook,
255-
Err(e) => {
256-
return Err(LightningError {
257-
err: e.message,
258-
action: ErrorAction::IgnoreAndLog(Level::Error),
259-
})
260-
},
261-
};
248+
let lsps_webhook_url = LSPS5WebhookUrl::new(webhook_url).map_err(|e| LightningError {
249+
err: e.message,
250+
action: ErrorAction::IgnoreAndLog(Level::Error),
251+
})?;
262252

263253
let request_id = generate_request_id(&self.entropy_source);
264254

@@ -296,15 +286,13 @@ where
296286
&self, counterparty_node_id: PublicKey,
297287
) -> Result<LSPSRequestId, LightningError> {
298288
let request_id = generate_request_id(&self.entropy_source);
289+
let now = self.time_provider.duration_since_epoch();
299290

300291
self.with_peer_state(counterparty_node_id, |peer_state| {
301-
peer_state
302-
.pending_list_webhooks_requests
303-
.insert(request_id.clone(), self.time_provider.duration_since_epoch());
292+
peer_state.pending_list_webhooks_requests.insert(request_id.clone(), now);
304293
})?;
305294

306295
let request = LSPS5Request::ListWebhooks(ListWebhooksRequest {});
307-
308296
let message = LSPS5Message::Request(request_id.clone(), request);
309297
self.pending_messages.enqueue(&counterparty_node_id, LSPSMessage::LSPS5(message));
310298

@@ -327,27 +315,21 @@ where
327315
pub fn remove_webhook(
328316
&self, counterparty_node_id: PublicKey, app_name: String,
329317
) -> Result<LSPSRequestId, LightningError> {
330-
let app_name = match LSPS5AppName::new(app_name) {
331-
Ok(app_name) => app_name,
332-
Err(e) => {
333-
return Err(LightningError {
334-
err: e.message,
335-
action: ErrorAction::IgnoreAndLog(Level::Error),
336-
})
337-
},
338-
};
318+
let app_name = LSPS5AppName::new(app_name).map_err(|e| LightningError {
319+
err: e.message,
320+
action: ErrorAction::IgnoreAndLog(Level::Error),
321+
})?;
339322

340323
let request_id = generate_request_id(&self.entropy_source);
324+
let now = self.time_provider.duration_since_epoch();
341325

342326
self.with_peer_state(counterparty_node_id, |peer_state| {
343-
peer_state.pending_remove_webhook_requests.insert(
344-
request_id.clone(),
345-
(app_name.clone(), self.time_provider.duration_since_epoch()),
346-
);
327+
peer_state
328+
.pending_remove_webhook_requests
329+
.insert(request_id.clone(), (app_name.clone(), now));
347330
})?;
348331

349332
let request = LSPS5Request::RemoveWebhook(RemoveWebhookRequest { app_name });
350-
351333
let message = LSPS5Message::Request(request_id.clone(), request);
352334
self.pending_messages.enqueue(&counterparty_node_id, LSPSMessage::LSPS5(message));
353335

@@ -513,39 +495,30 @@ where
513495
&self, counterparty_node_id: PublicKey, timestamp: &str, signature: &str,
514496
notification: &WebhookNotification,
515497
) -> Result<bool, LightningError> {
516-
match LSPSDateTime::from_str(timestamp) {
517-
Ok(timestamp_dt) => {
518-
let now = self.time_provider.duration_since_epoch();
519-
let signature_timestamp = timestamp_dt.timestamp();
520-
let diff = if now.as_secs() > signature_timestamp {
521-
now.as_secs() - signature_timestamp
522-
} else {
523-
signature_timestamp - now.as_secs()
524-
};
498+
LSPSDateTime::from_str(timestamp)
499+
.map_err(|_| LightningError {
500+
err: format!("Invalid timestamp format: {}", timestamp),
501+
action: ErrorAction::IgnoreAndLog(Level::Error),
502+
})
503+
.and_then(|signature_timestamp| {
504+
let now = LSPSDateTime::from(self.time_provider.duration_since_epoch());
505+
let diff = signature_timestamp.abs_diff(&now);
506+
525507
if diff > 600 {
526508
return Err(LightningError {
527509
err: format!("Timestamp too old: {}", timestamp),
528510
action: ErrorAction::IgnoreAndLog(Level::Error),
529511
});
530512
}
531-
},
532-
Err(_e) => {
533-
return Err(LightningError {
534-
err: format!("Invalid timestamp format: {}", timestamp),
535-
action: ErrorAction::IgnoreAndLog(Level::Error),
536-
});
537-
},
538-
}
539513

540-
let notification_json = match serde_json::to_string(notification) {
541-
Ok(json) => json,
542-
Err(e) => {
543-
return Err(LightningError {
544-
err: format!("Failed to serialize notification: {}", e),
545-
action: ErrorAction::IgnoreAndLog(Level::Error),
546-
});
547-
},
548-
};
514+
Ok(())
515+
})?;
516+
517+
let notification_json =
518+
serde_json::to_string(notification).map_err(|e| LightningError {
519+
err: format!("Failed to serialize notification: {}", e),
520+
action: ErrorAction::IgnoreAndLog(Level::Error),
521+
})?;
549522

550523
let message = format!(
551524
"LSPS5: DO NOT SIGN THIS MESSAGE MANUALLY: LSP: At {} I notify {}",
@@ -572,7 +545,7 @@ where
572545
for (stored_sig, _) in recent_signatures.iter() {
573546
if stored_sig == signature {
574547
return Err(LightningError {
575-
err: format!("Replay attack detected: signature has been used before"),
548+
err: "Replay attack detected: signature has been used before".to_string(),
576549
action: ErrorAction::IgnoreAndLog(Level::Warn),
577550
});
578551
}
@@ -594,11 +567,10 @@ where
594567
let retention_duration =
595568
Duration::from_secs(self.config.signature_config.retention_minutes * 60);
596569
while let Some((_, time)) = recent_signatures.front() {
597-
match now.checked_sub(*time) {
598-
Some(duration) if duration > retention_duration => {
599-
recent_signatures.pop_front();
600-
},
601-
_ => break,
570+
if now.checked_sub(*time).map_or(false, |duration| duration > retention_duration) {
571+
recent_signatures.pop_front();
572+
} else {
573+
break;
602574
}
603575
}
604576

@@ -627,15 +599,11 @@ where
627599
&self, counterparty_node_id: PublicKey, timestamp: &str, signature: &str,
628600
notification_json: &str,
629601
) -> Result<WebhookNotification, LightningError> {
630-
let notification: WebhookNotification = match serde_json::from_str(notification_json) {
631-
Ok(n) => n,
632-
Err(e) => {
633-
return Err(LightningError {
634-
err: format!("Failed to parse notification: {}", e),
635-
action: ErrorAction::IgnoreAndLog(Level::Error),
636-
});
637-
},
638-
};
602+
let notification: WebhookNotification =
603+
serde_json::from_str(notification_json).map_err(|e| LightningError {
604+
err: format!("Failed to parse notification: {}", e),
605+
action: ErrorAction::IgnoreAndLog(Level::Error),
606+
})?;
639607

640608
self.check_signature_exists(signature)?;
641609

0 commit comments

Comments
 (0)