1414
1515//! Configuration file management
1616
17+ use dirs;
1718use rand:: distributions:: { Alphanumeric , Distribution } ;
1819use rand:: thread_rng;
1920use std:: env;
@@ -34,17 +35,15 @@ use crate::util::logger::LoggingConfig;
3435/// Wallet configuration file name
3536pub const WALLET_CONFIG_FILE_NAME : & str = "grin-wallet.toml" ;
3637const WALLET_LOG_FILE_NAME : & str = "grin-wallet.log" ;
37- /// .grin folder, usually in home/.grin
38- pub const GRIN_HOME : & str = ".grin" ;
38+ const GRIN_HOME : & str = ".grin" ;
3939/// Wallet data directory
4040pub const GRIN_WALLET_DIR : & str = "wallet_data" ;
4141/// Node API secret
4242pub const API_SECRET_FILE_NAME : & str = ".foreign_api_secret" ;
4343/// Owner API secret
4444pub const OWNER_API_SECRET_FILE_NAME : & str = ".owner_api_secret" ;
4545
46- /// Function to get wallet dir and create dirs if not existing
47- pub fn get_wallet_path (
46+ fn get_grin_path (
4847 chain_type : & global:: ChainTypes ,
4948 create_path : bool ,
5049) -> Result < PathBuf , ConfigError > {
@@ -69,49 +68,6 @@ pub fn get_wallet_path(
6968 }
7069}
7170
72- /// Smart function to find the most likely .api_secret location for the node
73- pub fn get_node_path (
74- data_path : Option < PathBuf > ,
75- chain_type : & global:: ChainTypes ,
76- ) -> Result < PathBuf , ConfigError > {
77- let node_path = match data_path {
78- // 1) A If top dir provided and api_secret exist, return top dir
79- Some ( path) => {
80- let mut node_path = path;
81- node_path. push ( GRIN_HOME ) ;
82- node_path. push ( chain_type. shortname ( ) ) ;
83- node_path. push ( API_SECRET_FILE_NAME ) ;
84- if node_path. exists ( ) {
85- node_path. pop ( ) ;
86- Ok ( node_path)
87-
88- // 1) B If top dir exists, but no config there, return home dir
89- } else {
90- let mut node_path = match dirs:: home_dir ( ) {
91- Some ( p) => p,
92- None => PathBuf :: new ( ) ,
93- } ;
94- node_path. push ( GRIN_HOME ) ;
95- node_path. push ( chain_type. shortname ( ) ) ;
96- Ok ( node_path)
97- }
98- }
99-
100- // 2) If there is no top_dir provided, always return home dir
101- None => {
102- let mut node_path = match dirs:: home_dir ( ) {
103- Some ( p) => p,
104- None => PathBuf :: new ( ) ,
105- } ;
106- node_path. push ( GRIN_HOME ) ;
107- node_path. push ( chain_type. shortname ( ) ) ;
108- Ok ( node_path)
109- }
110- } ;
111- node_path
112- }
113-
114- /// Checks if config in current working dir
11571fn check_config_current_dir ( path : & str ) -> Option < PathBuf > {
11672 let p = env:: current_dir ( ) ;
11773 let mut c = match p {
@@ -166,7 +122,7 @@ fn check_api_secret_file(
166122) -> Result < ( ) , ConfigError > {
167123 let grin_path = match data_path {
168124 Some ( p) => p,
169- None => get_node_path ( data_path , chain_type ) ?,
125+ None => get_grin_path ( chain_type , false ) ?,
170126 } ;
171127 let mut api_secret_path = grin_path;
172128 api_secret_path. push ( file_name) ;
@@ -178,7 +134,6 @@ fn check_api_secret_file(
178134}
179135
180136/// Handles setup and detection of paths for wallet
181- /// Use config file in a) current dir as template, b) in top path, or c) .grin home
182137pub fn initial_setup_wallet (
183138 chain_type : & global:: ChainTypes ,
184139 data_path : Option < PathBuf > ,
@@ -189,45 +144,33 @@ pub fn initial_setup_wallet(
189144 fs:: create_dir_all ( p) ?;
190145 }
191146 }
192-
193- // Get wallet data_dir path, create it if it does not exist
194- let wallet_path = match data_path {
195- Some ( p) => {
196- let mut abs_wallet_path = std:: env:: current_dir ( ) ?;
197- abs_wallet_path. push ( p) ;
198- abs_wallet_path
199- }
200- None => get_wallet_path ( chain_type, create_path) ?,
201- } ;
202-
203- // Get path to the node dir(s), first try top dir, if no node api_secret return home./grin
204- let node_path = get_node_path ( Some ( wallet_path. clone ( ) ) , chain_type) ?;
205-
206- // Get path to the newwly to be created config file
207- let mut config_path = wallet_path. clone ( ) ;
208- config_path. push ( WALLET_CONFIG_FILE_NAME ) ;
209-
210- // Check if config exists in working dir, if so, use it as template for newly created config
147+ // Use config file if current directory if it exists, .grin home otherwise
211148 let ( path, config) = if let Some ( p) = check_config_current_dir ( WALLET_CONFIG_FILE_NAME ) {
212149 let mut path = p. clone ( ) ;
213150 path. pop ( ) ;
214- let mut config = GlobalWalletConfig :: new ( p. to_str ( ) . unwrap ( ) ) ?;
215- // Use template config, update data_dir, network and api secrets
216- config. config_file_path = Some ( config_path) ;
217- config. update_paths ( & wallet_path, & node_path) ;
218- ( path, config)
151+ ( path, GlobalWalletConfig :: new ( p. to_str ( ) . unwrap ( ) ) ?)
219152 } else {
220- // Return defaults config updated with node and wallet dir and chain dir
153+ // Check if grin dir exists
154+ let grin_path = match data_path {
155+ Some ( p) => p,
156+ None => get_grin_path ( chain_type, create_path) ?,
157+ } ;
158+
159+ // Get path to default config file
160+ let mut config_path = grin_path. clone ( ) ;
161+ config_path. push ( WALLET_CONFIG_FILE_NAME ) ;
162+
163+ // Return defaults if file doesn't exist
221164 match config_path. clone ( ) . exists ( ) {
222165 false => {
223166 let mut default_config = GlobalWalletConfig :: for_chain ( chain_type) ;
224167 default_config. config_file_path = Some ( config_path) ;
225- // Update paths relative to current dir
226- default_config. update_paths ( & wallet_path , & node_path ) ;
227- ( wallet_path , default_config)
168+ // update paths relative to current dir
169+ default_config. update_paths ( & grin_path ) ;
170+ ( grin_path , default_config)
228171 }
229172 true => {
230- let mut path = wallet_path . clone ( ) ;
173+ let mut path = config_path . clone ( ) ;
231174 path. pop ( ) ;
232175 (
233176 path,
@@ -236,7 +179,7 @@ pub fn initial_setup_wallet(
236179 }
237180 }
238181 } ;
239- // Check API secrets, if ok, return config
182+
240183 check_api_secret_file ( chain_type, Some ( path. clone ( ) ) , OWNER_API_SECRET_FILE_NAME ) ?;
241184 check_api_secret_file ( chain_type, Some ( path) , API_SECRET_FILE_NAME ) ?;
242185 Ok ( config)
@@ -325,7 +268,7 @@ impl GlobalWalletConfig {
325268 }
326269
327270 /// Update paths
328- pub fn update_paths ( & mut self , wallet_home : & PathBuf , node_home : & PathBuf ) {
271+ pub fn update_paths ( & mut self , wallet_home : & PathBuf ) {
329272 let mut wallet_path = wallet_home. clone ( ) ;
330273 wallet_path. push ( GRIN_WALLET_DIR ) ;
331274 self . members . as_mut ( ) . unwrap ( ) . wallet . data_file_dir =
@@ -334,7 +277,7 @@ impl GlobalWalletConfig {
334277 secret_path. push ( OWNER_API_SECRET_FILE_NAME ) ;
335278 self . members . as_mut ( ) . unwrap ( ) . wallet . api_secret_path =
336279 Some ( secret_path. to_str ( ) . unwrap ( ) . to_owned ( ) ) ;
337- let mut node_secret_path = node_home . clone ( ) ;
280+ let mut node_secret_path = wallet_home . clone ( ) ;
338281 node_secret_path. push ( API_SECRET_FILE_NAME ) ;
339282 self . members . as_mut ( ) . unwrap ( ) . wallet . node_api_secret_path =
340283 Some ( node_secret_path. to_str ( ) . unwrap ( ) . to_owned ( ) ) ;
0 commit comments