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
use crate::Derivation;
use std::{
    fmt::{self, Display},
    hash::{Hash, Hasher},
    ops::Deref,
    str::{self, FromStr},
};
use thiserror::Error;

/// any derivation path scheme
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy, Default)]
pub struct AnyScheme;

/// a derivation path with a tagged scheme (for example [`Bip44`]).
///
/// This allows following the specific set of rules for a derivation
/// path, preventing some errors. For example, the Bip44 scheme enforce
/// the 3 first derivation to be hard derivation indices and the 2 last
/// ones to be soft derivation indices.
///
/// [`Bip44`]: ./struct.Bip44.html
#[derive(Debug)]
pub struct DerivationPath<S> {
    path: Vec<Derivation>,
    _marker: std::marker::PhantomData<S>,
}

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct DerivationPathRange<S, R, T>
where
    R: Iterator<Item = T>,
    T: Into<Derivation>,
{
    root: DerivationPath<S>,
    range: R,
}

impl<S> DerivationPath<S> {
    /// Iterate through every derivation indices of the given derivation path
    ///
    pub fn iter(&self) -> std::slice::Iter<'_, Derivation> {
        self.path.iter()
    }

    /// create a new derivation path with appending the new derivation
    /// index to the current derivation path
    ///
    #[must_use = "this returns the result of the operation, without modifying the original"]
    pub fn append_unchecked(&self, derivation: Derivation) -> Self {
        let mut cloned = self.clone();
        cloned.path.push(derivation);
        cloned
    }

    /// create a range of derivation path with the given derivation range
    ///
    /// this will create an iterator of DerivationPath with the first derivation
    /// indices being the one fo the given derivation path and the append indices
    /// the one from the range.
    ///
    /// i.e. if the derivation path is `m/'1/42` and the range is `..20` it will
    /// create an iterator of derivation path for all: `m/'1/42/0` `m/'1/42/1`
    /// `m/'1/42/2` ...  `m/'1/42/19`.
    pub fn sub_range<R, T>(&self, range: R) -> DerivationPathRange<S, R, T>
    where
        R: Iterator<Item = T>,
        T: Into<Derivation>,
    {
        let root = self.clone();

        DerivationPathRange { root, range }
    }

    pub(crate) fn new_empty() -> Self {
        Self {
            path: Vec::new(),
            _marker: std::marker::PhantomData,
        }
    }

    pub(crate) fn push(&mut self, derivation: Derivation) {
        self.path.push(derivation)
    }

    #[inline]
    pub(crate) fn get_unchecked(&self, index: usize) -> Derivation {
        if let Some(v) = self.get(index).copied() {
            v
        } else {
            unsafe { std::hint::unreachable_unchecked() }
        }
    }

    pub fn coerce_unchecked<T>(self) -> DerivationPath<T> {
        DerivationPath {
            path: self.path,
            _marker: std::marker::PhantomData,
        }
    }
}

impl DerivationPath<AnyScheme> {
    pub fn new() -> Self {
        DerivationPath::new_empty()
    }
}

impl<S> Deref for DerivationPath<S> {
    type Target = [Derivation];
    fn deref(&self) -> &Self::Target {
        self.path.deref()
    }
}

/* Default ***************************************************************** */

impl Default for DerivationPath<AnyScheme> {
    fn default() -> Self {
        Self::new()
    }
}

/* Comparison ************************************************************** */

impl<T1, T2> PartialEq<DerivationPath<T1>> for DerivationPath<T2> {
    fn eq(&self, other: &DerivationPath<T1>) -> bool {
        self.path.eq(&other.path)
    }
}

impl<T> Eq for DerivationPath<T> {}

impl<T1, T2> PartialOrd<DerivationPath<T1>> for DerivationPath<T2> {
    fn partial_cmp(&self, other: &DerivationPath<T1>) -> Option<std::cmp::Ordering> {
        self.path.partial_cmp(&other.path)
    }
}

impl<T> Ord for DerivationPath<T> {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        self.path.cmp(&other.path)
    }
}

/* Hasher ****************************************************************** */

impl<T> Hash for DerivationPath<T> {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.path.hash(state);
        self._marker.hash(state);
    }
}

/* Iterator **************************************************************** */

impl<S, R, T> Iterator for DerivationPathRange<S, R, T>
where
    R: Iterator<Item = T>,
    T: Into<Derivation>,
{
    type Item = DerivationPath<S>;

    fn next(&mut self) -> Option<Self::Item> {
        let next = self.range.next()?.into();
        let path = self.root.append_unchecked(next);
        Some(path)
    }
}

impl<S, R, T> ExactSizeIterator for DerivationPathRange<S, R, T>
where
    R: Iterator<Item = T> + ExactSizeIterator,
    T: Into<Derivation>,
{
    fn len(&self) -> usize {
        self.range.len()
    }
}

impl<S, R, T> DoubleEndedIterator for DerivationPathRange<S, R, T>
where
    R: Iterator<Item = T> + DoubleEndedIterator,
    T: Into<Derivation>,
{
    fn next_back(&mut self) -> Option<Self::Item> {
        let next = self.range.next_back()?.into();
        let path = self.root.append_unchecked(next);
        Some(path)
    }
}

impl<S, R, T> std::iter::FusedIterator for DerivationPathRange<S, R, T>
where
    R: Iterator<Item = T> + std::iter::FusedIterator,
    T: Into<Derivation>,
{
}

impl<S> IntoIterator for DerivationPath<S> {
    type Item = Derivation;
    type IntoIter = std::vec::IntoIter<Self::Item>;

    fn into_iter(self) -> Self::IntoIter {
        self.path.into_iter()
    }
}

impl<'a, S> IntoIterator for &'a DerivationPath<S> {
    type Item = &'a Derivation;
    type IntoIter = std::slice::Iter<'a, Derivation>;

    fn into_iter(self) -> Self::IntoIter {
        self.iter()
    }
}

/* FromIterator ************************************************************ */

impl std::iter::FromIterator<Derivation> for DerivationPath<AnyScheme> {
    fn from_iter<T: IntoIterator<Item = Derivation>>(iter: T) -> Self {
        let mut dp = Self::new_empty();
        dp.path = iter.into_iter().collect();
        dp
    }
}

/* Display ***************************************************************** */

impl<S> Display for DerivationPath<S> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "m")?;
        for derivation in self.iter() {
            write!(f, "/{}", derivation)?;
        }
        Ok(())
    }
}

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

#[derive(Debug, Error)]
pub enum ParseDerivationPathError {
    #[error("Derivation Path should start with 'm'")]
    NotValidRoot,

    #[error("Invalid derivation at index '{index}'")]
    NotValidDerivation {
        index: usize,
        #[source]
        source: crate::ParseDerivationError,
    },

    #[error("Invalid number of derivation ({actual}), expected {expected}")]
    InvalidNumberOfDerivations { actual: usize, expected: usize },
}

impl FromStr for DerivationPath<AnyScheme> {
    type Err = ParseDerivationPathError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let mut derivations = s.split('/');

        let m = derivations
            .next()
            .ok_or(ParseDerivationPathError::NotValidRoot)?;
        if m != "m" {
            return Err(ParseDerivationPathError::NotValidRoot);
        }

        let mut path = Self::new_empty();
        for (index, derivation) in derivations.enumerate() {
            let derivation = derivation
                .parse()
                .map_err(|source| ParseDerivationPathError::NotValidDerivation { index, source })?;
            path.push(derivation);
        }

        Ok(path)
    }
}

/* Clone ******************************************************************* */

impl<S> Clone for DerivationPath<S> {
    fn clone(&self) -> Self {
        Self {
            path: self.path.clone(),
            _marker: std::marker::PhantomData,
        }
    }
}

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

    const MAX_DERIVATION_PATH_ANY_SCHEME_LENGTH: usize = 24;

    impl Arbitrary for DerivationPath<AnyScheme> {
        fn arbitrary<G: Gen>(g: &mut G) -> Self {
            let path_len = usize::arbitrary(g) % MAX_DERIVATION_PATH_ANY_SCHEME_LENGTH;

            std::iter::repeat_with(|| Derivation::arbitrary(g))
                .take(path_len)
                .collect()
        }
    }

    #[test]
    fn to_string() {
        let mut path = DerivationPath::<AnyScheme>::new_empty();
        assert_eq!(path.to_string(), "m");
        path.push(Derivation::new(0x0000_0000));
        assert_eq!(path.to_string(), "m/0");
        path.push(Derivation::new(0x0000_0007));
        assert_eq!(path.to_string(), "m/0/7");
        path.push(Derivation::new(0x8000_0001));
        assert_eq!(path.to_string(), "m/0/7/'1");
        path.push(Derivation::new(0x8000_000a));
        assert_eq!(path.to_string(), "m/0/7/'1/'10");
    }

    #[test]
    fn invalid_parse() {
        assert!("".parse::<DerivationPath<AnyScheme>>().is_err());
        assert!("a".parse::<DerivationPath<AnyScheme>>().is_err());
        assert!("M".parse::<DerivationPath<AnyScheme>>().is_err());
        assert!("m/a".parse::<DerivationPath<AnyScheme>>().is_err());
        assert!("m/\"1".parse::<DerivationPath<AnyScheme>>().is_err());
    }

    #[quickcheck]
    fn fmt_parse(derivation_path: DerivationPath<AnyScheme>) -> bool {
        let s = derivation_path.to_string();
        let v = s.parse::<DerivationPath<AnyScheme>>().unwrap();

        v == derivation_path
    }
}