import-wallet option for ethstore executable

This commit is contained in:
debris 2016-06-21 15:04:36 +02:00
parent a8a731ba11
commit be03a6acbd
5 changed files with 30 additions and 13 deletions

View File

@ -20,6 +20,7 @@ Usage:
ethstore change-pwd <address> <old-pwd> <new-pwd> [--dir DIR] ethstore change-pwd <address> <old-pwd> <new-pwd> [--dir DIR]
ethstore list [--dir DIR] ethstore list [--dir DIR]
ethstore import [--src DIR] [--dir DIR] ethstore import [--src DIR] [--dir DIR]
ethstore import-wallet <path> <password> [--dir DIR]
ethstore remove <address> <password> [--dir DIR] ethstore remove <address> <password> [--dir DIR]
ethstore sign <address> <password> <message> [--dir DIR] ethstore sign <address> <password> <message> [--dir DIR]
ethstore [-h | --help] ethstore [-h | --help]
@ -38,6 +39,7 @@ Commands:
change-pwd Change account password. change-pwd Change account password.
list List accounts. list List accounts.
import Import accounts from src. import Import accounts from src.
import-wallet Import presale wallet.
remove Remove account. remove Remove account.
sign Sign message. sign Sign message.
``` ```
@ -119,6 +121,19 @@ ethstore list
-- --
#### `import-wallet <path> <password> [--dir DIR]`
*Import account from presale wallet.*
- `<path>` - presale wallet path
- `<password>` - account password, any string
- `[--dir DIR]` - secret store directory, It may be either parity, parity-test, geth, geth-test or a path. default: parity
```
e6a3d25a7cb7cd21cb720df5b5e8afd154af1bbb
```
--
#### `remove <address> <password> [--dir DIR]` #### `remove <address> <password> [--dir DIR]`
*Remove account from secret store.* *Remove account from secret store.*

View File

@ -24,7 +24,7 @@ use std::str::FromStr;
use docopt::Docopt; use docopt::Docopt;
use ethstore::ethkey::{Secret, Address, Message}; use ethstore::ethkey::{Secret, Address, Message};
use ethstore::dir::{KeyDirectory, ParityDirectory, DiskDirectory, GethDirectory, DirectoryType}; use ethstore::dir::{KeyDirectory, ParityDirectory, DiskDirectory, GethDirectory, DirectoryType};
use ethstore::{EthStore, SecretStore, import_accounts, Error}; use ethstore::{EthStore, SecretStore, import_accounts, Error, PresaleWallet};
pub const USAGE: &'static str = r#" pub const USAGE: &'static str = r#"
Ethereum key management. Ethereum key management.
@ -35,6 +35,7 @@ Usage:
ethstore change-pwd <address> <old-pwd> <new-pwd> [--dir DIR] ethstore change-pwd <address> <old-pwd> <new-pwd> [--dir DIR]
ethstore list [--dir DIR] ethstore list [--dir DIR]
ethstore import [--src DIR] [--dir DIR] ethstore import [--src DIR] [--dir DIR]
ethstore import-wallet <path> <password> [--dir DIR]
ethstore remove <address> <password> [--dir DIR] ethstore remove <address> <password> [--dir DIR]
ethstore sign <address> <password> <message> [--dir DIR] ethstore sign <address> <password> <message> [--dir DIR]
ethstore [-h | --help] ethstore [-h | --help]
@ -53,6 +54,7 @@ Commands:
change-pwd Change password. change-pwd Change password.
list List accounts. list List accounts.
import Import accounts from src. import Import accounts from src.
import-wallet Import presale wallet.
remove Remove account. remove Remove account.
sign Sign message. sign Sign message.
"#; "#;
@ -63,6 +65,7 @@ struct Args {
cmd_change_pwd: bool, cmd_change_pwd: bool,
cmd_list: bool, cmd_list: bool,
cmd_import: bool, cmd_import: bool,
cmd_import_wallet: bool,
cmd_remove: bool, cmd_remove: bool,
cmd_sign: bool, cmd_sign: bool,
arg_secret: String, arg_secret: String,
@ -71,6 +74,7 @@ struct Args {
arg_new_pwd: String, arg_new_pwd: String,
arg_address: String, arg_address: String,
arg_message: String, arg_message: String,
arg_path: String,
flag_src: String, flag_src: String,
flag_dir: String, flag_dir: String,
} }
@ -128,6 +132,11 @@ fn execute<S, I>(command: I) -> Result<String, Error> where I: IntoIterator<Item
let dst = try!(key_dir(&args.flag_dir)); let dst = try!(key_dir(&args.flag_dir));
let accounts = try!(import_accounts(src.deref(), dst.deref())); let accounts = try!(import_accounts(src.deref(), dst.deref()));
Ok(format_accounts(&accounts)) Ok(format_accounts(&accounts))
} else if args.cmd_import_wallet {
let wallet = try!(PresaleWallet::open(&args.arg_path));
let kp = try!(wallet.decrypt(&args.arg_password));
let address = try!(store.insert_account(kp.secret().clone(), &args.arg_password));
Ok(format!("{}", address))
} else if args.cmd_remove { } else if args.cmd_remove {
let address = try!(Address::from_str(&args.arg_address)); let address = try!(Address::from_str(&args.arg_address));
let ok = store.remove_account(&address, &args.arg_password).is_ok(); let ok = store.remove_account(&address, &args.arg_password).is_ok();

View File

@ -67,7 +67,7 @@ impl Keccak256<[u8; 32]> for [u8] {
pub mod aes { pub mod aes {
use rcrypto::blockmodes::{CtrMode, CbcDecryptor, PkcsPadding}; use rcrypto::blockmodes::{CtrMode, CbcDecryptor, PkcsPadding};
use rcrypto::aessafe::{AesSafe128Encryptor, AesSafe128Decryptor}; use rcrypto::aessafe::{AesSafe128Encryptor, AesSafe128Decryptor};
use rcrypto::symmetriccipher::{Encryptor, Decryptor}; use rcrypto::symmetriccipher::{Encryptor, Decryptor, SymmetricCipherError};
use rcrypto::buffer::{RefReadBuffer, RefWriteBuffer}; use rcrypto::buffer::{RefReadBuffer, RefWriteBuffer};
/// Encrypt a message /// Encrypt a message
@ -83,9 +83,10 @@ pub mod aes {
} }
/// Decrypt a message using cbc mode /// Decrypt a message using cbc mode
pub fn decrypt_cbc(k: &[u8], iv: &[u8], encrypted: &[u8], dest: &mut [u8]) { pub fn decrypt_cbc(k: &[u8], iv: &[u8], encrypted: &[u8], dest: &mut [u8]) -> Result<(), SymmetricCipherError> {
let mut encryptor = CbcDecryptor::new(AesSafe128Decryptor::new(k), PkcsPadding, iv.to_vec()); let mut encryptor = CbcDecryptor::new(AesSafe128Decryptor::new(k), PkcsPadding, iv.to_vec());
encryptor.decrypt(&mut RefReadBuffer::new(encrypted), &mut RefWriteBuffer::new(dest), true).expect("Invalid length or padding"); try!(encryptor.decrypt(&mut RefReadBuffer::new(encrypted), &mut RefWriteBuffer::new(dest), true));
Ok(())
} }
} }

View File

@ -31,11 +31,3 @@ impl From<json::H160> for Address {
From::from(a) From::from(a)
} }
} }
impl<'a> From<&'a json::H160> for Address {
fn from(json: &'a json::H160) -> Self {
let mut a = [0u8; 20];
a.copy_from_slice(json);
From::from(a)
}
}

View File

@ -43,7 +43,7 @@ impl PresaleWallet {
pbkdf2(&mut h_mac, password.as_bytes(), 2000, &mut derived_key); pbkdf2(&mut h_mac, password.as_bytes(), 2000, &mut derived_key);
let mut key = [0u8; 64]; let mut key = [0u8; 64];
crypto::aes::decrypt_cbc(&derived_key, &self.iv, &self.ciphertext, &mut key); try!(crypto::aes::decrypt_cbc(&derived_key, &self.iv, &self.ciphertext, &mut key).map_err(|_| Error::InvalidPassword));
let secret = Secret::from(key.keccak256()); let secret = Secret::from(key.keccak256());
if let Ok(kp) = KeyPair::from_secret(secret) { if let Ok(kp) = KeyPair::from_secret(secret) {