Add conversion between u32 and ChildNumber, fix docs

This commit is contained in:
Carl Dong 2018-08-07 23:00:47 -07:00
parent 45c699f005
commit d4f28b6785
1 changed files with 21 additions and 2 deletions

View File

@ -90,10 +90,29 @@ pub struct ExtendedPubKey {
/// A child number for a derived key /// A child number for a derived key
#[derive(Copy, Clone, PartialEq, Eq, Debug)] #[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub enum ChildNumber { pub enum ChildNumber {
/// Hardened key index, within [0, 2^31 - 1]
Hardened(u32),
/// Non-hardened key, within [0, 2^31 - 1] /// Non-hardened key, within [0, 2^31 - 1]
Normal(u32), Normal(u32),
/// Hardened key index, within [2^31, 2^32 - 1]
Hardened(u32),
}
impl From<u32> for ChildNumber {
fn from(index: u32) -> Self {
if index & (1 << 31) != 0 {
ChildNumber::Hardened(index)
} else {
ChildNumber::Normal(index)
}
}
}
impl From<ChildNumber> for u32 {
fn from(cnum: ChildNumber) -> Self {
match cnum {
ChildNumber::Normal(index) => index,
ChildNumber::Hardened(index) => index,
}
}
} }
impl fmt::Display for ChildNumber { impl fmt::Display for ChildNumber {