diff --git a/lez/wallet/src/cli/mod.rs b/lez/wallet/src/cli/mod.rs index 43b6dc7a..a19012f0 100644 --- a/lez/wallet/src/cli/mod.rs +++ b/lez/wallet/src/cli/mod.rs @@ -19,6 +19,7 @@ use crate::{ config::ConfigSubcommand, group::GroupSubcommand, keycard::KeycardSubcommand, + network::handle_change_network, programs::{ amm::AmmProgramAgnosticSubcommand, ata::AtaSubcommand, bridge::BridgeSubcommand, native_token_transfer::AuthTransferSubcommand, pinata::PinataProgramAgnosticSubcommand, @@ -33,6 +34,7 @@ pub mod chain; pub mod config; pub mod group; pub mod keycard; +pub mod network; pub mod programs; pub(crate) trait WalletSubcommand { @@ -80,6 +82,8 @@ pub enum Command { /// Command to setup config, get and set config fields. #[command(subcommand)] Config(ConfigSubcommand), + /// Change the network the wallet points to (testnet, local, or a custom URL). + ChangeNetwork, /// Restoring keys from given password at given `depth`. /// /// !!!WARNING!!! will rewrite current storage. @@ -271,6 +275,11 @@ pub async fn execute_subcommand( Command::Config(config_subcommand) => { config_subcommand.handle_subcommand(wallet_core).await? } + Command::ChangeNetwork => { + handle_change_network(wallet_core).await?; + + SubcommandReturnValue::Empty + } Command::RestoreKeys { depth } => { let mnemonic = read_mnemonic_from_stdin()?; let password = read_password_from_stdin()?; diff --git a/lez/wallet/src/cli/network.rs b/lez/wallet/src/cli/network.rs new file mode 100644 index 00000000..1171ef15 --- /dev/null +++ b/lez/wallet/src/cli/network.rs @@ -0,0 +1,52 @@ +use std::io::Write as _; + +use anyhow::{Context as _, Result}; +use url::Url; + +use crate::WalletCore; + +const TESTNET_SEQUENCER_ADDR: &str = "https://testnet.lez.logos.co"; +const LOCAL_SEQUENCER_ADDR: &str = "http://127.0.0.1:3040"; + +/// Prompt the user to pick a network and update the wallet's `sequencer_addr` accordingly. +pub async fn handle_change_network(wallet_core: &mut WalletCore) -> Result<()> { + println!("Select network:"); + println!("1) testnet"); + println!("2) local"); + println!("3) custom"); + + let sequencer_addr = match prompt("Enter choice [1-3]: ")?.as_str() { + "1" => TESTNET_SEQUENCER_ADDR + .parse() + .expect("testnet sequencer addr must be a valid URL"), + "2" => LOCAL_SEQUENCER_ADDR + .parse() + .expect("local sequencer addr must be a valid URL"), + "3" => prompt("Enter sequencer URL: ")? + .parse::() + .context("Invalid sequencer URL")?, + other => anyhow::bail!("Invalid choice: {other}"), + }; + + let mut config = wallet_core.config().clone(); + config.sequencer_addr = sequencer_addr; + wallet_core.set_config(config); + wallet_core.store_config_changes().await?; + + println!( + "Sequencer address set to {}", + wallet_core.config().sequencer_addr + ); + + Ok(()) +} + +fn prompt(message: &str) -> Result { + print!("{message}"); + std::io::stdout().flush()?; + + let mut input = String::new(); + std::io::stdin().read_line(&mut input)?; + + Ok(input.trim().to_owned()) +} diff --git a/lez/wallet/src/config.rs b/lez/wallet/src/config.rs index 77c2729f..552e1e3c 100644 --- a/lez/wallet/src/config.rs +++ b/lez/wallet/src/config.rs @@ -47,7 +47,7 @@ pub struct WalletConfig { impl Default for WalletConfig { fn default() -> Self { Self { - sequencer_addr: "http://127.0.0.1:3040".parse().unwrap(), + sequencer_addr: "https://testnet.lez.logos.co".parse().unwrap(), seq_poll_timeout: Duration::from_secs(12), seq_tx_poll_max_blocks: 5, seq_poll_max_retries: 5,