Browse Source

wizard: some fixes

related: #5174
sqlite_db
SomberNight 6 years ago
parent
commit
2da6692f73
No known key found for this signature in database GPG Key ID: B33B5F232C6271E9
  1. 9
      electrum/base_wizard.py
  2. 84
      electrum/gui/qt/__init__.py
  3. 23
      electrum/gui/qt/installwizard.py
  4. 5
      electrum/storage.py

9
electrum/base_wizard.py

@ -299,7 +299,8 @@ class BaseWizard(object):
_('Debug message') + '\n', _('Debug message') + '\n',
debug_msg debug_msg
]) ])
self.confirm_dialog(title=title, message=msg, run_next= lambda x: self.choose_hw_device(purpose)) self.confirm_dialog(title=title, message=msg,
run_next=lambda x: self.choose_hw_device(purpose, storage=storage))
return return
# select device # select device
self.devices = devices self.devices = devices
@ -326,15 +327,15 @@ class BaseWizard(object):
+ _('Please try again.')) + _('Please try again.'))
devmgr = self.plugins.device_manager devmgr = self.plugins.device_manager
devmgr.unpair_id(device_info.device.id_) devmgr.unpair_id(device_info.device.id_)
self.choose_hw_device(purpose) self.choose_hw_device(purpose, storage=storage)
return return
except (UserCancelled, GoBack): except (UserCancelled, GoBack):
self.choose_hw_device(purpose) self.choose_hw_device(purpose, storage=storage)
return return
except BaseException as e: except BaseException as e:
traceback.print_exc(file=sys.stderr) traceback.print_exc(file=sys.stderr)
self.show_error(str(e)) self.show_error(str(e))
self.choose_hw_device(purpose) self.choose_hw_device(purpose, storage=storage)
return return
if purpose == HWD_SETUP_NEW_WALLET: if purpose == HWD_SETUP_NEW_WALLET:
def f(derivation, script_type): def f(derivation, script_type):

84
electrum/gui/qt/__init__.py

@ -28,6 +28,7 @@ import signal
import sys import sys
import traceback import traceback
import threading import threading
from typing import Optional
try: try:
@ -46,9 +47,9 @@ from electrum.plugin import run_hook
from electrum.base_wizard import GoBack from electrum.base_wizard import GoBack
from electrum.util import (UserCancelled, PrintError, profiler, from electrum.util import (UserCancelled, PrintError, profiler,
WalletFileException, BitcoinException, get_new_wallet_name) WalletFileException, BitcoinException, get_new_wallet_name)
from electrum.wallet import Wallet from electrum.wallet import Wallet, Abstract_Wallet
from .installwizard import InstallWizard from .installwizard import InstallWizard, WalletAlreadyOpenInMemory
from .util import get_default_language, read_QIcon, ColorScheme from .util import get_default_language, read_QIcon, ColorScheme
@ -191,7 +192,7 @@ class ElectrumGui(PrintError):
self.network_updated_signal_obj) self.network_updated_signal_obj)
self.nd.show() self.nd.show()
def create_window_for_wallet(self, wallet): def _create_window_for_wallet(self, wallet):
w = ElectrumWindow(self, wallet) w = ElectrumWindow(self, wallet)
self.windows.append(w) self.windows.append(w)
self.build_tray_menu() self.build_tray_menu()
@ -212,9 +213,10 @@ class ElectrumGui(PrintError):
return wrapper return wrapper
@count_wizards_in_progress @count_wizards_in_progress
def start_new_window(self, path, uri, app_is_starting=False): def start_new_window(self, path, uri, *, app_is_starting=False):
'''Raises the window for the wallet if it is open. Otherwise '''Raises the window for the wallet if it is open. Otherwise
opens the wallet and creates a new window for it''' opens the wallet and creates a new window for it'''
wallet = None
try: try:
wallet = self.daemon.load_wallet(path, None) wallet = self.daemon.load_wallet(path, None)
except BaseException as e: except BaseException as e:
@ -222,49 +224,20 @@ class ElectrumGui(PrintError):
d = QMessageBox(QMessageBox.Warning, _('Error'), d = QMessageBox(QMessageBox.Warning, _('Error'),
_('Cannot load wallet') + ' (1):\n' + str(e)) _('Cannot load wallet') + ' (1):\n' + str(e))
d.exec_() d.exec_()
if app_is_starting: # if app is starting, still let wizard to appear
# do not return so that the wizard can appear if not app_is_starting:
wallet = None
else:
return return
if not wallet: if not wallet:
wizard = InstallWizard(self.config, self.app, self.plugins) wallet = self._start_wizard_to_select_or_create_wallet(path)
storage = None if not wallet:
try: return
path, storage = wizard.select_storage(path, self.daemon.get_wallet) # create or raise window
# storage is None if file does not exist
if storage is None:
wizard.path = path # needed by trustedcoin plugin
wizard.run('new')
storage = wizard.create_storage(path)
else:
wizard.run_upgrades(storage)
except UserCancelled:
return
except GoBack as e:
self.print_error('[start_new_window] Exception caught (GoBack)', e)
except (WalletFileException, BitcoinException) as e:
traceback.print_exc(file=sys.stderr)
d = QMessageBox(QMessageBox.Warning, _('Error'),
_('Cannot load wallet') + ' (2):\n' + str(e))
d.exec_()
return
finally:
wizard.terminate()
# return if wallet creation is not complete
if storage is None or storage.get_action():
return
wallet = Wallet(storage)
if not self.daemon.get_wallet(wallet.storage.path):
# wallet was not in memory
wallet.start_network(self.daemon.network)
self.daemon.add_wallet(wallet)
try: try:
for w in self.windows: for w in self.windows:
if w.wallet.storage.path == wallet.storage.path: if w.wallet.storage.path == wallet.storage.path:
break break
else: else:
w = self.create_window_for_wallet(wallet) w = self._create_window_for_wallet(wallet)
except BaseException as e: except BaseException as e:
traceback.print_exc(file=sys.stdout) traceback.print_exc(file=sys.stdout)
d = QMessageBox(QMessageBox.Warning, _('Error'), d = QMessageBox(QMessageBox.Warning, _('Error'),
@ -284,6 +257,37 @@ class ElectrumGui(PrintError):
w.activateWindow() w.activateWindow()
return w return w
def _start_wizard_to_select_or_create_wallet(self, path) -> Optional[Abstract_Wallet]:
wizard = InstallWizard(self.config, self.app, self.plugins)
try:
path, storage = wizard.select_storage(path, self.daemon.get_wallet)
# storage is None if file does not exist
if storage is None:
wizard.path = path # needed by trustedcoin plugin
wizard.run('new')
storage = wizard.create_storage(path)
else:
wizard.run_upgrades(storage)
except (UserCancelled, GoBack):
return
except WalletAlreadyOpenInMemory as e:
return e.wallet
except (WalletFileException, BitcoinException) as e:
traceback.print_exc(file=sys.stderr)
d = QMessageBox(QMessageBox.Warning, _('Error'),
_('Cannot load wallet') + ' (2):\n' + str(e))
d.exec_()
return
finally:
wizard.terminate()
# return if wallet creation is not complete
if storage is None or storage.get_action():
return
wallet = Wallet(storage)
wallet.start_network(self.daemon.network)
self.daemon.add_wallet(wallet)
return wallet
def close_window(self, window): def close_window(self, window):
if window in self.windows: if window in self.windows:
self.windows.remove(window) self.windows.remove(window)

23
electrum/gui/qt/installwizard.py

@ -6,7 +6,7 @@ import os
import sys import sys
import threading import threading
import traceback import traceback
from typing import Tuple, List, Callable from typing import Tuple, List, Callable, NamedTuple, Optional
from PyQt5.QtCore import QRect, QEventLoop, Qt, pyqtSignal from PyQt5.QtCore import QRect, QEventLoop, Qt, pyqtSignal
from PyQt5.QtGui import QPalette, QPen, QPainter, QPixmap from PyQt5.QtGui import QPalette, QPen, QPainter, QPixmap
@ -14,7 +14,7 @@ from PyQt5.QtWidgets import (QWidget, QDialog, QLabel, QHBoxLayout, QMessageBox,
QVBoxLayout, QLineEdit, QFileDialog, QPushButton, QVBoxLayout, QLineEdit, QFileDialog, QPushButton,
QGridLayout, QSlider, QScrollArea) QGridLayout, QSlider, QScrollArea)
from electrum.wallet import Wallet from electrum.wallet import Wallet, Abstract_Wallet
from electrum.storage import WalletStorage from electrum.storage import WalletStorage
from electrum.util import UserCancelled, InvalidPassword, WalletFileException from electrum.util import UserCancelled, InvalidPassword, WalletFileException
from electrum.base_wizard import BaseWizard, HWD_SETUP_DECRYPT_WALLET, GoBack from electrum.base_wizard import BaseWizard, HWD_SETUP_DECRYPT_WALLET, GoBack
@ -104,6 +104,11 @@ def wizard_dialog(func):
return func_wrapper return func_wrapper
class WalletAlreadyOpenInMemory(Exception):
def __init__(self, wallet: Abstract_Wallet):
super().__init__()
self.wallet = wallet
# WindowModalDialog must come first as it overrides show_error # WindowModalDialog must come first as it overrides show_error
class InstallWizard(QDialog, MessageBoxMixin, BaseWizard): class InstallWizard(QDialog, MessageBoxMixin, BaseWizard):
@ -162,7 +167,7 @@ class InstallWizard(QDialog, MessageBoxMixin, BaseWizard):
self.raise_() self.raise_()
self.refresh_gui() # Need for QT on MacOSX. Lame. self.refresh_gui() # Need for QT on MacOSX. Lame.
def select_storage(self, path, get_wallet_from_daemon): def select_storage(self, path, get_wallet_from_daemon) -> Tuple[str, Optional[WalletStorage]]:
vbox = QVBoxLayout() vbox = QVBoxLayout()
hbox = QHBoxLayout() hbox = QHBoxLayout()
@ -254,7 +259,7 @@ class InstallWizard(QDialog, MessageBoxMixin, BaseWizard):
break break
wallet_from_memory = get_wallet_from_daemon(self.temp_storage.path) wallet_from_memory = get_wallet_from_daemon(self.temp_storage.path)
if wallet_from_memory: if wallet_from_memory:
return wallet_from_memory raise WalletAlreadyOpenInMemory(wallet_from_memory)
if self.temp_storage.file_exists() and self.temp_storage.is_encrypted(): if self.temp_storage.file_exists() and self.temp_storage.is_encrypted():
if self.temp_storage.is_encrypted_with_user_pw(): if self.temp_storage.is_encrypted_with_user_pw():
password = self.pw_e.text() password = self.pw_e.text()
@ -267,7 +272,7 @@ class InstallWizard(QDialog, MessageBoxMixin, BaseWizard):
except BaseException as e: except BaseException as e:
traceback.print_exc(file=sys.stdout) traceback.print_exc(file=sys.stdout)
QMessageBox.information(None, _('Error'), str(e)) QMessageBox.information(None, _('Error'), str(e))
return raise UserCancelled()
elif self.temp_storage.is_encrypted_with_hw_device(): elif self.temp_storage.is_encrypted_with_hw_device():
try: try:
self.run('choose_hw_device', HWD_SETUP_DECRYPT_WALLET, storage=self.temp_storage) self.run('choose_hw_device', HWD_SETUP_DECRYPT_WALLET, storage=self.temp_storage)
@ -281,11 +286,9 @@ class InstallWizard(QDialog, MessageBoxMixin, BaseWizard):
except BaseException as e: except BaseException as e:
traceback.print_exc(file=sys.stdout) traceback.print_exc(file=sys.stdout)
QMessageBox.information(None, _('Error'), str(e)) QMessageBox.information(None, _('Error'), str(e))
return raise UserCancelled()
if self.temp_storage.is_past_initial_decryption(): assert self.temp_storage.is_past_initial_decryption()
break break
else:
return
else: else:
raise Exception('Unexpected encryption version') raise Exception('Unexpected encryption version')

5
electrum/storage.py

@ -119,7 +119,10 @@ class WalletStorage(PrintError):
if encryption is disabled completely (self.is_encrypted() == False), if encryption is disabled completely (self.is_encrypted() == False),
or if encryption is enabled but the contents have already been decrypted. or if encryption is enabled but the contents have already been decrypted.
""" """
return bool(self.db.data) try:
return bool(self.db.data)
except AttributeError:
return False
def is_encrypted(self): def is_encrypted(self):
"""Return if storage encryption is currently enabled.""" """Return if storage encryption is currently enabled."""

Loading…
Cancel
Save