Skip to content

Commit 70eb351

Browse files
prusnakmatejcik
authored andcommitted
Refactor delimiter and use_binary_search
1 parent e81a50c commit 70eb351

File tree

1 file changed

+6
-11
lines changed

1 file changed

+6
-11
lines changed

src/mnemonic/mnemonic.py

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ def b58encode(v: bytes) -> str:
6767

6868
class Mnemonic(object):
6969
def __init__(self, language: str = "english"):
70-
self.language = language
7170
self.radix = 2048
7271
d = os.path.join(os.path.dirname(__file__), f"wordlist/{language}.txt")
7372
if os.path.exists(d) and os.path.isfile(d):
@@ -79,6 +78,10 @@ def __init__(self, language: str = "english"):
7978
)
8079
else:
8180
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 " "
8285

8386
@classmethod
8487
def list_languages(cls) -> List[str]:
@@ -150,15 +153,11 @@ def to_entropy(self, words: Union[List[str], str]) -> bytearray:
150153
concatLenBits = len(words) * 11
151154
concatBits = [False] * concatLenBits
152155
wordindex = 0
153-
if self.language == "english":
154-
use_binary_search = True
155-
else:
156-
use_binary_search = False
157156
for word in words:
158157
# Find the words index in the wordlist
159158
ndx = (
160159
binary_search(self.wordlist, word)
161-
if use_binary_search
160+
if self.use_binary_search
162161
else self.wordlist.index(word)
163162
)
164163
if ndx < 0:
@@ -202,11 +201,7 @@ def to_mnemonic(self, data: bytes) -> str:
202201
for i in range(len(b) // 11):
203202
idx = int(b[i * 11 : (i + 1) * 11], 2)
204203
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)
210205

211206
def check(self, mnemonic: str) -> bool:
212207
mnemonic_list = self.normalize_string(mnemonic).split(" ")

0 commit comments

Comments
 (0)