Skip to content

Commit a8d28dd

Browse files
willianpaixaomatejcik
authored andcommitted
Set English as default language
1 parent 2e2b141 commit a8d28dd

File tree

1 file changed

+15
-19
lines changed

1 file changed

+15
-19
lines changed

src/mnemonic/mnemonic.py

+15-19
Original file line numberDiff line numberDiff line change
@@ -66,28 +66,25 @@ def b58encode(v: bytes) -> str:
6666

6767

6868
class Mnemonic(object):
69-
def __init__(self, language: str):
69+
def __init__(self, language: str = "english"):
7070
self.language = language
7171
self.radix = 2048
72-
with open(
73-
"%s/%s.txt" % (self._get_directory(), language), "r", encoding="utf-8"
74-
) as f:
75-
self.wordlist = [w.strip() for w in f.readlines()]
76-
if len(self.wordlist) != self.radix:
77-
raise ConfigurationError(
78-
"Wordlist should contain %d words, but it contains %d words."
79-
% (self.radix, len(self.wordlist))
80-
)
81-
82-
@staticmethod
83-
def _get_directory() -> str:
84-
return os.path.join(os.path.dirname(__file__), "wordlist")
72+
d = os.path.join(os.path.dirname(__file__), f"wordlist/{language}.txt")
73+
if os.path.exists(d) and os.path.isfile(d):
74+
with open(d, "r", encoding="utf-8") as f:
75+
self.wordlist = [w.strip() for w in f.readlines()]
76+
if len(self.wordlist) != self.radix:
77+
raise ConfigurationError(
78+
f"Wordlist should contain {self.radix} words, but it's {len(self.wordlist)} words long instead."
79+
)
80+
else:
81+
raise ConfigurationError("Language not detected")
8582

8683
@classmethod
8784
def list_languages(cls) -> List[str]:
8885
return [
8986
f.split(".")[0]
90-
for f in os.listdir(cls._get_directory())
87+
for f in os.listdir(os.path.join(os.path.dirname(__file__), "wordlist"))
9188
if f.endswith(".txt")
9289
]
9390

@@ -124,7 +121,7 @@ def generate(self, strength: int = 128) -> str:
124121
125122
If not provided, the default entropy length will be set to 128 bits.
126123
127-
The return is a list of words that encodes the entropy generated.
124+
The return is a list of words that encodes the generated entropy.
128125
129126
:param strength: Number of bytes used as entropy
130127
:type strength: int
@@ -192,8 +189,7 @@ def to_entropy(self, words: Union[List[str], str]) -> bytearray:
192189
def to_mnemonic(self, data: bytes) -> str:
193190
if len(data) not in [16, 20, 24, 28, 32]:
194191
raise ValueError(
195-
"Data length should be one of the following: [16, 20, 24, 28, 32], but it is not (%d)."
196-
% len(data)
192+
f"Data length should be one of the following: [16, 20, 24, 28, 32], but it is not {len(data)}."
197193
)
198194
h = hashlib.sha256(data).hexdigest()
199195
b = (
@@ -205,7 +201,7 @@ def to_mnemonic(self, data: bytes) -> str:
205201
idx = int(b[i * 11 : (i + 1) * 11], 2)
206202
result.append(self.wordlist[idx])
207203
if self.language == "japanese": # Japanese must be joined by ideographic space.
208-
result_phrase = u"\u3000".join(result)
204+
result_phrase = "\u3000".join(result)
209205
else:
210206
result_phrase = " ".join(result)
211207
return result_phrase

0 commit comments

Comments
 (0)