RPC for importing geth keys (#1916)
* Address book for local addresses. * Simple disk-backed address book. * Add test and keep file with keys. * View and import Geth keys. * Fix test. * Fix up author info. [ci:skip]
This commit is contained in:
@@ -22,7 +22,7 @@ use ethkey::Address;
|
||||
use {json, SafeAccount, Error};
|
||||
use super::KeyDirectory;
|
||||
|
||||
const IGNORED_FILES: &'static [&'static str] = &["thumbs.db"];
|
||||
const IGNORED_FILES: &'static [&'static str] = &["thumbs.db", "address_book.json"];
|
||||
|
||||
#[cfg(not(windows))]
|
||||
fn restrict_permissions_to_owner(file_path: &Path) -> Result<(), i32> {
|
||||
@@ -149,6 +149,8 @@ impl KeyDirectory for DiskDirectory {
|
||||
Some((path, _)) => fs::remove_file(path).map_err(From::from)
|
||||
}
|
||||
}
|
||||
|
||||
fn path(&self) -> Option<&PathBuf> { Some(&self.path) }
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
use ethkey::Address;
|
||||
use std::path::{PathBuf};
|
||||
use {SafeAccount, Error};
|
||||
|
||||
mod disk;
|
||||
@@ -30,6 +31,7 @@ pub trait KeyDirectory: Send + Sync {
|
||||
fn load(&self) -> Result<Vec<SafeAccount>, Error>;
|
||||
fn insert(&self, account: SafeAccount) -> Result<SafeAccount, Error>;
|
||||
fn remove(&self, address: &Address) -> Result<(), Error>;
|
||||
fn path(&self) -> Option<&PathBuf> { None }
|
||||
}
|
||||
|
||||
pub use self::disk::DiskDirectory;
|
||||
|
||||
@@ -27,6 +27,7 @@ use {Error, SecretStore};
|
||||
use json;
|
||||
use json::UUID;
|
||||
use presale::PresaleWallet;
|
||||
use import;
|
||||
|
||||
pub struct EthStore {
|
||||
dir: Box<KeyDirectory>,
|
||||
@@ -173,4 +174,16 @@ impl SecretStore for EthStore {
|
||||
// save to file
|
||||
self.save(account)
|
||||
}
|
||||
|
||||
fn local_path(&self) -> String {
|
||||
self.dir.path().map(|p| p.to_string_lossy().into_owned()).unwrap_or_else(|| String::new())
|
||||
}
|
||||
|
||||
fn list_geth_accounts(&self, testnet: bool) -> Vec<Address> {
|
||||
import::read_geth_accounts(testnet)
|
||||
}
|
||||
|
||||
fn import_geth_accounts(&self, desired: Vec<Address>, testnet: bool) -> Result<Vec<Address>, Error> {
|
||||
import::import_geth_accounts(&*self.dir, desired.into_iter().collect(), testnet)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
|
||||
use std::collections::HashSet;
|
||||
use ethkey::Address;
|
||||
use dir::KeyDirectory;
|
||||
use dir::{GethDirectory, KeyDirectory, DirectoryType};
|
||||
use Error;
|
||||
|
||||
pub fn import_accounts(src: &KeyDirectory, dst: &KeyDirectory) -> Result<Vec<Address>, Error> {
|
||||
@@ -31,3 +31,39 @@ pub fn import_accounts(src: &KeyDirectory, dst: &KeyDirectory) -> Result<Vec<Add
|
||||
Ok(address)
|
||||
}).collect()
|
||||
}
|
||||
|
||||
/// Provide a `HashSet` of all accounts available for import from the Geth keystore.
|
||||
pub fn read_geth_accounts(testnet: bool) -> Vec<Address> {
|
||||
let t = if testnet {
|
||||
DirectoryType::Testnet
|
||||
} else {
|
||||
DirectoryType::Main
|
||||
};
|
||||
|
||||
GethDirectory::open(t)
|
||||
.load()
|
||||
.map(|d| d.into_iter().map(|a| a.address).collect())
|
||||
.unwrap_or_else(|_| Vec::new())
|
||||
}
|
||||
|
||||
/// Import specific `desired` accounts from the Geth keystore into `dst`.
|
||||
pub fn import_geth_accounts(dst: &KeyDirectory, desired: HashSet<Address>, testnet: bool) -> Result<Vec<Address>, Error> {
|
||||
let t = if testnet {
|
||||
DirectoryType::Testnet
|
||||
} else {
|
||||
DirectoryType::Main
|
||||
};
|
||||
|
||||
let src = GethDirectory::open(t);
|
||||
let accounts = try!(src.load());
|
||||
let existing_accounts = try!(dst.load()).into_iter().map(|a| a.address).collect::<HashSet<_>>();
|
||||
|
||||
accounts.into_iter()
|
||||
.filter(|a| !existing_accounts.contains(&a.address))
|
||||
.filter(|a| desired.contains(&a.address))
|
||||
.map(|a| {
|
||||
let address = a.address.clone();
|
||||
try!(dst.insert(a));
|
||||
Ok(address)
|
||||
}).collect()
|
||||
}
|
||||
|
||||
@@ -48,8 +48,7 @@ mod secret_store;
|
||||
pub use self::account::SafeAccount;
|
||||
pub use self::error::Error;
|
||||
pub use self::ethstore::EthStore;
|
||||
pub use self::import::import_accounts;
|
||||
pub use self::import::{import_accounts, read_geth_accounts};
|
||||
pub use self::presale::PresaleWallet;
|
||||
pub use self::secret_store::SecretStore;
|
||||
pub use self::random::random_phrase;
|
||||
|
||||
|
||||
@@ -42,5 +42,11 @@ pub trait SecretStore: Send + Sync {
|
||||
fn set_name(&self, address: &Address, name: String) -> Result<(), Error>;
|
||||
|
||||
fn set_meta(&self, address: &Address, meta: String) -> Result<(), Error>;
|
||||
|
||||
fn local_path(&self) -> String;
|
||||
|
||||
fn list_geth_accounts(&self, testnet: bool) -> Vec<Address>;
|
||||
|
||||
fn import_geth_accounts(&self, desired: Vec<Address>, testnet: bool) -> Result<Vec<Address>, Error>;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user