//! Module implementing parsing for BIP-0032 HD paths used for key derivation. use anyhow::{anyhow, Context as _, Result}; use std::{ fmt::{self, Display, Formatter}, str::FromStr, }; /// A parsed hierarchical derivation path. #[derive(Debug)] pub struct Path { components: Vec, } impl Path { /// Returns an iterator pub fn components(&self) -> impl Iterator + '_ { self.components.iter().copied() } } impl Display for Path { fn fmt(&self, f: &mut Formatter) -> fmt::Result { f.write_str("m")?; for component in self.components() { write!(f, "/{}", component)?; } Ok(()) } } impl FromStr for Path { type Err = anyhow::Error; fn from_str(s: &str) -> Result { let components = s .strip_prefix("m/") .ok_or_else(|| anyhow!("BIP-0032 path missing main node"))? .split('/') .map(Component::from_str) .collect::>()?; Ok(Self { components }) } } /// A hierarchical path component. #[derive(Clone, Copy, Debug)] pub enum Component { /// Component to generate a hardened child key. Hardened(u32), /// Component to generate a normal child key. Normal(u32), } impl Display for Component { fn fmt(&self, f: &mut Formatter) -> fmt::Result { match self { Self::Hardened(value) => write!(f, "{}'", value), Self::Normal(value) => write!(f, "{}", value), } } } impl FromStr for Component { type Err = anyhow::Error; fn from_str(s: &str) -> Result { let (value, hardened) = match s.strip_suffix('\'') { Some(value) => (value, true), None => (s, false), }; let value = value .parse() .with_context(|| format!("invalid BIP-0032 path component '{}'", s))?; Ok(if hardened { Component::Hardened(value) } else { Component::Normal(value) }) } }