Skip to content

Commit

Permalink
core: services: connect to specific fqdn node within url on wasm target.
Browse files Browse the repository at this point in the history
Signed-off-by: Dmitry Perchanov <demisrael@gmail.com>
  • Loading branch information
demisrael committed Sep 24, 2024
1 parent 0542fa2 commit a4dd2cf
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
42 changes: 42 additions & 0 deletions core/src/runtime/services/kaspa/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> {
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
}
})
})
}
}
}

Expand Down Expand Up @@ -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?;
Expand Down

0 comments on commit a4dd2cf

Please sign in to comment.