Skip to content

Commit

Permalink
Add --nosane command-line argument
Browse files Browse the repository at this point in the history
Signed-off-by: Ali Sherief <alihsherief@linuxmail.org>
  • Loading branch information
ZenulAbidin committed Dec 22, 2020
1 parent 7169c72 commit 286b4f6
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 23 deletions.
2 changes: 2 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ to log status messages to a file. The complete list of command-line arguments is
- turn off rich text formatting and progress bars for console output
* - -q, --quiet
- do not display details of test failures, only whether they succeeded or failed
* - --nosane
- Suppress wordlist sanity check. This might cause other tests to fail.
* - -v, --version
- print the version number and exit

Expand Down
11 changes: 6 additions & 5 deletions bip39validator/BIP39WordList.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,7 @@ def __init__(self, desc, string=None, handle=None, url=None):
self.words = contents2list(s)
else:
raise ValueError('`string`, `handle` or `url` must be specified')
if not all([is_all_lower(w) for w in self.words]):
dummy = {'is_sorted': False, 'length': len(self.words),
'has_invalid_chars': True, 'length_exact': len(self.words),
'err_lines': []}
raise InvalidWordList(**dummy)

self._assemble()

def _assemble(self):
Expand Down Expand Up @@ -116,6 +112,11 @@ def test_lowercase(self):
return self._test_lowercase_2(kwargs)

def _test_lowercase_1(self):
if not all([is_all_lower(w) for w in self.words]):
dummy = {'is_sorted': False, 'length': len(self.words),
'has_invalid_chars': True, 'length_exact': len(self.words),
'err_lines': []}
raise InvalidWordList(**dummy)
return validate_sanitized_preamble(self.words)

def _test_lowercase_2(self, kwargs):
Expand Down
48 changes: 31 additions & 17 deletions bip39validator/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,27 @@ def abort(debug):
logerror("Aborting")
exit(1)

def check_validity_warnings(validity):
if not validity.is_sorted:
logwarning('Wordlist is not sorted. It is recommended to sort the wordlist \
before publishing it.')
if not validity.has_2048_words:
logwarning('Wordlist has {} words. Exactly 2048 words are needed to map \
each word to an 11-bit value 1-to-1.'.format(validity.num_words))


def handle_invalid_wordlist(args, e):
if args.nosane:
return

check_validity_warnings(e)
for l in e.err_lines:
logerror("Word \"{}\" (line{}) has a non-lowercase character\
or is blank (Did you remove whitespace and empty lines?)".format(l.word, l.line))
logerror("Valid characters test failed")
logerror("Cannot perform additional tests")
abort(args.debug)

log_file = None
args = None

Expand Down Expand Up @@ -92,6 +113,8 @@ def main():
parser.add_argument('-q', '--quiet', dest='quiet',
help='do not display details of test failures, only whether they \
succeeded or failed', action='store_true')
parser.add_argument('--nosane', dest='nosane', action='store_true',
help='Suppress wordlist sanity check. This might cause other tests to fail.')
parser.add_argument('--debug', dest='debug', action='store_true',
help='turn on debugging mode (intended for developers)')
parser.add_argument('--pycharm-debug', dest='pycharm_debug', action='store_true',
Expand Down Expand Up @@ -140,8 +163,11 @@ def main():
f = open(args.input)
kwargs = {'handle': f}
logdefault("Reading wordlist file {}".format(args.input))
bip39 = BIP39WordList(desc=f"{args.input}", **kwargs)
loginfo("{} words read".format(len(bip39)))
try:
bip39 = BIP39WordList(desc=f"{args.input}", **kwargs)
loginfo("{} words read".format(len(bip39)))
except InvalidWordList as e:
handle_invalid_wordlist(args, e)
if not valid_url:
f.close()
except OSError as e:
Expand All @@ -153,14 +179,6 @@ def main():
total = 4 - int(args.no_lev_dist) - int(args.no_init_uniq)\
- int(args.no_max_length)

def check_validity_warnings(validity):
if not validity.is_sorted:
logwarning('Wordlist is not sorted. It is recommended to sort the wordlist \
before publishing it.')
if not validity.has_2048_words:
logwarning('Wordlist has {} words. Exactly 2048 words are needed to map \
each word to an 11-bit value 1-to-1.'.format(validity.num_words))

logdefault("Checking wordlist for invalid characters")
try:
tup = bip39._test_lowercase_1()
Expand All @@ -172,13 +190,9 @@ def check_validity_warnings(validity):
tally += 1
loginfo("Valid characters test succeeded")
except InvalidWordList as e:
check_validity_warnings(e)
for l in e.err_lines:
logerror("Word \"{}\" (line{}) has a non-lowercase character\
or is blank (Did you remove whitespace and empty lines?)".format(l.word, l.line))
logerror("Valid characters test failed")
logerror("Cannot perform additional test_vectors")
abort(args.debug)
handle_invalid_wordlist(args, e)
if args.nosane:
logwarning("Valid characters test failed, but --nosane passed; ignoring error")

logdefault("Finished checking wordlist for invalid characters")
separator()
Expand Down
2 changes: 1 addition & 1 deletion bip39validator/__version__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "1.0.6"
__version__ = "1.0.7"

0 comments on commit 286b4f6

Please sign in to comment.