API Lifecycle Implementation, Part 2 (#217)

* additon of 'get_mnemonic' API function

* rustfmt

* added 'change_password' api function

* rustfmt

* add 'delete_wallet' function'

* rustfmt
This commit is contained in:
Yeastplume
2019-09-11 10:27:30 +01:00
committed by GitHub
parent 41c0058e84
commit 78e30aa779
10 changed files with 430 additions and 14 deletions
+124
View File
@@ -1565,6 +1565,130 @@ where
let lc = w_lock.lc_provider()?;
lc.close_wallet(name)
}
/// Return the BIP39 mnemonic for the given wallet. This function will decrypt
/// the wallet's seed file with the given password, and thus does not need the
/// wallet to be open.
///
/// # Arguments
///
/// * `name`: Reserved for future use, use `None` for the time being.
/// * `password`: The password used to encrypt the seed file.
///
/// # Returns
/// * Ok(BIP-39 mneminc) if successful
/// * or [`libwallet::Error`](../grin_wallet_libwallet/struct.Error.html) if an error is encountered.
///
/// # Example
/// Set up as in [`new`](struct.Owner.html#method.new) method above.
/// ```
/// # grin_wallet_api::doctest_helper_setup_doc_env!(wallet, wallet_config);
///
/// use grin_core::global::ChainTypes;
///
/// // Set up as above
/// # let api_owner = Owner::new(wallet.clone());
///
/// let pw = ZeroingString::from("my_password");
/// let res = api_owner.get_mnemonic(None, pw);
///
/// if let Ok(mne) = res {
/// // ...
/// }
/// ```
pub fn get_mnemonic(
&self,
name: Option<&str>,
password: ZeroingString,
) -> Result<ZeroingString, Error> {
let mut w_lock = self.wallet_inst.lock();
let lc = w_lock.lc_provider()?;
lc.get_mnemonic(name, password)
}
/// Changes a wallet's password, meaning the old seed file is decrypted with the old password,
/// and a new seed file is created with the same mnemonic and encrypted with the new password.
///
/// This function temporarily backs up the old seed file until a test-decryption of the new
/// file is confirmed to contain the same seed as the original seed file, at which point the
/// backup is deleted. If this operation fails for an unknown reason, the backup file will still
/// exist in the wallet's data directory encrypted with the old password.
///
/// # Arguments
///
/// * `name`: Reserved for future use, use `None` for the time being.
/// * `old`: The password used to encrypt the existing seed file (i.e. old password)
/// * `new`: The password to be used to encrypt the new seed file
///
/// # Returns
/// * Ok(()) if successful
/// * or [`libwallet::Error`](../grin_wallet_libwallet/struct.Error.html) if an error is encountered.
///
/// # Example
/// Set up as in [`new`](struct.Owner.html#method.new) method above.
/// ```
/// # grin_wallet_api::doctest_helper_setup_doc_env!(wallet, wallet_config);
///
/// use grin_core::global::ChainTypes;
///
/// // Set up as above
/// # let api_owner = Owner::new(wallet.clone());
///
/// let old = ZeroingString::from("my_password");
/// let new = ZeroingString::from("new_password");
/// let res = api_owner.change_password(None, old, new);
///
/// if let Ok(mne) = res {
/// // ...
/// }
/// ```
pub fn change_password(
&self,
name: Option<&str>,
old: ZeroingString,
new: ZeroingString,
) -> Result<(), Error> {
let mut w_lock = self.wallet_inst.lock();
let lc = w_lock.lc_provider()?;
lc.change_password(name, old, new)
}
/// Deletes a wallet, removing the config file, seed file and all data files.
/// Obviously, use with extreme caution and plenty of user warning
///
/// Highly recommended that the wallet be explicitly closed first via the `close_wallet`
/// function.
///
/// # Arguments
///
/// * `name`: Reserved for future use, use `None` for the time being.
///
/// # Returns
/// * Ok if successful
/// * or [`libwallet::Error`](../grin_wallet_libwallet/struct.Error.html) if an error is encountered.
///
/// # Example
/// Set up as in [`new`](struct.Owner.html#method.new) method above.
/// ```
/// # grin_wallet_api::doctest_helper_setup_doc_env!(wallet, wallet_config);
///
/// use grin_core::global::ChainTypes;
///
/// // Set up as above
/// # let api_owner = Owner::new(wallet.clone());
///
/// let res = api_owner.delete_wallet(None);
///
/// if let Ok(_) = res {
/// // ...
/// }
/// ```
pub fn delete_wallet(&self, name: Option<&str>) -> Result<(), Error> {
let mut w_lock = self.wallet_inst.lock();
let lc = w_lock.lc_provider()?;
lc.delete_wallet(name)
}
}
#[doc(hidden)]
+119
View File
@@ -1587,6 +1587,102 @@ pub trait OwnerRpcS {
*/
fn close_wallet(&self, name: Option<String>) -> Result<(), ErrorKind>;
/**
Networked version of [Owner::get_mnemonic](struct.Owner.html#method.get_mnemonic).
```
# grin_wallet_api::doctest_helper_json_rpc_owner_assert_response!(
# r#"
{
"jsonrpc": "2.0",
"method": "get_mnemonic",
"params": {
"name": null,
"password": ""
},
"id": 1
}
# "#
# ,
# r#"
{
"id": 1,
"jsonrpc": "2.0",
"result": {
"Ok": "fat twenty mean degree forget shell check candy immense awful flame next during february bulb bike sun wink theory day kiwi embrace peace lunch"
}
}
# "#
# , true, 0, false, false, false);
```
*/
fn get_mnemonic(&self, name: Option<String>, password: String) -> Result<String, ErrorKind>;
/**
Networked version of [Owner::change_password](struct.Owner.html#method.change_password).
```
# grin_wallet_api::doctest_helper_json_rpc_owner_assert_response!(
# r#"
{
"jsonrpc": "2.0",
"method": "change_password",
"params": {
"name": null,
"old": "",
"new": "new_password"
},
"id": 1
}
# "#
# ,
# r#"
{
"id": 1,
"jsonrpc": "2.0",
"result": {
"Ok": null
}
}
# "#
# , true, 0, false, false, false);
```
*/
fn change_password(
&self,
name: Option<String>,
old: String,
new: String,
) -> Result<(), ErrorKind>;
/**
Networked version of [Owner::delete_wallet](struct.Owner.html#method.delete_wallet).
```
# grin_wallet_api::doctest_helper_json_rpc_owner_assert_response!(
# r#"
{
"jsonrpc": "2.0",
"method": "delete_wallet",
"params": {
"name": null
},
"id": 1
}
# "#
# ,
# r#"
{
"id": 1,
"jsonrpc": "2.0",
"result": {
"Ok": null
}
}
# "#
# , true, 0, false, false, false);
```
*/
fn delete_wallet(&self, name: Option<String>) -> Result<(), ErrorKind>;
}
impl<'a, L, C, K> OwnerRpcS for Owner<'a, L, C, K>
@@ -1843,4 +1939,27 @@ where
let n = name.as_ref().map(|s| s.as_str());
Owner::close_wallet(self, n).map_err(|e| e.kind())
}
fn get_mnemonic(&self, name: Option<String>, password: String) -> Result<String, ErrorKind> {
let n = name.as_ref().map(|s| s.as_str());
let res =
Owner::get_mnemonic(self, n, ZeroingString::from(password)).map_err(|e| e.kind())?;
Ok(format!("{}", &*res))
}
fn change_password(
&self,
name: Option<String>,
old: String,
new: String,
) -> Result<(), ErrorKind> {
let n = name.as_ref().map(|s| s.as_str());
Owner::change_password(self, n, ZeroingString::from(old), ZeroingString::from(new))
.map_err(|e| e.kind())
}
fn delete_wallet(&self, name: Option<String>) -> Result<(), ErrorKind> {
let n = name.as_ref().map(|s| s.as_str());
Owner::delete_wallet(self, n).map_err(|e| e.kind())
}
}