electrum-bitcoinprivate/gui/qt/installwizard.py

323 lines
8.9 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)
def restore_or_create(self):
2013-09-03 05:32:56 -07:00
grid = QGridLayout()
grid.setSpacing(5)
msg = _("Electrum could not find an existing wallet.")+"\n\n"+_("Did you use Electrum before and want to restore a previous wallet or is this your first time and do you want to create a new wallet?")+"\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)
b2.setText(_("Restore wallet from seed"))
b3 = QRadioButton(gb)
b3.setText(_("Restore wallet from master public key"))
grid.addWidget(b1,1,0)
grid.addWidget(b2,2,0)
grid.addWidget(b3,3,0)
2013-09-12 10:42:00 -07:00
vbox = QVBoxLayout(self)
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')))
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-09-12 10:42:00 -07:00
if self.layout(): QWidget().setLayout(self.layout())
vbox = QVBoxLayout(self)
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-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'))
if self.layout(): QWidget().setLayout(self.layout())
vbox = QVBoxLayout(self)
self.waiting_label = QLabel(msg)
vbox.addWidget(self.waiting_label)
t = threading.Thread(target = target)
t.start()
self.exec_()
2013-09-03 09:35:46 -07:00
def mpk_dialog(self):
2013-09-12 10:42:00 -07:00
if self.layout(): QWidget().setLayout(self.layout())
vbox = QVBoxLayout(self)
2013-09-03 09:35:46 -07:00
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)
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-10-01 05:28:43 -07:00
if not self.exec_(): return None, 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
2013-09-12 10:42:00 -07:00
if self.layout(): QWidget().setLayout(self.layout())
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-09-12 10:42:00 -07:00
vbox = QVBoxLayout(self)
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')))
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
def show_seed(self, wallet):
2013-09-12 10:42:00 -07:00
from seed_dialog import make_seed_dialog
2013-10-03 00:19:09 -07:00
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-09-12 10:42:00 -07:00
if self.layout(): QWidget().setLayout(self.layout())
2013-10-03 00:19:09 -07:00
self.setLayout(vbox)
if not self.exec_():
exit()
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
if self.layout(): QWidget().setLayout(self.layout())
make_password_dialog(self, wallet, msg)
run_password_dialog(self, wallet, self)
def run(self):
2013-09-03 01:58:07 -07:00
action = self.restore_or_create()
if not action: exit()
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)
self.show_seed(wallet)
if self.verify_seed(wallet):
2013-09-12 10:42:00 -07:00
def create():
wallet.save_seed()
wallet.create_accounts()
wallet.synchronize() # generate first addresses offline
self.waiting_dialog(create)
else:
2013-09-03 01:58:07 -07:00
return
elif action == 'restore':
# ask for seed and gap.
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)
except:
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
2013-09-03 05:32:56 -07:00
wallet.save_seed()
2013-09-03 01:58:07 -07:00
2013-09-03 05:32:56 -07:00
elif action == 'watching':
# ask for seed and gap.
2013-10-01 05:28:43 -07:00
K, chain = self.mpk_dialog()
2013-10-15 01:44:14 -07:00
if not K or not chain:
2013-09-03 05:32:56 -07:00
return
wallet.seed = ''
2013-10-01 05:28:43 -07:00
wallet.create_watching_only_wallet(chain,K)
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'):
self.network_dialog()
# 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
if wallet.is_found():
QMessageBox.information(None, _('Information'), _("Recovery successful"), _('OK'))
else:
QMessageBox.information(None, _('Information'), _("No transactions found for this seed"), _('OK'))
wallet.fill_addressbook()
self.password_dialog(wallet)
2013-09-12 10:42:00 -07:00
2013-09-03 01:09:13 -07:00
return wallet