use std::str::FromStr;
use anyhow::Result;
use bip39::{Language, Mnemonic};
use super::PrivateKey;
pub fn from_mnemonic(mnemonic: &str, password: &str) -> Result<PrivateKey> {
let mnemonic = Mnemonic::from_str(mnemonic)?;
generate_with_mnemonic(mnemonic, password)
}
pub fn create_with_mnemonic(password: &str) -> Result<(PrivateKey, String)> {
let mnemonic = Mnemonic::generate_in(Language::English, 24)?;
let secret_key = generate_with_mnemonic(mnemonic.clone(), password)?;
Ok((secret_key, mnemonic.to_string()))
}
fn generate_with_mnemonic(mnemonic: Mnemonic, password: &str) -> Result<PrivateKey> {
let mut seed: PrivateKey = PrivateKey::default();
seed.assign_from_slice(
&(mnemonic.to_seed(password).to_vec())[..std::mem::size_of::<PrivateKey>()],
);
Ok(seed)
}