installwizard: multisig wallets
[electrum-nvc.git] / gui / qt / installwizard.py
index 342279a..b0180ca 100644 (file)
@@ -3,15 +3,16 @@ from PyQt4.QtCore import *
 import PyQt4.QtCore as QtCore
 
 from electrum.i18n import _
-from electrum import Wallet, mnemonic
+from electrum import Wallet, Wallet_2of3
 
-from seed_dialog import SeedDialog
+import seed_dialog
 from network_dialog import NetworkDialog
 from util import *
 from amountedit import AmountEdit
 
 import sys
 import threading
+from electrum.plugins import run_hook
 
 class InstallWizard(QDialog):
 
@@ -24,13 +25,23 @@ class InstallWizard(QDialog):
         self.setWindowTitle('Electrum')
         self.connect(self, QtCore.SIGNAL('accept'), self.accept)
 
+        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):
 
         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"
+        msg = _("Electrum could not find an existing wallet.") + "\n\n" \
+            + _("What do you want to do?") + "\n"
         label = QLabel(msg)
         label.setWordWrap(True)
         grid.addWidget(label, 0, 0)
@@ -42,89 +53,77 @@ class InstallWizard(QDialog):
         b1.setChecked(True)
 
         b2 = QRadioButton(gb)
-        b2.setText(_("Restore wallet from seed"))
-
-        b3 = QRadioButton(gb)
-        b3.setText(_("Restore wallet from master public key"))
+        b2.setText(_("Restore an existing wallet"))
 
         grid.addWidget(b1,1,0)
         grid.addWidget(b2,2,0)
-        grid.addWidget(b3,3,0)
 
-        vbox = QVBoxLayout(self)
-        vbox.addLayout(grid)
+        vbox = QVBoxLayout()
+        self.set_layout(vbox)
 
+        vbox.addLayout(grid)
         vbox.addStretch(1)
         vbox.addLayout(ok_cancel_buttons(self, _('Next')))
 
         if not self.exec_():
             return
         
-        if b1.isChecked():
-            answer = 'create'
-        elif b2.isChecked():
-            answer = 'restore'
-        else:
-            answer = 'watching'
+        return 'create' if b1.isChecked() else 'restore'
 
-        return answer
 
 
-    def verify_seed(self, wallet):
-        r = self.seed_dialog(False)
+    def verify_seed(self, seed, sid):
+        r = self.enter_seed_dialog(False, sid)
         if not r:
             return
 
-        if r != wallet.seed:
+        if r != seed:
             QMessageBox.warning(None, _('Error'), _('Incorrect seed'), _('OK'))
             return False
         else:
             return True
 
 
-    def seed_dialog(self, is_restore=True):
-
-        if self.layout(): QWidget().setLayout(self.layout())
-
-        vbox = QVBoxLayout(self)
-        if is_restore:
-            msg = _("Please enter your wallet seed.") + "\n"
-        else:
-            msg = _("Your seed is important!") \
-                + "\n" + _("To make sure that you have properly saved your seed, please retype it here.")
-        
-        logo = QLabel()
-        logo.setPixmap(QPixmap(":icons/seed.png").scaledToWidth(56))
-        logo.setMaximumWidth(60)
-
-        label = QLabel(msg)
-        label.setWordWrap(True)
-
-        seed_e = QTextEdit()
-        seed_e.setMaximumHeight(100)
+    def get_seed_text(self, seed_e):
+        return unicode(seed_e.toPlainText())
 
-        vbox.addWidget(label)
 
-        grid = QGridLayout()
-        grid.addWidget(logo, 0, 0)
-        grid.addWidget(seed_e, 0, 1)
-
-        vbox.addLayout(grid)
+    def is_seed(self, seed_e):
+        text = self.get_seed_text(seed_e)
+        return Wallet.is_seed(text) or Wallet.is_mpk(text)
 
 
+    def enter_seed_dialog(self, is_restore, sid):
+        vbox, seed_e = seed_dialog.enter_seed_box(is_restore, sid)
         vbox.addStretch(1)
-        vbox.addLayout(ok_cancel_buttons(self, _('Next')))
-
+        hbox, button = ok_cancel_buttons2(self, _('Next'))
+        vbox.addLayout(hbox)
+        button.setEnabled(False)
+        seed_e.textChanged.connect(lambda: button.setEnabled(self.is_seed(seed_e)))
+        self.set_layout(vbox)
         if not self.exec_():
             return
+        return self.get_seed_text(seed_e)
 
-        seed = unicode(seed_e.toPlainText())
 
-        if not seed:
-            QMessageBox.warning(None, _('Error'), _('No seed'), _('OK'))
-            return
+    def double_seed_dialog(self):
+        vbox = QVBoxLayout()
+        vbox1, seed_e1 = seed_dialog.enter_seed_box(True, 'hot')
+        vbox2, seed_e2 = seed_dialog.enter_seed_box(True, 'cold')
+        vbox.addLayout(vbox1)
+        vbox.addLayout(vbox2)
+        vbox.addStretch(1)
+        hbox, button = ok_cancel_buttons2(self, _('Next'))
+        vbox.addLayout(hbox)
+        button.setEnabled(False)
+        f = lambda: button.setEnabled(self.is_seed(seed_e1) and self.is_seed(seed_e2))
+        seed_e1.textChanged.connect(f)
+        seed_e2.textChanged.connect(f)
+        self.set_layout(vbox)
+        if not self.exec_():
+            return 
+        return self.get_seed_text(seed_e1), self.get_seed_text(seed_e2)
 
-        return seed
 
 
 
@@ -133,55 +132,19 @@ class InstallWizard(QDialog):
             task()
             self.emit(QtCore.SIGNAL('accept'))
 
-        if self.layout(): QWidget().setLayout(self.layout())
-        vbox = QVBoxLayout(self)
+        vbox = QVBoxLayout()
         self.waiting_label = QLabel(msg)
         vbox.addWidget(self.waiting_label)
+        self.set_layout(vbox)
         t = threading.Thread(target = target)
         t.start()
         self.exec_()
 
 
 
-    def mpk_dialog(self):
-
-        if self.layout(): QWidget().setLayout(self.layout())
-
-        vbox = QVBoxLayout(self)
-
-        vbox.addWidget(QLabel(_("Please enter your master public key.")))
-
-        grid = QGridLayout()
-        grid.setSpacing(8)
-
-        label = QLabel(_("Key")) 
-        grid.addWidget(label, 0, 0)
-        mpk_e = QTextEdit()
-        mpk_e.setMaximumHeight(100)
-        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)
-
-        vbox.addLayout(grid)
-
-        vbox.addStretch(1)
-        vbox.addLayout(ok_cancel_buttons(self, _('Next')))
-
-        if not self.exec_(): return None, None
-
-        mpk = str(mpk_e.toPlainText()).strip()
-        chain = str(chain_e.toPlainText()).strip()
-        return mpk, chain
-
 
     def network_dialog(self):
         
-        if self.layout(): QWidget().setLayout(self.layout())
-
         grid = QGridLayout()
         grid.setSpacing(5)
 
@@ -206,12 +169,13 @@ class InstallWizard(QDialog):
         grid.addWidget(b2,2,0)
         #grid.addWidget(b3,3,0)
 
-        vbox = QVBoxLayout(self)
+        vbox = QVBoxLayout()
         vbox.addLayout(grid)
 
         vbox.addStretch(1)
         vbox.addLayout(ok_cancel_buttons(self, _('Next')))
 
+        self.set_layout(vbox)
         if not self.exec_():
             return
         
@@ -227,79 +191,175 @@ class InstallWizard(QDialog):
             self.config.set_key('auto_cycle', False, True)
             return
         
-        
-
-    def show_seed(self, wallet):
-        from seed_dialog import make_seed_dialog
 
-        vbox = make_seed_dialog(wallet.seed, wallet.imported_keys)
-        vbox.addLayout(ok_cancel_buttons(self, _("Next")))
+    def show_message(self, msg):
+        vbox = QVBoxLayout()
+        vbox.addWidget(QLabel(msg))
+        vbox.addStretch(1)
+        vbox.addLayout(close_button(self, _('Next')))
+        self.set_layout(vbox)
+        if not self.exec_(): 
+            return None
+
+    def question(self, msg):
+        vbox = QVBoxLayout()
+        vbox.addWidget(QLabel(msg))
+        vbox.addStretch(1)
+        vbox.addLayout(ok_cancel_buttons(self, _('OK')))
+        self.set_layout(vbox)
+        if not self.exec_(): 
+            return None
+        return True
 
-        if self.layout(): QWidget().setLayout(self.layout())
-        self.setLayout(vbox)
 
-        if not self.exec_():
-            exit()
+    def show_seed(self, seed, sid):
+        vbox = seed_dialog.show_seed_box(seed, sid)
+        vbox.addLayout(ok_cancel_buttons(self, _("Next")))
+        self.set_layout(vbox)
+        return self.exec_()
 
 
     def password_dialog(self, wallet):
         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)
+        self.set_layout( make_password_dialog(self, wallet, msg) )
+        return run_password_dialog(self, wallet, self)
 
 
-    def run(self):
+    def choose_wallet_type(self):
+        grid = QGridLayout()
+        grid.setSpacing(5)
 
-        action = self.restore_or_create()
-        if not action: exit()
+        msg = _("Choose your wallet.")
+        label = QLabel(msg)
+        label.setWordWrap(True)
+        grid.addWidget(label, 0, 0)
 
-        wallet = Wallet(self.storage)
-        gap = self.config.get('gap_limit', 5)
-        if gap != 5:
-            wallet.gap_limit = gap
-            wallet.storage.put('gap_limit', gap, True)
+        gb = QGroupBox()
 
-        if action == 'create':
-            wallet.init_seed(None)
-            self.show_seed(wallet)
-            if self.verify_seed(wallet):
-                def create():
-                    wallet.save_seed()
-                    wallet.create_accounts()
-                    wallet.synchronize()  # generate first addresses offline
-                self.waiting_dialog(create)
-            else:
+        b1 = QRadioButton(gb)
+        b1.setText(_("Standard wallet (protected by password)"))
+        b1.setChecked(True)
+
+        b2 = QRadioButton(gb)
+        b2.setText(_("Multi-signature wallet (two-factor authentication)"))
+
+        grid.addWidget(b1,1,0)
+        grid.addWidget(b2,2,0)
+
+        vbox = QVBoxLayout()
+
+        vbox.addLayout(grid)
+        vbox.addStretch(1)
+        vbox.addLayout(ok_cancel_buttons(self, _('Next')))
+
+        self.set_layout(vbox)
+        if not self.exec_():
+            return
+        
+        if b1.isChecked():
+            return 'standard'
+        elif b2.isChecked():
+            return '2of3'
+
+
+    def run(self, action = None):
+
+        if action is None:
+            action = self.restore_or_create()
+
+        if action is None: 
+            return
+
+        if action == 'create':            
+            t = self.choose_wallet_type()
+            if not t:
+                return 
+
+            if t == '2of3':
+                run_hook('create_cold_seed', self.storage, self)
                 return
-                
-        elif action == 'restore':
-            # ask for seed and gap.
-            seed = self.seed_dialog()
-            if not seed:
+
+
+        if action in ['create', 'create2of3']:
+
+            wallet = Wallet(self.storage)
+
+            wallet.init_seed(None)
+            seed = wallet.get_mnemonic(None)
+            sid = 'hot' if action == 'create2of3' else None
+            if not self.show_seed(seed, sid):
                 return
-            try:
-                wallet.init_seed(seed)
-            except:
-                QMessageBox.warning(None, _('Error'), _('Incorrect seed'), _('OK'))
+            if not self.verify_seed(seed, sid):
                 return
+            ok, old_password, password = self.password_dialog(wallet)
+            wallet.save_seed(password)
 
-            wallet.save_seed()
+            if action == 'create2of3':
+                run_hook('create_third_key', wallet, self)
+                if not wallet.master_public_keys.get("remote/"):
+                    return
 
-        elif action == 'watching':
-            # ask for seed and gap.
-            K, chain = self.mpk_dialog()
-            if not K or not chain:
+            wallet.create_accounts(password)
+            # generate first addresses offline
+            self.waiting_dialog(wallet.synchronize)
+
+        elif action == 'restore':
+            # dialog box will accept either seed or xpub. 
+            # use two boxes for 2of3
+            t = self.choose_wallet_type()
+            if not t: 
                 return
-            wallet.seed = ''
-            wallet.create_watching_only_wallet(chain,K)
+
+            if t == 'standard':
+                text = self.enter_seed_dialog(True, None)
+                if Wallet.is_seed(text):
+                    wallet = Wallet.from_seed(text, self.storage)
+                    ok, old_password, password = self.password_dialog(wallet)
+                    wallet.save_seed(password)
+                    wallet.create_accounts(password)
+                elif Wallet.is_mpk(text):
+                    wallet = Wallet.from_mpk(text, self.storage)
+                else:
+                    return
+
+            elif t in ['2of2', '2of3']:
+                r = self.double_seed_dialog()
+                if not r: 
+                    return
+                text1, text2 = r
+                wallet = Wallet_2of3(self.storage)
+
+                if Wallet.is_seed(text1):
+                    xpriv, xpub = bip32_root(text1)
+                elif Wallet.is_mpk(text1):
+                    xpub = text1
+                wallet.add_master_public_key("m/", xpub)
+
+                if Wallet.is_seed(text2):
+                    xpriv2, xpub2 = bip32_root(text2)
+                elif Wallet.is_mpk(text2):
+                    xpub2 = text2
+                wallet.add_master_public_key("cold/", xpub2)
+
+                run_hook('restore_third_key', wallet, self)
+
+                wallet.create_accounts(None)
+
+
 
 
         else: raise
                 
         #if not self.config.get('server'):
-        self.network_dialog()
+        if self.network:
+            if self.network.interfaces:
+                self.network_dialog()
+            else:
+                QMessageBox.information(None, _('Warning'), _('You are offline'), _('OK'))
+                self.network.stop()
+                self.network = None
 
         # start wallet threads
         wallet.start_threads(self.network)
@@ -308,13 +368,12 @@ class InstallWizard(QDialog):
 
             self.waiting_dialog(lambda: wallet.restore(self.waiting_label.setText))
 
-            if wallet.is_found():
-                QMessageBox.information(None, _('Information'), _("Recovery successful"), _('OK'))
+            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'))
             else:
-                QMessageBox.information(None, _('Information'), _("No transactions found for this seed"), _('OK'))
-            
-            wallet.fill_addressbook()
-
-        self.password_dialog(wallet)
+                QMessageBox.information(None, _('Information'), _("This wallet was restored offline. It may contain more addresses than displayed."), _('OK'))
 
         return wallet