keyfork-mnemonic-util: Mnemonic::seed(&self) -> Vec<u8>
This commit is contained in:
parent
98baaed81c
commit
da09b95bae
|
@ -197,6 +197,26 @@ impl Mnemonic {
|
||||||
Ok(Mnemonic { words, wordlist })
|
Ok(Mnemonic { words, wordlist })
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[must_use]
|
||||||
|
pub fn seed(&self) -> Vec<u8> {
|
||||||
|
let mut bits = vec![false; self.words.len() * 11];
|
||||||
|
for (index, word) in self.words.iter().enumerate() {
|
||||||
|
for bit in 0..11 {
|
||||||
|
bits[index * 11 + bit] = (word & (1 << (10 - bit))) > 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
bits.truncate(bits.len() - bits.len() % 32);
|
||||||
|
bits.chunks_exact(8)
|
||||||
|
.map(|chunk| {
|
||||||
|
let mut num = 0u8;
|
||||||
|
for i in 0..8 {
|
||||||
|
num += u8::from(chunk[7 - i]) << i;
|
||||||
|
}
|
||||||
|
num
|
||||||
|
})
|
||||||
|
.collect::<Vec<_>>()
|
||||||
|
}
|
||||||
|
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub fn into_inner(self) -> (Vec<usize>, Arc<Wordlist>) {
|
pub fn into_inner(self) -> (Vec<usize>, Arc<Wordlist>) {
|
||||||
(self.words, self.wordlist)
|
(self.words, self.wordlist)
|
||||||
|
@ -219,6 +239,17 @@ mod tests {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn reproduces_its_own_seed() {
|
||||||
|
let mut random_handle = File::open("/dev/random").unwrap();
|
||||||
|
let entropy = &mut [0u8; 256 / 8];
|
||||||
|
random_handle.read_exact(&mut entropy[..]).unwrap();
|
||||||
|
let wordlist = Wordlist::default().arc();
|
||||||
|
let mnemonic = super::Mnemonic::from_entropy(&entropy[..256 / 8], wordlist).unwrap();
|
||||||
|
let seed = mnemonic.seed();
|
||||||
|
assert_eq!(&seed, entropy);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn conforms_to_trezor_tests() {
|
fn conforms_to_trezor_tests() {
|
||||||
let content = include_str!("data/vectors.json");
|
let content = include_str!("data/vectors.json");
|
||||||
|
|
Loading…
Reference in New Issue