| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
import os |
| 27 |
import sys |
| 28 |
|
| 29 |
|
| 30 |
MIN_PYTHON_VERSION = "3.10.0" |
| 31 |
_min_python_version_tuple = tuple(map(int, (MIN_PYTHON_VERSION.split(".")))) |
| 32 |
|
| 33 |
|
| 34 |
if sys.version_info[:3] < _min_python_version_tuple: |
| 35 |
sys.exit("Error: Electrum requires Python version >= %s..." % MIN_PYTHON_VERSION) |
| 36 |
|
| 37 |
|
| 38 |
import warnings |
| 39 |
import asyncio |
| 40 |
from typing import TYPE_CHECKING, Optional, Dict |
| 41 |
|
| 42 |
|
| 43 |
script_dir = os.path.dirname(os.path.realpath(__file__)) |
| 44 |
is_pyinstaller = getattr(sys, 'frozen', False) |
| 45 |
is_android = 'ANDROID_DATA' in os.environ |
| 46 |
is_appimage = 'APPIMAGE' in os.environ |
| 47 |
is_binary_distributable = is_pyinstaller or is_android or is_appimage |
| 48 |
|
| 49 |
is_local = (not is_binary_distributable |
| 50 |
and os.path.exists(os.path.join(script_dir, "electrum.desktop"))) |
| 51 |
is_git_clone = is_local and os.path.exists(os.path.join(script_dir, ".git")) |
| 52 |
|
| 53 |
if is_git_clone: |
| 54 |
|
| 55 |
if not any(['DeprecationWarning' in x for x in sys.warnoptions]): |
| 56 |
warnings.simplefilter('default', DeprecationWarning) |
| 57 |
|
| 58 |
if is_local or is_android: |
| 59 |
sys.path.insert(0, os.path.join(script_dir, 'packages')) |
| 60 |
|
| 61 |
if is_pyinstaller: |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
_file = open(sys.executable, 'rb') |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
if is_local and os.name == 'nt': |
| 70 |
os.add_dll_directory(os.path.join(os.path.dirname(__file__), 'electrum')) |
| 71 |
|
| 72 |
|
| 73 |
def check_imports(): |
| 74 |
|
| 75 |
try: |
| 76 |
import dns |
| 77 |
import certifi |
| 78 |
import qrcode |
| 79 |
import aiorpcx |
| 80 |
import aiohttp |
| 81 |
import aiohttp_socks |
| 82 |
import electrum_ecc |
| 83 |
import jsonpatch |
| 84 |
import electrum_aionostr |
| 85 |
except ImportError as e: |
| 86 |
sys.exit(f"Error: {str(e)}. Some dependencies are missing. Have you read the README? Or just try '$ python3 -m pip install -r contrib/requirements/requirements.txt'") |
| 87 |
if not ((0, 25, 0) <= aiorpcx._version < (0, 26)): |
| 88 |
raise RuntimeError(f'aiorpcX version {aiorpcx._version} does not match required: 0.25.0<=ver<0.26') |
| 89 |
|
| 90 |
assert os.path.exists(certifi.where()) |
| 91 |
|
| 92 |
|
| 93 |
if not is_android: |
| 94 |
check_imports() |
| 95 |
|
| 96 |
|
| 97 |
if is_android: |
| 98 |
|
| 99 |
|
| 100 |
import ctypes |
| 101 |
ctypes.pythonapi = ctypes.PyDLL("libpython%d.%d.so" % sys.version_info[:2]) |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
os.umask(0o0077) |
| 111 |
|
| 112 |
|
| 113 |
sys._ELECTRUM_RUNNING_VIA_RUNELECTRUM = True |
| 114 |
|
| 115 |
from electrum.logging import get_logger, configure_logging |
| 116 |
from electrum import util |
| 117 |
from electrum.payment_identifier import PaymentIdentifier |
| 118 |
from electrum import SimpleConfig |
| 119 |
from electrum.wallet_db import WalletDB |
| 120 |
from electrum.wallet import Wallet |
| 121 |
from electrum.storage import WalletStorage |
| 122 |
from electrum.util import print_msg, print_stderr, json_encode, json_decode, UserCancelled |
| 123 |
from electrum.util import InvalidPassword |
| 124 |
from electrum.plugin import Plugins |
| 125 |
from electrum.commands import get_parser, get_simple_parser, known_commands, Commands, config_variables |
| 126 |
from electrum import daemon |
| 127 |
from electrum.util import create_and_start_event_loop, UserFacingException, JsonRPCError |
| 128 |
from electrum.i18n import set_language |
| 129 |
from electrum import constants |
| 130 |
|
| 131 |
if TYPE_CHECKING: |
| 132 |
import threading |
| 133 |
|
| 134 |
_logger = get_logger(__name__) |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
def prompt_password(prompt: str, *, confirm: bool = True) -> Optional[str]: |
| 139 |
import getpass |
| 140 |
password = getpass.getpass(prompt, stream=None) |
| 141 |
if password and confirm: |
| 142 |
password2 = getpass.getpass("Confirm: ") |
| 143 |
if password != password2: |
| 144 |
sys.exit("Error: Passwords do not match.") |
| 145 |
if not password: |
| 146 |
password = None |
| 147 |
return password |
| 148 |
|
| 149 |
|
| 150 |
def init_cmdline(config_options, wallet_path, *, rpcserver: bool, config: 'SimpleConfig'): |
| 151 |
cmdname = config.get('cmd') |
| 152 |
cmd = known_commands[cmdname] |
| 153 |
|
| 154 |
if cmdname in ['payto', 'paytomany'] and config.get('unsigned'): |
| 155 |
cmd.requires_password = False |
| 156 |
|
| 157 |
if cmdname in ['payto', 'paytomany'] and config.get('broadcast'): |
| 158 |
cmd.requires_network = True |
| 159 |
|
| 160 |
if cmd.requires_wallet and not wallet_path: |
| 161 |
print_msg("wallet path not provided.") |
| 162 |
sys_exit(1) |
| 163 |
|
| 164 |
|
| 165 |
storage = WalletStorage(wallet_path, allow_partial_writes=config.WALLET_PARTIAL_WRITES) if wallet_path else None |
| 166 |
|
| 167 |
if cmd.requires_wallet and not storage.file_exists(): |
| 168 |
print_msg("Error: Wallet file not found.") |
| 169 |
print_msg("Type 'electrum create' to create a new wallet, or provide a path to a wallet with the -w option") |
| 170 |
sys_exit(1) |
| 171 |
|
| 172 |
|
| 173 |
if cmd.name in ['getprivatekeys']: |
| 174 |
print_stderr("WARNING: ALL your private keys are secret.") |
| 175 |
print_stderr("Exposing a single private key can compromise your entire wallet!") |
| 176 |
print_stderr("In particular, DO NOT use 'redeem private key' services proposed by third parties.") |
| 177 |
|
| 178 |
|
| 179 |
if ((cmd.requires_wallet and storage.is_encrypted() and not rpcserver) |
| 180 |
or (cmdname == 'load_wallet' and storage.is_encrypted()) |
| 181 |
or (cmdname in ['password', 'unlock']) |
| 182 |
or (cmd.requires_password and not rpcserver)): |
| 183 |
if storage.is_encrypted_with_hw_device(): |
| 184 |
|
| 185 |
password = None |
| 186 |
elif config.get('password') is not None: |
| 187 |
password = config.get('password') |
| 188 |
if password == '': |
| 189 |
password = None |
| 190 |
else: |
| 191 |
password = prompt_password('Password:', confirm=False) |
| 192 |
else: |
| 193 |
password = None |
| 194 |
|
| 195 |
config_options['password'] = config_options.get('password') or password |
| 196 |
|
| 197 |
if cmd.name == 'password' and 'new_password' not in config_options: |
| 198 |
new_password = prompt_password('New password:') |
| 199 |
config_options['new_password'] = new_password |
| 200 |
|
| 201 |
|
| 202 |
def get_connected_hw_devices(plugins: 'Plugins'): |
| 203 |
supported_plugins = plugins.get_hardware_support() |
| 204 |
|
| 205 |
devices = [] |
| 206 |
devmgr = plugins.device_manager |
| 207 |
for splugin in supported_plugins: |
| 208 |
name, plugin = splugin.name, splugin.plugin |
| 209 |
if not plugin: |
| 210 |
e = splugin.exception |
| 211 |
_logger.error(f"{name}: error during plugin init: {repr(e)}") |
| 212 |
continue |
| 213 |
try: |
| 214 |
u = devmgr.list_pairable_device_infos(handler=None, plugin=plugin) |
| 215 |
except Exception as e: |
| 216 |
_logger.error(f'error getting device infos for {name}: {repr(e)}') |
| 217 |
continue |
| 218 |
devices += list(map(lambda x: (name, x), u)) |
| 219 |
return devices |
| 220 |
|
| 221 |
|
| 222 |
def get_password_for_hw_device_encrypted_storage(plugins: 'Plugins') -> str: |
| 223 |
devices = get_connected_hw_devices(plugins) |
| 224 |
if len(devices) == 0: |
| 225 |
raise UserFacingException("Error: No connected hw device found. Cannot decrypt this wallet.") |
| 226 |
elif len(devices) > 1: |
| 227 |
print_msg("Warning: multiple hardware devices detected. " |
| 228 |
"The first one will be used to decrypt the wallet.") |
| 229 |
|
| 230 |
name, device_info = devices[0] |
| 231 |
devmgr = plugins.device_manager |
| 232 |
try: |
| 233 |
client = devmgr.client_by_id(device_info.device.id_) |
| 234 |
client.handler = client.plugin.create_handler(None) |
| 235 |
return client.get_password_for_storage_encryption() |
| 236 |
except UserCancelled: |
| 237 |
raise |
| 238 |
|
| 239 |
|
| 240 |
async def run_offline_command(config: 'SimpleConfig', config_options: dict, wallet_path: str, plugins: 'Plugins'): |
| 241 |
cmdname = config.get('cmd') |
| 242 |
cmd = known_commands[cmdname] |
| 243 |
password = config_options.get('password') |
| 244 |
if 'wallet_path' in cmd.options and config_options.get('wallet_path') is None: |
| 245 |
config_options['wallet_path'] = wallet_path |
| 246 |
if cmd.requires_wallet: |
| 247 |
storage = WalletStorage(wallet_path, allow_partial_writes=config.WALLET_PARTIAL_WRITES) |
| 248 |
if storage.is_encrypted(): |
| 249 |
if storage.is_encrypted_with_hw_device(): |
| 250 |
password = get_password_for_hw_device_encrypted_storage(plugins) |
| 251 |
config_options['password'] = password |
| 252 |
storage.decrypt(password) |
| 253 |
db = WalletDB(storage.read(), storage=storage, upgrade=True) |
| 254 |
wallet = Wallet(db, config=config) |
| 255 |
config_options['wallet'] = wallet |
| 256 |
else: |
| 257 |
wallet = None |
| 258 |
|
| 259 |
if cmd.requires_password and wallet.has_password(): |
| 260 |
try: |
| 261 |
wallet.check_password(password) |
| 262 |
except InvalidPassword: |
| 263 |
print_msg("Error: This password does not decode this wallet.") |
| 264 |
raise |
| 265 |
if cmd.requires_network: |
| 266 |
print_msg("Warning: running command offline") |
| 267 |
|
| 268 |
args = [config.get(x) for x in cmd.params] |
| 269 |
|
| 270 |
if cmdname not in ('setconfig',): |
| 271 |
args = list(map(json_decode, args)) |
| 272 |
|
| 273 |
kwargs = {} |
| 274 |
for x in cmd.options: |
| 275 |
kwargs[x] = (config_options.get(x) if x in ['wallet_path', 'wallet', 'password', 'new_password'] else config.get(x)) |
| 276 |
cmd_runner = Commands(config=config) |
| 277 |
func = getattr(cmd_runner, cmd.name) |
| 278 |
result = await func(*args, **kwargs) |
| 279 |
|
| 280 |
if wallet: |
| 281 |
wallet.save_db() |
| 282 |
return result |
| 283 |
|
| 284 |
|
| 285 |
loop = None |
| 286 |
stop_loop = None |
| 287 |
loop_thread = None |
| 288 |
|
| 289 |
|
| 290 |
def sys_exit(i): |
| 291 |
|
| 292 |
if loop: |
| 293 |
loop.call_soon_threadsafe(stop_loop.set_result, 1) |
| 294 |
loop_thread.join(timeout=1) |
| 295 |
sys.exit(i) |
| 296 |
|
| 297 |
|
| 298 |
def read_config(config_options: dict) -> SimpleConfig: |
| 299 |
""" |
| 300 |
Reads the config file and returns SimpleConfig, on failure it will potentially |
| 301 |
show a GUI error dialog if a gui is available, and then re-raise the exception. |
| 302 |
""" |
| 303 |
try: |
| 304 |
return SimpleConfig(config_options) |
| 305 |
except Exception as config_error: |
| 306 |
|
| 307 |
full_config_options = parse_command_line(simple_parser=False) |
| 308 |
if full_config_options.get("cmd") == 'gui': |
| 309 |
gui_name = full_config_options.get(SimpleConfig.GUI_NAME.key(), 'qt') |
| 310 |
try: |
| 311 |
gui = __import__(f'electrum.gui.{gui_name}', fromlist=['electrum']) |
| 312 |
gui.standalone_exception_dialog(config_error) |
| 313 |
except Exception as e: |
| 314 |
print_stderr(f"Error showing standalone gui dialog: {e}") |
| 315 |
raise |
| 316 |
|
| 317 |
|
| 318 |
def parse_command_line(simple_parser=False) -> Dict: |
| 319 |
|
| 320 |
if simple_parser: |
| 321 |
parser = get_simple_parser() |
| 322 |
options, args = parser.parse_args() |
| 323 |
config_options = options.__dict__ |
| 324 |
config_options['cmd'] = 'gui' |
| 325 |
else: |
| 326 |
parser = get_parser() |
| 327 |
args = parser.parse_args() |
| 328 |
config_options = args.__dict__ |
| 329 |
f = lambda key: config_options[key] is not None and key not in config_variables.get(args.cmd, {}).keys() |
| 330 |
config_options = {key: config_options[key] for key in filter(f, config_options.keys())} |
| 331 |
if config_options.get(SimpleConfig.NETWORK_SERVER.key()): |
| 332 |
config_options[SimpleConfig.NETWORK_AUTO_CONNECT.key()] = False |
| 333 |
if config_options.get(SimpleConfig.NETWORK_PROXY.key()): |
| 334 |
config_options[SimpleConfig.NETWORK_PROXY_ENABLED.key()] = True |
| 335 |
|
| 336 |
config_options['cwd'] = cwd = os.getcwd() |
| 337 |
|
| 338 |
|
| 339 |
if is_pyinstaller and os.path.exists(os.path.join(sys._MEIPASS, 'is_portable')): |
| 340 |
config_options['portable'] = True |
| 341 |
|
| 342 |
if config_options.get('portable'): |
| 343 |
if is_local: |
| 344 |
|
| 345 |
datadir = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'electrum_data') |
| 346 |
else: |
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
datadir = os.path.join(os.path.realpath(cwd), 'electrum_data') |
| 354 |
config_options['electrum_path'] = datadir |
| 355 |
|
| 356 |
if not config_options.get('verbosity'): |
| 357 |
warnings.simplefilter('ignore', DeprecationWarning) |
| 358 |
return config_options |
| 359 |
|
| 360 |
|
| 361 |
def main(): |
| 362 |
global loop, stop_loop, loop_thread |
| 363 |
|
| 364 |
util.setup_thread_excepthook() |
| 365 |
|
| 366 |
sys.argv = list(filter(lambda x: not x.startswith('-psn'), sys.argv)) |
| 367 |
|
| 368 |
|
| 369 |
if len(sys.argv) > 1 and sys.argv[1] == 'help': |
| 370 |
sys.argv.remove('help') |
| 371 |
sys.argv.append('-h') |
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
try: |
| 378 |
i = sys.argv.index('-v') |
| 379 |
except ValueError: |
| 380 |
pass |
| 381 |
else: |
| 382 |
sys.argv[i] = '-v*' |
| 383 |
|
| 384 |
|
| 385 |
for i, arg in enumerate(sys.argv): |
| 386 |
if arg == '-': |
| 387 |
if not sys.stdin.isatty(): |
| 388 |
sys.argv[i] = sys.stdin.read() |
| 389 |
break |
| 390 |
else: |
| 391 |
raise Exception('Cannot get argument from stdin') |
| 392 |
elif arg == '?': |
| 393 |
sys.argv[i] = input("Enter argument:") |
| 394 |
elif arg == ':': |
| 395 |
sys.argv[i] = prompt_password('Enter argument (will not echo):', confirm=False) |
| 396 |
|
| 397 |
|
| 398 |
if is_android: |
| 399 |
import importlib.util |
| 400 |
config_options = { |
| 401 |
'verbosity': '*' if util.is_android_debug_apk() else '', |
| 402 |
'cmd': 'gui', |
| 403 |
SimpleConfig.GUI_NAME.key(): 'qml', |
| 404 |
SimpleConfig.WALLET_SHOULD_USE_SINGLE_PASSWORD.key(): True, |
| 405 |
SimpleConfig.DISABLE_MEMORY_HARDENING_LINUX.key(): True, |
| 406 |
} |
| 407 |
SimpleConfig.set_chain_config_opt_based_on_android_packagename(config_options) |
| 408 |
else: |
| 409 |
|
| 410 |
saved_sys_argv = sys.argv[:] |
| 411 |
|
| 412 |
for x in sys.argv: |
| 413 |
if x in ['-h', '--help']: |
| 414 |
sys.argv.remove(x) |
| 415 |
|
| 416 |
config_options = parse_command_line(simple_parser=True) |
| 417 |
tmp_config = read_config(config_options) |
| 418 |
|
| 419 |
_plugin_commands = Plugins(tmp_config, cmd_only=True) |
| 420 |
|
| 421 |
sys.argv = saved_sys_argv[:] |
| 422 |
config_options = parse_command_line() |
| 423 |
|
| 424 |
config = read_config(config_options) |
| 425 |
cmdname = config.get('cmd') |
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
if cmdname == 'gui': |
| 436 |
gui_name = config.GUI_NAME |
| 437 |
lang = config.LOCALIZATION_LANGUAGE |
| 438 |
if not lang: |
| 439 |
try: |
| 440 |
from electrum.gui.default_lang import get_default_language |
| 441 |
lang = get_default_language(gui_name=gui_name) |
| 442 |
_logger.info(f"get_default_language: detected default as {lang=!r}") |
| 443 |
except ImportError as e: |
| 444 |
_logger.info(f"get_default_language: failed. got exc={e!r}") |
| 445 |
set_language(lang) |
| 446 |
|
| 447 |
chain = config.get_selected_chain() |
| 448 |
chain.set_as_network() |
| 449 |
|
| 450 |
|
| 451 |
uri = config_options.get('url') |
| 452 |
if uri and not PaymentIdentifier(None, uri).is_valid(): |
| 453 |
print_stderr('unknown command:', uri) |
| 454 |
sys.exit(1) |
| 455 |
|
| 456 |
if sys.platform == "linux" and not config.DISABLE_MEMORY_HARDENING_LINUX: |
| 457 |
import electrum.harden_memory_linux |
| 458 |
electrum.harden_memory_linux.set_dumpable_safe(False) |
| 459 |
|
| 460 |
if cmdname == 'daemon' and config.get("detach"): |
| 461 |
|
| 462 |
|
| 463 |
lockfile = daemon.get_lockfile(config) |
| 464 |
if os.path.exists(lockfile): |
| 465 |
print_stderr("Daemon already running (lockfile detected).") |
| 466 |
print_stderr("Run 'electrum stop' to stop the daemon.") |
| 467 |
sys.exit(1) |
| 468 |
|
| 469 |
|
| 470 |
daemon.get_rpc_credentials(config) |
| 471 |
|
| 472 |
try: |
| 473 |
pid = os.fork() |
| 474 |
except AttributeError as e: |
| 475 |
print_stderr(f"Error: {e!r}") |
| 476 |
print_stderr("Running daemon in detached mode (-d) is not supported on this platform.") |
| 477 |
print_stderr("Try running the daemon in the foreground (without -d).") |
| 478 |
sys.exit(1) |
| 479 |
if pid: |
| 480 |
print_stderr("starting daemon (PID %d)" % pid) |
| 481 |
loop, stop_loop, loop_thread = create_and_start_event_loop() |
| 482 |
ready = daemon.wait_until_daemon_becomes_ready(config=config, timeout=5) |
| 483 |
if ready: |
| 484 |
sys_exit(0) |
| 485 |
else: |
| 486 |
print_stderr("timed out waiting for daemon to get ready") |
| 487 |
sys_exit(1) |
| 488 |
else: |
| 489 |
|
| 490 |
sys.stdout.flush() |
| 491 |
sys.stderr.flush() |
| 492 |
si = open(os.devnull, 'r') |
| 493 |
so = open(os.devnull, 'w') |
| 494 |
se = open(os.devnull, 'w') |
| 495 |
os.dup2(si.fileno(), sys.stdin.fileno()) |
| 496 |
os.dup2(so.fileno(), sys.stdout.fileno()) |
| 497 |
os.dup2(se.fileno(), sys.stderr.fileno()) |
| 498 |
|
| 499 |
loop, stop_loop, loop_thread = create_and_start_event_loop() |
| 500 |
|
| 501 |
try: |
| 502 |
handle_cmd( |
| 503 |
cmdname=cmdname, |
| 504 |
config=config, |
| 505 |
config_options=config_options, |
| 506 |
) |
| 507 |
except Exception: |
| 508 |
_logger.exception("") |
| 509 |
sys_exit(1) |
| 510 |
|
| 511 |
|
| 512 |
def handle_cmd(*, cmdname: str, config: 'SimpleConfig', config_options: dict): |
| 513 |
if cmdname == 'gui': |
| 514 |
configure_logging(config) |
| 515 |
fd = daemon.get_file_descriptor(config) |
| 516 |
if fd is not None: |
| 517 |
|
| 518 |
only_minimal_jsonrpc = not constants.net.TESTNET |
| 519 |
d = daemon.Daemon(config, fd, start_network=False, only_minimal_jsonrpc=only_minimal_jsonrpc) |
| 520 |
try: |
| 521 |
d.run_gui() |
| 522 |
except BaseException as e: |
| 523 |
_logger.exception('daemon.run_gui errored') |
| 524 |
sys_exit(1) |
| 525 |
else: |
| 526 |
sys_exit(0) |
| 527 |
else: |
| 528 |
try: |
| 529 |
result = daemon.request(config, 'gui', (config_options,)) |
| 530 |
except JsonRPCError as e: |
| 531 |
if e.code == JsonRPCError.Codes.USERFACING: |
| 532 |
print_stderr(e.message) |
| 533 |
elif e.code == JsonRPCError.Codes.INTERNAL: |
| 534 |
print_stderr("(inside daemon): " + e.data["traceback"]) |
| 535 |
print_stderr(e.message) |
| 536 |
else: |
| 537 |
raise Exception(f"unknown error code {e.code}") |
| 538 |
sys_exit(1) |
| 539 |
|
| 540 |
elif cmdname == 'daemon': |
| 541 |
configure_logging(config) |
| 542 |
fd = daemon.get_file_descriptor(config) |
| 543 |
if fd is not None: |
| 544 |
|
| 545 |
d = daemon.Daemon(config, fd, only_minimal_jsonrpc=False) |
| 546 |
d.run_daemon() |
| 547 |
sys_exit(0) |
| 548 |
else: |
| 549 |
|
| 550 |
print_msg("Daemon already running") |
| 551 |
sys_exit(1) |
| 552 |
else: |
| 553 |
|
| 554 |
configure_logging(config, log_to_file=False) |
| 555 |
cmd = known_commands[cmdname] |
| 556 |
wallet_path = config.get_wallet_path() |
| 557 |
if cmd.requires_wallet and not wallet_path: |
| 558 |
print_stderr('wallet path not provided') |
| 559 |
sys_exit(1) |
| 560 |
if not config.NETWORK_OFFLINE: |
| 561 |
init_cmdline(config_options, wallet_path, rpcserver=True, config=config) |
| 562 |
timeout = config.CLI_TIMEOUT |
| 563 |
try: |
| 564 |
result = daemon.request(config, 'run_cmdline', (config_options,), timeout) |
| 565 |
except daemon.DaemonNotRunning: |
| 566 |
print_msg("Daemon not running; try 'electrum daemon -d'") |
| 567 |
if not cmd.requires_network: |
| 568 |
print_msg("To run this command without a daemon, use --offline") |
| 569 |
if cmd.name == "stop": |
| 570 |
lockfile = daemon.get_lockfile(config) |
| 571 |
if os.path.exists(lockfile): |
| 572 |
print_msg("Found lingering lockfile for daemon. Removing.") |
| 573 |
daemon.remove_lockfile(lockfile) |
| 574 |
sys_exit(1) |
| 575 |
except JsonRPCError as e: |
| 576 |
if e.code == JsonRPCError.Codes.USERFACING: |
| 577 |
print_stderr(e.message) |
| 578 |
elif e.code == JsonRPCError.Codes.INTERNAL: |
| 579 |
print_stderr("(inside daemon): " + e.data["traceback"]) |
| 580 |
print_stderr(e.message) |
| 581 |
else: |
| 582 |
raise Exception(f"unknown error code {e.code}") |
| 583 |
sys_exit(1) |
| 584 |
except Exception as e: |
| 585 |
_logger.exception("error running command (with daemon)") |
| 586 |
sys_exit(1) |
| 587 |
else: |
| 588 |
if cmd.requires_network: |
| 589 |
print_msg("This command cannot be run offline") |
| 590 |
sys_exit(1) |
| 591 |
lockfile = daemon.get_lockfile(config) |
| 592 |
if os.path.exists(lockfile): |
| 593 |
print_stderr("Daemon already running (lockfile detected)") |
| 594 |
print_stderr("Run 'electrum stop' to stop the daemon.") |
| 595 |
print_stderr("Run this command without --offline to interact with the daemon") |
| 596 |
sys_exit(1) |
| 597 |
init_cmdline(config_options, wallet_path, rpcserver=False, config=config) |
| 598 |
plugins = Plugins(config, 'cmdline') |
| 599 |
coro = run_offline_command(config, config_options, wallet_path, plugins) |
| 600 |
fut = asyncio.run_coroutine_threadsafe(coro, loop) |
| 601 |
try: |
| 602 |
try: |
| 603 |
result = fut.result() |
| 604 |
finally: |
| 605 |
plugins.stop() |
| 606 |
plugins.stopped_event.wait(1) |
| 607 |
except UserFacingException as e: |
| 608 |
print_stderr(str(e)) |
| 609 |
sys_exit(1) |
| 610 |
except InvalidPassword: |
| 611 |
print_stderr("Invalid password") |
| 612 |
sys_exit(1) |
| 613 |
except UserCancelled: |
| 614 |
print_stderr("Aborted by user") |
| 615 |
sys_exit(1) |
| 616 |
except Exception as e: |
| 617 |
_logger.exception("error running command (without daemon)") |
| 618 |
sys_exit(1) |
| 619 |
|
| 620 |
if isinstance(result, str): |
| 621 |
print_msg(result) |
| 622 |
elif result is not None: |
| 623 |
print_msg(json_encode(result)) |
| 624 |
sys_exit(0) |
| 625 |
|
| 626 |
|
| 627 |
if __name__ == '__main__': |
| 628 |
main() |