Skip to content

Commit

Permalink
Add an explicit error message (Chia-Network#4758)
Browse files Browse the repository at this point in the history
* Add an explicit error message when mnemonic words are not in the dictionary; should help users self-service issues like Chia-Network#3425 faster.

* fix lint

* fix lint x2
  • Loading branch information
elliottback authored May 22, 2021
1 parent 9e9097b commit 6f06f04
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
2 changes: 2 additions & 0 deletions chia/util/keychain.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ def bytes_from_mnemonic(mnemonic_str: str) -> bytes:
bit_array = BitArray()
for i in range(0, len(mnemonic)):
word = mnemonic[i]
if word not in word_list:
raise ValueError(f"'{word}' is not in the mnemonic dictionary; may be misspelled")
value = word_list[word]
bit_array.append(BitArray(uint=value, length=11))

Expand Down
10 changes: 10 additions & 0 deletions tests/core/util/test_keychain.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ def test_basic_add_delete(self):
assert bytes_to_mnemonic(entropy) == mnemonic
mnemonic_2 = generate_mnemonic()

# misspelled words in the mnemonic
bad_mnemonic = mnemonic.split(" ")
bad_mnemonic[6] = "ZZZZZZ"
self.assertRaisesRegex(
ValueError,
"'ZZZZZZ' is not in the mnemonic dictionary; may be misspelled",
bytes_from_mnemonic,
" ".join(bad_mnemonic),
)

kc.add_private_key(mnemonic, "")
assert kc._get_free_private_key_index() == 1
assert len(kc.get_all_private_keys()) == 1
Expand Down

0 comments on commit 6f06f04

Please sign in to comment.