1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
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)
/// # Usage
///
/// ## Parse string
/// ```
/// use hdpath::CustomHDPath;
/// # use std::convert::TryFrom;
///
/// let hdpath = CustomHDPath::try_from("m/1'/2'/3/4/5'/6'/7").unwrap();
/// let hdpath = CustomHDPath::try_from("m/44'/0'/1'/0/0").unwrap();
/// //also support uppercase notation
/// let hdpath = CustomHDPath::try_from("M/44H/0H/1H/0/0").unwrap();
/// ```
/// ## Direct create
/// ```
/// use hdpath::{CustomHDPath, PathValue};
///
/// let hdpath = CustomHDPath::new(vec![
///    PathValue::hardened(44), PathValue::hardened(0), PathValue::hardened(1),
///    PathValue::normal(0), PathValue::normal(0)
/// ]);
/// ```
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct CustomHDPath(pub Vec<PathValue>);

impl CustomHDPath {
    pub fn new(values: Vec<PathValue>) -> CustomHDPath {
        CustomHDPath(values)
    }
}

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;

        let chars = value.as_bytes();
        if chars.len() < 2 {
            return Err(Error::InvalidFormat)
        }
        if chars[0] != 'm' as u8 && chars[0] != 'M' as u8 {
            return Err(Error::InvalidFormat)
        }
        if chars[1] != '/' as u8 {
            return Err(Error::InvalidFormat)
        }
        let mut keys: Vec<PathValue> = Vec::new();
        let mut pos = 2;
        let mut num: u32 = 0;
        let mut state = STATE_EXPECT_NUM;
        while chars.len() > pos {
            match chars[pos] {
                39 | 72 => { // (') apostrophe or H
                    if state != STATE_READING_NUM {
                        return Err(Error::InvalidFormat)
                    }
                    if !PathValue::is_ok(num) {
                        return Err(Error::InvalidFormat)
                    }
                    keys.push(PathValue::hardened(num));
                    state = STATE_READ_MARKER;
                    num = 0;
                },
                47 => { // slash
                    if state == STATE_READING_NUM {
                        if !PathValue::is_ok(num) {
                            return Err(Error::InvalidFormat)
                        }
                        keys.push(PathValue::normal(num));
                    } else if state != STATE_READ_MARKER {
                        return Err(Error::InvalidFormat)
                    }
                    state = STATE_EXPECT_NUM;
                    num = 0;
                },
                48..=57 => { //number
                    if state == STATE_EXPECT_NUM {
                        state = STATE_READING_NUM
                    } else if state != STATE_READING_NUM {
                        return Err(Error::InvalidFormat)
                    }
                    num = num * 10 + (chars[pos] - 48) as u32;
                },
                _ => {
                    return Err(Error::InvalidFormat)
                }
            }
            pos += 1;
            if chars.len() == pos && state == 1 {
                if !PathValue::is_ok(num) {
                    return Err(Error::InvalidFormat)
                }
                keys.push(PathValue::normal(num));
            }
        }
        if state == STATE_EXPECT_NUM {
            //finished with slash
            Err(Error::InvalidFormat)
        } else if keys.is_empty() {
            Err(Error::InvalidStructure)
        } else {
            Ok(CustomHDPath(keys))
        }
    }
}

#[cfg(feature = "with-bitcoin")]
impl std::convert::From<&CustomHDPath> for Vec<ChildNumber> {
    fn from(value: &CustomHDPath) -> Self {
        let mut result: Vec<ChildNumber> = Vec::with_capacity(value.0.len());
        for item in value.0.iter() {
            result.push(ChildNumber::from(item.to_raw()))
        }
        return result;
    }
}

#[cfg(feature = "with-bitcoin")]
impl std::convert::From<CustomHDPath> for Vec<ChildNumber> {
    fn from(value: CustomHDPath) -> Self {
        Vec::<ChildNumber>::from(&value)
    }
}

#[cfg(feature = "with-bitcoin")]
impl std::convert::From<CustomHDPath> for DerivationPath {
    fn from(value: CustomHDPath) -> Self {
        DerivationPath::from(Vec::<ChildNumber>::from(&value))
    }
}

#[cfg(feature = "with-bitcoin")]
impl std::convert::From<&CustomHDPath> for DerivationPath {
    fn from(value: &CustomHDPath) -> Self {
        DerivationPath::from(Vec::<ChildNumber>::from(value))
    }
}

#[cfg(test)]
mod tests {
    use super::*;

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

    #[test]
    pub fn try_from_bignum() {
        let act = CustomHDPath::try_from("m/44'/12'/345'/6789/101112").unwrap();
        assert_eq!(5, act.0.len());
        assert_eq!(&PathValue::Hardened(44), act.0.get(0).unwrap());
        assert_eq!(&PathValue::Hardened(12), act.0.get(1).unwrap());
        assert_eq!(&PathValue::Hardened(345), act.0.get(2).unwrap());
        assert_eq!(&PathValue::Normal(6789), act.0.get(3).unwrap());
        assert_eq!(&PathValue::Normal(101112), act.0.get(4).unwrap());
    }

    #[test]
    pub fn try_from_long() {
        let act = CustomHDPath::try_from("m/44'/0'/1'/2/3/4'/5/67'/8'/910").unwrap();
        assert_eq!(10, act.0.len());
        assert_eq!(&PathValue::Hardened(44), 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(2), act.0.get(3).unwrap());
        assert_eq!(&PathValue::Normal(3), act.0.get(4).unwrap());
        assert_eq!(&PathValue::Hardened(4), act.0.get(5).unwrap());
        assert_eq!(&PathValue::Normal(5), act.0.get(6).unwrap());
        assert_eq!(&PathValue::Hardened(67), act.0.get(7).unwrap());
        assert_eq!(&PathValue::Hardened(8), act.0.get(8).unwrap());
        assert_eq!(&PathValue::Normal(910), act.0.get(9).unwrap());
    }

    #[test]
    pub fn try_from_all_hardened() {
        let act = CustomHDPath::try_from("m/44'/0'/0'/0'/1'").unwrap();
        assert_eq!(5, act.0.len());
        assert_eq!(&PathValue::Hardened(44), act.0.get(0).unwrap());
        assert_eq!(&PathValue::Hardened(0), act.0.get(1).unwrap());
        assert_eq!(&PathValue::Hardened(0), act.0.get(2).unwrap());
        assert_eq!(&PathValue::Hardened(0), act.0.get(3).unwrap());
        assert_eq!(&PathValue::Hardened(1), act.0.get(4).unwrap());
    }

    #[test]
    pub fn try_from_all_normal() {
        let act = CustomHDPath::try_from("m/44/0/0/0/1").unwrap();
        assert_eq!(5, act.0.len());
        assert_eq!(&PathValue::Normal(44), act.0.get(0).unwrap());
        assert_eq!(&PathValue::Normal(0), act.0.get(1).unwrap());
        assert_eq!(&PathValue::Normal(0), act.0.get(2).unwrap());
        assert_eq!(&PathValue::Normal(0), act.0.get(3).unwrap());
        assert_eq!(&PathValue::Normal(1), act.0.get(4).unwrap());
    }

    #[test]
    pub fn try_from_other_format() {
        let act = CustomHDPath::try_from("M/44H/0H/0H/1/5").unwrap();
        assert_eq!(5, act.0.len());
        assert_eq!(&PathValue::Hardened(44), act.0.get(0).unwrap());
        assert_eq!(&PathValue::Hardened(0), act.0.get(1).unwrap());
        assert_eq!(&PathValue::Hardened(0), act.0.get(2).unwrap());
        assert_eq!(&PathValue::Normal(1), act.0.get(3).unwrap());
        assert_eq!(&PathValue::Normal(5), act.0.get(4).unwrap());
    }

    #[test]
    pub fn error_on_invalid_path() {
        let paths = vec![
            "", "1", "m44",
            "m/", "m/44/", "m/44/0/", "m/44''/0/0/0/1", "m/44/H0/0/0/1",
        ];
        for p in paths {
            assert!(CustomHDPath::try_from(p).is_err(), "test: {}", p);
        }
    }



    #[test]
    pub fn fail_incorrect_hardened() {
        let custom = CustomHDPath::try_from("m/2147483692'/0'/0'/0/0");
        assert!(custom.is_err());
    }
}

#[cfg(all(test, feature = "with-bitcoin"))]
mod tests_with_bitcoin {
    use super::*;
    use std::convert::TryFrom;
    use bitcoin::util::bip32::ChildNumber;

    #[test]
    pub fn convert_to_childnumbers() {
        let hdpath = CustomHDPath::try_from("m/44'/15'/2'/0/35/81/0").unwrap();
        let childs: Vec<ChildNumber> = hdpath.into();
        assert_eq!(childs.len(), 7);
        assert_eq!(childs[0], ChildNumber::from_hardened_idx(44).unwrap());
        assert_eq!(childs[1], ChildNumber::from_hardened_idx(15).unwrap());
        assert_eq!(childs[2], ChildNumber::from_hardened_idx(2).unwrap());
        assert_eq!(childs[3], ChildNumber::from_normal_idx(0).unwrap());
        assert_eq!(childs[4], ChildNumber::from_normal_idx(35).unwrap());
        assert_eq!(childs[5], ChildNumber::from_normal_idx(81).unwrap());
        assert_eq!(childs[6], ChildNumber::from_normal_idx(0).unwrap());
    }

}