1use app_dirs::{get_app_root, AppDataType, AppInfo};
6use cfxcore_accounts::{AccountProvider, AccountProviderSettings};
7use cfxstore::{accounts_dir::RootDiskDirectory, CfxStore};
8use dir::helpers::replace_home;
9use std::{path::PathBuf, time::Duration};
10
11pub fn account_provider(
12 dir: Option<String>, sstore_iterations: Option<u32>,
13 refresh_time: Option<Duration>,
14) -> Result<AccountProvider, String> {
15 let dir = match dir {
16 Some(dir) => dir,
17 None => keys_path(),
18 };
19
20 let dir = Box::new(keys_dir(dir)?);
21 let secret_store = Box::new(secret_store(dir, sstore_iterations)?);
22
23 if let Some(t) = refresh_time {
24 secret_store.set_refresh_time(t);
25 }
26
27 Ok(AccountProvider::new(
28 secret_store,
29 AccountProviderSettings::default(),
30 ))
31}
32
33pub fn keys_dir(path: String) -> Result<RootDiskDirectory, String> {
34 let path = PathBuf::from(&path);
35 RootDiskDirectory::create(path)
36 .map_err(|e| format!("Could not open keys directory: {}", e))
37}
38
39fn secret_store(
40 dir: Box<RootDiskDirectory>, iterations: Option<u32>,
41) -> Result<CfxStore, String> {
42 match iterations {
43 Some(i) => CfxStore::open_with_iterations(dir, i),
44 None => CfxStore::open(dir),
45 }
46 .map_err(|e| format!("Could not open keys store: {}", e))
47}
48
49fn default_data_path() -> String {
51 let app_info = AppInfo {
52 name: "Conflux",
53 author: "conflux",
54 };
55 get_app_root(AppDataType::UserData, &app_info)
56 .map(|p| p.to_string_lossy().into_owned())
57 .unwrap_or_else(|_| "$HOME/.conflux".to_owned())
58}
59
60pub fn keys_path() -> String {
61 let data_path = default_data_path();
62 replace_home(&data_path, "$BASE/keys")
63}