2017-01-25 18:51:41 +01:00
|
|
|
// Copyright 2015-2017 Parity Technologies (UK) Ltd.
|
2016-06-20 10:06:49 +02:00
|
|
|
// This file is part of Parity.
|
|
|
|
|
|
|
|
// Parity is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
|
|
|
// Parity is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2016-06-20 00:10:34 +02:00
|
|
|
use std::io::{Read, Write};
|
|
|
|
use serde::{Deserialize, Deserializer, Error};
|
|
|
|
use serde::de::{Visitor, MapVisitor};
|
|
|
|
use serde_json;
|
2016-12-09 23:01:43 +01:00
|
|
|
use super::{Uuid, Version, Crypto, H160};
|
2016-06-20 00:10:34 +02:00
|
|
|
|
|
|
|
#[derive(Debug, PartialEq, Serialize)]
|
|
|
|
pub struct KeyFile {
|
2016-12-09 23:01:43 +01:00
|
|
|
pub id: Uuid,
|
2016-06-20 00:10:34 +02:00
|
|
|
pub version: Version,
|
|
|
|
pub crypto: Crypto,
|
|
|
|
pub address: H160,
|
2016-07-24 17:38:21 +02:00
|
|
|
pub name: Option<String>,
|
|
|
|
pub meta: Option<String>,
|
2016-06-20 00:10:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
enum KeyFileField {
|
2016-12-09 23:01:43 +01:00
|
|
|
Id,
|
2016-06-20 00:10:34 +02:00
|
|
|
Version,
|
|
|
|
Crypto,
|
|
|
|
Address,
|
2016-07-24 17:38:21 +02:00
|
|
|
Name,
|
|
|
|
Meta,
|
2016-06-20 00:10:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Deserialize for KeyFileField {
|
|
|
|
fn deserialize<D>(deserializer: &mut D) -> Result<KeyFileField, D::Error>
|
|
|
|
where D: Deserializer
|
|
|
|
{
|
|
|
|
deserializer.deserialize(KeyFileFieldVisitor)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct KeyFileFieldVisitor;
|
|
|
|
|
|
|
|
impl Visitor for KeyFileFieldVisitor {
|
|
|
|
type Value = KeyFileField;
|
|
|
|
|
|
|
|
fn visit_str<E>(&mut self, value: &str) -> Result<Self::Value, E>
|
|
|
|
where E: Error
|
|
|
|
{
|
|
|
|
match value {
|
2016-12-09 23:01:43 +01:00
|
|
|
"id" => Ok(KeyFileField::Id),
|
2016-06-20 00:10:34 +02:00
|
|
|
"version" => Ok(KeyFileField::Version),
|
|
|
|
"crypto" => Ok(KeyFileField::Crypto),
|
|
|
|
"Crypto" => Ok(KeyFileField::Crypto),
|
|
|
|
"address" => Ok(KeyFileField::Address),
|
2016-07-24 17:38:21 +02:00
|
|
|
"name" => Ok(KeyFileField::Name),
|
|
|
|
"meta" => Ok(KeyFileField::Meta),
|
2016-06-20 00:10:34 +02:00
|
|
|
_ => Err(Error::custom(format!("Unknown field: '{}'", value))),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Deserialize for KeyFile {
|
|
|
|
fn deserialize<D>(deserializer: &mut D) -> Result<KeyFile, D::Error>
|
|
|
|
where D: Deserializer
|
|
|
|
{
|
|
|
|
static FIELDS: &'static [&'static str] = &["id", "version", "crypto", "Crypto", "address"];
|
|
|
|
deserializer.deserialize_struct("KeyFile", FIELDS, KeyFileVisitor)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct KeyFileVisitor;
|
|
|
|
|
|
|
|
impl Visitor for KeyFileVisitor {
|
|
|
|
type Value = KeyFile;
|
|
|
|
|
|
|
|
fn visit_map<V>(&mut self, mut visitor: V) -> Result<Self::Value, V::Error>
|
|
|
|
where V: MapVisitor
|
|
|
|
{
|
|
|
|
let mut id = None;
|
|
|
|
let mut version = None;
|
|
|
|
let mut crypto = None;
|
|
|
|
let mut address = None;
|
2016-07-24 17:38:21 +02:00
|
|
|
let mut name = None;
|
|
|
|
let mut meta = None;
|
2016-06-20 00:10:34 +02:00
|
|
|
|
|
|
|
loop {
|
2016-12-27 12:53:56 +01:00
|
|
|
match visitor.visit_key()? {
|
|
|
|
Some(KeyFileField::Id) => { id = Some(visitor.visit_value()?); }
|
|
|
|
Some(KeyFileField::Version) => { version = Some(visitor.visit_value()?); }
|
|
|
|
Some(KeyFileField::Crypto) => { crypto = Some(visitor.visit_value()?); }
|
|
|
|
Some(KeyFileField::Address) => { address = Some(visitor.visit_value()?); }
|
2016-09-28 15:47:52 +02:00
|
|
|
Some(KeyFileField::Name) => { name = visitor.visit_value().ok(); } // ignore anyhing that is not a string to be permissive.
|
2016-07-24 17:38:21 +02:00
|
|
|
Some(KeyFileField::Meta) => { meta = visitor.visit_value().ok(); } // ignore anyhing that is not a string to be permissive.
|
2016-06-20 00:10:34 +02:00
|
|
|
None => { break; }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let id = match id {
|
|
|
|
Some(id) => id,
|
2016-12-27 12:53:56 +01:00
|
|
|
None => visitor.missing_field("id")?,
|
2016-06-20 00:10:34 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
let version = match version {
|
|
|
|
Some(version) => version,
|
2016-12-27 12:53:56 +01:00
|
|
|
None => visitor.missing_field("version")?,
|
2016-06-20 00:10:34 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
let crypto = match crypto {
|
|
|
|
Some(crypto) => crypto,
|
2016-12-27 12:53:56 +01:00
|
|
|
None => visitor.missing_field("crypto")?,
|
2016-06-20 00:10:34 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
let address = match address {
|
|
|
|
Some(address) => address,
|
2016-12-27 12:53:56 +01:00
|
|
|
None => visitor.missing_field("address")?,
|
2016-06-20 00:10:34 +02:00
|
|
|
};
|
|
|
|
|
2016-12-27 12:53:56 +01:00
|
|
|
visitor.end()?;
|
2016-06-20 00:10:34 +02:00
|
|
|
|
|
|
|
let result = KeyFile {
|
|
|
|
id: id,
|
|
|
|
version: version,
|
|
|
|
crypto: crypto,
|
|
|
|
address: address,
|
2016-07-24 17:38:21 +02:00
|
|
|
name: name,
|
|
|
|
meta: meta,
|
2016-06-20 00:10:34 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
Ok(result)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl KeyFile {
|
|
|
|
pub fn load<R>(reader: R) -> Result<Self, serde_json::Error> where R: Read {
|
|
|
|
serde_json::from_reader(reader)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn write<W>(&self, writer: &mut W) -> Result<(), serde_json::Error> where W: Write {
|
|
|
|
serde_json::to_writer(writer, self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use std::str::FromStr;
|
|
|
|
use serde_json;
|
2016-12-09 23:01:43 +01:00
|
|
|
use json::{KeyFile, Uuid, Version, Crypto, Cipher, Aes128Ctr, Kdf, Scrypt};
|
2016-06-20 00:10:34 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn basic_keyfile() {
|
|
|
|
let json = r#"
|
|
|
|
{
|
|
|
|
"address": "6edddfc6349aff20bc6467ccf276c5b52487f7a8",
|
|
|
|
"crypto": {
|
|
|
|
"cipher": "aes-128-ctr",
|
|
|
|
"ciphertext": "7203da0676d141b138cd7f8e1a4365f59cc1aa6978dc5443f364ca943d7cb4bc",
|
|
|
|
"cipherparams": {
|
|
|
|
"iv": "b5a7ec855ec9e2c405371356855fec83"
|
|
|
|
},
|
|
|
|
"kdf": "scrypt",
|
|
|
|
"kdfparams": {
|
|
|
|
"dklen": 32,
|
|
|
|
"n": 262144,
|
|
|
|
"p": 1,
|
|
|
|
"r": 8,
|
|
|
|
"salt": "1e8642fdf1f87172492c1412fc62f8db75d796cdfa9c53c3f2b11e44a2a1b209"
|
|
|
|
},
|
|
|
|
"mac": "46325c5d4e8c991ad2683d525c7854da387138b6ca45068985aa4959fa2b8c8f"
|
|
|
|
},
|
|
|
|
"id": "8777d9f6-7860-4b9b-88b7-0b57ee6b3a73",
|
2016-07-24 17:38:21 +02:00
|
|
|
"version": 3,
|
|
|
|
"name": "Test",
|
|
|
|
"meta": "{}"
|
2016-06-20 00:10:34 +02:00
|
|
|
}"#;
|
|
|
|
|
|
|
|
let expected = KeyFile {
|
2016-12-09 23:01:43 +01:00
|
|
|
id: Uuid::from_str("8777d9f6-7860-4b9b-88b7-0b57ee6b3a73").unwrap(),
|
2016-06-20 00:10:34 +02:00
|
|
|
version: Version::V3,
|
2016-09-28 15:47:52 +02:00
|
|
|
address: "6edddfc6349aff20bc6467ccf276c5b52487f7a8".into(),
|
2016-06-20 00:10:34 +02:00
|
|
|
crypto: Crypto {
|
|
|
|
cipher: Cipher::Aes128Ctr(Aes128Ctr {
|
2016-09-28 15:47:52 +02:00
|
|
|
iv: "b5a7ec855ec9e2c405371356855fec83".into(),
|
2016-06-20 00:10:34 +02:00
|
|
|
}),
|
2016-09-28 15:47:52 +02:00
|
|
|
ciphertext: "7203da0676d141b138cd7f8e1a4365f59cc1aa6978dc5443f364ca943d7cb4bc".into(),
|
2016-06-20 00:10:34 +02:00
|
|
|
kdf: Kdf::Scrypt(Scrypt {
|
|
|
|
n: 262144,
|
|
|
|
dklen: 32,
|
|
|
|
p: 1,
|
|
|
|
r: 8,
|
2016-09-28 15:47:52 +02:00
|
|
|
salt: "1e8642fdf1f87172492c1412fc62f8db75d796cdfa9c53c3f2b11e44a2a1b209".into(),
|
2016-06-20 00:10:34 +02:00
|
|
|
}),
|
2016-09-28 15:47:52 +02:00
|
|
|
mac: "46325c5d4e8c991ad2683d525c7854da387138b6ca45068985aa4959fa2b8c8f".into(),
|
2016-06-20 00:10:34 +02:00
|
|
|
},
|
2016-07-24 17:38:21 +02:00
|
|
|
name: Some("Test".to_owned()),
|
|
|
|
meta: Some("{}".to_owned()),
|
2016-06-20 00:10:34 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
let keyfile: KeyFile = serde_json::from_str(json).unwrap();
|
|
|
|
assert_eq!(keyfile, expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn capital_crypto_keyfile() {
|
|
|
|
let json = r#"
|
|
|
|
{
|
|
|
|
"address": "6edddfc6349aff20bc6467ccf276c5b52487f7a8",
|
|
|
|
"Crypto": {
|
|
|
|
"cipher": "aes-128-ctr",
|
|
|
|
"ciphertext": "7203da0676d141b138cd7f8e1a4365f59cc1aa6978dc5443f364ca943d7cb4bc",
|
|
|
|
"cipherparams": {
|
|
|
|
"iv": "b5a7ec855ec9e2c405371356855fec83"
|
|
|
|
},
|
|
|
|
"kdf": "scrypt",
|
|
|
|
"kdfparams": {
|
|
|
|
"dklen": 32,
|
|
|
|
"n": 262144,
|
|
|
|
"p": 1,
|
|
|
|
"r": 8,
|
|
|
|
"salt": "1e8642fdf1f87172492c1412fc62f8db75d796cdfa9c53c3f2b11e44a2a1b209"
|
|
|
|
},
|
|
|
|
"mac": "46325c5d4e8c991ad2683d525c7854da387138b6ca45068985aa4959fa2b8c8f"
|
|
|
|
},
|
|
|
|
"id": "8777d9f6-7860-4b9b-88b7-0b57ee6b3a73",
|
|
|
|
"version": 3
|
|
|
|
}"#;
|
|
|
|
|
|
|
|
let expected = KeyFile {
|
2016-09-28 15:47:52 +02:00
|
|
|
id: "8777d9f6-7860-4b9b-88b7-0b57ee6b3a73".into(),
|
2016-06-20 00:10:34 +02:00
|
|
|
version: Version::V3,
|
2016-09-28 15:47:52 +02:00
|
|
|
address: "6edddfc6349aff20bc6467ccf276c5b52487f7a8".into(),
|
2016-06-20 00:10:34 +02:00
|
|
|
crypto: Crypto {
|
|
|
|
cipher: Cipher::Aes128Ctr(Aes128Ctr {
|
2016-09-28 15:47:52 +02:00
|
|
|
iv: "b5a7ec855ec9e2c405371356855fec83".into(),
|
2016-06-20 00:10:34 +02:00
|
|
|
}),
|
2016-09-28 15:47:52 +02:00
|
|
|
ciphertext: "7203da0676d141b138cd7f8e1a4365f59cc1aa6978dc5443f364ca943d7cb4bc".into(),
|
2016-06-20 00:10:34 +02:00
|
|
|
kdf: Kdf::Scrypt(Scrypt {
|
|
|
|
n: 262144,
|
|
|
|
dklen: 32,
|
|
|
|
p: 1,
|
|
|
|
r: 8,
|
2016-09-28 15:47:52 +02:00
|
|
|
salt: "1e8642fdf1f87172492c1412fc62f8db75d796cdfa9c53c3f2b11e44a2a1b209".into(),
|
2016-06-20 00:10:34 +02:00
|
|
|
}),
|
2016-09-28 15:47:52 +02:00
|
|
|
mac: "46325c5d4e8c991ad2683d525c7854da387138b6ca45068985aa4959fa2b8c8f".into(),
|
2016-06-20 00:10:34 +02:00
|
|
|
},
|
2016-07-24 17:38:21 +02:00
|
|
|
name: None,
|
|
|
|
meta: None,
|
2016-06-20 00:10:34 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
let keyfile: KeyFile = serde_json::from_str(json).unwrap();
|
|
|
|
assert_eq!(keyfile, expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn to_and_from_json() {
|
|
|
|
let file = KeyFile {
|
2016-09-28 15:47:52 +02:00
|
|
|
id: "8777d9f6-7860-4b9b-88b7-0b57ee6b3a73".into(),
|
2016-06-20 00:10:34 +02:00
|
|
|
version: Version::V3,
|
2016-09-28 15:47:52 +02:00
|
|
|
address: "6edddfc6349aff20bc6467ccf276c5b52487f7a8".into(),
|
2016-06-20 00:10:34 +02:00
|
|
|
crypto: Crypto {
|
|
|
|
cipher: Cipher::Aes128Ctr(Aes128Ctr {
|
2016-09-28 15:47:52 +02:00
|
|
|
iv: "b5a7ec855ec9e2c405371356855fec83".into(),
|
2016-06-20 00:10:34 +02:00
|
|
|
}),
|
2016-09-28 15:47:52 +02:00
|
|
|
ciphertext: "7203da0676d141b138cd7f8e1a4365f59cc1aa6978dc5443f364ca943d7cb4bc".into(),
|
2016-06-20 00:10:34 +02:00
|
|
|
kdf: Kdf::Scrypt(Scrypt {
|
|
|
|
n: 262144,
|
|
|
|
dklen: 32,
|
|
|
|
p: 1,
|
|
|
|
r: 8,
|
2016-09-28 15:47:52 +02:00
|
|
|
salt: "1e8642fdf1f87172492c1412fc62f8db75d796cdfa9c53c3f2b11e44a2a1b209".into(),
|
2016-06-20 00:10:34 +02:00
|
|
|
}),
|
2016-09-28 15:47:52 +02:00
|
|
|
mac: "46325c5d4e8c991ad2683d525c7854da387138b6ca45068985aa4959fa2b8c8f".into(),
|
2016-06-20 00:10:34 +02:00
|
|
|
},
|
2016-07-24 17:38:21 +02:00
|
|
|
name: Some("Test".to_owned()),
|
|
|
|
meta: None,
|
2016-06-20 00:10:34 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
let serialized = serde_json::to_string(&file).unwrap();
|
2016-07-24 17:38:21 +02:00
|
|
|
println!("{}", serialized);
|
2016-06-20 00:10:34 +02:00
|
|
|
let deserialized = serde_json::from_str(&serialized).unwrap();
|
|
|
|
|
|
|
|
assert_eq!(file, deserialized);
|
|
|
|
}
|
|
|
|
}
|