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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
use super::*;

/// Entry point to generate a hierarchical deterministic wallet using the BIP-0044 standard. It is a more structured
/// way to use the same seed for multiple coins, each with multiple accounts, each accounts with a new key for each
/// transaction request. The standard is built on BIP-0043 using the purpose code 44. And BIP-0043 itself uses
/// BIP-0032 to derive all nodes from a single master extended private key.
#[derive(Clone, Debug)]
pub struct Bip44;

impl Bip44 {
    /// Creates the entry point to a given coin based on the subtree defined by SLIP-0044 for it.
    pub fn network<C: KeyDerivationCrypto>(
        &self, seed: &Seed, network: &'static dyn Network<Suite = C>,
    ) -> Result<Bip44Coin<C>> {
        let path = Bip44Path::coin(network.slip44());
        let master = Bip32.master(seed, network.subtree());
        let node = master.derive_hardened(44)?.derive_hardened(network.slip44())?;
        Ok(Bip44Coin { path, network, node })
    }
}

#[derive(Clone, Debug)]
/// Represents a given coin in the BIP32 tree.
pub struct Bip44Coin<C: KeyDerivationCrypto + 'static> {
    path: Bip44CoinPath,
    network: &'static dyn Network<Suite = C>,
    node: Bip32Node<C>,
}

impl<C: KeyDerivationCrypto + 'static> Bip44Coin<C> {
    /// Backdoor to mount a full BIP44 hierarchy at any place in the BIP32 tree. Use only for
    /// implementing advanced or fringe non-standard use-cases.
    pub fn new(
        path: Bip44CoinPath, network: &'static dyn Network<Suite = C>, node: Bip32Node<C>,
    ) -> Self {
        Self { path, network, node }
    }

    /// Accessor for the underlying BIP32 node.
    pub fn node(&self) -> &Bip32Node<C> {
        &self.node
    }

    /// Creates an account in the coin with the given index.
    pub fn account(&self, account: i32) -> Result<Bip44Account<C>> {
        ensure!(account >= 0, "Cannot use negative account index");
        Ok(Bip44Account::new(
            self.path.clone().account(account),
            self.network,
            self.node.derive_hardened(account)?,
        ))
    }

    /// Accessor for the BIP44 path of the coin.
    pub fn bip44_path(&self) -> &Bip44CoinPath {
        &self.path
    }

    /// Accessor for the BIP32 path of the coin.
    pub fn bip32_path(&self) -> &bip32::Path {
        self.node.path()
    }

    /// Accessor for the underlying network.
    pub fn network(&self) -> &'static dyn Network<Suite = C> {
        self.network
    }
}

#[derive(Clone, Debug)]
/// Represents a given account of a given coin in the BIP32 tree.
pub struct Bip44Account<C: KeyDerivationCrypto + 'static> {
    path: Bip44AccountPath,
    network: &'static dyn Network<Suite = C>,
    node: Bip32Node<C>,
}

impl<C: KeyDerivationCrypto + 'static> Bip44Account<C> {
    /// Backdoor to mount a BIP44 account at any place in the BIP32 tree. Use only for
    /// implementing advanced or fringe non-standard use-cases.
    pub fn new(
        path: Bip44AccountPath, network: &'static dyn Network<Suite = C>, node: Bip32Node<C>,
    ) -> Self {
        Self { path, network, node }
    }

    /// Accessor for the underlying BIP32 node.
    pub fn node(&self) -> &Bip32Node<C> {
        &self.node
    }

    /// Creates a sub-account for either external keys (receiving addresses) or
    /// internal keys (change addresses). This distinction might help in accounting.
    pub fn chain(&self, chain: Chain) -> Result<Bip44SubAccount<C>> {
        Ok(Bip44SubAccount::new(
            self.path.clone().chain(chain),
            self.network,
            self.node.derive_normal(chain as i32)?,
        ))
    }

    /// Creates a key with a given index used on the chain for storing balance or
    /// authenticating actions. By default these keys are made on the receiving sub-account.
    pub fn key(&self, key: i32) -> Result<Bip44Key<C>> {
        self.chain(Chain::Receiving)?.key(key)
    }

    /// Accessor for the BIP44 path of the account.
    pub fn bip44_path(&self) -> &Bip44AccountPath {
        &self.path
    }

    /// Accessor for the BIP32 path of the account.
    pub fn bip32_path(&self) -> &bip32::Path {
        self.node.path()
    }

    /// Neuters the account and converts it into its public API
    pub fn neuter(&self) -> Bip44PublicAccount<C> {
        Bip44PublicAccount::new(self.path.clone(), self.network, self.node.neuter())
    }

    /// Accessor for the underlying nwtwork.
    pub fn network(&self) -> &'static dyn Network<Suite = C> {
        self.network
    }
}

#[derive(Clone, Debug)]
/// Represents a given account of a given coin in the BIP32 tree.
pub struct Bip44PublicAccount<C: KeyDerivationCrypto + 'static> {
    path: Bip44AccountPath,
    network: &'static dyn Network<Suite = C>,
    node: Bip32PublicNode<C>,
}

impl<C: KeyDerivationCrypto + 'static> Bip44PublicAccount<C> {
    /// Backdoor to mount a BIP44 account at any place in the BIP32 tree. Use only for
    /// implementing advanced or fringe non-standard use-cases.
    pub fn new(
        path: Bip44AccountPath, network: &'static dyn Network<Suite = C>, node: Bip32PublicNode<C>,
    ) -> Self {
        Self { path, network, node }
    }

    /// Accessor for the underlying BIP32 node.
    pub fn node(&self) -> &Bip32PublicNode<C> {
        &self.node
    }

    /// Creates a sub-account for either external keys (receiving addresses) or
    /// internal keys (change addresses). This distinction might help in accounting.
    pub fn chain(&self, chain: Chain) -> Result<Bip44PublicSubAccount<C>> {
        Ok(Bip44PublicSubAccount::new(
            self.path.clone().chain(chain),
            self.network,
            self.node.derive_normal(chain as i32)?,
        ))
    }

    /// Creates a key with a given index used on the chain for storing balance or
    /// authenticating actions. By default these keys are made on the receiving sub-account.
    pub fn key(&self, key: i32) -> Result<Bip44PublicKey<C>> {
        self.chain(Chain::Receiving)?.key(key)
    }

    /// Accessor for the BIP44 path of the account.
    pub fn bip44_path(&self) -> &Bip44AccountPath {
        &self.path
    }

    /// Accessor for the BIP32 path of the account.
    pub fn bip32_path(&self) -> &bip32::Path {
        self.node.path()
    }

    /// Accessor for the underlying nwtwork.
    pub fn network(&self) -> &'static dyn Network<Suite = C> {
        self.network
    }
}

#[derive(Copy, Clone, Debug, PartialEq, PartialOrd, Eq, Hash)]
/// Enumeration used for distinguishing external keys (receiving addresses) from
/// internal keys (change addresses). This distinction might help in accounting.
pub enum Chain {
    /// External keys (receiving addresses)
    Receiving = 0,
    /// Internal keys (change addresses)
    Change = 1,
}

impl From<bool> for Chain {
    fn from(change: bool) -> Self {
        if change {
            Chain::Change
        } else {
            Chain::Receiving
        }
    }
}

impl From<Chain> for bool {
    fn from(chain: Chain) -> bool {
        chain == Chain::Change
    }
}

#[derive(Clone, Debug)]
/// A sub-account of a given account on a given coin that is either used for external keys (receiving addresses) or
/// internal keys (change addresses). Some implementations do not distinguish these and just always use receiving
/// addresses.
pub struct Bip44SubAccount<C: KeyDerivationCrypto + 'static> {
    path: Bip44SubAccountPath,
    network: &'static dyn Network<Suite = C>,
    node: Bip32Node<C>,
}

impl<C: KeyDerivationCrypto + 'static> Bip44SubAccount<C> {
    /// Backdoor to mount a BIP44 sub-account at any place in the BIP32 tree. Use only for
    /// implementing advanced or fringe non-standard use-cases.
    pub fn new(
        path: Bip44SubAccountPath, network: &'static dyn Network<Suite = C>, node: Bip32Node<C>,
    ) -> Self {
        Self { path, network, node }
    }

    /// Accessor for the underlying BIP32 node.
    pub fn node(&self) -> &Bip32Node<C> {
        &self.node
    }

    /// Creates a key with a given index used on the chain for storing balance or
    /// authenticating actions.
    pub fn key(&self, key: i32) -> Result<Bip44Key<C>> {
        Ok(Bip44Key::new(self.path.clone().key(key), self.network, self.node.derive_normal(key)?))
    }

    /// Accessor for the BIP44 path of the sub-account.
    pub fn bip44_path(&self) -> &Bip44SubAccountPath {
        &self.path
    }

    /// Accessor for the BIP32 path of the sub-account.
    pub fn bip32_path(&self) -> &bip32::Path {
        self.node.path()
    }

    /// Neuters the sub-account and converts it into its public API
    pub fn neuter(&self) -> Bip44PublicSubAccount<C> {
        Bip44PublicSubAccount::new(self.path.clone(), self.network, self.node.neuter())
    }

    /// Accessor for the underlying nwtwork.
    pub fn network(&self) -> &'static dyn Network<Suite = C> {
        self.network
    }
}

#[derive(Clone, Debug)]
/// A sub-account of a given account on a given coin that is either used for external keys (receiving addresses) or
/// internal keys (change addresses). Some implementations do not distinguish these and just always use receiving
/// addresses.
pub struct Bip44PublicSubAccount<C: KeyDerivationCrypto + 'static> {
    path: Bip44SubAccountPath,
    network: &'static dyn Network<Suite = C>,
    node: Bip32PublicNode<C>,
}

impl<C: KeyDerivationCrypto + 'static> Bip44PublicSubAccount<C> {
    /// Backdoor to mount a BIP44 sub-account at any place in the BIP32 tree. Use only for
    /// implementing advanced or fringe non-standard use-cases.
    pub fn new(
        path: Bip44SubAccountPath, network: &'static dyn Network<Suite = C>,
        node: Bip32PublicNode<C>,
    ) -> Self {
        Self { path, network, node }
    }

    /// Accessor for the underlying BIP32 node.
    pub fn node(&self) -> &Bip32PublicNode<C> {
        &self.node
    }

    /// Creates a key with a given index used on the chain for storing balance or
    /// authenticating actions.
    pub fn key(&self, key: i32) -> Result<Bip44PublicKey<C>> {
        Ok(Bip44PublicKey::new(
            self.path.clone().key(key),
            self.network,
            self.node.derive_normal(key)?,
        ))
    }

    /// Accessor for the BIP44 path of the sub-account.
    pub fn bip44_path(&self) -> &Bip44SubAccountPath {
        &self.path
    }

    /// Accessor for the BIP32 path of the sub-account.
    pub fn bip32_path(&self) -> &bip32::Path {
        self.node.path()
    }

    /// Accessor for the underlying nwtwork.
    pub fn network(&self) -> &'static dyn Network<Suite = C> {
        self.network
    }
}

#[derive(Clone, Debug)]
/// Represents a key with a given index within a sub-account used on the chain for storing balance or authenticating actions.
pub struct Bip44Key<C: KeyDerivationCrypto + 'static> {
    path: Bip44KeyPath,
    network: &'static dyn Network<Suite = C>,
    node: Bip32Node<C>,
}

impl<C: KeyDerivationCrypto + 'static> Bip44Key<C> {
    /// Backdoor to mount a BIP44 key at any place in the BIP32 tree. Use only for
    /// implementing advanced or fringe non-standard use-cases.
    pub fn new(
        path: Bip44KeyPath, network: &'static dyn Network<Suite = C>, node: Bip32Node<C>,
    ) -> Self {
        Self { path, network, node }
    }

    /// Accessor for the underlying BIP32 node.
    pub fn node(&self) -> &Bip32Node<C> {
        &self.node
    }

    /// Accessor for the BIP44 path of the key.
    pub fn bip44_path(&self) -> &Bip44KeyPath {
        &self.path
    }

    /// Accessor for the BIP32 path of the key.
    pub fn bip32_path(&self) -> &bip32::Path {
        self.node.path()
    }

    /// Creates the private key for authenticating actions.
    pub fn to_private_key(&self) -> C::PrivateKey {
        self.node.private_key()
    }

    /// Neuters the key and converts it into its public API
    pub fn neuter(&self) -> Bip44PublicKey<C> {
        Bip44PublicKey::new(self.path.clone(), self.network, self.node.neuter())
    }

    /// Accessor for the underlying nwtwork.
    pub fn network(&self) -> &'static dyn Network<Suite = C> {
        self.network
    }
}

#[derive(Clone, Debug)]
/// Represents a public key with a given index used on the chain for verifying signatures or validating key identifiers.
pub struct Bip44PublicKey<C: KeyDerivationCrypto + 'static> {
    path: Bip44KeyPath,
    network: &'static dyn Network<Suite = C>,
    node: Bip32PublicNode<C>,
}

impl<C: KeyDerivationCrypto + 'static> Bip44PublicKey<C> {
    /// Backdoor to mount a BIP44 key at any place in the BIP32 tree. Use only for
    /// implementing advanced or fringe non-standard use-cases.
    pub fn new(
        path: Bip44KeyPath, network: &'static dyn Network<Suite = C>, node: Bip32PublicNode<C>,
    ) -> Self {
        Self { path, network, node }
    }

    /// Accessor for the underlying BIP32 node.
    pub fn node(&self) -> &Bip32PublicNode<C> {
        &self.node
    }

    /// Accessor for the BIP44 path of the key.
    pub fn bip44_path(&self) -> &Bip44KeyPath {
        &self.path
    }

    /// Accessor for the BIP32 path of the key.
    pub fn bip32_path(&self) -> &bip32::Path {
        self.node.path()
    }

    /// Creates the public key for verifying authentications done by this key.
    pub fn to_public_key(&self) -> C::PublicKey {
        self.node.public_key()
    }

    /// Creates the key identifier for the public key. This is an extra layer of security for single-use keys, so the
    /// revealing of the public key can be delayed to the point when the authenticated action (spending some coin or
    /// revoking access) makes the public key irrelevant after the action is successful.
    pub fn to_key_id(&self) -> C::KeyId {
        self.node.key_id()
    }

    /// Accessor for the underlying nwtwork.
    pub fn network(&self) -> &'static dyn Network<Suite = C> {
        self.network
    }
}

#[cfg(test)]
mod test {
    use super::{
        multicipher::*,
        secp256k1::{ark, hyd},
        *,
    };

    const PHRASE: &str = "blast cargo razor option vote shoe stock cruel mansion boy spot never album crop reflect kangaroo blouse slam empty shoot cable vital crane manual";

    #[test]
    fn path() -> Result<()> {
        let seed = Bip39::new().phrase(PHRASE)?.password("");

        let key = Bip44.network(&seed, &ark::Mainnet)?.account(0)?.chain(Chain::Change)?.key(0)?;
        let expected = "m/44'/111'/0'/1/0".parse::<Path>()?;
        assert_eq!(key.bip32_path(), &expected);
        Ok(())
    }

    #[test]
    fn derive() -> Result<()> {
        let seed = Bip39::new().phrase(PHRASE)?.password(Seed::PASSWORD);
        let key =
            Bip44.network(&seed, &hyd::Mainnet)?.account(0)?.chain(Chain::Receiving)?.key(0)?;

        assert_eq!(key.neuter().to_p2pkh_addr(), "hWNN8ymcsLdJivbwbBaPS8X1vekxB2pdwV");
        Ok(())
    }

    #[test]
    fn bip44_fantasy() -> Result<()> {
        let seed = Bip39::new().phrase(PHRASE)?.password(Seed::PASSWORD);

        let coin = Bip44.network(&seed, &hyd::Mainnet)?;
        assert_eq!(coin.bip32_path(), &"m/44'/4741444'".parse()?);
        assert_eq!(coin.to_xprv(), "HYDMVzSE83q9tomV2q935JF5mRZXUB37urS6ZpAvi17d5tFNK7K45ddZmjMZJKYQ8yLjKrq4HFewhXuL5AjDzb9Ft5efNT8upEy1ftARyhmxRFZH");

        let account = coin.account(0)?;
        assert_eq!(account.bip32_path(), &"m/44'/4741444'/0'".parse()?);
        assert_eq!(account.to_xprv(), "HYDMVzUVgP7S8GNPrKWhvoFPivfS25QnhfKi1iydA7jbWRwVuMJTVZzBQvBV86zpNJg83rrtvj6SWsftT3nNg5PQ9kwEdzTSpEDH5KbZsjbKBbhs");
        assert_eq!(account.neuter().to_xpub(), "hydmW129yVihVKVgXGWfvV3ZSePrhri2FgepKuyMEZ3Eg7bf91jrFZrmAo2hsVcS49dTRP5wZM7A8vudxWk5n8J2Ci12CSNvLy76CsnRaMK4A2b5");

        let key = account.key(5)?;
        assert_eq!(key.bip32_path(), &"m/44'/4741444'/0'/0/5".parse()?);
        assert_eq!(key.to_wif(), "HU6abSVnUK71QybJeoMNzriuLj7snTyzwM1vU1EtQVPxz9zVvBha");
        assert_eq!(
            hex::encode(key.to_private_key().to_bytes()),
            "51957a95a577cba15e1c4d4c4683bcaab17e03fbe7df12c60e69bf263485a00c"
        );
        assert_eq!(
            MPrivateKey::from(key.to_private_key()).public_key().to_string(),
            "pszxZN1JfKTPiZ2EfEzGW6eFmMnPV9gnUGCSTwHKyFbHAVK"
        );
        assert_eq!(
            hex::encode(key.neuter().to_public_key().to_bytes()),
            "03397d1e17dbecc754b9b1089ae2cce315def5e24df9a9b82230b730418e80110e"
        );
        assert_eq!(
            MPublicKey::from(key.neuter().to_public_key()).to_string(),
            "pszxZN1JfKTPiZ2EfEzGW6eFmMnPV9gnUGCSTwHKyFbHAVK"
        );
        assert_eq!(key.neuter().to_p2pkh_addr(), "hKQGzsR5sStKMEcX1EtGKa3Q33jWH6JfKC");
        assert_eq!(
            MKeyId::from(key.neuter().to_key_id()).to_string(),
            "isz5GewAWjJGfSB31qzds34UUSpnsmD"
        );

        let sub_account = account.chain(Chain::Receiving)?;
        assert_eq!(sub_account.bip32_path(), &"m/44'/4741444'/0'/0".parse()?);
        assert_eq!(sub_account.to_xprv(), "HYDMVzVXwQGdZsas8KUKcmTzXtR4Le1fioCn4r8aMFa1CLvCzXJJa28ogEUPdZD1BrSTuBLDGRv5SaPHStH7ZYABhdD6Tf6dCPLd5eaJ9aArC8po");
        assert_eq!(sub_account.neuter().to_xpub(), "hydmW13CEWstvvi9oGUHcTGAFc9V2RJuGpXtP38JRgseN2aNEBjhL21PS7KcNwqbz2jh787bMSPNkd7sYsdVm5rzdeyweau32iL6qzCgpn79R4o2");

        let recv0 = sub_account.key(0)?;
        assert_eq!(recv0.bip32_path(), &"m/44'/4741444'/0'/0/0".parse()?);
        assert_eq!(recv0.neuter().to_p2pkh_addr(), "hWNN8ymcsLdJivbwbBaPS8X1vekxB2pdwV");

        let change2 = account.chain(Chain::Change)?.key(2)?;
        assert_eq!(change2.bip32_path(), &"m/44'/4741444'/0'/1/2".parse()?);

        // let pub_account = account.neuter();

        Ok(())
    }
}