2023-09-01 04:10:56 +00:00
|
|
|
use crate::index::DerivationIndex;
|
|
|
|
use serde::{Deserialize, Serialize};
|
2023-09-01 04:49:35 +00:00
|
|
|
use thiserror::Error;
|
|
|
|
|
|
|
|
/// Errors associated with creating a [`DerivationPath`].
|
|
|
|
#[derive(Error, Debug)]
|
|
|
|
pub enum Error {
|
|
|
|
/// A [`DerivationIndex`] was not able to be created.
|
|
|
|
#[error("Unable to create index: {0}")]
|
|
|
|
UnableToCreateIndex(#[from] super::index::Error),
|
|
|
|
|
|
|
|
/// The path could not be parsed due to a bad prefix. Paths must be in the format:
|
|
|
|
///
|
|
|
|
/// m [/ index [']]+
|
|
|
|
///
|
|
|
|
/// The prefix for the path must be `m`, and all indices must be integers between 0 and
|
|
|
|
/// 2^31.
|
|
|
|
#[error("Unable to parse path due to bad path prefix")]
|
|
|
|
UnknownPathPrefix,
|
|
|
|
}
|
|
|
|
|
2023-09-01 04:57:05 +00:00
|
|
|
type Result<T, E = Error> = std::result::Result<T, E>;
|
2023-09-01 04:10:56 +00:00
|
|
|
|
|
|
|
const PREFIX: &str = "m";
|
|
|
|
|
2023-09-01 04:49:35 +00:00
|
|
|
/// A fully qualified path to derive a key.
|
2023-09-30 07:19:37 +00:00
|
|
|
#[derive(Serialize, Deserialize, Clone, Debug, Default, Eq, PartialEq, PartialOrd, Ord)]
|
2023-09-01 04:10:56 +00:00
|
|
|
pub struct DerivationPath {
|
|
|
|
pub(crate) path: Vec<DerivationIndex>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DerivationPath {
|
2023-09-01 04:49:35 +00:00
|
|
|
/// Returns an iterator over the [`DerivationPath`].
|
2023-09-01 04:10:56 +00:00
|
|
|
pub fn iter(&self) -> impl Iterator<Item = &DerivationIndex> {
|
|
|
|
self.path.iter()
|
|
|
|
}
|
2023-09-12 00:44:22 +00:00
|
|
|
|
|
|
|
pub fn len(&self) -> usize {
|
|
|
|
self.path.len()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_empty(&self) -> bool {
|
|
|
|
self.path.is_empty()
|
|
|
|
}
|
2023-09-01 04:10:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl std::str::FromStr for DerivationPath {
|
|
|
|
type Err = Error;
|
|
|
|
|
|
|
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
|
|
|
let mut iter = s.split('/');
|
|
|
|
if iter.next() != Some(PREFIX) {
|
|
|
|
return Err(Error::UnknownPathPrefix);
|
|
|
|
}
|
|
|
|
Ok(Self {
|
2023-09-01 04:49:35 +00:00
|
|
|
path: iter
|
|
|
|
.map(DerivationIndex::from_str)
|
|
|
|
.map(|maybe_err| maybe_err.map_err(From::from))
|
2023-09-06 15:21:47 +00:00
|
|
|
.collect::<Result<_>>()?,
|
2023-09-01 04:10:56 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl std::fmt::Display for DerivationPath {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
write!(f, "{PREFIX}")?;
|
|
|
|
for index in self.iter() {
|
|
|
|
write!(f, "/{index}")?;
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-30 07:19:37 +00:00
|
|
|
impl std::ops::Add<DerivationIndex> for DerivationPath {
|
|
|
|
type Output = DerivationPath;
|
|
|
|
|
|
|
|
fn add(self, rhs: DerivationIndex) -> Self::Output {
|
|
|
|
let mut output = self.clone();
|
|
|
|
output.path.push(rhs);
|
|
|
|
output
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl std::ops::Add<&[DerivationIndex]> for DerivationPath {
|
|
|
|
type Output = DerivationPath;
|
|
|
|
|
|
|
|
fn add(self, rhs: &[DerivationIndex]) -> Self::Output {
|
|
|
|
let mut output = self.clone();
|
|
|
|
output.path.extend(rhs.iter().cloned());
|
|
|
|
output
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-01 04:10:56 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
use std::str::FromStr;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[should_panic]
|
|
|
|
fn requires_master_path() {
|
|
|
|
DerivationPath::from_str("1234/5678'").unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn equivalency() -> Result<()> {
|
|
|
|
let paths = ["m/1234'/5678", "m/44'/0'/0'", "m"];
|
|
|
|
for path in paths {
|
|
|
|
assert_eq!(&DerivationPath::from_str(path)?.to_string(), path);
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
2023-09-30 07:19:37 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn add() -> Result<(), Box<dyn std::error::Error>> {
|
|
|
|
let path = DerivationPath::from_str("m")?;
|
|
|
|
let path = path + DerivationIndex::new(72, true)?;
|
|
|
|
let path = path + DerivationIndex::new(47, false)?;
|
|
|
|
let path = path + DerivationIndex::new((i32::MAX) as u32, false)?;
|
|
|
|
assert_eq!(path, DerivationPath::from_str("m/72'/47/2147483647")?);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn add_vec() -> Result<(), Box<dyn std::error::Error>> {
|
|
|
|
let path = DerivationPath::from_str("m")?;
|
2023-11-05 06:29:10 +00:00
|
|
|
let other_path = [
|
|
|
|
DerivationIndex::new(72, true)?,
|
|
|
|
DerivationIndex::new(47, false)?,
|
|
|
|
DerivationIndex::new((i32::MAX) as u32, false)?,
|
|
|
|
];
|
2023-09-30 07:19:37 +00:00
|
|
|
let path = path + &other_path[..];
|
|
|
|
assert_eq!(path, DerivationPath::from_str("m/72'/47/2147483647")?);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2023-09-01 04:10:56 +00:00
|
|
|
}
|