Skip to content

Commit

Permalink
solution: FromStr
Browse files Browse the repository at this point in the history
  • Loading branch information
splix committed Sep 9, 2020
1 parent dc96dd4 commit dba1d49
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 22 deletions.
4 changes: 2 additions & 2 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ hdpath = "{lib-version}"
[source, rust]
----
use hdpath::StandardHDPath;
use std::convert::TryFrom;
use std::str::FromStr;
let hd_path = StandardHDPath::try_from("m/44'/0'/0'/0/0").unwrap();
let hd_path = StandardHDPath::from_str("m/44'/0'/0'/0/0").unwrap();
//prints "m/44'/0'/0'/0/0"
println!("{:?}", hd_path);
Expand Down
13 changes: 6 additions & 7 deletions crates.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ All supported standards:
# Examples

## Basic usage
```
```rust
use hdpath::StandardHDPath;
use std::convert::TryFrom;
use std::str::FromStr;

let hd_path = StandardHDPath::try_from("m/44'/0'/0'/0/0").unwrap();
let hd_path = StandardHDPath::from_str("m/44'/0'/0'/0/0").unwrap();
// prints "m/44'/0'/0'/0/0"
println!("{:?}", hd_path);

Expand All @@ -41,7 +41,7 @@ println!("purpose: {:?}, coin: {}, account: {}, change: {}, index: {}",
```

## Create from values
```
```rust
use hdpath::{StandardHDPath, Purpose};

let hd_path = StandardHDPath::new(Purpose::Witness, 0, 1, 0, 101);
Expand All @@ -50,9 +50,8 @@ println!("{:?}", hd_path);
```

## Create account and derive addresses
```
```rust
use hdpath::{AccountHDPath, StandardHDPath, Purpose};
use std::convert::TryFrom;

let hd_account = AccountHDPath::new(Purpose::Witness, 0, 1);
// prints "m/44'/0'/1'/x/x"
Expand All @@ -72,7 +71,7 @@ for marking a _hardened_ value. Therefore, if you're getting individual values f
input, you should verify the value before passing to `::new`. Otherwise the constructor may
fail with _panic_ if an invalid value was passed.

```
```rust
use hdpath::{StandardHDPath, PathValue, Purpose};

fn user_path(index: u32) -> Result<StandardHDPath, ()> {
Expand Down
5 changes: 2 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
//! ## Basic usage
//! ```
//! use hdpath::StandardHDPath;
//! # use std::convert::TryFrom;
//! # use std::str::FromStr;
//!
//! let hdpath = StandardHDPath::try_from("m/44'/0'/0'/0/0").unwrap();
//! let hdpath = StandardHDPath::from_str("m/44'/0'/0'/0/0").unwrap();
//! //prints "m/44'/0'/0'/0/0"
//! println!("{:?}", hdpath);
//!
Expand Down Expand Up @@ -51,7 +51,6 @@
//! ## Create account and derive addresses
//! ```
//! use hdpath::{AccountHDPath, StandardHDPath, Purpose};
//! use std::convert::TryFrom;
//!
//! let hd_account = AccountHDPath::new(Purpose::Witness, 0, 1);
//! // prints "m/44'/0'/1'/x/x"
Expand Down
31 changes: 25 additions & 6 deletions src/path_account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::{Purpose, CustomHDPath, Error, PathValue, StandardHDPath};
use std::convert::TryFrom;
#[cfg(feature = "with-bitcoin")]
use bitcoin::util::bip32::{ChildNumber, DerivationPath};
use std::str::FromStr;


/// Account-only HD Path for [BIP-44](https://github.com/bitcoin/bips/blob/master/bip-0044.mediawiki),
Expand All @@ -22,18 +23,18 @@ use bitcoin::util::bip32::{ChildNumber, DerivationPath};
/// # Parse string
/// ```
/// use hdpath::{AccountHDPath, Purpose};
/// # use std::convert::TryFrom;
/// # use std::str::FromStr;
///
/// //creates path m/84'/0'/0'/0/0
/// let hd_account = AccountHDPath::try_from("m/84'/0'/0'").unwrap();
/// let hd_account = AccountHDPath::from_str("m/84'/0'/0'").unwrap();
/// ```
///
/// # Create actial path
/// ```
/// use hdpath::{AccountHDPath, Purpose, StandardHDPath};
/// # use std::convert::TryFrom;
/// # use std::str::FromStr;
///
/// let hd_account = AccountHDPath::try_from("m/84'/0'/0'").unwrap();
/// let hd_account = AccountHDPath::from_str("m/84'/0'/0'").unwrap();
/// // gives hd path m/84'/0'/0'/0/4
/// let hd_path: StandardHDPath = hd_account.address_at(0, 4).unwrap();
/// ```
Expand Down Expand Up @@ -137,7 +138,15 @@ impl TryFrom<&str> for AccountHDPath
type Error = Error;

fn try_from(value: &str) -> Result<Self, Self::Error> {
let value = CustomHDPath::try_from(value)?;
AccountHDPath::from_str(value)
}
}

impl FromStr for AccountHDPath {
type Err = Error;

fn from_str(s: &str) -> Result<Self, Self::Err> {
let value = CustomHDPath::from_str(s)?;
AccountHDPath::try_from(value)
}
}
Expand Down Expand Up @@ -194,7 +203,7 @@ mod tests {
use std::convert::TryFrom;

#[test]
fn create_from_string() {
fn create_try_from_string() {
let hd_account = AccountHDPath::try_from("m/84'/0'/5'");
assert!(hd_account.is_ok());
let hd_account = hd_account.unwrap();
Expand All @@ -203,6 +212,16 @@ mod tests {
assert_eq!(5, hd_account.account);
}

#[test]
fn create_from_string() {
let hd_account = AccountHDPath::from_str("m/84'/0'/5'");
assert!(hd_account.is_ok());
let hd_account = hd_account.unwrap();
assert_eq!(Purpose::Witness, hd_account.purpose);
assert_eq!(0, hd_account.coin_type);
assert_eq!(5, hd_account.account);
}

#[test]
fn create_from_string_sh() {
let hd_account = AccountHDPath::try_from("m/49'/0'/5'");
Expand Down
9 changes: 9 additions & 0 deletions src/path_custom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::{PathValue, Error};
use std::convert::TryFrom;
#[cfg(feature = "with-bitcoin")]
use bitcoin::util::bip32::{ChildNumber, DerivationPath};
use std::str::FromStr;

/// A custom HD Path, that can be any length and contain any Hardened and non-Hardened values in
/// any order. Direct implementation for [BIP-32](https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki#The_default_wallet_layout)
Expand Down Expand Up @@ -39,6 +40,14 @@ impl TryFrom<&str> for CustomHDPath {
type Error = Error;

fn try_from(value: &str) -> Result<Self, Self::Error> {
CustomHDPath::from_str(value)
}
}

impl FromStr for CustomHDPath {
type Err = Error;

fn from_str(value: &str) -> Result<Self, Self::Err> {
const STATE_EXPECT_NUM: usize = 0;
const STATE_READING_NUM: usize = 1;
const STATE_READ_MARKER: usize = 2;
Expand Down
11 changes: 10 additions & 1 deletion src/path_short.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::{Purpose, CustomHDPath, Error, PathValue};
use std::convert::TryFrom;
#[cfg(feature = "with-bitcoin")]
use bitcoin::util::bip32::{ChildNumber, DerivationPath};
use std::str::FromStr;

#[derive(Debug, Clone, Eq, PartialEq)]
pub struct ShortHDPath {
Expand Down Expand Up @@ -44,7 +45,15 @@ impl TryFrom<&str> for ShortHDPath
type Error = Error;

fn try_from(value: &str) -> Result<Self, Self::Error> {
let value = CustomHDPath::try_from(value)?;
ShortHDPath::from_str(value)
}
}

impl FromStr for ShortHDPath {
type Err = Error;

fn from_str(s: &str) -> Result<Self, Self::Err> {
let value = CustomHDPath::from_str(s)?;
ShortHDPath::try_from(value)
}
}
Expand Down
27 changes: 24 additions & 3 deletions src/path_standard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::convert::{TryFrom, TryInto};
use byteorder::{WriteBytesExt, BigEndian};
#[cfg(feature = "with-bitcoin")]
use bitcoin::util::bip32::{ChildNumber, DerivationPath};
use std::str::FromStr;

/// Standard HD Path for [BIP-44](https://github.com/bitcoin/bips/blob/master/bip-0044.mediawiki),
/// [BIP-49](https://github.com/bitcoin/bips/blob/master/bip-0049.mediawiki), [BIP-84](https://github.com/bitcoin/bips/blob/master/bip-0084.mediawiki)
Expand All @@ -18,10 +19,10 @@ use bitcoin::util::bip32::{ChildNumber, DerivationPath};
/// # Parse string
/// ```
/// use hdpath::{StandardHDPath, Purpose};
/// # use std::convert::TryFrom;
/// # use std::str::FromStr;
///
/// //creates path m/84'/0'/0'/0/0
/// let hdpath = StandardHDPath::try_from("m/84'/0'/0'/0/0").unwrap();
/// let hdpath = StandardHDPath::from_str("m/84'/0'/0'/0/0").unwrap();
/// ```
///
#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd)]
Expand Down Expand Up @@ -202,7 +203,15 @@ impl TryFrom<&str> for StandardHDPath
type Error = Error;

fn try_from(value: &str) -> Result<Self, Self::Error> {
let value = CustomHDPath::try_from(value)?;
StandardHDPath::from_str(value)
}
}

impl FromStr for StandardHDPath {
type Err = Error;

fn from_str(s: &str) -> Result<Self, Self::Err> {
let value = CustomHDPath::from_str(s)?;
StandardHDPath::try_from(value)
}
}
Expand Down Expand Up @@ -290,6 +299,18 @@ mod tests {
);
}

#[test]
pub fn create_from_str() {
let standard = StandardHDPath::from_str("m/49'/0'/1'/0/5").unwrap();
let act = CustomHDPath::from(standard);
assert_eq!(5, act.0.len());
assert_eq!(&PathValue::Hardened(49), act.0.get(0).unwrap());
assert_eq!(&PathValue::Hardened(0), act.0.get(1).unwrap());
assert_eq!(&PathValue::Hardened(1), act.0.get(2).unwrap());
assert_eq!(&PathValue::Normal(0), act.0.get(3).unwrap());
assert_eq!(&PathValue::Normal(5), act.0.get(4).unwrap());
}

#[test]
pub fn from_standard_to_custom() {
let standard = StandardHDPath::try_from("m/49'/0'/1'/0/5").unwrap();
Expand Down

0 comments on commit dba1d49

Please sign in to comment.