whisky_wallet/wallet/
derivation_indices.rs

1use crate::wallet_constants::HARDENED_KEY_START;
2
3pub struct DerivationIndices(pub Vec<u32>);
4
5impl Default for DerivationIndices {
6    fn default() -> Self {
7        DerivationIndices(vec![
8            HARDENED_KEY_START + 1852, // purpose
9            HARDENED_KEY_START + 1815, // coin type
10            HARDENED_KEY_START,        // account
11            0,                         // payment
12            0,                         // key index
13        ])
14    }
15}
16
17impl DerivationIndices {
18    pub fn payment(account_index: u32, key_index: u32) -> Self {
19        DerivationIndices(vec![
20            HARDENED_KEY_START + 1852,          // purpose
21            HARDENED_KEY_START + 1815,          // coin type
22            HARDENED_KEY_START + account_index, // account
23            0,                                  // payment
24            key_index,                          // key index
25        ])
26    }
27
28    pub fn stake(account_index: u32, key_index: u32) -> Self {
29        DerivationIndices(vec![
30            HARDENED_KEY_START + 1852,          // purpose
31            HARDENED_KEY_START + 1815,          // coin type
32            HARDENED_KEY_START + account_index, // account
33            2,                                  // stake
34            key_index,                          // key index
35        ])
36    }
37
38    pub fn drep(account_index: u32, key_index: u32) -> Self {
39        DerivationIndices(vec![
40            HARDENED_KEY_START + 1852,          // purpose
41            HARDENED_KEY_START + 1815,          // coin type
42            HARDENED_KEY_START + account_index, // account
43            3,                                  // stake
44            key_index,                          // key index
45        ])
46    }
47
48    pub fn from_str(derivation_path_str: &str) -> Self {
49        let derivation_path_vec: Vec<&str> = derivation_path_str.split('/').collect();
50        let derivation_path_vec_u32: Vec<u32> = derivation_path_vec
51            .iter()
52            .skip(1)
53            .map(|&s| {
54                if s.ends_with("'") {
55                    // Remove the last character (')
56                    let path_str = s.strip_suffix("'").unwrap();
57                    // Parse the string to u32 and add 0x80000000 for hardening
58                    path_str.parse::<u32>().unwrap() + 0x80000000
59                } else {
60                    s.parse::<u32>().unwrap()
61                }
62            })
63            .collect();
64        DerivationIndices(derivation_path_vec_u32)
65    }
66}