Merge pull request #871 from rphmeier/geth_keystore

Find geth data store cross-platform.
This commit is contained in:
Arkadiy Paronyan 2016-04-03 21:39:57 +02:00
commit ebd9eb1715
2 changed files with 34 additions and 6 deletions

View File

@ -19,6 +19,7 @@
use common::*; use common::*;
use keys::store::SecretStore; use keys::store::SecretStore;
use keys::directory::KeyFileContent; use keys::directory::KeyFileContent;
use std::path::PathBuf;
/// Enumerates all geth keys in the directory and returns collection of tuples `(accountId, filename)` /// Enumerates all geth keys in the directory and returns collection of tuples `(accountId, filename)`
pub fn enumerate_geth_keys(path: &Path) -> Result<Vec<(Address, String)>, ImportError> { pub fn enumerate_geth_keys(path: &Path) -> Result<Vec<(Address, String)>, ImportError> {
@ -92,6 +93,37 @@ pub fn import_geth_keys(secret_store: &mut SecretStore, geth_keyfiles_directory:
Ok(()) Ok(())
} }
/// Gets the default geth keystore directory.
///
/// Based on https://github.com/ethereum/go-ethereum/blob/e553215/common/path.go#L75
pub fn keystore_dir() -> PathBuf {
#[cfg(target_os = "macos")]
fn data_dir(mut home: PathBuf) -> PathBuf {
home.push("Library");
home.push("Ethereum");
home
}
#[cfg(windows)]
fn data_dir(mut home: PathBuf) -> PathBuf {
home.push("AppData");
home.push("Roaming");
home.push("Ethereum");
home
}
#[cfg(not(any(target_os = "macos", windows)))]
fn data_dir(mut home: PathBuf) -> PathBuf {
home.push(".ethereum");
home
}
let mut data_dir = data_dir(::std::env::home_dir().expect("Failed to get home dir"));
data_dir.push("keystore");
data_dir
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;

View File

@ -150,7 +150,7 @@ impl AccountService {
self.secret_store.write().unwrap().collect_garbage(); self.secret_store.write().unwrap().collect_garbage();
} }
/// Unlocks account for use (no expiration of unlock) /// Unlocks account for use (no expiration of unlock)
pub fn unlock_account_no_expire(&self, account: &Address, pass: &str) -> Result<(), EncryptedHashMapError> { pub fn unlock_account_no_expire(&self, account: &Address, pass: &str) -> Result<(), EncryptedHashMapError> {
self.secret_store.write().unwrap().unlock_account_with_expiration(account, pass, None) self.secret_store.write().unwrap().unlock_account_with_expiration(account, pass, None)
} }
@ -183,13 +183,9 @@ impl SecretStore {
/// trys to import keys in the known locations /// trys to import keys in the known locations
pub fn try_import_existing(&mut self) { pub fn try_import_existing(&mut self) {
use std::path::PathBuf;
use keys::geth_import; use keys::geth_import;
let mut import_path = PathBuf::new(); let import_path = geth_import::keystore_dir();
import_path.push(::std::env::home_dir().expect("Failed to get home dir"));
import_path.push(".ethereum");
import_path.push("keystore");
if let Err(e) = geth_import::import_geth_keys(self, &import_path) { if let Err(e) = geth_import::import_geth_keys(self, &import_path) {
trace!(target: "sstore", "Geth key not imported: {:?}", e); trace!(target: "sstore", "Geth key not imported: {:?}", e);
} }