Add MAX_NB_WORDS

This commit is contained in:
Steven Roose 2020-09-22 12:34:26 +02:00
parent 75b8e06f6a
commit dc4c9999ea
No known key found for this signature in database
GPG Key ID: 2F2A88D7F8D68E87
1 changed files with 4 additions and 2 deletions

View File

@ -42,6 +42,8 @@ mod pbkdf2;
pub use language::Language; pub use language::Language;
/// The maximum number of words in a mnemonic.
const MAX_NB_WORDS: usize = 24;
/// A BIP39 error. /// A BIP39 error.
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq)]
@ -156,7 +158,7 @@ impl Mnemonic {
/// For the different supported word counts, see documentation on [Mnemonoc]. /// For the different supported word counts, see documentation on [Mnemonoc].
#[cfg(feature = "rand")] #[cfg(feature = "rand")]
pub fn generate_in(language: Language, word_count: usize) -> Result<Mnemonic, Error> { pub fn generate_in(language: Language, word_count: usize) -> Result<Mnemonic, Error> {
if word_count < 6 || word_count % 6 != 0 || word_count > 24 { if word_count < 6 || word_count % 6 != 0 || word_count > MAX_NB_WORDS {
return Err(Error::BadWordCount(word_count)); return Err(Error::BadWordCount(word_count));
} }
@ -177,7 +179,7 @@ impl Mnemonic {
/// Static method to validate a mnemonic in a given language. /// Static method to validate a mnemonic in a given language.
pub fn validate_in(language: Language, s: &str) -> Result<(), Error> { pub fn validate_in(language: Language, s: &str) -> Result<(), Error> {
let words: Vec<&str> = s.split_whitespace().collect(); let words: Vec<&str> = s.split_whitespace().collect();
if words.len() < 6 || words.len() % 6 != 0 || words.len() > 24 { if words.len() < 6 || words.len() % 6 != 0 || words.len() > MAX_NB_WORDS {
return Err(Error::BadWordCount(words.len())); return Err(Error::BadWordCount(words.len()));
} }