use alloc::string::String;
use alloc::vec::Vec;
use zeroize::Zeroizing;
use crate::DeriveError;
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct DerivedAccount {
pub path: String,
pub private_key: Zeroizing<String>,
pub public_key: String,
pub address: String,
}
impl DerivedAccount {
#[must_use]
pub const fn new(
path: String,
private_key: Zeroizing<String>,
public_key: String,
address: String,
) -> Self {
Self {
path,
private_key,
public_key,
address,
}
}
}
pub trait Derive {
type Error: core::fmt::Debug + core::fmt::Display + From<DeriveError>;
fn derive(&self, index: u32) -> Result<DerivedAccount, Self::Error>;
fn derive_path(&self, path: &str) -> Result<DerivedAccount, Self::Error>;
}
pub trait DeriveExt: Derive {
fn derive_many(&self, start: u32, count: u32) -> Result<Vec<DerivedAccount>, Self::Error> {
let end = start.checked_add(count).ok_or(DeriveError::IndexOverflow)?;
(start..end).map(|i| self.derive(i)).collect()
}
}
impl<T: Derive> DeriveExt for T {}