Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add word index to invalid word error #48

Merged
merged 1 commit into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading