Merge pull request #18 from tcharding/word-counts

Allow word count multiples of 3 instead of 6
This commit is contained in:
Steven Roose 2022-05-13 13:44:25 +01:00 committed by GitHub
commit aeccaf661a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 14 additions and 2 deletions

View File

@ -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));
}
@ -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));
}
@ -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::*;
@ -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