1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
pub mod addressbook;
mod contracts;
mod keyfile;
mod storage_ctrl;
pub use self::addressbook::error::AddressbookError;
pub use self::addressbook::AddressbookStorage;
pub use self::contracts::ContractStorage;
pub use self::keyfile::*;
pub use self::storage_ctrl::StorageController;
pub use self::KeystoreError;
use std::boxed::Box;
use std::fs;
use std::path::{Path, PathBuf};
use dirs;
#[derive(Debug, Clone)]
pub struct Storages {
base_dir: PathBuf,
}
#[cfg(all(
unix,
not(target_os = "macos"),
not(target_os = "ios"),
not(target_os = "android")
))]
pub fn default_path() -> PathBuf {
let mut config_dir = dirs::home_dir().expect("Expect path to home dir");
config_dir.push(".emerald");
config_dir
}
#[cfg(target_os = "macos")]
pub fn default_path() -> PathBuf {
let mut config_dir = dirs::home_dir().expect("Expect path to home dir");
config_dir.push("Library");
config_dir.push("Emerald");
config_dir
}
#[cfg(target_os = "windows")]
pub fn default_path() -> PathBuf {
let app_data_var = env::var("APPDATA").expect("Expect 'APPDATA' environment variable");
let mut config_dir = PathBuf::from(app_data_var);
config_dir.push(".emerald");
config_dir
}
pub fn build_path(base_path: &Path, chain: &str, folder: &str) -> PathBuf {
let mut path = PathBuf::from(base_path);
path.push(chain);
path.push(folder);
path
}
pub fn build_keyfile_storage<P>(path: P) -> Result<Box<KeyfileStorage>, KeystoreError>
where
P: AsRef<Path>,
{
#[cfg(feature = "default")]
{
let mut p = PathBuf::new();
p.push(path);
p.push(".db");
match DbStorage::new(p) {
Ok(db) => Ok(Box::new(db)),
Err(_) => Err(KeystoreError::StorageError(
"Can't create database Keyfile storage".to_string(),
)),
}
}
#[cfg(feature = "fs-storage")]
match FsStorage::new(path) {
Ok(fs) => Ok(Box::new(fs)),
Err(_) => Err(KeystoreError::StorageError(
"Can't create filesystem Keyfile storage".to_string(),
)),
}
}
pub fn build_contract_storage<P>(path: P) -> Result<Box<ContractStorage>, KeystoreError>
where
P: AsRef<Path>,
{
let mut p = PathBuf::new();
p.push(path);
fs::create_dir_all(&p)?;
Ok(Box::new(ContractStorage::new(p)))
}
pub fn build_addressbook_storage<P>(path: P) -> Result<Box<AddressbookStorage>, KeystoreError>
where
P: AsRef<Path>,
{
let mut p = PathBuf::new();
p.push(path);
fs::create_dir_all(&p)?;
Ok(Box::new(AddressbookStorage::new(p)))
}