fix: cache wordlists

This commit is contained in:
Manuel Barkhau 2019-11-14 18:57:18 +00:00
parent b3f913340c
commit af86c7e3fd

View file

@ -89,20 +89,29 @@ def normalize_text(seed: str) -> str:
seed = u''.join([seed[i] for i in range(len(seed)) if not (seed[i] in string.whitespace and is_CJK(seed[i-1]) and is_CJK(seed[i+1]))]) seed = u''.join([seed[i] for i in range(len(seed)) if not (seed[i] in string.whitespace and is_CJK(seed[i-1]) and is_CJK(seed[i+1]))])
return seed return seed
_WORDLIST_CACHE = {}
def load_wordlist(filename): def load_wordlist(filename):
path = resource_path('wordlist', filename) path = resource_path('wordlist', filename)
with open(path, 'r', encoding='utf-8') as f: if path not in _WORDLIST_CACHE:
s = f.read().strip() with open(path, 'r', encoding='utf-8') as f:
s = unicodedata.normalize('NFKD', s) s = f.read().strip()
lines = s.split('\n') s = unicodedata.normalize('NFKD', s)
wordlist = [] lines = s.split('\n')
for line in lines: wordlist = []
line = line.split('#')[0] for line in lines:
line = line.strip(' \r') line = line.split('#')[0]
assert ' ' not in line line = line.strip(' \r')
if line: assert ' ' not in line
wordlist.append(line) if line:
return wordlist wordlist.append(line)
# wordlists shouldn't be mutated, but just in case,
# convert it to a tuple
_WORDLIST_CACHE[path] = tuple(wordlist)
return _WORDLIST_CACHE[path]
filenames = { filenames = {
@ -114,8 +123,6 @@ filenames = {
} }
# FIXME every time we instantiate this class, we read the wordlist from disk
# and store a new copy of it in memory
class Mnemonic(Logger): class Mnemonic(Logger):
# Seed derivation does not follow BIP39 # Seed derivation does not follow BIP39
# Mnemonic phrase uses a hash based checksum, instead of a wordlist-dependent checksum # Mnemonic phrase uses a hash based checksum, instead of a wordlist-dependent checksum