From e9eba6b7cd7d39faedf12660858d8af88f43dd5c Mon Sep 17 00:00:00 2001 From: Ermine Jose Date: Fri, 13 Dec 2024 00:13:42 +0530 Subject: [PATCH 1/2] feat: create wallet file from env sk --- ant-cli/src/wallet/fs.rs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/ant-cli/src/wallet/fs.rs b/ant-cli/src/wallet/fs.rs index 136ddf5c4f..41415e7201 100644 --- a/ant-cli/src/wallet/fs.rs +++ b/ant-cli/src/wallet/fs.rs @@ -15,7 +15,7 @@ use autonomi::{get_evm_network_from_env, RewardsAddress, Wallet}; use const_hex::traits::FromHex; use prettytable::{Cell, Row, Table}; use std::ffi::OsString; -use std::io::Read; +use std::io::{Read, Write}; use std::path::PathBuf; use std::sync::OnceLock; @@ -137,7 +137,15 @@ pub(crate) fn select_wallet_address() -> Result { 0 => { let secret_key = get_secret_key_from_env().map_err(|_| Error::NoWalletsFoundAndNoSecretKeysInEnv)?; - Ok(secret_key) + let network = get_evm_network_from_env().expect("Could not load EVM network from environment"); + let wallet = Wallet::new_from_private_key(network, &secret_key).expect("Could not initialize wallet"); + let public_key = wallet.address().to_string(); + let wallet_directory = get_client_wallet_dir_path()?; + let file_path = std::path::Path::new(&wallet_directory).join(&public_key); + let mut file = std::fs::File::create(&file_path).expect("Could not create file on disk"); + file.write_all(secret_key.as_bytes()).expect("Could not write secret key to file"); + + Ok(public_key) } 1 => Ok(filter_wallet_file_extension(&wallet_files[0])), _ => get_wallet_selection(wallet_files), From 0843bba2a608f8f38a2d42b76dc8616f5775b4fa Mon Sep 17 00:00:00 2001 From: Warm Beer Date: Fri, 13 Dec 2024 10:52:26 +0100 Subject: [PATCH 2/2] refactor: auto select wallet from env if exists --- ant-cli/src/wallet/error.rs | 4 ++-- ant-cli/src/wallet/fs.rs | 23 ++++++++--------------- 2 files changed, 10 insertions(+), 17 deletions(-) diff --git a/ant-cli/src/wallet/error.rs b/ant-cli/src/wallet/error.rs index 1dd8fa6c91..26ca97bcbe 100644 --- a/ant-cli/src/wallet/error.rs +++ b/ant-cli/src/wallet/error.rs @@ -24,8 +24,8 @@ pub enum Error { FailedToCreateWalletsFolder, #[error("Could not find private key file")] PrivateKeyFileNotFound, - #[error("No wallets found and No secret Keys found in ENV, create one using `wallet create`")] - NoWalletsFoundAndNoSecretKeysInEnv, + #[error("No wallets found. Create one using `wallet create` or supply a private key using the `SECRET_KEY` environment variable")] + NoWalletsFound, #[error("Invalid wallet selection input")] InvalidSelection, } diff --git a/ant-cli/src/wallet/fs.rs b/ant-cli/src/wallet/fs.rs index 41415e7201..38c13e1537 100644 --- a/ant-cli/src/wallet/fs.rs +++ b/ant-cli/src/wallet/fs.rs @@ -6,7 +6,7 @@ // KIND, either express or implied. Please review the Licences for the specific language governing // permissions and limitations relating to use of the SAFE Network Software. -use crate::keys::get_secret_key_from_env; +use crate::keys::load_evm_wallet_from_env; use crate::wallet::encryption::{decrypt_private_key, encrypt_private_key}; use crate::wallet::error::Error; use crate::wallet::input::{get_password_input, get_wallet_selection_input}; @@ -15,7 +15,7 @@ use autonomi::{get_evm_network_from_env, RewardsAddress, Wallet}; use const_hex::traits::FromHex; use prettytable::{Cell, Row, Table}; use std::ffi::OsString; -use std::io::{Read, Write}; +use std::io::Read; use std::path::PathBuf; use std::sync::OnceLock; @@ -115,6 +115,11 @@ pub(crate) fn load_wallet_from_address(wallet_address: &str) -> Result Result { + // try if there is a wallet set in the ENV first + if let Ok(env_wallet) = load_evm_wallet_from_env() { + return Ok(env_wallet); + } + let wallet_address = select_wallet_address()?; load_wallet_from_address(&wallet_address) } @@ -134,19 +139,7 @@ pub(crate) fn select_wallet_address() -> Result { let wallet_files = get_wallet_files(&wallets_folder)?; let wallet_address = match wallet_files.len() { - 0 => { - let secret_key = - get_secret_key_from_env().map_err(|_| Error::NoWalletsFoundAndNoSecretKeysInEnv)?; - let network = get_evm_network_from_env().expect("Could not load EVM network from environment"); - let wallet = Wallet::new_from_private_key(network, &secret_key).expect("Could not initialize wallet"); - let public_key = wallet.address().to_string(); - let wallet_directory = get_client_wallet_dir_path()?; - let file_path = std::path::Path::new(&wallet_directory).join(&public_key); - let mut file = std::fs::File::create(&file_path).expect("Could not create file on disk"); - file.write_all(secret_key.as_bytes()).expect("Could not write secret key to file"); - - Ok(public_key) - } + 0 => Err(Error::NoWalletsFound), 1 => Ok(filter_wallet_file_extension(&wallet_files[0])), _ => get_wallet_selection(wallet_files), }?;