kaspa_cli_lib/modules/
connect.rs1use crate::imports::*;
2
3#[derive(Default, Handler)]
4#[help("Connect to a Kaspa network")]
5pub struct Connect;
6
7impl Connect {
8 async fn main(self: Arc<Self>, ctx: &Arc<dyn Context>, argv: Vec<String>, _cmd: &str) -> Result<()> {
9 let ctx = ctx.clone().downcast_arc::<KaspaCli>()?;
10 if let Some(wrpc_client) = ctx.wallet().try_wrpc_client().as_ref() {
11 let network_id = ctx.wallet().network_id()?;
12
13 let arg_or_server_address = argv.first().cloned().or_else(|| ctx.wallet().settings().get(WalletSettings::Server));
14 let (is_public, url) = match arg_or_server_address.as_deref() {
15 Some("public") => {
16 tprintln!(ctx, "Connecting to a public node");
17 (true, Resolver::default().get_url(WrpcEncoding::Borsh, network_id).await.map_err(|e| e.to_string())?)
18 }
19 None => {
20 tprintln!(ctx, "No server set, connecting to a public node");
21 (true, Resolver::default().get_url(WrpcEncoding::Borsh, network_id).await.map_err(|e| e.to_string())?)
22 }
23 Some(url) => {
24 (false, wrpc_client.parse_url_with_network_type(url.to_string(), network_id.into()).map_err(|e| e.to_string())?)
25 }
26 };
27
28 if is_public {
29 static WARNING: AtomicBool = AtomicBool::new(false);
30 if !WARNING.load(Ordering::Relaxed) {
31 WARNING.store(true, Ordering::Relaxed);
32
33 tprintln!(ctx);
34
35 tpara!(
36 ctx,
37 "Please note that public node infrastructure is operated by contributors and \
38 accessing it may expose your IP address to different node providers. \
39 Consider running your own node for better privacy. \
40 ",
41 );
42 tprintln!(ctx);
43 tpara!(ctx, "Please do not connect to public nodes directly as they are load-balanced.");
44 tprintln!(ctx);
45 }
46 }
47
48 let options = ConnectOptions {
49 block_async_connect: true,
50 strategy: ConnectStrategy::Fallback,
51 url: Some(url),
52 ..Default::default()
53 };
54 wrpc_client.connect(Some(options)).await.map_err(|e| e.to_string())?;
55 } else {
56 terrorln!(ctx, "Unable to connect with non-wRPC client");
57 }
58 Ok(())
59 }
60}