use crate::common::cli_prompts::cli_options; use crate::common::network_paths_and_settings::block_extension_and_paths; use crate::exit; use crate::fs; use crate::log::error; use crate::miner::flag::{ request_mining_stop, set_mining_state, set_node_mode, MiningState, NodeMode, }; use crate::records::memory::connections::initialize_connection; use crate::wallets::structures::Wallet; use crate::Path; pub async fn create_file_paths() { // get all final network-scoped paths from the shared path helper let ( _network_name, _padded_base_coin, _short_base_coin, torrent_path, wallet_path, block_path, db_path, balance_path, log_path, ) = block_extension_and_paths(); // create_dir_all creates missing parent folders too, so the base // settings paths do not need to be created separately. fs::create_dir_all(&block_path).expect("Failed to create blocks folder"); fs::create_dir_all(&torrent_path).expect("Failed to create torrents folder"); fs::create_dir_all(&db_path).expect("Failed to create db folder"); // wallet_path points to the wallet file itself, so only its parent directory is created here. if let Some(wallet_parent) = Path::new(&wallet_path).parent() { if !wallet_parent.as_os_str().is_empty() { fs::create_dir_all(wallet_parent).expect("Failed to create wallet folder"); } } fs::create_dir_all(&balance_path).expect("Failed to create balance sheet folder"); fs::create_dir_all(&log_path).expect("Failed to create log folder"); } pub async fn obtain_valid_wallet() -> (String, Wallet) { // keep prompting until a wallet can be opened or created // using the supplied key material let wallet_path = Wallet::get_wallet_path().await; let wallet_exists = Path::new(&wallet_path).exists(); loop { let wallet_key = cli_options().await; match Wallet::try_obtain_wallet(wallet_key.clone(), None).await { Ok(wallet) => return (wallet_key, wallet), Err(e) => { // Existing wallets fail closed because a bad key cannot safely create a replacement. if wallet_exists { eprintln!("Wallet error: {e}."); error!("Wallet error: {e}."); exit(1); } println!("Wallet error: {e}. Please try again.\n"); } } } } pub async fn prepare_pre_wallet_startup() { // Prepare the local filesystem and in-memory startup state before any // wallet-dependent node identity or network activity begins. set_node_mode(NodeMode::Startup); request_mining_stop(); set_mining_state(MiningState::Idle); initialize_connection().await; create_file_paths().await; } pub async fn obtain_startup_wallet() -> Wallet { // Open or create the configured wallet and return the validated // wallet once it can be used safely. let (_wallet_key, wallet) = obtain_valid_wallet().await; wallet } pub async fn open_chain_state() -> sled::Db { // Open the sled state database once the process is fully ready to continue startup. let ( _network_name, _padded_base_coin, _suffix, _torrent_path, _wallet_path, _block_path, db_path, _balance_path, _log_path, ) = block_extension_and_paths(); let db = sled::Config::new() .path(db_path) .open() .expect("Failed to open the database"); db }