Skip to content

Commit

Permalink
Merge #999: Improve genwallet.py
Browse files Browse the repository at this point in the history
53e7bf1 genwallet.py: Add option --recovery-seed-file (Erik Arvstedt)
e4be034 Extract function `read_password_stdin` (Erik Arvstedt)
c5621ad genwallet.py: Enable Fidelity Bonds by default (Erik Arvstedt)
91a8f7f genwallet.py: Minor improvments (Erik Arvstedt)
  • Loading branch information
AdamISZ committed Sep 2, 2021
2 parents 856f404 + 53e7bf1 commit 050b28d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 11 deletions.
7 changes: 5 additions & 2 deletions jmclient/jmclient/wallet_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
"""
Expand Down
36 changes: 27 additions & 9 deletions scripts/genwallet.py
Original file line number Diff line number Diff line change
@@ -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()
Expand Down

0 comments on commit 050b28d

Please sign in to comment.