iop_keyvault/
bip44path.rs

1use super::*;
2
3/// Entry point to generate a [BIP-0044](https://github.com/bitcoin/bips/blob/master/bip-0044.mediawiki) compliant key hierarchy.
4pub struct Bip44Path;
5
6impl Bip44Path {
7    /// Creates a path for a BIP-0044 coin with a given coin index (check [SLIP-0044](https://github.com/satoshilabs/slips/blob/master/slip-0044.md)
8    /// for registered coin indexes)
9    pub fn coin(slip44: i32) -> Bip44CoinPath {
10        Bip44CoinPath { slip44 }
11    }
12}
13
14/// Valid operations for a bip44 coin.
15#[derive(Clone, Debug)]
16pub struct Bip44CoinPath {
17    slip44: i32,
18}
19
20impl Bip44CoinPath {
21    /// Accessor for the coin index.
22    pub fn slip44(&self) -> i32 {
23        self.slip44
24    }
25
26    /// Creates an account of the current coin.
27    pub fn account(self, idx: i32) -> Bip44AccountPath {
28        Bip44AccountPath { parent: self, account: idx }
29    }
30
31    /// Returns the BIP-0032 path of the coin.
32    pub fn bip32_path(&self) -> bip32::Path {
33        Bip43Path::purpose(44).bip32_path().append(ChildIndex::Hardened(self.slip44))
34    }
35}
36
37/// Valid operations for a bip44 account.
38#[derive(Clone, Debug)]
39pub struct Bip44AccountPath {
40    parent: Bip44CoinPath,
41    account: i32,
42}
43
44impl Bip44AccountPath {
45    /// Accessor for the coin this account is in.
46    pub fn parent(&self) -> &Bip44CoinPath {
47        &self.parent
48    }
49
50    /// Accessor for the account index.
51    pub fn account(&self) -> i32 {
52        self.account
53    }
54
55    /// Creates a sub-account of the current account. For blockchains with UTXO-based accounting
56    /// it makes sense to separate change addresses from receiving addresses. For balance-based
57    /// accounting, just use the `key` method.
58    pub fn chain(self, chain: Chain) -> Bip44SubAccountPath {
59        Bip44SubAccountPath { parent: self, chain }
60    }
61
62    /// Creates a receiving key of the current account. See also the `chain` method for UTXO-based
63    /// blockchains.
64    pub fn key(self, idx: i32) -> Bip44KeyPath {
65        self.chain(Chain::Receiving).key(idx)
66    }
67
68    /// Returns the BIP-0032 path of the account.
69    pub fn bip32_path(&self) -> bip32::Path {
70        self.parent.bip32_path().append(ChildIndex::Hardened(self.account))
71    }
72}
73
74#[derive(Clone, Debug)]
75/// Valid operations for a bip44 sub-account.
76pub struct Bip44SubAccountPath {
77    parent: Bip44AccountPath,
78    chain: Chain,
79}
80
81impl Bip44SubAccountPath {
82    /// Accessor for the account this sub-account is in.
83    pub fn parent(&self) -> &Bip44AccountPath {
84        &self.parent
85    }
86
87    /// Accessor for the chain (receiving or change).
88    pub fn chain(&self) -> Chain {
89        self.chain
90    }
91
92    /// Creates a receiving key of the current sub-account.
93    pub fn key(self, idx: i32) -> Bip44KeyPath {
94        Bip44KeyPath { parent: self, key: idx }
95    }
96
97    /// Returns the BIP-0032 path of the sub-account.
98    pub fn bip32_path(&self) -> bip32::Path {
99        self.parent.bip32_path().append(ChildIndex::Normal(self.chain() as i32))
100    }
101}
102
103#[derive(Clone, Debug)]
104/// Valid operations for a bip44 key.
105pub struct Bip44KeyPath {
106    parent: Bip44SubAccountPath,
107    key: i32,
108}
109
110impl Bip44KeyPath {
111    /// Accessor for the sub-account this key is in.
112    pub fn parent(&self) -> &Bip44SubAccountPath {
113        &self.parent
114    }
115
116    /// Accessor for the key index.
117    pub fn key(&self) -> i32 {
118        self.key
119    }
120
121    /// Returns the BIP-0032 path of the key.
122    pub fn bip32_path(&self) -> bip32::Path {
123        self.parent.bip32_path().append(ChildIndex::Normal(self.key()))
124    }
125}