11use crate :: labels:: LabelStorage ;
22use crate :: ldkstorage:: CHANNEL_CLOSURE_PREFIX ;
33use crate :: logging:: LOGGING_KEY ;
4+ use crate :: lsp:: voltage;
45use crate :: utils:: { sleep, spawn} ;
56use crate :: MutinyInvoice ;
67use crate :: MutinyWalletConfig ;
@@ -59,6 +60,7 @@ use std::sync::atomic::{AtomicBool, Ordering};
5960#[ cfg( not( target_arch = "wasm32" ) ) ]
6061use std:: time:: Instant ;
6162use std:: { collections:: HashMap , ops:: Deref , sync:: Arc } ;
63+ use url:: Url ;
6264#[ cfg( target_arch = "wasm32" ) ]
6365use web_time:: Instant ;
6466
@@ -347,7 +349,15 @@ impl<S: MutinyStorage> NodeManagerBuilder<S> {
347349 let lsp_config = if c. safe_mode {
348350 None
349351 } else {
350- create_lsp_config ( c. lsp_url , c. lsp_connection_string , c. lsp_token ) ?
352+ create_lsp_config ( c. lsp_url , c. lsp_connection_string , c. lsp_token ) . unwrap_or_else (
353+ |_| {
354+ log_warn ! (
355+ logger,
356+ "Failed to create lsp config, falling back to no LSP configured"
357+ ) ;
358+ None
359+ } ,
360+ )
351361 } ;
352362 log_trace ! ( logger, "finished creating lsp config" ) ;
353363
@@ -1338,7 +1348,7 @@ impl<S: MutinyStorage> NodeManager<S> {
13381348 /// current LSP, it will fail to change the LSP.
13391349 ///
13401350 /// Requires a restart of the node manager to take effect.
1341- pub async fn change_lsp ( & self , lsp_config : Option < LspConfig > ) -> Result < ( ) , MutinyError > {
1351+ pub async fn change_lsp ( & self , mut lsp_config : Option < LspConfig > ) -> Result < ( ) , MutinyError > {
13421352 log_trace ! ( self . logger, "calling change_lsp" ) ;
13431353
13441354 // if we are in safe mode we don't load the lightning state so we can't know if it is safe to change the LSP.
@@ -1362,6 +1372,28 @@ impl<S: MutinyStorage> NodeManager<S> {
13621372 }
13631373 drop ( nodes) ;
13641374
1375+ // verify that the LSP config is valid
1376+ match lsp_config. as_mut ( ) {
1377+ Some ( LspConfig :: VoltageFlow ( config) ) => {
1378+ let http_client = Client :: new ( ) ;
1379+
1380+ // try to connect to the LSP, update the config if successful
1381+ let ( pk, str) = voltage:: LspClient :: fetch_connection_info (
1382+ & http_client,
1383+ & config. url ,
1384+ & self . logger ,
1385+ )
1386+ . await ?;
1387+ config. pubkey = Some ( pk) ;
1388+ config. connection_string = Some ( str) ;
1389+ }
1390+ Some ( LspConfig :: Lsps ( config) ) => {
1391+ // make sure a valid connection string was provided
1392+ PubkeyConnectionInfo :: new ( & config. connection_string ) ?;
1393+ }
1394+ None => { } // Nothing to verify
1395+ }
1396+
13651397 // edit node storage
13661398 let mut node_storage = self . node_storage . write ( ) . await ;
13671399 node_storage. nodes . iter_mut ( ) . for_each ( |( _, n) | {
@@ -2036,8 +2068,14 @@ pub fn create_lsp_config(
20362068) -> Result < Option < LspConfig > , MutinyError > {
20372069 match ( lsp_url. clone ( ) , lsp_connection_string. clone ( ) ) {
20382070 ( Some ( lsp_url) , None ) => {
2039- if !lsp_url. is_empty ( ) {
2040- Ok ( Some ( LspConfig :: new_voltage_flow ( lsp_url) ) )
2071+ let trimmed = lsp_url. trim ( ) . to_string ( ) ;
2072+ if !trimmed. is_empty ( ) {
2073+ // make sure url is valid
2074+ if Url :: parse ( & trimmed) . is_err ( ) {
2075+ return Err ( MutinyError :: InvalidArgumentsError ) ;
2076+ }
2077+
2078+ Ok ( Some ( LspConfig :: new_voltage_flow ( trimmed) ) )
20412079 } else {
20422080 Ok ( None )
20432081 }
0 commit comments