add main module changes from electrum 3.1.3

This commit is contained in:
zebra-lucky 2018-06-15 01:44:50 +03:00
parent fb23570b0e
commit 66fc1e57cf
1 changed files with 75 additions and 31 deletions

View File

@ -26,21 +26,6 @@
import os
import sys
# from https://gist.github.com/tito/09c42fb4767721dc323d
import threading
try:
import jnius
except:
jnius = None
if jnius:
orig_thread_run = threading.Thread.run
def thread_check_run(*args, **kwargs):
try:
return orig_thread_run(*args, **kwargs)
finally:
jnius.detach()
threading.Thread.run = thread_check_run
script_dir = os.path.dirname(os.path.realpath(__file__))
is_bundle = getattr(sys, 'frozen', False)
is_local = not is_bundle and os.path.exists(os.path.join(script_dir, "electrum-zcash.desktop"))
@ -87,12 +72,14 @@ if is_local or is_android:
imp.load_module('electrum_zcash_plugins', *imp.find_module('plugins'))
from electrum_zcash import bitcoin
from electrum_zcash import bitcoin, util
from electrum_zcash import constants
from electrum_zcash import SimpleConfig, Network
from electrum_zcash.wallet import Wallet, Imported_Wallet
from electrum_zcash.storage import WalletStorage
from electrum_zcash.util import print_msg, print_stderr, json_encode, json_decode
from electrum_zcash.util import set_verbosity, InvalidPassword, check_www_dir
from electrum_zcash.storage import WalletStorage, get_derivation_used_for_hw_device_encryption
from electrum_zcash.util import print_msg, print_stderr, json_encode, json_decode, UserCancelled
from electrum_zcash.util import set_verbosity, InvalidPassword
from electrum_zcash.commands import get_parser, known_commands, Commands, config_variables
from electrum_zcash import daemon
from electrum_zcash import keystore
@ -160,6 +147,8 @@ def run_non_RPC(config):
print_msg("Recovering wallet...")
wallet.synchronize()
wallet.wait_until_synchronized()
wallet.stop_threads()
# note: we don't wait for SPV
msg = "Recovery successful" if wallet.is_found() else "Found no history for this wallet"
else:
msg = "This wallet was restored offline. It may contain more addresses than displayed."
@ -192,7 +181,10 @@ def init_daemon(config_options):
print_msg("Type 'electrum-zcash create' to create a new wallet, or provide a path to a wallet with the -w option")
sys.exit(0)
if storage.is_encrypted():
if config.get('password'):
if storage.is_encrypted_with_hw_device():
plugins = init_plugins(config, 'cmdline')
password = get_password_for_hw_device_encrypted_storage(plugins)
elif config.get('password'):
password = config.get('password')
else:
password = prompt_password('Password:', False)
@ -219,7 +211,7 @@ def init_cmdline(config_options, server):
if cmdname in ['payto', 'paytomany'] and config.get('broadcast'):
cmd.requires_network = True
# instanciate wallet for command-line
# instantiate wallet for command-line
storage = WalletStorage(config.get_wallet_path())
if cmd.requires_wallet and not storage.file_exists():
@ -236,7 +228,10 @@ def init_cmdline(config_options, server):
# commands needing password
if (cmd.requires_wallet and storage.is_encrypted() and server is None)\
or (cmd.requires_password and (storage.get('use_encryption') or storage.is_encrypted())):
if config.get('password'):
if storage.is_encrypted_with_hw_device():
# this case is handled later in the control flow
password = None
elif config.get('password'):
password = config.get('password')
else:
password = prompt_password('Password:', False)
@ -255,19 +250,60 @@ def init_cmdline(config_options, server):
return cmd, password
def run_offline_command(config, config_options):
def get_connected_hw_devices(plugins):
support = plugins.get_hardware_support()
if not support:
print_msg('No hardware wallet support found on your system.')
sys.exit(1)
# scan devices
devices = []
devmgr = plugins.device_manager
for name, description, plugin in support:
try:
u = devmgr.unpaired_device_infos(None, plugin)
except:
devmgr.print_error("error", name)
continue
devices += list(map(lambda x: (name, x), u))
return devices
def get_password_for_hw_device_encrypted_storage(plugins):
devices = get_connected_hw_devices(plugins)
if len(devices) == 0:
print_msg("Error: No connected hw device found. Cannot decrypt this wallet.")
sys.exit(1)
elif len(devices) > 1:
print_msg("Warning: multiple hardware devices detected. "
"The first one will be used to decrypt the wallet.")
# FIXME we use the "first" device, in case of multiple ones
name, device_info = devices[0]
plugin = plugins.get_plugin(name)
derivation = get_derivation_used_for_hw_device_encryption()
try:
xpub = plugin.get_xpub(device_info.device.id_, derivation, 'standard', plugin.handler)
except UserCancelled:
sys.exit(0)
password = keystore.Xpub.get_pubkey_from_xpub(xpub, ())
return password
def run_offline_command(config, config_options, plugins):
cmdname = config.get('cmd')
cmd = known_commands[cmdname]
password = config_options.get('password')
if cmd.requires_wallet:
storage = WalletStorage(config.get_wallet_path())
if storage.is_encrypted():
if storage.is_encrypted_with_hw_device():
password = get_password_for_hw_device_encrypted_storage(plugins)
config_options['password'] = password
storage.decrypt(password)
wallet = Wallet(storage)
else:
wallet = None
# check password
if cmd.requires_password and storage.get('use_encryption'):
if cmd.requires_password and wallet.has_password():
try:
seed = wallet.check_password(password)
except InvalidPassword:
@ -296,9 +332,11 @@ def init_plugins(config, gui_name):
from electrum_zcash.plugins import Plugins
return Plugins(config, is_local or is_android, gui_name)
if __name__ == '__main__':
# on osx, delete Process Serial Number arg generated for apps launched in Finder
if __name__ == '__main__':
# The hook will only be used in the Qt GUI right now
util.setup_thread_excepthook()
# on macOS, delete Process Serial Number arg generated for apps launched in Finder
sys.argv = list(filter(lambda x: not x.startswith('-psn'), sys.argv))
# old 'help' syntax
@ -313,7 +351,7 @@ if __name__ == '__main__':
sys.argv[i] = sys.stdin.read()
break
else:
raise BaseException('Cannot get argument from stdin')
raise Exception('Cannot get argument from stdin')
elif arg == '?':
sys.argv[i] = input("Enter argument:")
elif arg == ':':
@ -362,7 +400,9 @@ if __name__ == '__main__':
cmdname = config.get('cmd')
if config.get('testnet'):
bitcoin.NetworkConstants.set_testnet()
constants.set_testnet()
elif config.get('regtest'):
constants.set_regtest()
# run non-RPC commands separately
if cmdname in ['create', 'restore']:
@ -400,7 +440,11 @@ if __name__ == '__main__':
from electrum_zcash import websockets
websockets.WebSocketServer(config, d.network).start()
if config.get('requests_dir'):
check_www_dir(config.get('requests_dir'))
path = os.path.join(config.get('requests_dir'), 'index.html')
if not os.path.exists(path):
print("Requests directory not configured.")
print("You can configure it using https://github.com/spesmilo/electrum-merchant")
sys.exit(1)
d.join()
sys.exit(0)
else:
@ -424,8 +468,8 @@ if __name__ == '__main__':
print_msg("Daemon not running; try 'electrum-zcash daemon start'")
sys.exit(1)
else:
init_plugins(config, 'cmdline')
result = run_offline_command(config, config_options)
plugins = init_plugins(config, 'cmdline')
result = run_offline_command(config, config_options, plugins)
# print result
if isinstance(result, str):
print_msg(result)