2019-01-07 11:33:07 +01:00
|
|
|
// Copyright 2015-2019 Parity Technologies (UK) Ltd.
|
|
|
|
// This file is part of Parity Ethereum.
|
2018-06-04 10:19:50 +02:00
|
|
|
|
2019-01-07 11:33:07 +01:00
|
|
|
// Parity Ethereum is free software: you can redistribute it and/or modify
|
2018-06-04 10:19:50 +02:00
|
|
|
// 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.
|
|
|
|
|
2019-01-07 11:33:07 +01:00
|
|
|
// Parity Ethereum is distributed in the hope that it will be useful,
|
2018-06-04 10:19:50 +02:00
|
|
|
// 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
|
2019-01-07 11:33:07 +01:00
|
|
|
// along with Parity Ethereum. If not, see <http://www.gnu.org/licenses/>.
|
2018-06-04 10:19:50 +02:00
|
|
|
|
2016-06-21 13:30:32 +02:00
|
|
|
use crypto::{self, pbkdf2, Keccak256};
|
|
|
|
use ethkey::{Address, KeyPair, Password, Secret};
|
|
|
|
use json;
|
2018-06-22 15:09:15 +02:00
|
|
|
use std::{fs, num::NonZeroU32, path::Path};
|
2016-06-21 13:09:27 +02:00
|
|
|
use Error;
|
|
|
|
|
2017-03-23 13:23:03 +01:00
|
|
|
/// Pre-sale wallet.
|
2016-06-21 13:30:32 +02:00
|
|
|
pub struct PresaleWallet {
|
|
|
|
iv: [u8; 16],
|
2016-08-03 15:24:09 +02:00
|
|
|
ciphertext: Vec<u8>,
|
2016-06-21 13:30:32 +02:00
|
|
|
address: Address,
|
|
|
|
}
|
2016-06-21 13:09:27 +02:00
|
|
|
|
2016-06-21 13:30:32 +02:00
|
|
|
impl From<json::PresaleWallet> for PresaleWallet {
|
|
|
|
fn from(wallet: json::PresaleWallet) -> Self {
|
|
|
|
let mut iv = [0u8; 16];
|
|
|
|
iv.copy_from_slice(&wallet.encseed[..16]);
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-08-03 15:24:09 +02:00
|
|
|
let mut ciphertext = vec![];
|
|
|
|
ciphertext.extend_from_slice(&wallet.encseed[16..]);
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-06-21 13:30:32 +02:00
|
|
|
PresaleWallet {
|
|
|
|
iv: iv,
|
|
|
|
ciphertext: ciphertext,
|
|
|
|
address: Address::from(wallet.address),
|
2016-06-21 13:09:27 +02:00
|
|
|
}
|
|
|
|
}
|
2016-06-21 13:30:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl PresaleWallet {
|
2017-03-23 13:23:03 +01:00
|
|
|
/// Open a pre-sale wallet.
|
2016-06-21 13:30:32 +02:00
|
|
|
pub fn open<P>(path: P) -> Result<Self, Error>
|
|
|
|
where
|
|
|
|
P: AsRef<Path>,
|
|
|
|
{
|
2016-12-27 12:53:56 +01:00
|
|
|
let file = fs::File::open(path)?;
|
|
|
|
let presale =
|
|
|
|
json::PresaleWallet::load(file).map_err(|e| Error::InvalidKeyFile(format!("{}", e)))?;
|
2016-06-21 13:30:32 +02:00
|
|
|
Ok(PresaleWallet::from(presale))
|
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2017-03-23 13:23:03 +01:00
|
|
|
/// Decrypt the wallet.
|
2018-06-22 15:09:15 +02:00
|
|
|
pub fn decrypt(&self, password: &Password) -> Result<KeyPair, Error> {
|
2018-05-05 11:02:33 +02:00
|
|
|
let mut derived_key = [0u8; 32];
|
|
|
|
let salt = pbkdf2::Salt(password.as_bytes());
|
|
|
|
let sec = pbkdf2::Secret(password.as_bytes());
|
2019-02-06 17:53:34 +01:00
|
|
|
let iter = NonZeroU32::new(2000).expect("2000 > 0; qed");
|
|
|
|
pbkdf2::sha256(iter, salt, sec, &mut derived_key);
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-08-03 15:24:09 +02:00
|
|
|
let mut key = vec![0; self.ciphertext.len()];
|
2018-05-05 11:02:33 +02:00
|
|
|
let len =
|
|
|
|
crypto::aes::decrypt_128_cbc(&derived_key[0..16], &self.iv, &self.ciphertext, &mut key)
|
|
|
|
.map_err(|_| Error::InvalidPassword)?;
|
2016-08-03 15:24:09 +02:00
|
|
|
let unpadded = &key[..len];
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2017-05-19 17:06:36 +02:00
|
|
|
let secret = Secret::from_unsafe_slice(&unpadded.keccak256())?;
|
2016-06-21 13:30:32 +02:00
|
|
|
if let Ok(kp) = KeyPair::from_secret(secret) {
|
|
|
|
if kp.address() == self.address {
|
|
|
|
return Ok(kp);
|
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
}
|
|
|
|
|
2016-06-21 13:30:32 +02:00
|
|
|
Err(Error::InvalidPassword)
|
|
|
|
}
|
2016-06-21 13:09:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2016-06-21 13:30:32 +02:00
|
|
|
use super::PresaleWallet;
|
|
|
|
use json;
|
2016-06-21 13:09:27 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test() {
|
|
|
|
let json = r#"
|
|
|
|
{
|
|
|
|
"encseed": "137103c28caeebbcea5d7f95edb97a289ded151b72159137cb7b2671f394f54cff8c121589dcb373e267225547b3c71cbdb54f6e48ec85cd549f96cf0dedb3bc0a9ac6c79b9c426c5878ca2c9d06ff42a23cb648312fc32ba83649de0928e066",
|
|
|
|
"ethaddr": "ede84640d1a1d3e06902048e67aa7db8d52c2ce1",
|
|
|
|
"email": "123@gmail.com",
|
|
|
|
"btcaddr": "1JvqEc6WLhg6GnyrLBe2ztPAU28KRfuseH"
|
|
|
|
} "#;
|
|
|
|
|
2016-06-21 13:30:32 +02:00
|
|
|
let wallet = json::PresaleWallet::load(json.as_bytes()).unwrap();
|
|
|
|
let wallet = PresaleWallet::from(wallet);
|
2018-06-22 15:09:15 +02:00
|
|
|
assert!(wallet.decrypt(&"123".into()).is_ok());
|
|
|
|
assert!(wallet.decrypt(&"124".into()).is_err());
|
2016-06-21 13:09:27 +02:00
|
|
|
}
|
|
|
|
}
|