From d4f28b6785975a3257e92004aff19f0b046599a5 Mon Sep 17 00:00:00 2001 From: Carl Dong Date: Tue, 7 Aug 2018 23:00:47 -0700 Subject: [PATCH] Add conversion between u32 and ChildNumber, fix docs --- src/util/bip32.rs | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/src/util/bip32.rs b/src/util/bip32.rs index 3af594da..6408c04d 100644 --- a/src/util/bip32.rs +++ b/src/util/bip32.rs @@ -90,10 +90,29 @@ pub struct ExtendedPubKey { /// A child number for a derived key #[derive(Copy, Clone, PartialEq, Eq, Debug)] pub enum ChildNumber { - /// Hardened key index, within [0, 2^31 - 1] - Hardened(u32), /// Non-hardened key, within [0, 2^31 - 1] Normal(u32), + /// Hardened key index, within [2^31, 2^32 - 1] + Hardened(u32), +} + +impl From for ChildNumber { + fn from(index: u32) -> Self { + if index & (1 << 31) != 0 { + ChildNumber::Hardened(index) + } else { + ChildNumber::Normal(index) + } + } +} + +impl From for u32 { + fn from(cnum: ChildNumber) -> Self { + match cnum { + ChildNumber::Normal(index) => index, + ChildNumber::Hardened(index) => index, + } + } } impl fmt::Display for ChildNumber {