use bitcoin::util::bip32::{ChildNumber, DerivationPath};
use super::UnhardenedIndex;
pub trait DerivePublicKey {
fn derive_public_key(
&self,
child_index: UnhardenedIndex,
) -> bitcoin::PublicKey;
}
pub trait DerivationPathMaster {
fn master() -> Self;
fn is_master(&self) -> bool;
}
impl DerivationPathMaster for DerivationPath {
fn master() -> DerivationPath {
vec![].into()
}
fn is_master(&self) -> bool {
self.into_iter().len() == 0
}
}
pub trait HardenedNormalSplit {
fn hardened_normal_split(&self) -> (DerivationPath, Vec<u32>);
}
impl HardenedNormalSplit for DerivationPath {
fn hardened_normal_split(&self) -> (DerivationPath, Vec<u32>) {
let mut terminal_path = vec![];
let branch_path = self
.into_iter()
.rev()
.by_ref()
.skip_while(|child| {
if let ChildNumber::Normal { index } = child {
terminal_path.push(index);
true
} else {
false
}
})
.cloned()
.collect::<DerivationPath>();
let branch_path = branch_path.into_iter().rev().cloned().collect();
let terminal_path = terminal_path.into_iter().rev().cloned().collect();
(branch_path, terminal_path)
}
}