Skip to content

Commit

Permalink
solution: conversion from trait type
Browse files Browse the repository at this point in the history
  • Loading branch information
splix committed Nov 5, 2020
1 parent a13b0e4 commit 7f5591c
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/path_custom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,16 @@ impl TryFrom<&str> for CustomHDPath {
}
}

impl std::convert::From<&dyn HDPath> for CustomHDPath {
fn from(value: &dyn HDPath) -> Self {
let mut path = Vec::with_capacity(value.len() as usize);
for i in 0..value.len() {
path.push(value.get(i).expect("no-path-element"));
}
CustomHDPath(path)
}
}

impl std::fmt::Display for CustomHDPath {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "m")?;
Expand Down Expand Up @@ -190,6 +200,7 @@ impl std::convert::From<&CustomHDPath> for DerivationPath {
#[cfg(test)]
mod tests {
use super::*;
use crate::StandardHDPath;

#[test]
pub fn to_string() {
Expand Down Expand Up @@ -226,6 +237,15 @@ mod tests {
assert_eq!(&PathValue::Normal(0), act.0.get(4).unwrap());
}

#[test]
pub fn try_from_common_trait() {
let source = StandardHDPath::from_str("m/84'/0'/1'/2/3").unwrap();
let act = CustomHDPath::from(source.to_trait());
assert_eq!(
CustomHDPath::try_from("m/84'/0'/1'/2/3").unwrap(), act
);
}

#[test]
pub fn try_from_bignum() {
let act = CustomHDPath::try_from("m/44'/12'/345'/6789/101112").unwrap();
Expand Down
36 changes: 36 additions & 0 deletions src/traits.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use crate::{PathValue, CustomHDPath};
use byteorder::{BigEndian, WriteBytesExt};
#[cfg(feature = "with-bitcoin")]
use bitcoin::util::bip32::{ChildNumber, DerivationPath};

/// General trait for an HDPath.
/// Common implementations are [`StandardHDPath`], [`AccountHDPath`] and [`CustomHDPath`]
Expand Down Expand Up @@ -51,12 +53,29 @@ pub trait HDPath {
}
}

#[cfg(feature = "with-bitcoin")]
impl std::convert::From<&dyn HDPath> for DerivationPath {
fn from(value: &dyn HDPath) -> Self {
let mut path = Vec::with_capacity(value.len() as usize);
for i in 0..value.len() {
path.push(ChildNumber::from(value.get(i).expect("no-path-element")));
}
DerivationPath::from(path)
}
}

#[cfg(test)]
mod tests {
use super::*;
use crate::{StandardHDPath, AccountHDPath};
use std::str::FromStr;

impl StandardHDPath {
pub fn to_trait(&self) -> &dyn HDPath {
self
}
}

#[test]
fn get_parent_from_std() {
let act = StandardHDPath::from_str("m/44'/0'/1'/1/2").unwrap();
Expand Down Expand Up @@ -100,4 +119,21 @@ mod tests {
"m/84'/0'/1'/0", parent.to_string()
);
}
}

#[cfg(all(test, feature = "with-bitcoin"))]
mod tests_with_bitcoin {
use crate::{StandardHDPath};
use std::str::FromStr;
use bitcoin::util::bip32::{DerivationPath};

#[test]
fn convert_to_bitcoin() {
let source = StandardHDPath::from_str("m/44'/0'/1'/1/2").unwrap();
let act = DerivationPath::from(source.to_trait());
assert_eq!(
DerivationPath::from_str("m/44'/0'/1'/1/2").unwrap(),
act
)
}
}

0 comments on commit 7f5591c

Please sign in to comment.