use crate::epic_keychain::{ChildNumber, ExtKeychain, Identifier, Keychain};
use crate::epic_util::secp::key::SecretKey;
use crate::error::{Error, ErrorKind};
use crate::types::{AcctPathMapping, NodeClient, WalletBackend};
pub fn next_available_key<'a, T: ?Sized, C, K>(
wallet: &mut T,
keychain_mask: Option<&SecretKey>,
) -> Result<Identifier, Error>
where
T: WalletBackend<'a, C, K>,
C: NodeClient + 'a,
K: Keychain + 'a,
{
let child = wallet.next_child(keychain_mask)?;
Ok(child)
}
pub fn retrieve_existing_key<'a, T: ?Sized, C, K>(
wallet: &T,
key_id: Identifier,
mmr_index: Option<u64>,
) -> Result<(Identifier, u32), Error>
where
T: WalletBackend<'a, C, K>,
C: NodeClient + 'a,
K: Keychain + 'a,
{
let existing = wallet.get(&key_id, &mmr_index)?;
let key_id = existing.key_id.clone();
let derivation = existing.n_child;
Ok((key_id, derivation))
}
pub fn accounts<'a, T: ?Sized, C, K>(wallet: &mut T) -> Result<Vec<AcctPathMapping>, Error>
where
T: WalletBackend<'a, C, K>,
C: NodeClient + 'a,
K: Keychain + 'a,
{
Ok(wallet.acct_path_iter().collect())
}
pub fn new_acct_path<'a, T: ?Sized, C, K>(
wallet: &mut T,
keychain_mask: Option<&SecretKey>,
label: &str,
) -> Result<Identifier, Error>
where
T: WalletBackend<'a, C, K>,
C: NodeClient + 'a,
K: Keychain + 'a,
{
let label = label.to_owned();
if let Some(_) = wallet.acct_path_iter().find(|l| l.label == label) {
return Err(ErrorKind::AccountLabelAlreadyExists(label.clone()).into());
}
let highest_entry = wallet.acct_path_iter().max_by(|a, b| {
<u32>::from(a.path.to_path().path[0]).cmp(&<u32>::from(b.path.to_path().path[0]))
});
let return_id = {
if let Some(e) = highest_entry {
let mut p = e.path.to_path();
p.path[0] = ChildNumber::from(<u32>::from(p.path[0]) + 1);
p.to_identifier()
} else {
ExtKeychain::derive_key_id(2, 0, 0, 0, 0)
}
};
let save_path = AcctPathMapping {
label: label.to_owned(),
path: return_id.clone(),
};
let mut batch = wallet.batch(keychain_mask)?;
batch.save_acct_path(save_path)?;
batch.commit()?;
Ok(return_id)
}
pub fn set_acct_path<'a, T: ?Sized, C, K>(
wallet: &mut T,
keychain_mask: Option<&SecretKey>,
label: &str,
path: &Identifier,
) -> Result<(), Error>
where
T: WalletBackend<'a, C, K>,
C: NodeClient + 'a,
K: Keychain + 'a,
{
let label = label.to_owned();
let save_path = AcctPathMapping {
label: label.to_owned(),
path: path.clone(),
};
let mut batch = wallet.batch(keychain_mask)?;
batch.save_acct_path(save_path)?;
batch.commit()?;
Ok(())
}