Browse Source

Extract function `read_password_stdin`

This ensures that `wallet_utils.py` and `genwallet.py` use the same logic for
reading the password.

The current algorithm for reading the password entails some
non-obvious details like utf-8 encoding and not stripping trailing
newlines.
master
Erik Arvstedt 4 years ago
parent
commit
e4be0343eb
  1. 7
      jmclient/jmclient/wallet_utils.py
  2. 3
      scripts/genwallet.py

7
jmclient/jmclient/wallet_utils.py

@ -1368,8 +1368,7 @@ def open_test_wallet_maybe(path, seed, max_mixdepth,
return test_wallet_cls(storage, **kwargs) return test_wallet_cls(storage, **kwargs)
if wallet_password_stdin is True: if wallet_password_stdin is True:
stdin = sys.stdin.read() password = read_password_stdin()
password = stdin.encode('utf-8')
return open_wallet(path, ask_for_password=False, password=password, mixdepth=max_mixdepth, **kwargs) return open_wallet(path, ask_for_password=False, password=password, mixdepth=max_mixdepth, **kwargs)
return open_wallet(path, mixdepth=max_mixdepth, **kwargs) return open_wallet(path, mixdepth=max_mixdepth, **kwargs)
@ -1444,6 +1443,10 @@ def get_wallet_path(file_name, wallet_dir=None):
return os.path.join(wallet_dir, file_name) return os.path.join(wallet_dir, file_name)
def read_password_stdin():
return sys.stdin.read().encode('utf-8')
def wallet_tool_main(wallet_root_path): def wallet_tool_main(wallet_root_path):
"""Main wallet tool script function; returned is a string (output or error) """Main wallet tool script function; returned is a string (output or error)
""" """

3
scripts/genwallet.py

@ -3,7 +3,6 @@
# A script for noninteractively creating wallets. # A script for noninteractively creating wallets.
# The implementation is similar to wallet_generate_recover_bip39 in jmclient/wallet_utils.py # The implementation is similar to wallet_generate_recover_bip39 in jmclient/wallet_utils.py
import sys
import os import os
from optparse import OptionParser from optparse import OptionParser
from jmclient import ( from jmclient import (
@ -23,7 +22,7 @@ def main():
(options, args) = parser.parse_args() (options, args) = parser.parse_args()
wallet_name = args[0] wallet_name = args[0]
if options.wallet_password_stdin: if options.wallet_password_stdin:
password = sys.stdin.read().encode("utf-8") password = wallet_utils.read_password_stdin()
else: else:
assert len(args) > 1, "must provide password via stdin (see --help), or as second argument." assert len(args) > 1, "must provide password via stdin (see --help), or as second argument."
password = args[1].encode("utf-8") password = args[1].encode("utf-8")

Loading…
Cancel
Save