use bitcoin::util::bip32::{ChildNumber, DerivationPath};
use crate::{AccountStep, SegmentIndexes, TerminalStep};
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) -> (Vec<AccountStep>, Vec<TerminalStep>);
}
impl HardenedNormalSplit for DerivationPath {
fn hardened_normal_split(&self) -> (Vec<AccountStep>, Vec<TerminalStep>) {
let mut terminal_path = vec![];
let account_path = self
.into_iter()
.rev()
.by_ref()
.skip_while(|child| {
if let ChildNumber::Normal { index } = child {
terminal_path.push(
TerminalStep::from_index(*index)
.expect("ChildNumber::Normal contains hardened index"),
);
true
} else {
false
}
})
.cloned()
.map(AccountStep::try_from)
.collect::<Result<Vec<_>, _>>()
.expect("ChildNumber indexes are broken");
let account_path = account_path.into_iter().rev().collect();
let terminal_path = terminal_path.into_iter().rev().collect();
(account_path, terminal_path)
}
}