lssa/wallet/src/helperfunctions.rs

50 lines
1.5 KiB
Rust
Raw Normal View History

2025-08-07 14:07:34 +03:00
use std::{fs::File, io::BufReader, path::PathBuf, str::FromStr};
2025-08-13 13:42:00 +03:00
use anyhow::Result;
2025-08-18 16:15:25 +03:00
use key_protocol::key_protocol_core::NSSAUserData;
2025-08-14 14:03:48 +03:00
use nssa::Address;
2025-08-07 14:07:34 +03:00
2025-08-11 08:55:08 +03:00
use crate::{config::WalletConfig, HOME_DIR_ENV_VAR};
2025-08-07 14:07:34 +03:00
///Get home dir for wallet. Env var `NSSA_WALLET_HOME_DIR` must be set before execution to succeed.
pub fn get_home() -> Result<PathBuf> {
Ok(PathBuf::from_str(&std::env::var(HOME_DIR_ENV_VAR)?)?)
}
///Fetch config from `NSSA_WALLET_HOME_DIR`
2025-08-11 08:55:08 +03:00
pub fn fetch_config() -> Result<WalletConfig> {
2025-08-07 14:07:34 +03:00
let config_home = get_home()?;
2025-08-11 08:55:08 +03:00
let file = File::open(config_home.join("wallet_config.json"))?;
2025-08-07 14:07:34 +03:00
let reader = BufReader::new(file);
Ok(serde_json::from_reader(reader)?)
}
//ToDo: Replace with structures conversion in future
2025-08-13 13:42:00 +03:00
pub fn produce_account_addr_from_hex(hex_str: String) -> Result<Address> {
2025-08-14 14:03:48 +03:00
Ok(hex_str.parse()?)
2025-08-07 14:07:34 +03:00
}
2025-08-08 15:22:04 +03:00
///Fetch list of accounts stored at `NSSA_WALLET_HOME_DIR/curr_accounts.json`
///
/// If file not present, it is considered as empty list of persistent accounts
2025-08-18 16:15:25 +03:00
///
2025-08-15 14:27:36 +03:00
/// ToDo: NOT USER DATA, ACCOUNT
pub fn fetch_persistent_accounts() -> Result<Vec<NSSAUserData>> {
2025-08-08 15:22:04 +03:00
let home = get_home()?;
let accs_path = home.join("curr_accounts.json");
match File::open(accs_path) {
Ok(file) => {
let reader = BufReader::new(file);
Ok(serde_json::from_reader(reader)?)
}
Err(err) => match err.kind() {
std::io::ErrorKind::NotFound => Ok(vec![]),
_ => {
anyhow::bail!("IO error {err:#?}");
}
},
}
}