electrum-bitcoinprivate/gui/qt/installwizard.py

329 lines
9.2 KiB
Python
Raw Normal View History

from PyQt4.QtGui import *
from PyQt4.QtCore import *
import PyQt4.QtCore as QtCore
2013-09-11 04:55:49 -07:00
from electrum.i18n import _
2013-09-01 09:58:09 -07:00
from electrum import Wallet, mnemonic
2013-08-29 07:07:55 -07:00
from seed_dialog import SeedDialog
from network_dialog import NetworkDialog
2013-09-24 07:57:12 -07:00
from util import *
2013-08-29 07:07:55 -07:00
from amountedit import AmountEdit
import sys
2013-09-12 10:42:00 -07:00
import threading
class InstallWizard(QDialog):
2013-09-10 05:20:44 -07:00
def __init__(self, config, network, storage):
QDialog.__init__(self)
self.config = config
2013-09-10 05:20:44 -07:00
self.network = network
self.storage = storage
2013-09-12 10:42:00 -07:00
self.setMinimumSize(575, 400)
self.setWindowTitle('Electrum')
self.connect(self, QtCore.SIGNAL('accept'), self.accept)
2013-11-03 02:03:45 -08:00
self.stack = QStackedLayout()
self.setLayout(self.stack)
def set_layout(self, layout):
w = QWidget()
w.setLayout(layout)
self.stack.setCurrentIndex(self.stack.addWidget(w))
def restore_or_create(self):
2013-09-03 05:32:56 -07:00
grid = QGridLayout()
grid.setSpacing(5)
2013-12-16 22:06:58 -08:00
msg = _("Electrum could not find an existing wallet.") + "\n\n" \
+ _("What do you want to do?") + "\n"
2013-09-03 05:32:56 -07:00
label = QLabel(msg)
label.setWordWrap(True)
grid.addWidget(label, 0, 0)
gb = QGroupBox()
b1 = QRadioButton(gb)
b1.setText(_("Create new wallet"))
b1.setChecked(True)
b2 = QRadioButton(gb)
2013-12-16 22:06:58 -08:00
b2.setText(_("Restore an existing wallet from its seed"))
2013-09-03 05:32:56 -07:00
b3 = QRadioButton(gb)
2013-12-16 21:56:33 -08:00
b3.setText(_("Create a watching-only version of an existing wallet"))
2013-09-03 05:32:56 -07:00
grid.addWidget(b1,1,0)
grid.addWidget(b2,2,0)
grid.addWidget(b3,3,0)
2013-11-03 02:03:45 -08:00
vbox = QVBoxLayout()
self.set_layout(vbox)
2013-09-03 05:32:56 -07:00
2013-11-03 02:03:45 -08:00
vbox.addLayout(grid)
2013-09-12 10:42:00 -07:00
vbox.addStretch(1)
vbox.addLayout(ok_cancel_buttons(self, _('Next')))
if not self.exec_():
2013-09-03 05:32:56 -07:00
return
if b1.isChecked():
2013-09-12 10:42:00 -07:00
answer = 'create'
2013-09-03 05:32:56 -07:00
elif b2.isChecked():
2013-09-12 10:42:00 -07:00
answer = 'restore'
2013-09-03 05:32:56 -07:00
else:
2013-09-12 10:42:00 -07:00
answer = 'watching'
2013-09-03 05:32:56 -07:00
2013-09-12 10:42:00 -07:00
return answer
def verify_seed(self, wallet):
r = self.seed_dialog(False)
2013-09-03 05:32:56 -07:00
if not r:
return
2013-10-31 05:28:52 -07:00
if r != wallet.get_mnemonic(None):
QMessageBox.warning(None, _('Error'), _('Incorrect seed'), _('OK'))
return False
else:
return True
def seed_dialog(self, is_restore=True):
2013-11-03 02:03:45 -08:00
vbox = QVBoxLayout()
if is_restore:
2013-09-30 08:40:05 -07:00
msg = _("Please enter your wallet seed.") + "\n"
else:
2013-09-12 10:49:51 -07:00
msg = _("Your seed is important!") \
2013-10-26 02:54:11 -07:00
+ "\n" + _("To make sure that you have properly saved your seed, please retype it here.")
2013-09-12 10:42:00 -07:00
logo = QLabel()
logo.setPixmap(QPixmap(":icons/seed.png").scaledToWidth(56))
logo.setMaximumWidth(60)
2013-09-12 10:49:51 -07:00
2013-09-12 10:42:00 -07:00
label = QLabel(msg)
label.setWordWrap(True)
2013-09-12 10:42:00 -07:00
2013-09-12 10:49:51 -07:00
seed_e = QTextEdit()
seed_e.setMaximumHeight(100)
vbox.addWidget(label)
2013-09-12 10:42:00 -07:00
grid = QGridLayout()
grid.addWidget(logo, 0, 0)
2013-09-12 10:49:51 -07:00
grid.addWidget(seed_e, 0, 1)
2013-09-12 10:42:00 -07:00
vbox.addLayout(grid)
2013-09-12 10:42:00 -07:00
vbox.addStretch(1)
vbox.addLayout(ok_cancel_buttons(self, _('Next')))
2013-11-03 02:03:45 -08:00
self.set_layout(vbox)
2013-09-12 10:42:00 -07:00
if not self.exec_():
return
2013-10-26 02:54:11 -07:00
seed = unicode(seed_e.toPlainText())
if not seed:
QMessageBox.warning(None, _('Error'), _('No seed'), _('OK'))
return
2013-09-12 07:08:17 -07:00
return seed
def waiting_dialog(self, task, msg= _("Electrum is generating your addresses, please wait.")):
2013-09-12 10:42:00 -07:00
def target():
task()
self.emit(QtCore.SIGNAL('accept'))
2013-11-03 02:03:45 -08:00
vbox = QVBoxLayout()
2013-09-12 10:42:00 -07:00
self.waiting_label = QLabel(msg)
vbox.addWidget(self.waiting_label)
2013-11-03 02:03:45 -08:00
self.set_layout(vbox)
2013-09-12 10:42:00 -07:00
t = threading.Thread(target = target)
t.start()
self.exec_()
2013-09-03 09:35:46 -07:00
def mpk_dialog(self):
2013-11-03 02:03:45 -08:00
vbox = QVBoxLayout()
2013-10-01 05:28:43 -07:00
vbox.addWidget(QLabel(_("Please enter your master public key.")))
2013-09-03 09:35:46 -07:00
2013-10-01 05:28:43 -07:00
grid = QGridLayout()
grid.setSpacing(8)
label = QLabel(_("Key"))
grid.addWidget(label, 0, 0)
2013-09-03 09:35:46 -07:00
mpk_e = QTextEdit()
mpk_e.setMaximumHeight(100)
2013-10-01 05:28:43 -07:00
grid.addWidget(mpk_e, 0, 1)
label = QLabel(_("Chain"))
#grid.addWidget(label, 1, 0)
2013-10-01 05:28:43 -07:00
chain_e = QTextEdit()
chain_e.setMaximumHeight(100)
#grid.addWidget(chain_e, 1, 1)
2013-09-03 09:35:46 -07:00
vbox.addLayout(grid)
2013-09-12 10:42:00 -07:00
vbox.addStretch(1)
vbox.addLayout(ok_cancel_buttons(self, _('Next')))
2013-09-03 09:35:46 -07:00
2013-11-03 02:03:45 -08:00
self.set_layout(vbox)
if not self.exec_():
return None
2013-09-03 09:35:46 -07:00
2013-10-01 05:28:43 -07:00
mpk = str(mpk_e.toPlainText()).strip()
chain = str(chain_e.toPlainText()).strip()
return mpk, chain
2013-09-03 09:35:46 -07:00
def network_dialog(self):
2013-09-03 05:32:56 -07:00
grid = QGridLayout()
grid.setSpacing(5)
label = QLabel(_("Electrum communicates with remote servers to get information about your transactions and addresses. The servers all fulfil the same purpose only differing in hardware. In most cases you simply want to let Electrum pick one at random if you have a preference though feel free to select a server manually.") + "\n\n" \
+ _("How do you want to connect to a server:")+" ")
label.setWordWrap(True)
2013-09-03 05:32:56 -07:00
grid.addWidget(label, 0, 0)
gb = QGroupBox()
b1 = QRadioButton(gb)
b1.setText(_("Auto connect"))
b1.setChecked(True)
b2 = QRadioButton(gb)
b2.setText(_("Select server manually"))
#b3 = QRadioButton(gb)
#b3.setText(_("Stay offline"))
2013-09-03 05:32:56 -07:00
grid.addWidget(b1,1,0)
grid.addWidget(b2,2,0)
#grid.addWidget(b3,3,0)
2013-09-03 05:32:56 -07:00
2013-11-03 02:03:45 -08:00
vbox = QVBoxLayout()
2013-09-03 05:32:56 -07:00
vbox.addLayout(grid)
2013-09-12 10:42:00 -07:00
vbox.addStretch(1)
vbox.addLayout(ok_cancel_buttons(self, _('Next')))
2013-11-03 02:03:45 -08:00
self.set_layout(vbox)
2013-09-12 10:42:00 -07:00
if not self.exec_():
2013-09-03 05:32:56 -07:00
return
if b2.isChecked():
return NetworkDialog(self.network, self.config, None).do_exec()
2013-09-03 05:32:56 -07:00
elif b1.isChecked():
self.config.set_key('auto_cycle', True, True)
return
else:
self.config.set_key("server", None, True)
self.config.set_key('auto_cycle', False, True)
return
2013-11-03 02:03:45 -08:00
def show_seed(self, wallet):
2013-09-12 10:42:00 -07:00
from seed_dialog import make_seed_dialog
2013-10-31 05:28:52 -07:00
vbox = make_seed_dialog(wallet.get_mnemonic(None), wallet.imported_keys)
2013-10-03 00:19:09 -07:00
vbox.addLayout(ok_cancel_buttons(self, _("Next")))
2013-11-03 02:03:45 -08:00
self.set_layout(vbox)
2013-11-12 13:55:42 -08:00
return self.exec_()
def password_dialog(self, wallet):
2013-09-12 10:42:00 -07:00
msg = _("Please choose a password to encrypt your wallet keys.")+'\n'\
+_("Leave these fields empty if you want to disable encryption.")
from password_dialog import make_password_dialog, run_password_dialog
2013-11-03 02:03:45 -08:00
self.set_layout( make_password_dialog(self, wallet, msg) )
return run_password_dialog(self, wallet, self)
def run(self):
2013-09-03 01:58:07 -07:00
action = self.restore_or_create()
2013-11-12 13:55:42 -08:00
if not action:
return
2013-09-03 01:58:07 -07:00
wallet = Wallet(self.storage)
2013-09-12 10:42:00 -07:00
gap = self.config.get('gap_limit', 5)
if gap != 5:
2013-09-12 07:08:17 -07:00
wallet.gap_limit = gap
2013-09-12 10:42:00 -07:00
wallet.storage.put('gap_limit', gap, True)
2013-09-03 05:32:56 -07:00
if action == 'create':
wallet.init_seed(None)
2013-11-12 13:55:42 -08:00
if not self.show_seed(wallet):
return
if not self.verify_seed(wallet):
2013-09-03 01:58:07 -07:00
return
ok, old_password, password = self.password_dialog(wallet)
2013-11-12 13:55:42 -08:00
def create():
wallet.save_seed(password)
2013-11-12 13:55:42 -08:00
wallet.synchronize() # generate first addresses offline
self.waiting_dialog(create)
2013-09-03 01:58:07 -07:00
elif action == 'restore':
2013-09-12 07:08:17 -07:00
seed = self.seed_dialog()
2013-09-03 01:58:07 -07:00
if not seed:
return
2013-10-26 02:54:11 -07:00
try:
wallet.init_seed(seed)
2013-11-10 12:30:57 -08:00
except Exception:
2013-10-31 07:40:10 -07:00
import traceback
traceback.print_exc(file=sys.stdout)
2013-10-26 02:54:11 -07:00
QMessageBox.warning(None, _('Error'), _('Incorrect seed'), _('OK'))
return
ok, old_password, password = self.password_dialog(wallet)
wallet.save_seed(password)
2013-09-03 01:58:07 -07:00
2013-09-03 05:32:56 -07:00
elif action == 'watching':
mpk = self.mpk_dialog()
if not mpk:
2013-09-03 05:32:56 -07:00
return
wallet.seed = ''
wallet.create_watching_only_wallet(mpk)
2013-09-03 09:35:46 -07:00
2013-09-03 05:32:56 -07:00
else: raise
2013-09-03 01:58:07 -07:00
#if not self.config.get('server'):
2013-11-05 09:55:53 -08:00
if self.network:
if self.network.interfaces:
self.network_dialog()
else:
QMessageBox.information(None, _('Warning'), _('You are offline'), _('OK'))
self.network.stop()
# start wallet threads
2013-09-10 05:20:44 -07:00
wallet.start_threads(self.network)
2013-09-03 05:32:56 -07:00
if action == 'restore':
self.waiting_dialog(lambda: wallet.restore(self.waiting_label.setText))
2013-09-12 10:42:00 -07:00
2013-11-05 09:55:53 -08:00
if self.network:
if wallet.is_found():
QMessageBox.information(None, _('Information'), _("Recovery successful"), _('OK'))
else:
QMessageBox.information(None, _('Information'), _("No transactions found for this seed"), _('OK'))
2013-09-12 10:42:00 -07:00
else:
2013-11-05 09:55:53 -08:00
QMessageBox.information(None, _('Information'), _("This wallet was restored offline. It may contain more addresses than displayed."), _('OK'))
2013-09-03 01:09:13 -07:00
return wallet