lssa/wallet/src/helperfunctions.rs

85 lines
2.3 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-20 17:16:51 +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-19 14:14:09 +03:00
use crate::{
HOME_DIR_ENV_VAR,
2025-09-04 14:38:41 +03:00
config::{PersistentAccountData, WalletConfig},
2025-08-19 14:14:09 +03:00
};
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-09-02 09:01:33 +03:00
pub fn fetch_persistent_accounts() -> Result<Vec<PersistentAccountData>> {
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:#?}");
}
},
}
}
2025-08-20 17:16:51 +03:00
2025-08-22 15:58:43 +03:00
///Produces a list of accounts for storage
2025-09-02 09:01:33 +03:00
pub fn produce_data_for_storage(user_data: &NSSAUserData) -> Vec<PersistentAccountData> {
2025-08-20 17:16:51 +03:00
let mut vec_for_storage = vec![];
2025-09-08 15:03:02 +03:00
for (addr, key) in &user_data.pub_account_signing_keys {
2025-09-02 09:01:33 +03:00
vec_for_storage.push(PersistentAccountData {
2025-08-20 17:16:51 +03:00
address: *addr,
pub_sign_key: key.clone(),
});
}
vec_for_storage
}
2025-08-26 09:16:46 +03:00
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_get_home_get_env_var() {
2025-09-04 14:38:41 +03:00
unsafe {
std::env::set_var(HOME_DIR_ENV_VAR, "/path/to/configs");
}
2025-08-26 09:16:46 +03:00
let home = get_home().unwrap();
assert_eq!(PathBuf::from_str("/path/to/configs").unwrap(), home);
2025-09-04 14:38:41 +03:00
unsafe {
std::env::remove_var(HOME_DIR_ENV_VAR);
}
2025-08-26 09:16:46 +03:00
}
}