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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
//! # Bip44 derivation scheme
//!
//! based on the [BIP-0044] scheme.
//!
//! While nearly only the full bip44 address is indeed interesting, it is
//! valuable to keep the different intermediate steps as they can be reused
//! to define specific API objects.
//!
//! For example, for example a wallet with support to multiple coin types
//! will be interested to keep the `m/'44` path. For every account it is
//! interesting to keep the `m/'44/'<coin_type>/'<account>`.
//!
//! We have the 5 levels of Derivations: `m / purpose' / coin_type' / account' / change / address_index`
//!
//! # Examples
//!
//! basic usage:
//!
//! ```
//! # use chain_path_derivation::{Derivation, HardDerivation};
//! # const BITCOIN: HardDerivation = HardDerivation::new_unchecked(Derivation::new(0x8000_0000));
//! # const ACCOUNT_01: HardDerivation = HardDerivation::new_unchecked(Derivation::new(0x8000_0000));
//! #
//! use chain_path_derivation::bip44;
//!
//! let account = bip44::new().bip44().coin_type(BITCOIN).account(ACCOUNT_01);
//! assert_eq!(account.to_string(), "m/'44/'0/'0")
//! ```
//!
//! then it is possible to generate addresses from there:
//!
//! ```
//! # use chain_path_derivation::{Derivation, HardDerivation, SoftDerivation};
//! # const BITCOIN: HardDerivation = HardDerivation::new_unchecked(Derivation::new(0x8000_0000));
//! # const ACCOUNT_01: HardDerivation = HardDerivation::new_unchecked(Derivation::new(0x8000_0000));
//! #
//! # use chain_path_derivation::bip44;
//! #
//! # let account = bip44::new().bip44().coin_type(BITCOIN).account(ACCOUNT_01);
//! let change = account.external();
//! let first_address = change.address(SoftDerivation::min_value().wrapping_add(0));
//! let second_address = change.address(SoftDerivation::min_value().wrapping_add(1));
//! assert_eq!(first_address.to_string(), "m/'44/'0/'0/0/0");
//! assert_eq!(second_address.to_string(), "m/'44/'0/'0/0/1");
//! ```

use crate::{
    AnyScheme, Derivation, DerivationPath, DerivationPathRange, HardDerivation,
    ParseDerivationPathError, SoftDerivation, SoftDerivationRange,
};
use std::str::{self, FromStr};

/// scheme for the Bip44 chain path derivation
///
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Bip44<P>(std::marker::PhantomData<P>);

#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Root;
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Purpose;
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct CoinType;
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Account;
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Change;
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Address;

const INDEX_PURPOSE: usize = 0;
const INDEX_COIN_TYPE: usize = 1;
const INDEX_ACCOUNT: usize = 2;
const INDEX_CHANGE: usize = 3;
const INDEX_ADDRESS: usize = 4;

/// the BIP44 purpose ('44). This is the first item of the derivation path
pub const PURPOSE_BIP44: HardDerivation =
    HardDerivation::new_unchecked(Derivation::new(0x8000_002C));

/// the Chimeric BIP44 purpose ('1852). This is the first item of the derivation path
///
pub const PURPOSE_CHIMERIC: HardDerivation =
    HardDerivation::new_unchecked(Derivation::new(0x8000_073C));

/// create a Bip44 chain path derivation
///
/// This derivation level is not really interesting, though it is interesting
/// to consider the following levels which can then be constructed via
/// each individual type.
///
/// See [module documentation] for more details
///
/// [module documentation]: ./index.html
#[inline]
pub fn new() -> DerivationPath<Bip44<Root>> {
    DerivationPath::new_empty()
}

impl DerivationPath<Bip44<Root>> {
    pub fn bip44(&self) -> DerivationPath<Bip44<Purpose>> {
        let mut p = self.clone();
        p.push(PURPOSE_BIP44.into());
        p.coerce_unchecked()
    }

    /// use the same "model" of 5 derivation level but instead of starting with the
    /// bip44 Hard Derivation uses the `'1852` (`'0x073C`) derivation path.
    ///
    /// see <https://input-output-hk.github.io/adrestia/docs/key-concepts/hierarchical-deterministic-wallets/>
    ///
    pub fn chimeric(&self) -> DerivationPath<Bip44<Purpose>> {
        let mut p = self.clone();
        p.push(PURPOSE_CHIMERIC.into());
        p.coerce_unchecked()
    }
}

impl DerivationPath<Bip44<Purpose>> {
    /// add the next derivation level for the Bip44 chain path derivation.
    ///
    /// See [module documentation] for more details
    ///
    /// [module documentation]: ./index.html
    pub fn coin_type(&self, coin_type: HardDerivation) -> DerivationPath<Bip44<CoinType>> {
        let mut ct = self.clone();
        ct.push(coin_type.into());
        ct.coerce_unchecked()
    }

    #[inline]
    pub fn purpose(&self) -> HardDerivation {
        HardDerivation::new_unchecked(self.get_unchecked(INDEX_PURPOSE))
    }
}

impl DerivationPath<Bip44<CoinType>> {
    /// See [module documentation] for more details
    ///
    /// [module documentation]: ./index.html
    pub fn account(&self, account: HardDerivation) -> DerivationPath<Bip44<Account>> {
        let mut a = self.clone();
        a.push(account.into());
        a.coerce_unchecked()
    }

    #[inline]
    pub fn purpose(&self) -> HardDerivation {
        HardDerivation::new_unchecked(self.get_unchecked(INDEX_PURPOSE))
    }

    #[inline]
    pub fn coin_type(&self) -> HardDerivation {
        HardDerivation::new_unchecked(self.get_unchecked(INDEX_COIN_TYPE))
    }
}

impl DerivationPath<Bip44<Account>> {
    pub const EXTERNAL: SoftDerivation = SoftDerivation::new_unchecked(Derivation::new(0));
    pub const INTERNAL: SoftDerivation = SoftDerivation::new_unchecked(Derivation::new(1));
    pub const ACCOUNT: SoftDerivation = SoftDerivation::new_unchecked(Derivation::new(2));

    /// See [module documentation] for more details
    ///
    /// [module documentation]: ./index.html
    fn change(&self, change: SoftDerivation) -> DerivationPath<Bip44<Change>> {
        let mut c = self.clone();
        c.push(change.into());
        c.coerce_unchecked()
    }

    /// See [module documentation] for more details
    ///
    /// [module documentation]: ./index.html
    pub fn external(&self) -> DerivationPath<Bip44<Change>> {
        self.change(Self::EXTERNAL)
    }

    /// See [module documentation] for more details
    ///
    /// [module documentation]: ./index.html
    pub fn internal(&self) -> DerivationPath<Bip44<Change>> {
        self.change(Self::INTERNAL)
    }

    /// See [module documentation] for more details
    ///
    /// [module documentation]: ./index.html
    pub fn reward_account(&self) -> DerivationPath<Bip44<Change>> {
        debug_assert_eq!(self.purpose(), PURPOSE_CHIMERIC,);
        self.change(Self::ACCOUNT)
    }

    #[inline]
    pub fn purpose(&self) -> HardDerivation {
        HardDerivation::new_unchecked(self.get_unchecked(INDEX_PURPOSE))
    }

    #[inline]
    pub fn coin_type(&self) -> HardDerivation {
        HardDerivation::new_unchecked(self.get_unchecked(INDEX_COIN_TYPE))
    }

    #[inline]
    pub fn account(&self) -> HardDerivation {
        HardDerivation::new_unchecked(self.get_unchecked(INDEX_ACCOUNT))
    }
}

impl DerivationPath<Bip44<Change>> {
    /// See [module documentation] for more details
    ///
    /// [module documentation]: ./index.html
    pub fn address(&self, address: SoftDerivation) -> DerivationPath<Bip44<Address>> {
        let mut a = self.clone();
        a.push(address.into());
        a.coerce_unchecked()
    }

    /// build a range of addresses
    ///
    /// # panics
    ///
    /// This function will panic is the range is out of bounds for a valid
    /// address (`SoftDerivation`).
    ///
    /// # Examples
    ///
    /// Generate the first 20 chain path derivation addresses, from 0 to 19 (inclusive):
    ///
    /// ```
    /// # use chain_path_derivation::{Derivation, HardDerivation, SoftDerivation};
    /// # const BITCOIN: HardDerivation = HardDerivation::new_unchecked(Derivation::new(0x8000_0000));
    /// # const ACCOUNT_01: HardDerivation = HardDerivation::new_unchecked(Derivation::new(0x8000_0000));
    /// #
    /// # use chain_path_derivation::bip44;
    /// #
    /// let account = bip44::new().bip44().coin_type(BITCOIN).account(ACCOUNT_01);
    /// let change = account.external();
    /// let end = SoftDerivation::min_value().saturating_add(20);
    ///
    /// let addresses = change.addresses((..end)).collect::<Vec<_>>();
    ///
    /// assert_eq!(addresses[0].to_string(), "m/'44/'0/'0/0/0");
    /// assert_eq!(addresses[1].to_string(), "m/'44/'0/'0/0/1");
    /// // ..
    /// assert_eq!(addresses[19].to_string(), "m/'44/'0/'0/0/19");
    /// ```
    pub fn addresses<R, T>(
        &self,
        range: R,
    ) -> DerivationPathRange<DerivationPath<Bip44<Address>>, SoftDerivationRange, SoftDerivation>
    where
        R: std::ops::RangeBounds<T>,
        T: std::convert::TryInto<SoftDerivation> + Copy,
        <T as std::convert::TryInto<SoftDerivation>>::Error: std::error::Error,
    {
        let range = SoftDerivationRange::new(range);

        self.clone().coerce_unchecked().sub_range(range)
    }

    #[inline]
    pub fn purpose(&self) -> HardDerivation {
        HardDerivation::new_unchecked(self.get_unchecked(INDEX_PURPOSE))
    }

    #[inline]
    pub fn coin_type(&self) -> HardDerivation {
        HardDerivation::new_unchecked(self.get_unchecked(INDEX_COIN_TYPE))
    }

    #[inline]
    pub fn account(&self) -> HardDerivation {
        HardDerivation::new_unchecked(self.get_unchecked(INDEX_ACCOUNT))
    }

    #[inline]
    pub fn change(&self) -> SoftDerivation {
        SoftDerivation::new_unchecked(self.get_unchecked(INDEX_CHANGE))
    }
}

impl DerivationPath<Bip44<Address>> {
    #[inline]
    pub fn purpose(&self) -> HardDerivation {
        HardDerivation::new_unchecked(self.get_unchecked(INDEX_PURPOSE))
    }

    #[inline]
    pub fn coin_type(&self) -> HardDerivation {
        HardDerivation::new_unchecked(self.get_unchecked(INDEX_COIN_TYPE))
    }

    #[inline]
    pub fn account(&self) -> HardDerivation {
        HardDerivation::new_unchecked(self.get_unchecked(INDEX_ACCOUNT))
    }

    #[inline]
    pub fn change(&self) -> SoftDerivation {
        SoftDerivation::new_unchecked(self.get_unchecked(INDEX_CHANGE))
    }

    #[inline]
    pub fn address(&self) -> SoftDerivation {
        SoftDerivation::new_unchecked(self.get_unchecked(INDEX_ADDRESS))
    }
}

/* FromStr ***************************************************************** */

macro_rules! mk_from_str_dp_bip44 {
    ($t:ty, $len:expr) => {
        impl FromStr for DerivationPath<$t> {
            type Err = ParseDerivationPathError;

            fn from_str(s: &str) -> Result<Self, Self::Err> {
                let dp = s.parse::<DerivationPath<AnyScheme>>()?;

                if dp.len() == $len {
                    Ok(dp.coerce_unchecked())
                } else {
                    Err(ParseDerivationPathError::InvalidNumberOfDerivations {
                        actual: dp.len(),
                        expected: $len,
                    })
                }
            }
        }
    };
}

mk_from_str_dp_bip44!(Bip44<Root>, 0);
mk_from_str_dp_bip44!(Bip44<Purpose>, 1);
mk_from_str_dp_bip44!(Bip44<CoinType>, 2);
mk_from_str_dp_bip44!(Bip44<Account>, 3);
mk_from_str_dp_bip44!(Bip44<Change>, 4);
mk_from_str_dp_bip44!(Bip44<Address>, 5);

#[cfg(test)]
mod tests {
    use super::*;
    use quickcheck::{Arbitrary, Gen};

    macro_rules! mk_arbitrary_dp_bip44 {
        ($t:ty, $len:expr) => {
            impl Arbitrary for DerivationPath<$t> {
                fn arbitrary<G: Gen>(g: &mut G) -> Self {
                    let dp = std::iter::repeat_with(|| Derivation::arbitrary(g))
                        .take($len)
                        .collect::<DerivationPath<AnyScheme>>();
                    dp.coerce_unchecked()
                }
            }
        };
    }

    mk_arbitrary_dp_bip44!(Bip44<Root>, 0);
    mk_arbitrary_dp_bip44!(Bip44<Purpose>, 1);
    mk_arbitrary_dp_bip44!(Bip44<CoinType>, 2);
    mk_arbitrary_dp_bip44!(Bip44<Account>, 3);
    mk_arbitrary_dp_bip44!(Bip44<Change>, 4);
    mk_arbitrary_dp_bip44!(Bip44<Address>, 5);

    macro_rules! mk_quickcheck_dp_bip44 {
        ($t:ty) => {
            paste::item! {
                #[quickcheck]
                #[allow(non_snake_case)]
                fn [< fmt_parse $t>](derivation_path: DerivationPath<Bip44<$t>>) -> bool {
                    let s = derivation_path.to_string();
                    let v = s.parse::<DerivationPath<Bip44<$t>>>().unwrap();

                    v == derivation_path
                }
            }
        };
    }

    mk_quickcheck_dp_bip44!(Root);
    mk_quickcheck_dp_bip44!(Purpose);
    mk_quickcheck_dp_bip44!(CoinType);
    mk_quickcheck_dp_bip44!(Account);
    mk_quickcheck_dp_bip44!(Change);
    mk_quickcheck_dp_bip44!(Address);
}