Skip to content

Commit

Permalink
fix: use separator from WordList when decoding (#197)
Browse files Browse the repository at this point in the history
* fix: use separator from `WordList` when decoding

* docs: changelog

* fix: review comments

* Update tests/bip39.rs

Co-authored-by: Thoralf-M <[email protected]>

* fix: change severity to patch

* fix: spell checking

---------

Co-authored-by: Thoralf-M <[email protected]>
  • Loading branch information
qrayven and Thoralf-M authored May 25, 2023
1 parent 4e4d25b commit 57a3251
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changes/bip39-decode-separator.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"iota-crypto": patch
---

Use word separator provided in `WordList` when decoding. Disallow toleration for multiple whitespace when single whitespace is defined as a word separator.
4 changes: 3 additions & 1 deletion src/keys/bip39.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,9 @@ pub mod wordlist {
let mut acc = 0;
let mut i = 0;
let ms = ms.chars().nfkd().collect::<String>();
for ref w in ms.split_whitespace() {
let separator = wordlist.separator.chars().nfkd().collect::<String>();

for ref w in ms.split(&separator) {
match wordlist.words.iter().position(|v| v == w) {
None => return Err(Error::NoSuchWord(w.to_string())),
Some(idx) => {
Expand Down
46 changes: 46 additions & 0 deletions tests/bip39.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,52 @@ fn test_wordlist_codec() {
}
}

#[test]
fn test_mnemonic_phrase_when_separator_is_repeated() {
let test_cases = &[
// U+3000 separator
(" ", true),
// whitespace(U+0020) is also allowed as a separator, because U+3000 is normalized to the whitespace
(" ", true),
("  ", false),
(" ", false),
("  ", false),
("   ", false),
];

for case in test_cases {
let mnemonic_phrase = format!("あいこくしん{}あいこくしん あいこくしん あいこくしん あいこくしん あいこくしん あいこくしん あいこくしん あいこくしん あいこくしん あいこくしん あおぞら", case.0);
assert_eq!(
wordlist::decode(&mnemonic_phrase, &wordlist::JAPANESE).is_ok(),
case.1,
"{}",
mnemonic_phrase
);
assert_eq!(
wordlist::verify(&mnemonic_phrase, &wordlist::JAPANESE).is_ok(),
case.1,
"{}",
mnemonic_phrase
);
}
}

#[test]
fn test_mnemonic_phrase_additional_whitespace() {
// additional whitespace at the beginning
assert!(
wordlist::decode(" sand luggage rack used middle crater deal scare high ring swim fish use then video visa can foot clog base quality all elephant retreat", &wordlist::ENGLISH).is_err(),
);
// additional whitespace in between
assert!(
wordlist::decode("sand luggage rack used middle crater deal scare high ring swim fish use then video visa can foot clog base quality all elephant retreat", &wordlist::ENGLISH).is_err(),
);
// additional whitespace at the end
assert!(
wordlist::decode("sand luggage rack used middle crater deal scare high ring swim fish use then video visa can foot clog base quality all elephant retreat ", &wordlist::ENGLISH).is_err(),
);
}

// #[test]
// fn test_wordlist_codec_different_data_different_encodings() {
// for _ in 0..1000 {
Expand Down

0 comments on commit 57a3251

Please sign in to comment.