use ::error::{Error, ErrorKind};
#[derive(Debug, Copy, Clone)]
pub enum MnemonicType {
Type12Words,
Type15Words,
Type18Words,
Type21Words,
Type24Words
}
impl MnemonicType {
pub fn for_word_count(size: usize) -> Result<MnemonicType, Error> {
let mnemonic_type = match size {
12 => MnemonicType::Type12Words,
15 => MnemonicType::Type15Words,
18 => MnemonicType::Type18Words,
21 => MnemonicType::Type21Words,
24 => MnemonicType::Type24Words,
_ => { return Err(ErrorKind::InvalidWordLength.into()) }
};
Ok(mnemonic_type)
}
pub fn for_key_size(size: usize) -> Result<MnemonicType, Error> {
let mnemonic_type = match size {
128 => MnemonicType::Type12Words,
160 => MnemonicType::Type15Words,
192 => MnemonicType::Type18Words,
224 => MnemonicType::Type21Words,
256 => MnemonicType::Type24Words,
_ => { return Err(ErrorKind::InvalidKeysize.into()) }
};
Ok(mnemonic_type)
}
pub fn for_phrase<S>(phrase: S) -> Result<MnemonicType, Error> where S: Into<String> {
let m = phrase.into();
let v: Vec<&str> = m.split(" ").into_iter().collect();
let mnemonic_type = match v.len() {
12 => MnemonicType::Type12Words,
15 => MnemonicType::Type15Words,
18 => MnemonicType::Type18Words,
21 => MnemonicType::Type21Words,
24 => MnemonicType::Type24Words,
_ => { return Err(ErrorKind::InvalidWordLength.into()) }
};
Ok(mnemonic_type)
}
pub fn total_bits(&self) -> usize {
let total_bits: usize = match *self {
MnemonicType::Type12Words => 132,
MnemonicType::Type15Words => 165,
MnemonicType::Type18Words => 198,
MnemonicType::Type21Words => 231,
MnemonicType::Type24Words => 264
};
total_bits
}
pub fn entropy_bits(&self) -> usize {
let entropy_bits: usize = match *self {
MnemonicType::Type12Words => 128,
MnemonicType::Type15Words => 160,
MnemonicType::Type18Words => 192,
MnemonicType::Type21Words => 224,
MnemonicType::Type24Words => 256
};
entropy_bits
}
pub fn checksum_bits(&self) -> usize {
let checksum_bits: usize = match *self {
MnemonicType::Type12Words => 4,
MnemonicType::Type15Words => 5,
MnemonicType::Type18Words => 6,
MnemonicType::Type21Words => 7,
MnemonicType::Type24Words => 8
};
checksum_bits
}
pub fn word_count(&self) -> usize {
let word_count: usize = match *self {
MnemonicType::Type12Words => 12,
MnemonicType::Type15Words => 15,
MnemonicType::Type18Words => 18,
MnemonicType::Type21Words => 21,
MnemonicType::Type24Words => 24
};
word_count
}
}