@@ -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