API Lifecycle Implementation, Pt. 1 (#211)

* allow owner api to start up without a wallet, begin to add lifecycle functions

* rustfmt

* test and typos fix

* updated with lifecycle functions

* rustfmt

* updates to allow owner api tests to be executed against local wallet proxy

* rustfmt

* fix for windows test

* add ability to pass configuration to , begin to add documentation and doctests

* add ability to pass configuration to , begin to add documentation and doctests

* doctests for lifecycle functions

* rustfmt

* ensure foreign API also has mask updated when being run along owner api, add more tests to lifecycle

* rustfmt

* documentation for lifecycle methods + init_secure_api

* rustfmt

* failing doctest
This commit is contained in:
Yeastplume
2019-09-02 16:03:35 +01:00
committed by GitHub
parent 22dfa9b3f5
commit 41c0058e84
34 changed files with 1557 additions and 132 deletions
+1 -1
View File
@@ -367,7 +367,7 @@ impl SlateReceiver for KeybaseAllChannels {
>,
>;
let lc = wallet.lc_provider().unwrap();
lc.set_wallet_directory(&config.data_file_dir);
lc.set_top_level_directory(&config.data_file_dir)?;
let mask = lc.open_wallet(None, passphrase, true, false)?;
let wallet_inst = lc.wallet_inst()?;
wallet_inst.set_parent_key_id_by_name(account)?;
+55 -4
View File
@@ -14,7 +14,9 @@
//! Default wallet lifecycle provider
use crate::config::{config, GlobalWalletConfig, GRIN_WALLET_DIR};
use crate::config::{
config, GlobalWalletConfig, GlobalWalletConfigMembers, WalletConfig, GRIN_WALLET_DIR,
};
use crate::core::global;
use crate::keychain::Keychain;
use crate::libwallet::{Error, ErrorKind, NodeClient, WalletBackend, WalletLCProvider};
@@ -23,6 +25,8 @@ use crate::util::secp::key::SecretKey;
use crate::util::ZeroingString;
use crate::LMDBBackend;
use failure::ResultExt;
use grin_wallet_util::grin_util::LoggingConfig;
use std::fs;
use std::path::PathBuf;
pub struct DefaultLCProvider<'a, C, K>
@@ -55,15 +59,51 @@ where
C: NodeClient + 'a,
K: Keychain + 'a,
{
fn set_wallet_directory(&mut self, dir: &str) {
fn set_top_level_directory(&mut self, dir: &str) -> Result<(), Error> {
self.data_dir = dir.to_owned();
Ok(())
}
fn create_config(&self, chain_type: &global::ChainTypes, file_name: &str) -> Result<(), Error> {
fn get_top_level_directory(&self) -> Result<String, Error> {
Ok(self.data_dir.to_owned())
}
fn create_config(
&self,
chain_type: &global::ChainTypes,
file_name: &str,
wallet_config: Option<WalletConfig>,
logging_config: Option<LoggingConfig>,
) -> Result<(), Error> {
let mut default_config = GlobalWalletConfig::for_chain(chain_type);
let logging = match logging_config {
Some(l) => Some(l),
None => match default_config.members.as_ref() {
Some(m) => m.clone().logging.clone(),
None => None,
},
};
let wallet = match wallet_config {
Some(w) => w,
None => match default_config.members {
Some(m) => m.wallet,
None => WalletConfig::default(),
},
};
default_config = GlobalWalletConfig {
members: Some(GlobalWalletConfigMembers { wallet, logging }),
..default_config
};
let mut config_file_name = PathBuf::from(self.data_dir.clone());
config_file_name.push(file_name);
// create top level dir if it doesn't exist
let dd = PathBuf::from(self.data_dir.clone());
if !dd.exists() {
// try create
fs::create_dir_all(dd)?;
}
let mut data_dir_name = PathBuf::from(self.data_dir.clone());
data_dir_name.push(GRIN_WALLET_DIR);
@@ -81,7 +121,10 @@ where
return Ok(());
}
default_config.update_paths(&PathBuf::from(self.data_dir.clone()));
let mut abs_path = std::env::current_dir()?;
abs_path.push(self.data_dir.clone());
default_config.update_paths(&PathBuf::from(abs_path));
let res = default_config.write_to_file(config_file_name.to_str().unwrap());
if let Err(e) = res {
let msg = format!(
@@ -114,10 +157,18 @@ where
mnemonic: Option<ZeroingString>,
mnemonic_length: usize,
password: ZeroingString,
test_mode: bool,
) -> Result<(), Error> {
let mut data_dir_name = PathBuf::from(self.data_dir.clone());
data_dir_name.push(GRIN_WALLET_DIR);
let data_dir_name = data_dir_name.to_str().unwrap();
let exists = WalletSeed::seed_file_exists(&data_dir_name);
if !test_mode {
if let Ok(true) = exists {
let msg = format!("Wallet seed already exists at: {}", data_dir_name);
return Err(ErrorKind::WalletSeedExists(msg))?;
}
}
let _ = WalletSeed::init_file(&data_dir_name, mnemonic_length, mnemonic, password);
info!("Wallet seed file created");
let _wallet: LMDBBackend<'a, C, K> =
+8 -4
View File
@@ -86,7 +86,7 @@ impl WalletSeed {
pub fn seed_file_exists(data_file_dir: &str) -> Result<bool, Error> {
let seed_file_path = &format!("{}{}{}", data_file_dir, MAIN_SEPARATOR, SEED_FILE,);
println!("Seed file path: {}", seed_file_path);
debug!("Seed file path: {}", seed_file_path);
if Path::new(seed_file_path).exists() {
Ok(true)
} else {
@@ -123,9 +123,9 @@ impl WalletSeed {
password: util::ZeroingString,
) -> Result<(), Error> {
let seed_file_path = &format!("{}{}{}", data_file_dir, MAIN_SEPARATOR, SEED_FILE,);
println!("data file dir: {}", data_file_dir);
debug!("data file dir: {}", data_file_dir);
if let Ok(true) = WalletSeed::seed_file_exists(data_file_dir) {
println!("seed file exists");
debug!("seed file exists");
WalletSeed::backup_seed(data_file_dir)?;
}
if !Path::new(&data_file_dir).exists() {
@@ -157,7 +157,11 @@ impl WalletSeed {
let seed_file_path = &format!("{}{}{}", data_file_dir, MAIN_SEPARATOR, SEED_FILE,);
warn!("Generating wallet seed file at: {}", seed_file_path);
let _ = WalletSeed::seed_file_exists(data_file_dir)?;
let exists = WalletSeed::seed_file_exists(data_file_dir)?;
if exists {
let msg = format!("Wallet seed already exists at: {}", data_file_dir);
return Err(ErrorKind::WalletSeedExists(msg))?;
}
let seed = match recovery_phrase {
Some(p) => WalletSeed::from_mnemonic(p)?,