From a4dd2cfd570974de9a5747adaf5a8e815b72dfe6 Mon Sep 17 00:00:00 2001 From: Dmitry Perchanov Date: Tue, 24 Sep 2024 11:05:23 +0300 Subject: [PATCH] core: services: connect to specific fqdn node within url on wasm target. Signed-off-by: Dmitry Perchanov --- Cargo.lock | 1 + core/Cargo.toml | 1 + core/src/runtime/services/kaspa/mod.rs | 42 ++++++++++++++++++++++++++ 3 files changed, 44 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index b355d62..0afa532 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3975,6 +3975,7 @@ dependencies = [ "thiserror", "tokio", "toml", + "url", "vergen", "walkdir", "wasm-bindgen", diff --git a/core/Cargo.toml b/core/Cargo.toml index 909bd4a..6877d95 100644 --- a/core/Cargo.toml +++ b/core/Cargo.toml @@ -95,6 +95,7 @@ eframe = { workspace = true, default-features = false, features = [ egui-phosphor = { workspace = true, features = ["light"] } # egui-phosphor = { workspace = true, features = ["thin","light","regular","bold"] } egui-notify.workspace = true +url = "2.2" [dependencies.web-sys] workspace = true diff --git a/core/src/runtime/services/kaspa/mod.rs b/core/src/runtime/services/kaspa/mod.rs index 559445f..2308681 100644 --- a/core/src/runtime/services/kaspa/mod.rs +++ b/core/src/runtime/services/kaspa/mod.rs @@ -70,7 +70,30 @@ cfg_if! { Disable { network : Network }, Exit, } + } +} +cfg_if! { + if #[cfg(target_arch = "wasm32")] { + #[cfg(target_arch = "wasm32")] + use wasm_bindgen::prelude::*; + use web_sys::window; + + #[wasm_bindgen] + pub fn get_wrpc_url() -> Option { + window() + .and_then(|win| win.location().href().ok()) + .and_then(|url| { + let url = url::Url::parse(&url).ok()?; + url.query_pairs().find_map(|(key, value)| { + if key == "wrpc" { + Some(value.to_string()) + } else { + None + } + }) + }) + } } } @@ -579,8 +602,27 @@ impl KaspaService { } else { self.stop_all_services().await?; + #[cfg(not(target_arch = "wasm32"))] + let url = "".to_string(); + #[cfg(target_arch = "wasm32")] + let url = get_wrpc_url().unwrap_or_else(|| "".to_string()); + let network = match url.as_str() { + u if u.contains("testnet-10") => Network::Testnet10, + u if u.contains("testnet-11") => Network::Testnet11, + _ => network, + }; + self.handle_network_change(network).await?; + let rpc_config = if url.is_empty() { + rpc_config.clone() + } else { + RpcConfig::Wrpc { + url: url.into(), + encoding: WrpcEncoding::Borsh, + resolver_urls: None, + } + }; let rpc = Self::create_rpc_client(&rpc_config, network) .expect("Kaspad Service - unable to create wRPC client"); self.start_all_services(Some(rpc), network).await?;