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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
mod address;
#[macro_use]
pub mod byte_array;
mod crypto;
mod error;
pub use self::address::try_extract_address;
pub use self::crypto::{CoreCrypto, Iv, Mac};
pub use self::error::Error;
use super::core::{self, Address};
use super::util;
use super::HdwalletCrypto;
use super::{Cipher, CryptoType, KdfParams, KeyFile, Salt, CIPHER_IV_BYTES};
use serde::ser;
use serde::{Serialize, Serializer};
use serde_json;
use uuid::Uuid;
pub const CURRENT_VERSION: u8 = 3;
pub const SUPPORTED_VERSIONS: &[u8] = &[CURRENT_VERSION];
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct SerializableKeyFileCore {
version: u8,
id: Uuid,
address: Address,
name: Option<String>,
description: Option<String>,
visible: Option<bool>,
crypto: CoreCrypto,
}
impl SerializableKeyFileCore {
fn try_from(kf: KeyFile) -> Result<Self, Error> {
let cr = CoreCrypto::try_from(&kf)?;
Ok(SerializableKeyFileCore {
version: CURRENT_VERSION,
id: kf.uuid,
address: kf.address,
name: kf.name.clone(),
description: kf.description.clone(),
visible: kf.visible,
crypto: cr,
})
}
}
impl Into<KeyFile> for SerializableKeyFileCore {
fn into(self) -> KeyFile {
KeyFile {
name: self.name,
description: self.description,
address: self.address,
visible: self.visible,
uuid: self.id,
crypto: CryptoType::Core(self.crypto),
}
}
}
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct SerializableKeyFileHD {
version: u8,
id: Uuid,
address: Address,
name: Option<String>,
description: Option<String>,
visible: Option<bool>,
crypto: HdwalletCrypto,
}
impl SerializableKeyFileHD {
fn try_from(kf: &KeyFile) -> Result<Self, Error> {
let cr = HdwalletCrypto::try_from(kf)?;
Ok(SerializableKeyFileHD {
version: CURRENT_VERSION,
id: kf.uuid,
address: kf.address,
name: kf.name.clone(),
description: kf.description.clone(),
visible: kf.visible,
crypto: cr,
})
}
}
impl Into<KeyFile> for SerializableKeyFileHD {
fn into(self) -> KeyFile {
KeyFile {
name: self.name,
description: self.description,
address: self.address,
visible: self.visible,
uuid: self.id,
crypto: CryptoType::HdWallet(self.crypto),
}
}
}
impl KeyFile {
pub fn decode(f: &str) -> Result<KeyFile, Error> {
let buf = f.to_string().to_lowercase();
let mut ver = 0;
let kf = serde_json::from_str::<SerializableKeyFileCore>(&buf)
.and_then(|core| {
ver = core.version;
Ok(core.into())
})
.or_else(|_| {
serde_json::from_str::<SerializableKeyFileHD>(&buf).and_then(|hd| {
ver = hd.version;
Ok(hd.into())
})
})
.map_err(Error::from)?;
if !SUPPORTED_VERSIONS.contains(&ver) {
return Err(Error::UnsupportedVersion(ver));
}
Ok(kf)
}
}
impl Serialize for KeyFile {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
match SerializableKeyFileCore::try_from(self.clone()) {
Ok(sf) => sf.serialize(serializer),
Err(_) => match SerializableKeyFileHD::try_from(self) {
Ok(s) => s.serialize(serializer),
Err(e) => Err(ser::Error::custom(e)),
},
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use storage::generate_filename;
use tests::*;
#[test]
fn should_catch_unsupported_keyfile_version() {
let s = r#"{
"version": 2,
"id": "9bec4728-37f9-4444-9990-2ba70ee038e9"
}"#;
assert!(KeyFile::decode(s).is_err());
}
#[test]
fn should_catch_keyfile_version_malformed() {
let s = r#"{
"version": "x",
"id": "9bec4728-37f9-4444-9990-2ba70ee038e9"
}"#;
assert!(KeyFile::decode(s).is_err());
}
#[test]
fn should_catch_keyfile_uuid_malformed() {
let s = r#"{
"version": 3,
"id": "__ec4728-37f9-4444-9990-2ba70ee038e9"
}"#;
assert!(KeyFile::decode(s).is_err());
}
#[test]
fn should_catch_absent_keyfile_uuid() {
let s = r#"{"version": 3}"#;
assert!(KeyFile::decode(s).is_err());
}
#[test]
fn should_generate_filename() {
let re = Regex::new(r"^UTC--\d{4}-\d{2}-\d{2}T\d{2}-\d{2}-\d{2}Z--*").unwrap();
assert!(re.is_match(&generate_filename("9bec4728-37f9-4444-9990-2ba70ee038e9"),));
}
}