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

Commit f00bc5e

Browse files
Merge pull request #1217 from MutinyWallet/some-lsp-fixes
Make sure LSP url is a valid url when switching
2 parents 10bbc27 + eb8de4b commit f00bc5e

2 files changed

Lines changed: 46 additions & 8 deletions

File tree

mutiny-core/src/lsp/voltage.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,12 +147,12 @@ impl LspClient {
147147
}
148148

149149
/// Get the pubkey and connection string from the LSP from the /info endpoint
150-
async fn fetch_connection_info(
150+
pub(crate) async fn fetch_connection_info(
151151
http_client: &Client,
152152
url: &str,
153153
logger: &MutinyLogger,
154154
) -> Result<(PublicKey, String), MutinyError> {
155-
let builder = http_client.get(format!("{}{}", url, GET_INFO_PATH));
155+
let builder = http_client.get(format!("{}{}", url.trim(), GET_INFO_PATH));
156156
let request = add_x_auth_token_if_needed(url, builder)?;
157157

158158
let response: reqwest::Response = utils::fetch_with_timeout(http_client, request)
@@ -296,7 +296,7 @@ impl Lsp for LspClient {
296296

297297
let builder = self
298298
.http_client
299-
.post(format!("{}{}", &self.url, PROPOSAL_PATH))
299+
.post(format!("{}{}", &self.url.trim(), PROPOSAL_PATH))
300300
.json(&payload);
301301

302302
let request = add_x_auth_token_if_needed(&self.url, builder)?;
@@ -353,7 +353,7 @@ impl Lsp for LspClient {
353353
async fn get_lsp_fee_msat(&self, fee_request: FeeRequest) -> Result<FeeResponse, MutinyError> {
354354
let builder = self
355355
.http_client
356-
.post(format!("{}{}", &self.url, FEE_PATH))
356+
.post(format!("{}{}", &self.url.trim(), FEE_PATH))
357357
.json(&fee_request);
358358

359359
let request = add_x_auth_token_if_needed(&self.url, builder)?;

mutiny-core/src/nodemanager.rs

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::labels::LabelStorage;
22
use crate::ldkstorage::CHANNEL_CLOSURE_PREFIX;
33
use crate::logging::LOGGING_KEY;
4+
use crate::lsp::voltage;
45
use crate::utils::{sleep, spawn};
56
use crate::MutinyInvoice;
67
use crate::MutinyWalletConfig;
@@ -59,6 +60,7 @@ use std::sync::atomic::{AtomicBool, Ordering};
5960
#[cfg(not(target_arch = "wasm32"))]
6061
use std::time::Instant;
6162
use std::{collections::HashMap, ops::Deref, sync::Arc};
63+
use url::Url;
6264
#[cfg(target_arch = "wasm32")]
6365
use 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

Comments
 (0)