@@ -66,28 +66,25 @@ def b58encode(v: bytes) -> str:
66
66
67
67
68
68
class Mnemonic (object ):
69
- def __init__ (self , language : str ):
69
+ def __init__ (self , language : str = "english" ):
70
70
self .language = language
71
71
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" )
85
82
86
83
@classmethod
87
84
def list_languages (cls ) -> List [str ]:
88
85
return [
89
86
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" ))
91
88
if f .endswith (".txt" )
92
89
]
93
90
@@ -124,7 +121,7 @@ def generate(self, strength: int = 128) -> str:
124
121
125
122
If not provided, the default entropy length will be set to 128 bits.
126
123
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 .
128
125
129
126
:param strength: Number of bytes used as entropy
130
127
:type strength: int
@@ -192,8 +189,7 @@ def to_entropy(self, words: Union[List[str], str]) -> bytearray:
192
189
def to_mnemonic (self , data : bytes ) -> str :
193
190
if len (data ) not in [16 , 20 , 24 , 28 , 32 ]:
194
191
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 )} ."
197
193
)
198
194
h = hashlib .sha256 (data ).hexdigest ()
199
195
b = (
@@ -205,7 +201,7 @@ def to_mnemonic(self, data: bytes) -> str:
205
201
idx = int (b [i * 11 : (i + 1 ) * 11 ], 2 )
206
202
result .append (self .wordlist [idx ])
207
203
if self .language == "japanese" : # Japanese must be joined by ideographic space.
208
- result_phrase = u "\u3000 " .join (result )
204
+ result_phrase = "\u3000 " .join (result )
209
205
else :
210
206
result_phrase = " " .join (result )
211
207
return result_phrase
0 commit comments