From 08c1ba52798bb2774c304fd28cb80822f8e3d4f3 Mon Sep 17 00:00:00 2001 From: DaughterOfMars Date: Tue, 31 Dec 2024 04:01:05 -0500 Subject: [PATCH] chore(iota): Remove extra address generation in client commands (#4644) --- Cargo.lock | 30 ++++++++++++++-------- crates/iota/src/iota_commands.rs | 44 +++++++++++++++++--------------- 2 files changed, 43 insertions(+), 31 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 550aa462be0..0468f5ef326 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2461,9 +2461,9 @@ dependencies = [ [[package]] name = "clap" -version = "4.5.18" +version = "4.5.23" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b0956a43b323ac1afaffc053ed5c4b7c1f1800bacd1683c353aabbb752515dd3" +checksum = "3135e7ec2ef7b10c6ed8950f0f792ed96ee093fa088608f1c76e569722700c84" dependencies = [ "clap_builder", "clap_derive", @@ -2471,15 +2471,15 @@ dependencies = [ [[package]] name = "clap_builder" -version = "4.5.18" +version = "4.5.23" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4d72166dd41634086d5803a47eb71ae740e61d84709c36f3c34110173db3961b" +checksum = "30582fc632330df2bd26877bde0c1f4470d57c582bbc070376afcd04d8cb4838" dependencies = [ "anstream", "anstyle", "clap_lex", "strsim 0.11.1", - "terminal_size", + "terminal_size 0.4.1", ] [[package]] @@ -2496,9 +2496,9 @@ dependencies = [ [[package]] name = "clap_lex" -version = "0.7.2" +version = "0.7.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1462739cb27611015575c0c11df5df7601141071f07518d56fcc1be504cbec97" +checksum = "f46ad14479a25103f283c0f10005961cf086d8dc42205bb44c46ac563475dca6" [[package]] name = "clipboard-win" @@ -9005,7 +9005,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4979f22fdb869068da03c9f7528f8297c6fd2606bc3a4affe42e6a823fdb8da4" dependencies = [ "cfg-if", - "windows-targets 0.48.5", + "windows-targets 0.52.6", ] [[package]] @@ -9318,7 +9318,7 @@ dependencies = [ "supports-color", "supports-hyperlinks", "supports-unicode", - "terminal_size", + "terminal_size 0.3.0", "textwrap", "thiserror", "unicode-width", @@ -14638,6 +14638,16 @@ dependencies = [ "windows-sys 0.48.0", ] +[[package]] +name = "terminal_size" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5352447f921fda68cf61b4101566c0bdb5104eff6804d0678e5227580ab6a4e9" +dependencies = [ + "rustix", + "windows-sys 0.59.0", +] + [[package]] name = "termtree" version = "0.4.1" @@ -15497,7 +15507,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "97fee6b57c6a41524a810daee9286c02d7752c4253064d0b05472833a438f675" dependencies = [ "cfg-if", - "rand 0.7.3", + "rand 0.8.5", "static_assertions", ] diff --git a/crates/iota/src/iota_commands.rs b/crates/iota/src/iota_commands.rs index e607b8dc183..36030ac3b38 100644 --- a/crates/iota/src/iota_commands.rs +++ b/crates/iota/src/iota_commands.rs @@ -439,7 +439,7 @@ impl IotaCommand { } IotaCommand::Console { config } => { let config = config.unwrap_or(iota_config_dir()?.join(IOTA_CLIENT_CONFIG)); - prompt_if_no_config(&config, false).await?; + prompt_if_no_config(&config, false, true).await?; let context = WalletContext::new(&config, None, None)?; start_console(context, &mut stdout(), &mut stderr()).await } @@ -450,7 +450,12 @@ impl IotaCommand { accept_defaults, } => { let config_path = config.unwrap_or(iota_config_dir()?.join(IOTA_CLIENT_CONFIG)); - prompt_if_no_config(&config_path, accept_defaults).await?; + prompt_if_no_config( + &config_path, + accept_defaults, + !matches!(cmd, Some(IotaClientCommands::NewAddress { .. })), + ) + .await?; let mut context = WalletContext::new(&config_path, None, None)?; if let Some(cmd) = cmd { cmd.execute(&mut context).await?.print(!json); @@ -469,7 +474,7 @@ impl IotaCommand { accept_defaults, } => { let config_path = config.unwrap_or(iota_config_dir()?.join(IOTA_CLIENT_CONFIG)); - prompt_if_no_config(&config_path, accept_defaults).await?; + prompt_if_no_config(&config_path, accept_defaults, true).await?; let mut context = WalletContext::new(&config_path, None, None)?; if let Some(cmd) = cmd { cmd.execute(&mut context).await?.print(!json); @@ -496,7 +501,7 @@ impl IotaCommand { // management. let config = client_config.unwrap_or(iota_config_dir()?.join(IOTA_CLIENT_CONFIG)); - prompt_if_no_config(&config, false).await?; + prompt_if_no_config(&config, false, true).await?; let context = WalletContext::new(&config, None, None)?; let client = context.get_client().await?; let chain_id = client.read_api().get_chain_identifier().await.ok(); @@ -1148,7 +1153,8 @@ async fn genesis( async fn prompt_if_no_config( wallet_conf_path: &Path, accept_defaults: bool, -) -> Result<(), anyhow::Error> { + generate_address: bool, +) -> anyhow::Result<()> { // Prompt user for connect to devnet fullnode if config does not exist. if !wallet_conf_path.exists() { let env = match std::env::var_os("IOTA_CONFIG_WITH_RPC_URL") { @@ -1199,12 +1205,13 @@ async fn prompt_if_no_config( .parent() .unwrap_or(&iota_config_dir()?) .join(IOTA_KEYSTORE_FILENAME); - let mut keystore = Keystore::from(FileBasedKeystore::new(&keystore_path)?); + let keystore = Keystore::from(FileBasedKeystore::new(&keystore_path)?); + let mut config = IotaClientConfig::new(keystore).with_envs([env]); // Get an existing address or generate a new one - let active_address = if let Some(existing_address) = keystore.addresses().first() { + if let Some(existing_address) = config.keystore().addresses().first() { println!("Using existing address {existing_address} as active address."); - *existing_address - } else { + config = config.with_active_address(*existing_address); + } else if generate_address { let key_scheme = if accept_defaults { SignatureScheme::ED25519 } else { @@ -1216,23 +1223,18 @@ async fn prompt_if_no_config( Err(e) => return Err(anyhow!("{e}")), } }; - let (new_address, phrase, scheme) = - keystore.generate_and_add_new_key(key_scheme, None, None, None)?; - let alias = keystore.get_alias_by_address(&new_address)?; + let (new_address, phrase, scheme) = config + .keystore_mut() + .generate_and_add_new_key(key_scheme, None, None, None)?; + let alias = config.keystore().get_alias_by_address(&new_address)?; println!( "Generated new keypair and alias for address with scheme {:?} [{alias}: {new_address}]", scheme.to_string() ); println!("Secret Recovery Phrase : [{phrase}]"); - new_address - }; - let alias = env.alias().clone(); - IotaClientConfig::new(keystore) - .with_envs([env]) - .with_active_address(active_address) - .with_active_env(alias) - .persisted(wallet_conf_path) - .save()?; + config = config.with_active_address(new_address); + } + config.persisted(wallet_conf_path).save()?; } } Ok(())