This repository has been archived by the owner on Nov 6, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* importing presale wallet in progress * PresaleWallet data structure
- Loading branch information
Showing
7 changed files
with
170 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
use std::io::Read; | ||
use serde_json; | ||
use super::{H160, H768}; | ||
|
||
#[derive(Debug, PartialEq, Deserialize)] | ||
pub struct PresaleWallet { | ||
pub encseed: H768, | ||
#[serde(rename = "ethaddr")] | ||
pub address: H160, | ||
} | ||
|
||
impl PresaleWallet { | ||
pub fn load<R>(reader: R) -> Result<Self, serde_json::Error> where R: Read { | ||
serde_json::from_reader(reader) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use std::str::FromStr; | ||
use serde_json; | ||
use json::{PresaleWallet, H160, H768}; | ||
|
||
#[test] | ||
fn presale_wallet() { | ||
let json = r#" | ||
{ | ||
"encseed": "137103c28caeebbcea5d7f95edb97a289ded151b72159137cb7b2671f394f54cff8c121589dcb373e267225547b3c71cbdb54f6e48ec85cd549f96cf0dedb3bc0a9ac6c79b9c426c5878ca2c9d06ff42a23cb648312fc32ba83649de0928e066", | ||
"ethaddr": "ede84640d1a1d3e06902048e67aa7db8d52c2ce1", | ||
"email": "[email protected]", | ||
"btcaddr": "1JvqEc6WLhg6GnyrLBe2ztPAU28KRfuseH" | ||
} "#; | ||
|
||
let expected = PresaleWallet { | ||
encseed: H768::from_str("137103c28caeebbcea5d7f95edb97a289ded151b72159137cb7b2671f394f54cff8c121589dcb373e267225547b3c71cbdb54f6e48ec85cd549f96cf0dedb3bc0a9ac6c79b9c426c5878ca2c9d06ff42a23cb648312fc32ba83649de0928e066").unwrap(), | ||
address: H160::from_str("ede84640d1a1d3e06902048e67aa7db8d52c2ce1").unwrap(), | ||
}; | ||
|
||
let wallet: PresaleWallet = serde_json::from_str(json).unwrap(); | ||
assert_eq!(expected, wallet); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
use std::fs; | ||
use std::path::Path; | ||
use rcrypto::pbkdf2::pbkdf2; | ||
use rcrypto::sha2::Sha256; | ||
use rcrypto::hmac::Hmac; | ||
use json; | ||
use ethkey::{Address, Secret, KeyPair}; | ||
use crypto::Keccak256; | ||
use {crypto, Error}; | ||
|
||
pub struct PresaleWallet { | ||
iv: [u8; 16], | ||
ciphertext: [u8; 80], | ||
address: Address, | ||
} | ||
|
||
impl From<json::PresaleWallet> for PresaleWallet { | ||
fn from(wallet: json::PresaleWallet) -> Self { | ||
let mut iv = [0u8; 16]; | ||
iv.copy_from_slice(&wallet.encseed[..16]); | ||
|
||
let mut ciphertext = [0u8; 80]; | ||
ciphertext.copy_from_slice(&wallet.encseed[16..]); | ||
|
||
PresaleWallet { | ||
iv: iv, | ||
ciphertext: ciphertext, | ||
address: Address::from(wallet.address), | ||
} | ||
} | ||
} | ||
|
||
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)] | ||
mod tests { | ||
use ethkey::Address; | ||
use super::PresaleWallet; | ||
use json; | ||
|
||
#[test] | ||
fn test() { | ||
let json = r#" | ||
{ | ||
"encseed": "137103c28caeebbcea5d7f95edb97a289ded151b72159137cb7b2671f394f54cff8c121589dcb373e267225547b3c71cbdb54f6e48ec85cd549f96cf0dedb3bc0a9ac6c79b9c426c5878ca2c9d06ff42a23cb648312fc32ba83649de0928e066", | ||
"ethaddr": "ede84640d1a1d3e06902048e67aa7db8d52c2ce1", | ||
"email": "[email protected]", | ||
"btcaddr": "1JvqEc6WLhg6GnyrLBe2ztPAU28KRfuseH" | ||
} "#; | ||
|
||
let wallet = json::PresaleWallet::load(json.as_bytes()).unwrap(); | ||
let wallet = PresaleWallet::from(wallet); | ||
let kp = wallet.decrypt("123").unwrap(); | ||
assert_eq!(kp.address(), Address::from(wallet.address)); | ||
} | ||
} |