Skip to content

Commit

Permalink
bug: Could not detect languages where wordlists are mixed across >1 l…
Browse files Browse the repository at this point in the history
…angs
  • Loading branch information
fubuloubu committed Feb 15, 2020
1 parent ad88ed7 commit bad39f5
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions eth_account/hdaccount/mnemonic.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,24 @@ def list_languages(_Mnemonic):
@classmethod
def detect_language(cls, raw_mnemonic):
mnemonic = normalize_string(raw_mnemonic)
first = mnemonic.split(" ")[0]
words = mnemonic.split(" ")
languages = cls.list_languages()

language_counts = {lang: 0 for lang in languages}
for lang in languages:
if first in cls(lang).wordlist:
return lang

raise ValidationError("Language not detected")
wordlist = cls(lang).wordlist
for word in words:
if word in wordlist:
language_counts[lang] += 1

# No language had all words match it, so the language can't be fully determined
if max(language_counts.values()) < len(words):
raise ValidationError("Language not detected")

# Because certain wordlists share some similar words, we detect the language
# by seeing which of the languages have the most hits, and returning that one
most_matched_language = max(language_counts.items(), key=lambda c: c[1])[0]
return most_matched_language

def generate(self, num_words=12):
if num_words not in VALID_WORD_LENGTHS:
Expand Down

0 comments on commit bad39f5

Please sign in to comment.