hdpath/
lib.rs

1//! Common structures and utilities to operate HD Path defined by Bitcoin's BIP-32 standard.
2//!
3//! The main specification for the Hierarchical Deterministic Wallets is [BIP-32](https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki),
4//! and HD Path is a part of it which specifies the format for the hierarchy path.
5//!
6//! The crate doesn't try to implement Key Derivation specification, but instead implements all common
7//! functionality for creating, parsing and displaying an HD Path, especially standard paths defined
8//! by BIP-44 and related.
9//!
10//! The common structure, defined by BIP-43, is `m/purpose'/coin_type'/account'/change/address_index`, for example `m/44'/0'/0'/0/0`
11//!
12//! All supported standards:
13//! - [BIP-32](https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki)
14//! - [BIP-43](https://github.com/bitcoin/bips/blob/master/bip-0043.mediawiki)
15//! - [BIP-44](https://github.com/bitcoin/bips/blob/master/bip-0044.mediawiki)
16//! - [BIP-49](https://github.com/bitcoin/bips/blob/master/bip-0049.mediawiki)
17//! - [BIP-84](https://github.com/bitcoin/bips/blob/master/bip-0084.mediawiki)
18//!
19//! Base traits is [HDPath](trait.HDPath.html), with few specific implementations and general [`CustomHDPath`](struct.CustomHDPath.html)
20//!
21//! # Examples
22//!
23//! ## Basic usage
24//! ```
25//! use hdpath::StandardHDPath;
26//! # use std::str::FromStr;
27//!
28//! let hdpath = StandardHDPath::from_str("m/44'/0'/0'/0/0").unwrap();
29//! //prints "m/44'/0'/0'/0/0"
30//! println!("{:?}", hdpath);
31//!
32//! //prints "0", which is account id
33//! println!("{:?}", hdpath.account());
34//!
35//! //prints: "purpose: Pubkey, coin: 0, account: 0, change: 0, index: 0"
36//! println!("purpose: {:?}, coin: {}, account: {}, change: {}, index: {}",
37//!    hdpath.purpose(),
38//!    hdpath.coin_type(),
39//!    hdpath.account(),
40//!    hdpath.change(),
41//!    hdpath.index())
42//! ```
43//!
44//! ## Create from values
45//! ```
46//! use hdpath::{StandardHDPath, Purpose};
47//!
48//! let hdpath = StandardHDPath::new(Purpose::Witness, 0, 1, 0, 101);
49//! //prints "m/84'/0'/1'/0/101"
50//! println!("{:?}", hdpath);
51//! ```
52//!
53//! ## Create account and derive addresses
54//! ```
55//! use hdpath::{AccountHDPath, StandardHDPath, Purpose};
56//!
57//! let hd_account = AccountHDPath::new(Purpose::Witness, 0, 1);
58//! // prints "m/44'/0'/1'/x/x"
59//! println!("{:?}", hd_account);
60//!
61//! // get actual address on the account path. Returns StandardHDPath
62//! let hd_path = hd_account.address_at(0, 7);
63//!
64//! //prints: "m/44'/0'/1'/0/7"
65//! println!("{:?}", hd_path);
66//! ```
67//!
68//! ## Verify before create
69//!
70//! Please note that values for HD Path are limited to `2^31-1` because the highest bit is reserved
71//! for marking a _hardened_ value. Therefore, if you're getting individual values from some user
72//! input, you should verify the value before passing to `::new`. Otherwise the constructor may
73//! fail with _panic_ if an invalid value was passed.
74//!
75//! ```
76//! use hdpath::{StandardHDPath, PathValue, Purpose};
77//!
78//! fn user_path(index: u32) -> Result<StandardHDPath, ()> {
79//!   let user_id = 1234 as u32;
80//!   if PathValue::is_ok(index) {
81//!      Ok(StandardHDPath::new(Purpose::Witness, 0, user_id, 0, index))
82//!   } else {
83//!      Err(())
84//!   }
85//! }
86//! ```
87//!
88extern crate byteorder;
89#[cfg(feature = "with-bitcoin")]
90extern crate bitcoin;
91
92mod errors;
93mod traits;
94mod path_account;
95mod path_custom;
96mod path_short;
97mod path_standard;
98mod path_value;
99mod purpose;
100
101pub use errors::Error;
102pub use traits::HDPath;
103pub use path_account::AccountHDPath;
104pub use path_custom::CustomHDPath;
105pub use path_standard::StandardHDPath;
106pub use path_value::{PathValue};
107pub use purpose::Purpose;