Skip to content

Commit

Permalink
Add word index to invalid word error (#48)
Browse files Browse the repository at this point in the history
Invalid word error contains an index of the word in the user mnemonic.
  • Loading branch information
karbyshev authored Apr 15, 2024
1 parent 7076c6f commit 743d537
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use thiserror::Error;
pub enum ErrorKind {
#[error("invalid checksum")]
InvalidChecksum,
#[error("invalid word in phrase")]
InvalidWord,
#[error("invalid word in phrase with index {0}")]
InvalidWord(usize),
#[error("invalid keysize: {0}")]
InvalidKeysize(usize),
#[error("invalid number of words in phrase: {0}")]
Expand Down
8 changes: 2 additions & 6 deletions src/language.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::error::ErrorKind;
use crate::util::{Bits, Bits11};
use rustc_hash::FxHashMap;

Expand All @@ -11,11 +10,8 @@ pub struct WordList {
}

impl WordMap {
pub fn get_bits(&self, word: &str) -> Result<Bits11, ErrorKind> {
match self.inner.get(word) {
Some(n) => Ok(*n),
None => Err(ErrorKind::InvalidWord)?,
}
pub fn get_bits(&self, word: &str) -> Option<Bits11> {
self.inner.get(word).cloned()
}
}

Expand Down
5 changes: 3 additions & 2 deletions src/mnemonic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,9 @@ impl Mnemonic {
// Preallocate enough space for the longest possible word list
let mut bits = BitWriter::with_capacity(264);

for word in phrase.split(" ") {
bits.push(wordmap.get_bits(&word)?);
for (idx, word) in phrase.split(' ').enumerate() {
let word_bits = wordmap.get_bits(word).ok_or(ErrorKind::InvalidWord(idx))?;
bits.push(word_bits);
}

let mtype = MnemonicType::for_word_count(bits.len() / 11)?;
Expand Down

0 comments on commit 743d537

Please sign in to comment.