Find geth data store cross-platform.

Fixes #869
This commit is contained in:
Robert Habermeier 2016-04-03 00:42:33 -04:00
parent 54d594c486
commit 2f02b43352
2 changed files with 34 additions and 6 deletions

View File

@ -19,6 +19,7 @@
use common::*;
use keys::store::SecretStore;
use keys::directory::KeyFileContent;
use std::path::PathBuf;
/// 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)>, io::Error> {
@ -93,6 +94,37 @@ pub fn import_geth_keys(secret_store: &mut SecretStore, geth_keyfiles_directory:
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)]
mod tests {
use super::*;

View File

@ -150,7 +150,7 @@ impl AccountService {
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> {
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
pub fn try_import_existing(&mut self) {
use std::path::PathBuf;
use keys::geth_import;
let mut import_path = PathBuf::new();
import_path.push(::std::env::home_dir().expect("Failed to get home dir"));
import_path.push(".ethereum");
import_path.push("keystore");
let import_path = geth_import::keystore_dir();
if let Err(e) = geth_import::import_geth_keys(self, &import_path) {
trace!(target: "sstore", "Geth key not imported: {:?}", e);
}