Skip to content

Commit

Permalink
Merge pull request #18 from tcharding/word-counts
Browse files Browse the repository at this point in the history
Allow word count multiples of 3 instead of 6
  • Loading branch information
stevenroose authored May 13, 2022
2 parents 520e15a + af958a2 commit aeccaf6
Showing 1 changed file with 14 additions and 2 deletions.
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

0 comments on commit aeccaf6

Please sign in to comment.