Skip to content

Commit c12ac39

Browse files
Test with incorrect entropy size
Make sure `from_entropy_in` produces error if length of entropy bits is less than 128, greater than 256 and not a multipe of 32.
1 parent 5d56110 commit c12ac39

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

src/keys/bip39/mod.rs

+24-2
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ const MIN_WORDS: usize = 12;
4747
const MAX_WORDS: usize = 24;
4848

4949
/// A BIP39 error
50-
#[derive(Debug)]
50+
#[derive(Debug, PartialEq)]
5151
pub enum Error {
5252
/// Mnemonic has a word count that is not a multiple of 6 and between 12 and 24
5353
InvalidWordCount(usize),
@@ -87,6 +87,7 @@ pub enum WordCount {
8787
}
8888

8989
/// A mnemonic is group of easy to remember words used in the generation of deterministic wallets
90+
#[derive(Debug, PartialEq)]
9091
pub struct Mnemonic {
9192
/// The language this mnemoic belongs to
9293
language: Language,
@@ -370,7 +371,7 @@ impl<Ctx: ScriptContext> GeneratableKey<Ctx> for Mnemonic {
370371

371372
#[cfg(test)]
372373
mod test {
373-
use super::{Language, Mnemonic};
374+
use super::{Error, Language, Mnemonic};
374375
use crate::keys::{any_network, bip39::WordCount, GeneratableKey, GeneratedKey};
375376
use bitcoin::{hashes::hex::FromHex, util::bip32};
376377
use std::str::FromStr;
@@ -821,4 +822,25 @@ mod test {
821822
Mnemonic::generate((Some(WordCount::Words24), Language::English)).unwrap();
822823
assert_eq!(generated_mnemonic.valid_networks, any_network());
823824
}
825+
826+
#[test]
827+
fn test_invalid_entropies() {
828+
//testing with entropy less than 128 bits
829+
assert_eq!(
830+
Mnemonic::from_entropy_in(Language::English, &vec![b'0'; 15]),
831+
Err(Error::InvalidEntropyLength(120))
832+
);
833+
834+
//testing with entropy greater than 256 bits
835+
assert_eq!(
836+
Mnemonic::from_entropy_in(Language::English, &vec![b'0'; 33]),
837+
Err(Error::InvalidEntropyLength(264))
838+
);
839+
840+
//testing entropy which is not a multiple of 32 bits
841+
assert_eq!(
842+
Mnemonic::from_entropy_in(Language::English, &vec![b'0'; 31]),
843+
Err(Error::InvalidEntropyLength(248))
844+
);
845+
}
824846
}

src/keys/bip39/wordlists/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ mod portuguese;
2020
mod spanish;
2121

2222
/// List of supported languages for mnemonics
23+
#[derive(Debug, PartialEq)]
2324
pub enum Language {
2425
/// The English language
2526
English,

0 commit comments

Comments
 (0)