diff --git a/jmclient/jmclient/wallet_utils.py b/jmclient/jmclient/wallet_utils.py index d175c76..649541f 100644 --- a/jmclient/jmclient/wallet_utils.py +++ b/jmclient/jmclient/wallet_utils.py @@ -1368,8 +1368,7 @@ def open_test_wallet_maybe(path, seed, max_mixdepth, return test_wallet_cls(storage, **kwargs) if wallet_password_stdin is True: - stdin = sys.stdin.read() - password = stdin.encode('utf-8') + password = read_password_stdin() return open_wallet(path, ask_for_password=False, password=password, 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) +def read_password_stdin(): + return sys.stdin.read().encode('utf-8') + + def wallet_tool_main(wallet_root_path): """Main wallet tool script function; returned is a string (output or error) """ diff --git a/scripts/genwallet.py b/scripts/genwallet.py index 369b88f..ddffaf0 100755 --- a/scripts/genwallet.py +++ b/scripts/genwallet.py @@ -1,33 +1,51 @@ #!/usr/bin/env python3 -import sys +# A script for noninteractively creating wallets. +# The implementation is similar to wallet_generate_recover_bip39 in jmclient/wallet_utils.py + import os from optparse import OptionParser -from jmclient import load_program_config, add_base_options, SegwitWallet, SegwitLegacyWallet, create_wallet, jm_single +from pathlib import Path +from jmclient import ( + load_program_config, add_base_options, SegwitWalletFidelityBonds, SegwitLegacyWallet, + create_wallet, jm_single, wallet_utils +) from jmbase.support import get_log, jmprint log = get_log() def main(): parser = OptionParser( - usage='usage: %prog [options] wallet_file_name password', - description='Create a wallet with the given wallet name and password.') + usage='usage: %prog [options] wallet_file_name [password]', + description='Create a wallet with the given wallet name and password.' + ) add_base_options(parser) + parser.add_option( + '--recovery-seed-file', + dest='seed_file', + default=None, + help=('File containing a mnemonic recovery phrase. If provided, the wallet ' + 'is recovered from this seed instead of being newly generated.') + ) (options, args) = parser.parse_args() + wallet_name = args[0] if options.wallet_password_stdin: - stdin = sys.stdin.read() - password = stdin.encode("utf-8") + password = wallet_utils.read_password_stdin() else: assert len(args) > 1, "must provide password via stdin (see --help), or as second argument." password = args[1].encode("utf-8") + seed = options.seed_file and Path(options.seed_file).read_text().rstrip() + load_program_config(config_path=options.datadir) wallet_root_path = os.path.join(jm_single().datadir, "wallets") - wallet_name = os.path.join(wallet_root_path, args[0]) + wallet_path = os.path.join(wallet_root_path, wallet_name) if jm_single().config.get("POLICY", "native") == "true": - walletclass = SegwitWallet + walletclass = SegwitWalletFidelityBonds else: + # Fidelity Bonds are not available for segwit legacy wallets walletclass = SegwitLegacyWallet - wallet = create_wallet(wallet_name, password, 4, walletclass) + entropy = seed and SegwitLegacyWallet.entropy_from_mnemonic(seed) + wallet = create_wallet(wallet_path, password, wallet_utils.DEFAULT_MIXDEPTH, walletclass, entropy=entropy) jmprint("recovery_seed:{}" .format(wallet.get_mnemonic_words()[0]), "important") wallet.close()