diff --git a/wallet/src/helperfunctions.rs b/wallet/src/helperfunctions.rs index a261e6c..e602b02 100644 --- a/wallet/src/helperfunctions.rs +++ b/wallet/src/helperfunctions.rs @@ -1,10 +1,7 @@ use base64::{Engine, engine::general_purpose::STANDARD as BASE64}; use nssa_core::account::Nonce; use rand::{RngCore, rngs::OsRng}; -use std::{ - path::{Path, PathBuf}, - str::FromStr, -}; +use std::{path::PathBuf, str::FromStr}; use tokio::io::{AsyncReadExt, AsyncWriteExt}; use anyhow::Result; @@ -20,18 +17,29 @@ use crate::{ }; /// Get home dir for wallet. Env var `NSSA_WALLET_HOME_DIR` must be set before execution to succeed. -pub fn get_home() -> Result { +pub fn get_home_nssa_var() -> Result { Ok(PathBuf::from_str(&std::env::var(HOME_DIR_ENV_VAR)?)?) } -pub fn get_home_default_path_linux() -> PathBuf { - let home = std::env::var("HOME").unwrap(); - Path::new(&home).join(".nssa").join("wallet") +/// Get home dir for wallet. Env var `HOME` must be set before execution to succeed. +pub fn get_home_default_path() -> Result { + std::env::home_dir() + .map(|path| path.join(".nssa").join("wallet")) + .ok_or(anyhow::anyhow!("Failed to get HOME")) } -/// Fetch config from `NSSA_WALLET_HOME_DIR` -pub async fn fetch_config_default_path_linux() -> Result { - let config_home = get_home_default_path_linux(); +/// Get home dir for wallet. +pub fn get_home() -> Result { + if let Ok(home) = get_home_nssa_var() { + Ok(home) + } else { + get_home_default_path() + } +} + +/// Fetch config from default home +pub async fn fetch_config() -> Result { + let config_home = get_home()?; let mut config_needs_setup = false; let config = match tokio::fs::OpenOptions::new() @@ -79,15 +87,7 @@ pub async fn fetch_config_default_path_linux() -> Result { Ok(config) } -/// Fetch config from `NSSA_WALLET_HOME_DIR` -pub async fn fetch_config() -> Result { - let config_home = get_home()?; - let config_contents = tokio::fs::read(config_home.join("wallet_config.json")).await?; - - Ok(serde_json::from_slice(&config_contents)?) -} - -/// Fetch data stored at `NSSA_WALLET_HOME_DIR/storage.json` +/// Fetch data stored at home /// /// If file not present, it is considered as empty list of persistent accounts pub async fn fetch_persistent_storage() -> Result { diff --git a/wallet/src/lib.rs b/wallet/src/lib.rs index 94be7e3..bfc2e54 100644 --- a/wallet/src/lib.rs +++ b/wallet/src/lib.rs @@ -28,7 +28,7 @@ use crate::{ token_program::TokenProgramAgnosticSubcommand, }, config::PersistentStorage, - helperfunctions::{fetch_config_default_path_linux, fetch_persistent_storage}, + helperfunctions::fetch_persistent_storage, }; use crate::{ helperfunctions::{fetch_config, get_home, produce_data_for_storage}, @@ -216,7 +216,10 @@ pub enum Command { /// Check the wallet can connect to the node and builtin local programs /// match the remote versions CheckHealth {}, - TestCommand {}, + /// Command to explicitly setup config and storage + /// + /// Does nothing in case if both already present + Setup {}, } ///To execute commands, env var NSSA_WALLET_HOME_DIR must be set into directory with config @@ -246,7 +249,7 @@ pub enum SubcommandReturnValue { } pub async fn execute_subcommand(command: Command) -> Result { - let wallet_config = fetch_config_default_path_linux().await?; + let wallet_config = fetch_config().await?; let mut wallet_core = WalletCore::start_from_config_update_chain(wallet_config).await?; let subcommand_ret = match command { @@ -301,8 +304,10 @@ pub async fn execute_subcommand(command: Command) -> Result { token_subcommand.handle_subcommand(&mut wallet_core).await? } - Command::TestCommand {} => { - println!("Hello from test"); + Command::Setup {} => { + let path = wallet_core.store_persistent_data().await?; + + println!("Stored persistent accounts at {path:#?}"); SubcommandReturnValue::Empty }