pub enum Error {
InvalidEntropyLength {
length: usize,
},
InvalidMnemonic {
reason: String,
},
InvalidWordCount {
count: usize,
},
InvalidWord {
word: String,
position: usize,
},
InvalidChecksum,
RandomGeneration,
Bip39Error {
message: String,
},
}Expand description
Comprehensive error types for BIP39 mnemonic operations.
This enum represents all possible errors that can occur when working with BIP39 mnemonics, from entropy generation to seed derivation.
§Error Categories
- Input Validation:
InvalidEntropyLength,InvalidWordCount,InvalidMnemonic - Mnemonic Validation:
InvalidWord,InvalidChecksum - External Dependencies:
RandomGeneration,Bip39Error
Variants§
InvalidEntropyLength
The provided entropy has an invalid length.
BIP39 requires entropy to be one of the following lengths:
- 16 bytes (128 bits) → 12 words
- 20 bytes (160 bits) → 15 words
- 24 bytes (192 bits) → 18 words
- 28 bytes (224 bits) → 21 words
- 32 bytes (256 bits) → 24 words
§Example
let error = Error::InvalidEntropyLength { length: 10 };
println!("{}", error); // "Invalid entropy length: 10 bytes. Valid lengths are..."InvalidMnemonic
The provided mnemonic phrase is invalid.
This error occurs when a mnemonic phrase fails general validation, such as having the wrong format, length, or structure.
§Example
let error = Error::InvalidMnemonic {
reason: "Too few words".to_string()
};InvalidWordCount
The provided word count is not supported.
BIP39 supports only specific word counts that correspond to
valid entropy lengths. See InvalidEntropyLength for the mapping.
InvalidWord
A word in the mnemonic phrase is not in the BIP39 word list.
Each word in a BIP39 mnemonic must be from the official 2048-word list. This error provides both the invalid word and its position for debugging.
§Example
let error = Error::InvalidWord {
word: "invalidword".to_string(),
position: 5,
};
println!("{}", error); // "Invalid word 'invalidword' at position 5"Fields
InvalidChecksum
The mnemonic phrase has an invalid checksum.
BIP39 mnemonics include a checksum derived from the entropy. This error occurs when the checksum verification fails, indicating the mnemonic may be corrupted or incorrectly generated.
RandomGeneration
Error occurred during random number generation.
This error is automatically converted from rand::Error and
indicates a failure in the random number generator used for
entropy generation.
Bip39Error
Error from the underlying BIP39 crate.
This error wraps errors from the external bip39 crate that
we use for some low-level operations.
Trait Implementations§
Source§impl Error for Error
impl Error for Error
1.30.0 · Source§fn source(&self) -> Option<&(dyn Error + 'static)>
fn source(&self) -> Option<&(dyn Error + 'static)>
1.0.0 · Source§fn description(&self) -> &str
fn description(&self) -> &str
Source§impl PartialEq for Error
Custom equality implementation for Error.
impl PartialEq for Error
Custom equality implementation for Error.
This implementation allows comparing errors for equality, which is useful
in tests and error matching. For errors containing external types
(like rand::Error or bip39_upstream::Error), we compare only
by error type since the wrapped errors may not implement PartialEq.
§Examples
let error1 = Error::InvalidWordCount { count: 12 };
let error2 = Error::InvalidWordCount { count: 12 };
let error3 = Error::InvalidWordCount { count: 15 };
assert_eq!(error1, error2); // Same variant and data
assert_ne!(error1, error3); // Same variant, different dataimpl Eq for Error
Marker trait indicating that Error can be compared for equality.
This is automatically implemented since we provide PartialEq.