use crate::errors::{Error, Result};
use bip32::{DerivationPath, Language, Mnemonic, XPrv};
use rand_core::OsRng;
pub const AVAX_ACCOUNT_DERIV_PATH: &str = "m/44'/9000'/0'";
pub const AVAX_ACCOUNT_DERIV_PATH_0: &str = "m/44'/9000'/0'/0/0";
pub const AVAX_ACCOUNT_EXT_PUB_KEY_DERIV_PATH: &str = "m/44'/9000'/0'";
pub const ETH_ACCOUNT_EXT_PUB_KEY_DERIV_PATH: &str = "m/44'/60'/0'/0/0";
pub fn gen_24() -> String {
let m = Mnemonic::random(OsRng, Language::English);
let s = m.phrase();
assert_eq!(s.split(' ').count(), 24);
String::from(s)
}
impl crate::key::secp256k1::private_key::Key {
pub fn from_mnemonic_phrase<S>(phrase: S, derive_path: S) -> Result<Self>
where
S: AsRef<str>,
{
let deriv: DerivationPath = derive_path.as_ref().parse().map_err(|e| Error::Other {
message: format!("failed to parse derive path ({})", e),
retryable: false,
})?;
let mnemonic = Mnemonic::new(phrase, Language::English).map_err(|e| Error::Other {
message: format!("failed to read mnemonic phrase ({})", e),
retryable: false,
})?;
let seed = mnemonic.to_seed("password");
let child_xprv = XPrv::derive_from_path(&seed, &deriv).map_err(|e| Error::Other {
message: format!("failed to derive AVAX account path ({})", e),
retryable: false,
})?;
let pk = child_xprv.private_key().to_bytes();
Self::from_bytes(&pk)
}
}