14
14
15
15
//! Configuration file management
16
16
17
+ use dirs;
17
18
use rand:: distributions:: { Alphanumeric , Distribution } ;
18
19
use rand:: thread_rng;
19
20
use std:: env;
@@ -34,17 +35,15 @@ use crate::util::logger::LoggingConfig;
34
35
/// Wallet configuration file name
35
36
pub const WALLET_CONFIG_FILE_NAME : & str = "grin-wallet.toml" ;
36
37
const 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" ;
39
39
/// Wallet data directory
40
40
pub const GRIN_WALLET_DIR : & str = "wallet_data" ;
41
41
/// Node API secret
42
42
pub const API_SECRET_FILE_NAME : & str = ".foreign_api_secret" ;
43
43
/// Owner API secret
44
44
pub const OWNER_API_SECRET_FILE_NAME : & str = ".owner_api_secret" ;
45
45
46
- /// Function to get wallet dir and create dirs if not existing
47
- pub fn get_wallet_path (
46
+ fn get_grin_path (
48
47
chain_type : & global:: ChainTypes ,
49
48
create_path : bool ,
50
49
) -> Result < PathBuf , ConfigError > {
@@ -69,49 +68,6 @@ pub fn get_wallet_path(
69
68
}
70
69
}
71
70
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
115
71
fn check_config_current_dir ( path : & str ) -> Option < PathBuf > {
116
72
let p = env:: current_dir ( ) ;
117
73
let mut c = match p {
@@ -166,7 +122,7 @@ fn check_api_secret_file(
166
122
) -> Result < ( ) , ConfigError > {
167
123
let grin_path = match data_path {
168
124
Some ( p) => p,
169
- None => get_node_path ( data_path , chain_type ) ?,
125
+ None => get_grin_path ( chain_type , false ) ?,
170
126
} ;
171
127
let mut api_secret_path = grin_path;
172
128
api_secret_path. push ( file_name) ;
@@ -178,7 +134,6 @@ fn check_api_secret_file(
178
134
}
179
135
180
136
/// 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
182
137
pub fn initial_setup_wallet (
183
138
chain_type : & global:: ChainTypes ,
184
139
data_path : Option < PathBuf > ,
@@ -189,45 +144,33 @@ pub fn initial_setup_wallet(
189
144
fs:: create_dir_all ( p) ?;
190
145
}
191
146
}
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
211
148
let ( path, config) = if let Some ( p) = check_config_current_dir ( WALLET_CONFIG_FILE_NAME ) {
212
149
let mut path = p. clone ( ) ;
213
150
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 ( ) ) ?)
219
152
} 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
221
164
match config_path. clone ( ) . exists ( ) {
222
165
false => {
223
166
let mut default_config = GlobalWalletConfig :: for_chain ( chain_type) ;
224
167
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)
228
171
}
229
172
true => {
230
- let mut path = wallet_path . clone ( ) ;
173
+ let mut path = config_path . clone ( ) ;
231
174
path. pop ( ) ;
232
175
(
233
176
path,
@@ -236,7 +179,7 @@ pub fn initial_setup_wallet(
236
179
}
237
180
}
238
181
} ;
239
- // Check API secrets, if ok, return config
182
+
240
183
check_api_secret_file ( chain_type, Some ( path. clone ( ) ) , OWNER_API_SECRET_FILE_NAME ) ?;
241
184
check_api_secret_file ( chain_type, Some ( path) , API_SECRET_FILE_NAME ) ?;
242
185
Ok ( config)
@@ -325,7 +268,7 @@ impl GlobalWalletConfig {
325
268
}
326
269
327
270
/// 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 ) {
329
272
let mut wallet_path = wallet_home. clone ( ) ;
330
273
wallet_path. push ( GRIN_WALLET_DIR ) ;
331
274
self . members . as_mut ( ) . unwrap ( ) . wallet . data_file_dir =
@@ -334,7 +277,7 @@ impl GlobalWalletConfig {
334
277
secret_path. push ( OWNER_API_SECRET_FILE_NAME ) ;
335
278
self . members . as_mut ( ) . unwrap ( ) . wallet . api_secret_path =
336
279
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 ( ) ;
338
281
node_secret_path. push ( API_SECRET_FILE_NAME ) ;
339
282
self . members . as_mut ( ) . unwrap ( ) . wallet . node_api_secret_path =
340
283
Some ( node_secret_path. to_str ( ) . unwrap ( ) . to_owned ( ) ) ;
0 commit comments