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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
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),
/// [BIP-49](https://github.com/bitcoin/bips/blob/master/bip-0049.mediawiki), [BIP-84](https://github.com/bitcoin/bips/blob/master/bip-0084.mediawiki)
/// and similar.
///
/// It's not supposed to be used to derive actual addresses, but only to build other path based on this
///
/// Represents `m/purpose'/coin_type'/account'/x/x`, like `m/44'/0'/0'/x/x`.
///
/// # Create new
/// ```
/// use hdpath::{AccountHDPath, Purpose};
///
/// //creates path m/84'/0'/0'/0/0
/// let hd_account = AccountHDPath::new(Purpose::Witness, 0, 0);
/// ```
/// # Parse string
/// ```
/// use hdpath::{AccountHDPath, Purpose};
/// # use std::str::FromStr;
///
/// //creates path m/84'/0'/0'/0/0
/// let hd_account = AccountHDPath::from_str("m/84'/0'/0'").unwrap();
/// ```
///
/// # Create actial path
/// ```
/// use hdpath::{AccountHDPath, Purpose, StandardHDPath};
/// # use std::str::FromStr;
///
/// 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();
/// ```
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct AccountHDPath {
    purpose: Purpose,
    coin_type: u32,
    account: u32,
}

impl AccountHDPath {

    pub fn new(purpose: Purpose, coin_type: u32, account: u32) -> AccountHDPath {
        match Self::try_new(purpose, coin_type, account) {
            Ok(path) => path,
            Err(err) => panic!("Invalid {}: {}", err.0, err.1)
        }
    }

    pub fn try_new(purpose: Purpose, coin_type: u32, account: u32) -> Result<AccountHDPath, (String, u32)> {
        if let Purpose::Custom(n) = purpose {
            if !PathValue::is_ok(n) {
                return Err(("purpose".to_string(), n));
            }
        }
        if !PathValue::is_ok(coin_type) {
            return Err(("coin_type".to_string(), coin_type));
        }
        if !PathValue::is_ok(account) {
            return Err(("account".to_string(), account));
        }
        Ok(AccountHDPath {
            purpose,
            coin_type,
            account,
        })
    }

    /// Derive path to an address withing this account path
    /// ```
    /// # use hdpath::{AccountHDPath, Purpose, StandardHDPath};
    /// # use std::convert::TryFrom;
    /// let hd_account = AccountHDPath::try_from("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();
    /// ```
    ///
    /// Return error `(field_name, invalid_value)` if the field has an incorrect value.
    /// It may happed if change or index are in _hardened_ space.
    pub fn address_at(&self, change: u32, index: u32) -> Result<StandardHDPath, (String, u32)> {
        StandardHDPath::try_new(
            self.purpose.clone(),
            self.coin_type,
            self.account,
            change,
            index
        )
    }

    pub fn purpose(&self) -> &Purpose {
        &self.purpose
    }

    pub fn coin_type(&self) -> u32 {
        self.coin_type
    }

    pub fn account(&self) -> u32 {
        self.account
    }

}

impl TryFrom<CustomHDPath> for AccountHDPath {
    type Error = Error;

    fn try_from(value: CustomHDPath) -> Result<Self, Self::Error> {
        if value.0.len() < 3 {
            return Err(Error::InvalidLength(value.0.len()))
        }
        if let Some(PathValue::Hardened(p)) = value.0.get(0) {
            let purpose = Purpose::try_from(*p)?;
            if let Some(PathValue::Hardened(coin_type)) = value.0.get(1) {
                if let Some(PathValue::Hardened(account)) = value.0.get(2) {
                    return Ok(AccountHDPath {
                        purpose,
                        coin_type: *coin_type,
                        account: *account,
                    })
                }
            }
            Err(Error::InvalidStructure)
        } else {
            Err(Error::InvalidStructure)
        }
    }
}

impl TryFrom<&str> for AccountHDPath
{
    type Error = Error;

    fn try_from(value: &str) -> Result<Self, Self::Error> {
        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)
    }
}

impl ToString for AccountHDPath {
    fn to_string(&self) -> String {
        format!("m/{}'/{}'/{}'/x/x",
                self.purpose.as_value().as_number(),
                self.coin_type,
                self.account,
        )
    }
}

#[cfg(feature = "with-bitcoin")]
impl std::convert::From<&AccountHDPath> for Vec<ChildNumber> {
    fn from(value: &AccountHDPath) -> Self {
        let result = [
            ChildNumber::from_hardened_idx(value.purpose.as_value().as_number())
                .expect("Purpose is not Hardened"),
            ChildNumber::from_hardened_idx(value.coin_type)
                .expect("Coin Type is not Hardened"),
            ChildNumber::from_hardened_idx(value.account)
                .expect("Account is not Hardened"),
        ];
        return result.to_vec();
    }
}

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

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

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

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

    #[test]
    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();
        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() {
        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'");
        assert!(hd_account.is_ok());
        let hd_account = hd_account.unwrap();
        assert_eq!(Purpose::ScriptHash, hd_account.purpose);
        assert_eq!(0, hd_account.coin_type());
        assert_eq!(5, hd_account.account());
    }

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

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

    #[test]
    fn create_from_full_string() {
        let hd_account = AccountHDPath::try_from("m/84'/0'/5'/0/101");
        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 to_string() {
        let hd_account = AccountHDPath::try_from("m/84'/0'/5'/0/101").unwrap();
        assert_eq!("m/84'/0'/5'/x/x", hd_account.to_string());
    }

    #[test]
    fn create_change_address() {
        let hd_account = AccountHDPath::try_from("m/84'/0'/0'").unwrap();
        let hd_path = hd_account.address_at(1, 3).expect("address create");
        assert_eq!(
            StandardHDPath::try_from("m/84'/0'/0'/1/3").unwrap(),
            hd_path
        );
    }

    #[test]
    fn create_receive_address() {
        let hd_account = AccountHDPath::try_from("m/84'/0'/0'").unwrap();
        let hd_path = hd_account.address_at(0, 15).expect("address create");
        assert_eq!(
            StandardHDPath::try_from("m/84'/0'/0'/0/15").unwrap(),
            hd_path
        );
    }
}

#[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 = AccountHDPath::try_from("m/44'/60'/2'/0/3581").unwrap();
        let children: Vec<ChildNumber> = hdpath.into();
        assert_eq!(children.len(), 3);
        assert_eq!(children[0], ChildNumber::from_hardened_idx(44).unwrap());
        assert_eq!(children[1], ChildNumber::from_hardened_idx(60).unwrap());
        assert_eq!(children[2], ChildNumber::from_hardened_idx(2).unwrap());
    }

}