@@ -67,7 +67,6 @@ def b58encode(v: bytes) -> str:
67
67
68
68
class Mnemonic (object ):
69
69
def __init__ (self , language : str = "english" ):
70
- self .language = language
71
70
self .radix = 2048
72
71
d = os .path .join (os .path .dirname (__file__ ), f"wordlist/{ language } .txt" )
73
72
if os .path .exists (d ) and os .path .isfile (d ):
@@ -79,6 +78,10 @@ def __init__(self, language: str = "english"):
79
78
)
80
79
else :
81
80
raise ConfigurationError ("Language not detected" )
81
+ # We can use binary search for English language
82
+ self .use_binary_search = language == "english"
83
+ # Japanese must be joined by ideographic space
84
+ self .delimiter = "\u3000 " if language == "japanese" else " "
82
85
83
86
@classmethod
84
87
def list_languages (cls ) -> List [str ]:
@@ -150,15 +153,11 @@ def to_entropy(self, words: Union[List[str], str]) -> bytearray:
150
153
concatLenBits = len (words ) * 11
151
154
concatBits = [False ] * concatLenBits
152
155
wordindex = 0
153
- if self .language == "english" :
154
- use_binary_search = True
155
- else :
156
- use_binary_search = False
157
156
for word in words :
158
157
# Find the words index in the wordlist
159
158
ndx = (
160
159
binary_search (self .wordlist , word )
161
- if use_binary_search
160
+ if self . use_binary_search
162
161
else self .wordlist .index (word )
163
162
)
164
163
if ndx < 0 :
@@ -202,11 +201,7 @@ def to_mnemonic(self, data: bytes) -> str:
202
201
for i in range (len (b ) // 11 ):
203
202
idx = int (b [i * 11 : (i + 1 ) * 11 ], 2 )
204
203
result .append (self .wordlist [idx ])
205
- if self .language == "japanese" : # Japanese must be joined by ideographic space.
206
- result_phrase = "\u3000 " .join (result )
207
- else :
208
- result_phrase = " " .join (result )
209
- return result_phrase
204
+ return self .delimiter .join (result )
210
205
211
206
def check (self , mnemonic : str ) -> bool :
212
207
mnemonic_list = self .normalize_string (mnemonic ).split (" " )
0 commit comments