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
use crate::{KeyMaterial, Path, KEY_SIZE};
use hmac::{Hmac, Mac};
use sha2::Sha512;
use zeroize::Zeroize;
const BIP39_BASE_DERIVATION_KEY: [u8; 12] = [
0x42, 0x69, 0x74, 0x63, 0x6f, 0x69, 0x6e, 0x20, 0x73, 0x65, 0x65, 0x64,
];
pub const SEED_SIZE: usize = 64;
pub struct Seed(pub(crate) [u8; SEED_SIZE]);
impl Seed {
pub fn as_bytes(&self) -> &[u8] {
&self.0
}
pub fn derive_subkey(self, path: impl AsRef<Path>) -> KeyMaterial {
let mut hmac = Hmac::<Sha512>::new_varkey(&BIP39_BASE_DERIVATION_KEY).unwrap();
hmac.input(&self.0);
let root_key = KeyMaterial::from_bytes(&hmac.result().code()[KEY_SIZE..]).unwrap();
root_key.derive_subkey(path)
}
}
impl Drop for Seed {
fn drop(&mut self) {
self.0.zeroize();
}
}