Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ impl Mnemonic {
where
R: rand_core::RngCore + rand_core::CryptoRng,
{
if word_count < MIN_NB_WORDS || word_count % 6 != 0 || word_count > MAX_NB_WORDS {
if is_invalid_word_count(word_count) {
return Err(Error::BadWordCount(word_count));
}

Expand Down Expand Up @@ -377,7 +377,7 @@ impl Mnemonic {
/// Parse a mnemonic in normalized UTF8 in the given language.
pub fn parse_in_normalized(language: Language, s: &str) -> Result<Mnemonic, Error> {
let nb_words = s.split_whitespace().count();
if nb_words < MIN_NB_WORDS || nb_words % 6 != 0 || nb_words > MAX_NB_WORDS {
if is_invalid_word_count(nb_words) {
return Err(Error::BadWordCount(nb_words));
}

Expand Down Expand Up @@ -558,6 +558,10 @@ impl str::FromStr for Mnemonic {
}
}

fn is_invalid_word_count(word_count: usize) -> bool {
word_count < MIN_NB_WORDS || word_count % 3 != 0 || word_count > MAX_NB_WORDS
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down Expand Up @@ -605,6 +609,14 @@ mod tests {
let _ = Mnemonic::generate_in_with(&mut rand::thread_rng(), Language::English, 24).unwrap();
}

#[cfg(feature = "rand")]
#[test]
fn test_generate_word_counts() {
for word_count in [12, 15, 18, 21, 24].iter() {
let _ = Mnemonic::generate(*word_count).unwrap();
}
}

#[test]
fn test_vectors_english() {
// These vectors are tuples of
Expand Down