PresaleWallet data structure
This commit is contained in:
parent
c2ffa90478
commit
a8a731ba11
@ -45,5 +45,6 @@ pub use self::account::SafeAccount;
|
|||||||
pub use self::error::Error;
|
pub use self::error::Error;
|
||||||
pub use self::ethstore::EthStore;
|
pub use self::ethstore::EthStore;
|
||||||
pub use self::import::import_accounts;
|
pub use self::import::import_accounts;
|
||||||
|
pub use self::presale::PresaleWallet;
|
||||||
pub use self::secret_store::SecretStore;
|
pub use self::secret_store::SecretStore;
|
||||||
|
|
||||||
|
@ -1,40 +1,66 @@
|
|||||||
|
use std::fs;
|
||||||
|
use std::path::Path;
|
||||||
use rcrypto::pbkdf2::pbkdf2;
|
use rcrypto::pbkdf2::pbkdf2;
|
||||||
use rcrypto::sha2::Sha256;
|
use rcrypto::sha2::Sha256;
|
||||||
use rcrypto::hmac::Hmac;
|
use rcrypto::hmac::Hmac;
|
||||||
use json::PresaleWallet;
|
use json;
|
||||||
use ethkey::{Address, Secret, KeyPair};
|
use ethkey::{Address, Secret, KeyPair};
|
||||||
use crypto::Keccak256;
|
use crypto::Keccak256;
|
||||||
use {crypto, Error};
|
use {crypto, Error};
|
||||||
|
|
||||||
fn decrypt_presale_wallet(wallet: &PresaleWallet, password: &str) -> Result<KeyPair, Error> {
|
pub struct PresaleWallet {
|
||||||
let mut iv = [0u8; 16];
|
iv: [u8; 16],
|
||||||
iv.copy_from_slice(&wallet.encseed[..16]);
|
ciphertext: [u8; 80],
|
||||||
|
address: Address,
|
||||||
|
}
|
||||||
|
|
||||||
let mut ciphertext = [0u8; 80];
|
impl From<json::PresaleWallet> for PresaleWallet {
|
||||||
ciphertext.copy_from_slice(&wallet.encseed[16..]);
|
fn from(wallet: json::PresaleWallet) -> Self {
|
||||||
|
let mut iv = [0u8; 16];
|
||||||
|
iv.copy_from_slice(&wallet.encseed[..16]);
|
||||||
|
|
||||||
let mut h_mac = Hmac::new(Sha256::new(), password.as_bytes());
|
let mut ciphertext = [0u8; 80];
|
||||||
let mut derived_key = vec![0u8; 16];
|
ciphertext.copy_from_slice(&wallet.encseed[16..]);
|
||||||
pbkdf2(&mut h_mac, password.as_bytes(), 2000, &mut derived_key);
|
|
||||||
|
|
||||||
let mut key = [0u8; 64];
|
PresaleWallet {
|
||||||
crypto::aes::decrypt_cbc(&derived_key, &iv, &ciphertext, &mut key);
|
iv: iv,
|
||||||
|
ciphertext: ciphertext,
|
||||||
let secret = Secret::from(key.keccak256());
|
address: Address::from(wallet.address),
|
||||||
if let Ok(kp) = KeyPair::from_secret(secret) {
|
|
||||||
if kp.address() == Address::from(&wallet.address) {
|
|
||||||
return Ok(kp)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Err(Error::InvalidPassword)
|
impl PresaleWallet {
|
||||||
|
pub fn open<P>(path: P) -> Result<Self, Error> where P: AsRef<Path> {
|
||||||
|
let file = try!(fs::File::open(path));
|
||||||
|
let presale = json::PresaleWallet::load(file).unwrap();
|
||||||
|
Ok(PresaleWallet::from(presale))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn decrypt(&self, password: &str) -> Result<KeyPair, Error> {
|
||||||
|
let mut h_mac = Hmac::new(Sha256::new(), password.as_bytes());
|
||||||
|
let mut derived_key = vec![0u8; 16];
|
||||||
|
pbkdf2(&mut h_mac, password.as_bytes(), 2000, &mut derived_key);
|
||||||
|
|
||||||
|
let mut key = [0u8; 64];
|
||||||
|
crypto::aes::decrypt_cbc(&derived_key, &self.iv, &self.ciphertext, &mut key);
|
||||||
|
|
||||||
|
let secret = Secret::from(key.keccak256());
|
||||||
|
if let Ok(kp) = KeyPair::from_secret(secret) {
|
||||||
|
if kp.address() == self.address {
|
||||||
|
return Ok(kp)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Err(Error::InvalidPassword)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use ethkey::{KeyPair, Address};
|
use ethkey::Address;
|
||||||
use super::decrypt_presale_wallet;
|
use super::PresaleWallet;
|
||||||
use json::PresaleWallet;
|
use json;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test() {
|
fn test() {
|
||||||
@ -46,8 +72,9 @@ mod tests {
|
|||||||
"btcaddr": "1JvqEc6WLhg6GnyrLBe2ztPAU28KRfuseH"
|
"btcaddr": "1JvqEc6WLhg6GnyrLBe2ztPAU28KRfuseH"
|
||||||
} "#;
|
} "#;
|
||||||
|
|
||||||
let wallet = PresaleWallet::load(json.as_bytes()).unwrap();
|
let wallet = json::PresaleWallet::load(json.as_bytes()).unwrap();
|
||||||
let kp = decrypt_presale_wallet(&wallet, "123").unwrap();
|
let wallet = PresaleWallet::from(wallet);
|
||||||
|
let kp = wallet.decrypt("123").unwrap();
|
||||||
assert_eq!(kp.address(), Address::from(wallet.address));
|
assert_eq!(kp.address(), Address::from(wallet.address));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user