2017-02-05 14:31:17 +03:00
|
|
|
#!/usr/bin/env python3
|
2015-02-21 12:24:40 +01:00
|
|
|
# -*- mode: python -*-
|
2011-11-04 18:00:37 +01:00
|
|
|
#
|
|
|
|
|
# Electrum - lightweight Bitcoin client
|
|
|
|
|
# Copyright (C) 2011 thomasv@gitorious
|
|
|
|
|
#
|
2016-02-23 11:36:42 +01:00
|
|
|
# Permission is hereby granted, free of charge, to any person
|
|
|
|
|
# obtaining a copy of this software and associated documentation files
|
|
|
|
|
# (the "Software"), to deal in the Software without restriction,
|
|
|
|
|
# including without limitation the rights to use, copy, modify, merge,
|
|
|
|
|
# publish, distribute, sublicense, and/or sell copies of the Software,
|
|
|
|
|
# and to permit persons to whom the Software is furnished to do so,
|
|
|
|
|
# subject to the following conditions:
|
2011-11-04 18:00:37 +01:00
|
|
|
#
|
2016-02-23 11:36:42 +01:00
|
|
|
# The above copyright notice and this permission notice shall be
|
|
|
|
|
# included in all copies or substantial portions of the Software.
|
2011-11-04 18:00:37 +01:00
|
|
|
#
|
2016-02-23 11:36:42 +01:00
|
|
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
|
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
|
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
|
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
|
|
|
|
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
|
|
|
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
|
|
|
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
|
# SOFTWARE.
|
2013-11-12 19:19:32 -08:00
|
|
|
import os
|
|
|
|
|
import sys
|
2015-08-26 17:44:19 +02:00
|
|
|
|
2015-02-24 18:41:29 +01:00
|
|
|
script_dir = os.path.dirname(os.path.realpath(__file__))
|
2015-01-27 13:50:02 +01:00
|
|
|
is_bundle = getattr(sys, 'frozen', False)
|
2017-10-24 22:52:53 +02:00
|
|
|
is_local = not is_bundle and os.path.exists(os.path.join(script_dir, "electrum.desktop"))
|
2013-03-12 13:48:16 +01:00
|
|
|
is_android = 'ANDROID_DATA' in os.environ
|
|
|
|
|
|
2016-03-07 21:00:55 +01:00
|
|
|
# move this back to gui/kivy/__init.py once plugins are moved
|
2018-07-11 17:38:47 +02:00
|
|
|
os.environ['KIVY_DATA_DIR'] = os.path.abspath(os.path.dirname(__file__)) + '/electrum/gui/kivy/data/'
|
2016-03-07 21:00:55 +01:00
|
|
|
|
2015-02-24 18:41:29 +01:00
|
|
|
if is_local or is_android:
|
|
|
|
|
sys.path.insert(0, os.path.join(script_dir, 'packages'))
|
2015-01-27 13:50:02 +01:00
|
|
|
|
2016-01-28 14:38:10 +01:00
|
|
|
|
|
|
|
|
def check_imports():
|
2016-01-27 20:21:20 +05:30
|
|
|
# pure-python dependencies need to be imported here for pyinstaller
|
|
|
|
|
try:
|
|
|
|
|
import dns
|
2017-02-10 17:05:15 +01:00
|
|
|
import pyaes
|
2016-01-27 20:21:20 +05:30
|
|
|
import ecdsa
|
|
|
|
|
import requests
|
|
|
|
|
import qrcode
|
|
|
|
|
import google.protobuf
|
2017-02-16 10:54:24 +01:00
|
|
|
import jsonrpclib
|
2016-01-27 20:21:20 +05:30
|
|
|
except ImportError as e:
|
2017-10-20 09:53:54 +02:00
|
|
|
sys.exit("Error: %s. Try 'sudo pip install <module-name>'"%str(e))
|
2016-01-27 20:21:20 +05:30
|
|
|
# the following imports are for pyinstaller
|
|
|
|
|
from google.protobuf import descriptor
|
|
|
|
|
from google.protobuf import message
|
|
|
|
|
from google.protobuf import reflection
|
|
|
|
|
from google.protobuf import descriptor_pb2
|
2017-02-16 10:54:24 +01:00
|
|
|
from jsonrpclib import SimpleJSONRPCServer
|
2016-01-28 14:38:10 +01:00
|
|
|
# make sure that certificates are here
|
|
|
|
|
assert os.path.exists(requests.utils.DEFAULT_CA_BUNDLE_PATH)
|
|
|
|
|
|
2015-01-26 14:14:16 +01:00
|
|
|
|
2016-01-28 14:38:10 +01:00
|
|
|
if not is_android:
|
|
|
|
|
check_imports()
|
2015-01-26 14:14:16 +01:00
|
|
|
|
2017-03-28 15:49:50 +02:00
|
|
|
|
|
|
|
|
from electrum import bitcoin, util
|
2018-03-04 22:10:59 +01:00
|
|
|
from electrum import constants
|
2016-08-12 11:56:27 +02:00
|
|
|
from electrum import SimpleConfig, Network
|
2018-07-13 14:19:02 +02:00
|
|
|
from electrum.wallet import Wallet, Imported_Wallet
|
2018-07-11 17:38:47 +02:00
|
|
|
from electrum import bitcoin, util, constants
|
2018-02-10 19:18:48 +01:00
|
|
|
from electrum.storage import WalletStorage, get_derivation_used_for_hw_device_encryption
|
2018-03-14 12:42:42 +01:00
|
|
|
from electrum.util import print_msg, print_stderr, json_encode, json_decode, UserCancelled
|
2018-01-12 10:37:14 +01:00
|
|
|
from electrum.util import set_verbosity, InvalidPassword
|
2015-06-07 17:45:13 +02:00
|
|
|
from electrum.commands import get_parser, known_commands, Commands, config_variables
|
2016-02-01 10:20:22 +01:00
|
|
|
from electrum import daemon
|
2016-08-25 09:48:11 +02:00
|
|
|
from electrum import keystore
|
2016-08-12 11:56:27 +02:00
|
|
|
from electrum.mnemonic import Mnemonic
|
2012-06-06 19:26:05 +02:00
|
|
|
|
2012-10-12 01:50:54 +02:00
|
|
|
# get password routine
|
|
|
|
|
def prompt_password(prompt, confirm=True):
|
|
|
|
|
import getpass
|
2015-10-28 09:33:35 +01:00
|
|
|
password = getpass.getpass(prompt, stream=None)
|
2015-08-16 16:11:52 +02:00
|
|
|
if password and confirm:
|
|
|
|
|
password2 = getpass.getpass("Confirm: ")
|
|
|
|
|
if password != password2:
|
|
|
|
|
sys.exit("Error: Passwords do not match.")
|
2012-10-12 01:50:54 +02:00
|
|
|
if not password:
|
|
|
|
|
password = None
|
|
|
|
|
return password
|
|
|
|
|
|
2013-11-12 19:19:32 -08:00
|
|
|
|
2015-05-28 15:22:30 +02:00
|
|
|
|
2015-12-23 10:54:31 +01:00
|
|
|
def run_non_RPC(config):
|
|
|
|
|
cmdname = config.get('cmd')
|
|
|
|
|
|
|
|
|
|
storage = WalletStorage(config.get_wallet_path())
|
2017-03-06 08:33:35 +01:00
|
|
|
if storage.file_exists():
|
2015-12-23 10:54:31 +01:00
|
|
|
sys.exit("Error: Remove the existing wallet first!")
|
|
|
|
|
|
|
|
|
|
def password_dialog():
|
|
|
|
|
return prompt_password("Password (hit return if you do not wish to encrypt your wallet):")
|
|
|
|
|
|
|
|
|
|
if cmdname == 'restore':
|
2016-10-05 10:25:48 +02:00
|
|
|
text = config.get('text').strip()
|
2016-08-25 09:48:11 +02:00
|
|
|
passphrase = config.get('passphrase', '')
|
|
|
|
|
password = password_dialog() if keystore.is_private(text) else None
|
2017-03-03 14:23:09 +01:00
|
|
|
if keystore.is_address_list(text):
|
|
|
|
|
wallet = Imported_Wallet(storage)
|
|
|
|
|
for x in text.split():
|
|
|
|
|
wallet.import_address(x)
|
2017-09-25 21:35:14 +02:00
|
|
|
elif keystore.is_private_key_list(text):
|
|
|
|
|
k = keystore.Imported_KeyStore({})
|
|
|
|
|
storage.put('keystore', k.dump())
|
|
|
|
|
storage.put('use_encryption', bool(password))
|
|
|
|
|
wallet = Imported_Wallet(storage)
|
|
|
|
|
for x in text.split():
|
|
|
|
|
wallet.import_private_key(x, password)
|
|
|
|
|
storage.write()
|
2016-08-25 09:48:11 +02:00
|
|
|
else:
|
2017-03-03 14:23:09 +01:00
|
|
|
if keystore.is_seed(text):
|
2017-11-01 14:46:44 +01:00
|
|
|
k = keystore.from_seed(text, passphrase, False)
|
2017-09-25 21:35:14 +02:00
|
|
|
elif keystore.is_master_key(text):
|
|
|
|
|
k = keystore.from_master_key(text)
|
2017-03-03 14:23:09 +01:00
|
|
|
else:
|
|
|
|
|
sys.exit("Error: Seed or key not recognized")
|
|
|
|
|
if password:
|
|
|
|
|
k.update_password(None, password)
|
|
|
|
|
storage.put('keystore', k.dump())
|
|
|
|
|
storage.put('wallet_type', 'standard')
|
|
|
|
|
storage.put('use_encryption', bool(password))
|
|
|
|
|
storage.write()
|
|
|
|
|
wallet = Wallet(storage)
|
2015-12-23 10:54:31 +01:00
|
|
|
if not config.get('offline'):
|
|
|
|
|
network = Network(config)
|
|
|
|
|
network.start()
|
|
|
|
|
wallet.start_threads(network)
|
|
|
|
|
print_msg("Recovering wallet...")
|
|
|
|
|
wallet.synchronize()
|
|
|
|
|
wallet.wait_until_synchronized()
|
2018-02-26 01:33:02 +01:00
|
|
|
wallet.stop_threads()
|
|
|
|
|
# note: we don't wait for SPV
|
2015-12-23 10:54:31 +01:00
|
|
|
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."
|
|
|
|
|
print_msg(msg)
|
|
|
|
|
|
|
|
|
|
elif cmdname == 'create':
|
|
|
|
|
password = password_dialog()
|
2016-08-25 09:48:11 +02:00
|
|
|
passphrase = config.get('passphrase', '')
|
2017-09-07 21:40:38 +02:00
|
|
|
seed_type = 'segwit' if config.get('segwit') else 'standard'
|
|
|
|
|
seed = Mnemonic('en').make_seed(seed_type)
|
2017-11-01 14:46:44 +01:00
|
|
|
k = keystore.from_seed(seed, passphrase, False)
|
2016-08-25 09:48:11 +02:00
|
|
|
storage.put('keystore', k.dump())
|
2016-08-12 11:56:27 +02:00
|
|
|
storage.put('wallet_type', 'standard')
|
2015-12-23 10:54:31 +01:00
|
|
|
wallet = Wallet(storage)
|
2017-10-15 09:18:14 +02:00
|
|
|
wallet.update_password(None, password, True)
|
2015-12-23 10:54:31 +01:00
|
|
|
wallet.synchronize()
|
|
|
|
|
print_msg("Your wallet generation seed is:\n\"%s\"" % seed)
|
|
|
|
|
print_msg("Please keep it in a safe place; if you lose it, you will not be able to restore your wallet.")
|
|
|
|
|
|
|
|
|
|
wallet.storage.write()
|
|
|
|
|
print_msg("Wallet saved in '%s'" % wallet.storage.path)
|
|
|
|
|
sys.exit(0)
|
2013-11-05 18:55:53 +01:00
|
|
|
|
2015-12-23 10:54:31 +01:00
|
|
|
|
2017-02-09 17:08:27 +01:00
|
|
|
def init_daemon(config_options):
|
|
|
|
|
config = SimpleConfig(config_options)
|
|
|
|
|
storage = WalletStorage(config.get_wallet_path())
|
2017-03-06 08:33:35 +01:00
|
|
|
if not storage.file_exists():
|
2017-02-09 17:08:27 +01:00
|
|
|
print_msg("Error: Wallet file not found.")
|
|
|
|
|
print_msg("Type 'electrum create' to create a new wallet, or provide a path to a wallet with the -w option")
|
|
|
|
|
sys.exit(0)
|
|
|
|
|
if storage.is_encrypted():
|
2017-12-07 11:35:10 +01:00
|
|
|
if storage.is_encrypted_with_hw_device():
|
2018-02-10 19:18:48 +01:00
|
|
|
plugins = init_plugins(config, 'cmdline')
|
|
|
|
|
password = get_password_for_hw_device_encrypted_storage(plugins)
|
|
|
|
|
elif config.get('password'):
|
2017-02-09 17:08:27 +01:00
|
|
|
password = config.get('password')
|
|
|
|
|
else:
|
|
|
|
|
password = prompt_password('Password:', False)
|
|
|
|
|
if not password:
|
|
|
|
|
print_msg("Error: Password required")
|
|
|
|
|
sys.exit(1)
|
|
|
|
|
else:
|
|
|
|
|
password = None
|
|
|
|
|
config_options['password'] = password
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def init_cmdline(config_options, server):
|
2015-12-23 10:54:31 +01:00
|
|
|
config = SimpleConfig(config_options)
|
2015-05-28 15:22:30 +02:00
|
|
|
cmdname = config.get('cmd')
|
|
|
|
|
cmd = known_commands[cmdname]
|
2011-11-04 18:00:37 +01:00
|
|
|
|
2015-05-31 17:21:02 +02:00
|
|
|
if cmdname == 'signtransaction' and config.get('privkey'):
|
|
|
|
|
cmd.requires_wallet = False
|
|
|
|
|
cmd.requires_password = False
|
|
|
|
|
|
2015-06-10 23:21:25 +02:00
|
|
|
if cmdname in ['payto', 'paytomany'] and config.get('unsigned'):
|
2015-05-31 17:38:57 +02:00
|
|
|
cmd.requires_password = False
|
|
|
|
|
|
2015-06-10 23:21:25 +02:00
|
|
|
if cmdname in ['payto', 'paytomany'] and config.get('broadcast'):
|
|
|
|
|
cmd.requires_network = True
|
|
|
|
|
|
2018-02-10 19:18:48 +01:00
|
|
|
# instantiate wallet for command-line
|
2015-05-08 10:58:54 +02:00
|
|
|
storage = WalletStorage(config.get_wallet_path())
|
2013-08-22 12:39:41 +02:00
|
|
|
|
2017-03-06 08:33:35 +01:00
|
|
|
if cmd.requires_wallet and not storage.file_exists():
|
2015-10-28 11:13:45 +01:00
|
|
|
print_msg("Error: Wallet file not found.")
|
|
|
|
|
print_msg("Type 'electrum create' to create a new wallet, or provide a path to a wallet with the -w option")
|
|
|
|
|
sys.exit(0)
|
|
|
|
|
|
2012-10-02 13:15:10 +02:00
|
|
|
# important warning
|
2015-08-16 16:30:55 +02:00
|
|
|
if cmd.name in ['getprivatekeys']:
|
2014-04-30 15:27:50 +02:00
|
|
|
print_stderr("WARNING: ALL your private keys are secret.")
|
|
|
|
|
print_stderr("Exposing a single private key can compromise your entire wallet!")
|
|
|
|
|
print_stderr("In particular, DO NOT use 'redeem private key' services proposed by third parties.")
|
2012-10-02 13:15:10 +02:00
|
|
|
|
2011-11-14 20:35:54 +01:00
|
|
|
# commands needing password
|
2017-03-03 16:05:13 +01:00
|
|
|
if (cmd.requires_wallet and storage.is_encrypted() and server is None)\
|
2017-02-09 17:08:27 +01:00
|
|
|
or (cmd.requires_password and (storage.get('use_encryption') or storage.is_encrypted())):
|
2017-12-07 11:35:10 +01:00
|
|
|
if storage.is_encrypted_with_hw_device():
|
2018-02-10 19:18:48 +01:00
|
|
|
# this case is handled later in the control flow
|
|
|
|
|
password = None
|
|
|
|
|
elif config.get('password'):
|
2015-08-16 16:11:52 +02:00
|
|
|
password = config.get('password')
|
|
|
|
|
else:
|
2013-01-02 13:39:50 +01:00
|
|
|
password = prompt_password('Password:', False)
|
|
|
|
|
if not password:
|
|
|
|
|
print_msg("Error: Password required")
|
2013-12-13 17:53:13 +01:00
|
|
|
sys.exit(1)
|
2013-02-26 13:56:48 +01:00
|
|
|
else:
|
|
|
|
|
password = None
|
|
|
|
|
|
2015-12-23 10:54:31 +01:00
|
|
|
config_options['password'] = password
|
2012-05-13 01:32:28 +02:00
|
|
|
|
2015-12-23 10:54:31 +01:00
|
|
|
if cmd.name == 'password':
|
2012-07-06 21:45:57 -07:00
|
|
|
new_password = prompt_password('New password:')
|
2015-12-23 10:54:31 +01:00
|
|
|
config_options['new_password'] = new_password
|
2015-08-26 17:44:19 +02:00
|
|
|
|
2015-12-23 10:54:31 +01:00
|
|
|
return cmd, password
|
2015-08-26 17:44:19 +02:00
|
|
|
|
|
|
|
|
|
2018-02-10 19:18:48 +01:00
|
|
|
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:
|
2018-04-15 20:45:30 +03:00
|
|
|
print_msg("Error: No connected hw device found. Cannot decrypt this wallet.")
|
2018-02-10 19:18:48 +01:00
|
|
|
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()
|
2018-03-14 12:42:42 +01:00
|
|
|
try:
|
|
|
|
|
xpub = plugin.get_xpub(device_info.device.id_, derivation, 'standard', plugin.handler)
|
|
|
|
|
except UserCancelled:
|
|
|
|
|
sys.exit(0)
|
2018-02-10 19:18:48 +01:00
|
|
|
password = keystore.Xpub.get_pubkey_from_xpub(xpub, ())
|
|
|
|
|
return password
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def run_offline_command(config, config_options, plugins):
|
2015-12-23 10:54:31 +01:00
|
|
|
cmdname = config.get('cmd')
|
|
|
|
|
cmd = known_commands[cmdname]
|
2017-02-09 17:08:27 +01:00
|
|
|
password = config_options.get('password')
|
2017-03-03 16:05:13 +01:00
|
|
|
if cmd.requires_wallet:
|
|
|
|
|
storage = WalletStorage(config.get_wallet_path())
|
2017-03-06 08:33:35 +01:00
|
|
|
if storage.is_encrypted():
|
2017-12-07 11:35:10 +01:00
|
|
|
if storage.is_encrypted_with_hw_device():
|
2018-02-10 19:18:48 +01:00
|
|
|
password = get_password_for_hw_device_encrypted_storage(plugins)
|
|
|
|
|
config_options['password'] = password
|
2017-03-06 08:33:35 +01:00
|
|
|
storage.decrypt(password)
|
2017-03-03 16:05:13 +01:00
|
|
|
wallet = Wallet(storage)
|
|
|
|
|
else:
|
|
|
|
|
wallet = None
|
2015-12-23 10:54:31 +01:00
|
|
|
# check password
|
2017-12-07 11:35:10 +01:00
|
|
|
if cmd.requires_password and wallet.has_password():
|
2015-12-23 10:54:31 +01:00
|
|
|
try:
|
|
|
|
|
seed = wallet.check_password(password)
|
|
|
|
|
except InvalidPassword:
|
|
|
|
|
print_msg("Error: This password does not decode this wallet.")
|
|
|
|
|
sys.exit(1)
|
|
|
|
|
if cmd.requires_network:
|
2017-02-09 17:08:27 +01:00
|
|
|
print_msg("Warning: running command offline")
|
2015-08-26 17:44:19 +02:00
|
|
|
# arguments passed to function
|
2017-01-22 21:25:24 +03:00
|
|
|
args = [config.get(x) for x in cmd.params]
|
2015-08-30 17:46:51 +02:00
|
|
|
# decode json arguments
|
2018-01-13 22:43:57 +01:00
|
|
|
if cmdname not in ('setconfig',):
|
|
|
|
|
args = list(map(json_decode, args))
|
2015-08-26 17:44:19 +02:00
|
|
|
# options
|
2017-10-09 11:54:17 +02:00
|
|
|
kwargs = {}
|
|
|
|
|
for x in cmd.options:
|
|
|
|
|
kwargs[x] = (config_options.get(x) if x in ['password', 'new_password'] else config.get(x))
|
2017-07-02 11:44:48 +02:00
|
|
|
cmd_runner = Commands(config, wallet, None)
|
2015-08-26 17:44:19 +02:00
|
|
|
func = getattr(cmd_runner, cmd.name)
|
2017-10-09 11:54:17 +02:00
|
|
|
result = func(*args, **kwargs)
|
2015-12-23 10:54:31 +01:00
|
|
|
# save wallet
|
2015-12-23 15:23:33 +01:00
|
|
|
if wallet:
|
|
|
|
|
wallet.storage.write()
|
2015-08-26 17:44:19 +02:00
|
|
|
return result
|
|
|
|
|
|
2016-02-01 22:09:55 +01:00
|
|
|
def init_plugins(config, gui_name):
|
2018-07-11 17:38:47 +02:00
|
|
|
from electrum.plugin import Plugins
|
2017-08-07 20:06:43 +02:00
|
|
|
return Plugins(config, is_local or is_android, gui_name)
|
2015-08-26 17:44:19 +02:00
|
|
|
|
2015-05-28 15:22:30 +02:00
|
|
|
|
2017-03-28 15:49:50 +02:00
|
|
|
if __name__ == '__main__':
|
|
|
|
|
# The hook will only be used in the Qt GUI right now
|
|
|
|
|
util.setup_thread_excepthook()
|
2018-04-15 20:45:30 +03:00
|
|
|
# on macOS, delete Process Serial Number arg generated for apps launched in Finder
|
2017-01-22 21:25:24 +03:00
|
|
|
sys.argv = list(filter(lambda x: not x.startswith('-psn'), sys.argv))
|
2015-05-28 15:22:30 +02:00
|
|
|
|
|
|
|
|
# old 'help' syntax
|
2017-01-22 21:25:24 +03:00
|
|
|
if len(sys.argv) > 1 and sys.argv[1] == 'help':
|
2015-05-28 15:22:30 +02:00
|
|
|
sys.argv.remove('help')
|
|
|
|
|
sys.argv.append('-h')
|
|
|
|
|
|
2018-08-30 18:34:04 +02:00
|
|
|
# old '-v' syntax
|
|
|
|
|
try:
|
|
|
|
|
i = sys.argv.index('-v')
|
|
|
|
|
except ValueError:
|
|
|
|
|
pass
|
|
|
|
|
else:
|
|
|
|
|
sys.argv[i] = '-v*'
|
|
|
|
|
|
2015-08-17 09:46:50 +02:00
|
|
|
# read arguments from stdin pipe and prompt
|
|
|
|
|
for i, arg in enumerate(sys.argv):
|
|
|
|
|
if arg == '-':
|
|
|
|
|
if not sys.stdin.isatty():
|
2015-08-30 17:46:51 +02:00
|
|
|
sys.argv[i] = sys.stdin.read()
|
2015-08-17 09:46:50 +02:00
|
|
|
break
|
|
|
|
|
else:
|
2018-04-07 17:10:30 +02:00
|
|
|
raise Exception('Cannot get argument from stdin')
|
2015-08-17 09:46:50 +02:00
|
|
|
elif arg == '?':
|
2017-01-22 21:25:24 +03:00
|
|
|
sys.argv[i] = input("Enter argument:")
|
2015-10-28 09:33:35 +01:00
|
|
|
elif arg == ':':
|
2017-11-28 18:20:14 +01:00
|
|
|
sys.argv[i] = prompt_password('Enter argument (will not echo):', False)
|
2015-08-17 09:46:50 +02:00
|
|
|
|
2015-08-26 17:44:19 +02:00
|
|
|
# parse command line
|
|
|
|
|
parser = get_parser()
|
2015-05-28 15:22:30 +02:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
|
|
# config is an object passed to the various constructors (wallet, interface, gui)
|
|
|
|
|
if is_android:
|
|
|
|
|
config_options = {
|
2018-07-18 13:31:41 +02:00
|
|
|
'verbosity': '',
|
2015-09-07 16:44:17 +02:00
|
|
|
'cmd': 'gui',
|
2016-01-26 15:23:33 +01:00
|
|
|
'gui': 'kivy',
|
2015-05-28 15:22:30 +02:00
|
|
|
}
|
|
|
|
|
else:
|
|
|
|
|
config_options = args.__dict__
|
2017-01-22 21:25:24 +03:00
|
|
|
f = lambda key: config_options[key] is not None and key not in config_variables.get(args.cmd, {}).keys()
|
|
|
|
|
config_options = {key: config_options[key] for key in filter(f, config_options.keys())}
|
2015-05-28 15:22:30 +02:00
|
|
|
if config_options.get('server'):
|
|
|
|
|
config_options['auto_connect'] = False
|
|
|
|
|
|
2016-02-24 16:57:58 +01:00
|
|
|
config_options['cwd'] = os.getcwd()
|
|
|
|
|
|
2017-11-28 18:20:14 +01:00
|
|
|
# fixme: this can probably be achieved with a runtime hook (pyinstaller)
|
|
|
|
|
if is_bundle and os.path.exists(os.path.join(sys._MEIPASS, 'is_portable')):
|
2017-11-28 18:26:58 +01:00
|
|
|
config_options['portable'] = True
|
2017-11-28 18:20:14 +01:00
|
|
|
|
2015-07-06 18:15:22 -06:00
|
|
|
if config_options.get('portable'):
|
2015-05-30 06:56:45 +02:00
|
|
|
config_options['electrum_path'] = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'electrum_data')
|
|
|
|
|
|
2017-01-23 15:57:45 +01:00
|
|
|
# kivy sometimes freezes when we write to sys.stderr
|
2018-07-18 13:31:41 +02:00
|
|
|
set_verbosity(config_options.get('verbosity') if config_options.get('gui') != 'kivy' else '')
|
2015-05-28 15:22:30 +02:00
|
|
|
|
2015-09-03 18:07:15 +09:00
|
|
|
# check uri
|
2015-09-03 11:18:35 +02:00
|
|
|
uri = config_options.get('url')
|
2015-09-03 18:07:15 +09:00
|
|
|
if uri:
|
2016-01-24 19:52:37 +09:00
|
|
|
if not uri.startswith('bitcoin:'):
|
2015-09-03 18:07:15 +09:00
|
|
|
print_stderr('unknown command:', uri)
|
|
|
|
|
sys.exit(1)
|
2015-09-03 11:18:35 +02:00
|
|
|
config_options['url'] = uri
|
|
|
|
|
|
2017-03-14 08:56:30 +01:00
|
|
|
# todo: defer this to gui
|
2015-09-03 11:18:35 +02:00
|
|
|
config = SimpleConfig(config_options)
|
2015-12-23 15:59:32 +01:00
|
|
|
cmdname = config.get('cmd')
|
2015-09-03 18:07:15 +09:00
|
|
|
|
2017-01-07 16:58:23 +01:00
|
|
|
if config.get('testnet'):
|
2018-03-04 22:10:59 +01:00
|
|
|
constants.set_testnet()
|
2018-04-11 20:10:14 +03:00
|
|
|
elif config.get('regtest'):
|
|
|
|
|
constants.set_regtest()
|
2018-06-22 17:07:07 +02:00
|
|
|
elif config.get('simnet'):
|
|
|
|
|
constants.set_simnet()
|
2017-03-04 11:01:32 +09:00
|
|
|
|
2015-12-23 10:54:31 +01:00
|
|
|
# run non-RPC commands separately
|
2016-10-05 10:53:08 +02:00
|
|
|
if cmdname in ['create', 'restore']:
|
2015-12-23 10:54:31 +01:00
|
|
|
run_non_RPC(config)
|
|
|
|
|
sys.exit(0)
|
|
|
|
|
|
2015-12-23 15:59:32 +01:00
|
|
|
if cmdname == 'gui':
|
2016-02-01 13:10:01 +01:00
|
|
|
fd, server = daemon.get_fd_or_server(config)
|
|
|
|
|
if fd is not None:
|
2016-02-01 22:09:55 +01:00
|
|
|
plugins = init_plugins(config, config.get('gui', 'qt'))
|
2018-01-07 23:53:25 +01:00
|
|
|
d = daemon.Daemon(config, fd, True)
|
2016-02-01 10:20:22 +01:00
|
|
|
d.start()
|
|
|
|
|
d.init_gui(config, plugins)
|
2016-02-01 09:00:10 +01:00
|
|
|
sys.exit(0)
|
2016-02-01 09:25:57 +01:00
|
|
|
else:
|
|
|
|
|
result = server.gui(config_options)
|
2016-02-01 09:00:10 +01:00
|
|
|
|
2015-12-23 15:59:32 +01:00
|
|
|
elif cmdname == 'daemon':
|
2016-02-01 10:20:22 +01:00
|
|
|
subcommand = config.get('subcommand')
|
2017-03-05 20:25:42 +01:00
|
|
|
if subcommand in ['load_wallet']:
|
2017-02-09 17:08:27 +01:00
|
|
|
init_daemon(config_options)
|
|
|
|
|
|
2017-01-07 10:10:51 +01:00
|
|
|
if subcommand in [None, 'start']:
|
2016-02-01 13:10:01 +01:00
|
|
|
fd, server = daemon.get_fd_or_server(config)
|
|
|
|
|
if fd is not None:
|
2017-01-07 10:10:51 +01:00
|
|
|
if subcommand == 'start':
|
|
|
|
|
pid = os.fork()
|
|
|
|
|
if pid:
|
|
|
|
|
print_stderr("starting daemon (PID %d)" % pid)
|
|
|
|
|
sys.exit(0)
|
2016-02-01 22:09:55 +01:00
|
|
|
init_plugins(config, 'cmdline')
|
2018-01-07 23:53:25 +01:00
|
|
|
d = daemon.Daemon(config, fd, False)
|
2016-02-01 10:20:22 +01:00
|
|
|
d.start()
|
|
|
|
|
if config.get('websocket_server'):
|
|
|
|
|
from electrum import websockets
|
|
|
|
|
websockets.WebSocketServer(config, d.network).start()
|
|
|
|
|
if config.get('requests_dir'):
|
2018-01-12 10:37:14 +01:00
|
|
|
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)
|
2016-02-01 10:20:22 +01:00
|
|
|
d.join()
|
2016-02-01 09:00:10 +01:00
|
|
|
sys.exit(0)
|
2016-02-01 10:20:22 +01:00
|
|
|
else:
|
|
|
|
|
result = server.daemon(config_options)
|
2016-02-01 09:00:10 +01:00
|
|
|
else:
|
2016-02-01 10:20:22 +01:00
|
|
|
server = daemon.get_server(config)
|
|
|
|
|
if server is not None:
|
|
|
|
|
result = server.daemon(config_options)
|
|
|
|
|
else:
|
|
|
|
|
print_msg("Daemon not running")
|
|
|
|
|
sys.exit(1)
|
2015-08-26 17:44:19 +02:00
|
|
|
else:
|
2015-12-23 15:59:32 +01:00
|
|
|
# command line
|
2016-02-01 10:20:22 +01:00
|
|
|
server = daemon.get_server(config)
|
2017-02-09 17:08:27 +01:00
|
|
|
init_cmdline(config_options, server)
|
2016-02-01 09:00:10 +01:00
|
|
|
if server is not None:
|
|
|
|
|
result = server.run_cmdline(config_options)
|
|
|
|
|
else:
|
2015-12-23 15:59:32 +01:00
|
|
|
cmd = known_commands[cmdname]
|
|
|
|
|
if cmd.requires_network:
|
2016-01-31 11:43:11 +09:00
|
|
|
print_msg("Daemon not running; try 'electrum daemon start'")
|
2015-12-23 15:59:32 +01:00
|
|
|
sys.exit(1)
|
2016-02-01 09:00:10 +01:00
|
|
|
else:
|
2018-02-10 19:18:48 +01:00
|
|
|
plugins = init_plugins(config, 'cmdline')
|
|
|
|
|
result = run_offline_command(config, config_options, plugins)
|
2017-01-22 21:25:24 +03:00
|
|
|
# print result
|
2017-10-24 14:04:16 +02:00
|
|
|
if isinstance(result, str):
|
2015-12-23 15:59:32 +01:00
|
|
|
print_msg(result)
|
|
|
|
|
elif type(result) is dict and result.get('error'):
|
|
|
|
|
print_stderr(result.get('error'))
|
|
|
|
|
elif result is not None:
|
|
|
|
|
print_msg(json_encode(result))
|
|
|
|
|
sys.exit(0)
|