twizard: add capability to restore multisig - electrum - Electrum Bitcoin wallet HTML git clone https://git.parazyd.org/electrum DIR Log DIR Files DIR Refs DIR Submodules --- DIR commit 342d6633a671c0b33721e34693c99925a01c225b DIR parent 5bcadd6651537f93fcbe49fb77921d7bf2e3dc35 HTML Author: ThomasV <thomasv@electrum.org> Date: Fri, 1 Jul 2016 11:44:26 +0200 wizard: add capability to restore multisig Diffstat: M gui/kivy/uix/dialogs/installwizard… | 29 ++++++++++++++++++++++++----- M gui/qt/installwizard.py | 26 +++++++++++++++++++++----- M lib/base_wizard.py | 95 +++++++++++++++++++------------ M lib/wallet.py | 42 +++++++++++--------------------- 4 files changed, 117 insertions(+), 75 deletions(-) --- DIR diff --git a/gui/kivy/uix/dialogs/installwizard.py b/gui/kivy/uix/dialogs/installwizard.py t@@ -212,6 +212,7 @@ Builder.load_string(''' <RestoreSeedDialog> + message: '' word: '' BigLabel: text: "ENTER YOUR SEED PHRASE" t@@ -384,6 +385,7 @@ Builder.load_string(''' <ShowSeedDialog> + message: '' spacing: '12dp' value: 'next' BigLabel: t@@ -493,7 +495,8 @@ class WizardChoiceDialog(WizardDialog): class ShowSeedDialog(WizardDialog): seed_text = StringProperty('') - message = StringProperty('') + message = _("If you forget your PIN or lose your device, your seed phrase will be the " + "only way to recover your funds.") def on_parent(self, instance, value): if value: t@@ -513,8 +516,6 @@ class WizardButton(Button): class RestoreSeedDialog(WizardDialog): - message = StringProperty('') - def __init__(self, wizard, **kwargs): super(RestoreSeedDialog, self).__init__(wizard, **kwargs) self._test = kwargs['is_valid'] t@@ -522,6 +523,8 @@ class RestoreSeedDialog(WizardDialog): from electrum.old_mnemonic import words as old_wordlist self.words = set(Mnemonic('en').wordlist).union(set(old_wordlist)) self.ids.text_input_seed.text = test_seed if is_test else '' + self.message = _('Please type your seed phrase using the virtual keyboard.') + self.title = _('Enter Seed') def get_suggestions(self, prefix): for w in self.words: t@@ -708,8 +711,24 @@ class InstallWizard(BaseWizard, Widget): def choice_dialog(self, **kwargs): WizardChoiceDialog(self, **kwargs).open() def multisig_dialog(self, **kwargs): WizardMultisigDialog(self, **kwargs).open() def show_seed_dialog(self, **kwargs): ShowSeedDialog(self, **kwargs).open() - def enter_seed_dialog(self, **kwargs): RestoreSeedDialog(self, **kwargs).open() - def add_xpub_dialog(self, **kwargs): AddXpubDialog(self, **kwargs).open() + + def confirm_seed_dialog(self, **kwargs): + kwargs['title'] = _('Confirm Seed') + kwargs['message'] = _('Please retype your seed phrase, to confirm that you properly saved it') + RestoreSeedDialog(self, **kwargs).open() + + def restore_seed_dialog(self, **kwargs): + RestoreSeedDialog(self, **kwargs).open() + + def restore_keys_dialog(self, **kwargs): + kwargs['message'] += ' ' + _('Use the camera button to scan a QR code.') + AddXpubDialog(self, **kwargs).open() + + def add_cosigner_dialog(self, **kwargs): + kwargs['title'] = _("Add Cosigner") + " %d"%kwargs['index'] + kwargs['message'] = _('Please paste your cosigners master public key, or scan it using the camera button.') + AddXpubDialog(self, **kwargs).open() + def show_xpub_dialog(self, **kwargs): ShowXpubDialog(self, **kwargs).open() def show_error(self, msg): DIR diff --git a/gui/qt/installwizard.py b/gui/qt/installwizard.py t@@ -24,7 +24,6 @@ MSG_GENERATING_WAIT = _("Electrum is generating your addresses, please wait...") MSG_ENTER_ANYTHING = _("Please enter a seed phrase, a master key, a list of " "Bitcoin addresses, or a list of private keys") MSG_ENTER_SEED_OR_MPK = _("Please enter a seed phrase or a master key (xpub or xprv):") -MSG_VERIFY_SEED = _("Your seed is important!\nTo make sure that you have properly saved your seed, please retype it here.") MSG_COSIGNER = _("Please enter the master public key of cosigner #%d:") MSG_SHOW_MPK = _("Here is your master public key:") MSG_ENTER_PASSWORD = _("Choose a password to encrypt your wallet keys. " t@@ -236,16 +235,33 @@ class InstallWizard(QDialog, MessageBoxMixin, BaseWizard): return seed @wizard_dialog - def add_xpub_dialog(self, title, message, is_valid, run_next): + def restore_keys_dialog(self, title, message, is_valid, run_next): return self.text_input(title, message, is_valid) @wizard_dialog - def enter_seed_dialog(self, run_next, title, message, is_valid): + def add_cosigner_dialog(self, run_next, index, is_valid): + title = _("Add Cosigner") + " %d"%index + message = ' '.join([ + _('Please enter the master public key of your cosigner.'), + _('Enter their seed or master private key if you want to be able to sign for them.') + ]) + return self.text_input(title, message, is_valid) + + @wizard_dialog + def restore_seed_dialog(self, run_next, is_valid): + title = _('Enter Seed') + message = _('Please enter your seed phrase in order to restore your wallet.') + return self.text_input(title, message, is_valid) + + @wizard_dialog + def confirm_seed_dialog(self, run_next, is_valid): self.app.clipboard().clear() + title = _('Confirm Seed') + message = _("Your seed is important!\nTo make sure that you have properly saved your seed, please retype it here.") return self.text_input(title, message, is_valid) @wizard_dialog - def show_seed_dialog(self, run_next, message, seed_text): + def show_seed_dialog(self, run_next, seed_text): slayout = SeedWarningLayout(seed_text) self.set_main_layout(slayout.layout()) return seed_text t@@ -326,7 +342,7 @@ class InstallWizard(QDialog, MessageBoxMixin, BaseWizard): @wizard_dialog def show_xpub_dialog(self, xpub, run_next): vbox = QVBoxLayout() - layout = SeedDisplayLayout(xpub, title=MSG_SHOW_MPK, sid='hot') + layout = SeedDisplayLayout(xpub, title=_('Master Public Key'), sid='hot') vbox.addLayout(layout.layout()) self.set_main_layout(vbox, MSG_SHOW_MPK) return None DIR diff --git a/lib/base_wizard.py b/lib/base_wizard.py t@@ -28,6 +28,11 @@ from electrum.wallet import Wallet, Multisig_Wallet, WalletStorage from i18n import _ +is_any_key = lambda x: Wallet.is_old_mpk(x) or Wallet.is_xprv(x) or Wallet.is_xpub(x) or Wallet.is_address(x) or Wallet.is_private_key(x) +is_private_key = lambda x: Wallet.is_xprv(x) or Wallet.is_private_key(x) +is_bip32_key = lambda x: Wallet.is_xprv(x) or Wallet.is_xpub(x) + + class BaseWizard(object): def __init__(self, config, network, path): t@@ -116,13 +121,13 @@ class BaseWizard(object): self.multisig_dialog(run_next=on_multisig) def choose_seed(self): - title = _('Private Keys') + title = _('Choose Seed') message = _("Do you want to create a new seed, or to restore a wallet using an existing seed?") if self.wallet_type == 'standard': choices = [ ('create_seed', _('Create a new seed')), ('restore_seed', _('I already have a seed')), - ('restore_xpub', _('Watching-only wallet')), + ('restore_from_key', _('Import keys')), ] elif self.wallet_type == 'twofactor': choices = [ t@@ -133,26 +138,42 @@ class BaseWizard(object): choices = [ ('create_seed', _('Create a new seed')), ('restore_seed', _('I already have a seed')), - ('restore_xpub', _('Watching-only wallet')), + ('restore_from_key', _('I have a master key')), ('choose_hw', _('Cosign with hardware wallet')), ] self.choice_dialog(title=title, message=message, choices=choices, run_next=self.run) def create_2fa(self): - print 'create 2fa' self.storage.put('wallet_type', '2fa') self.wallet = Wallet(self.storage) self.run_wallet() def restore_seed(self): - msg = _('Please type your seed phrase using the virtual keyboard.') - title = _('Enter Seed') - self.enter_seed_dialog(run_next=self.add_password, title=title, message=msg, is_valid=Wallet.is_seed) + # TODO: return derivation password too + self.restore_seed_dialog(run_next=self.add_password, is_valid=Wallet.is_seed) + + def on_restore(self, text): + if is_private_key(text): + self.add_password(text) + else: + self.create_wallet(text, None) - def restore_xpub(self): - title = "MASTER PUBLIC KEY" - message = _('To create a watching-only wallet, paste your master public key, or scan it using the camera button.') - self.add_xpub_dialog(run_next=lambda xpub: self.create_wallet(xpub, None), title=title, message=message, is_valid=Wallet.is_mpk) + def restore_from_key(self): + if self.wallet_type == 'standard': + v = is_any_key + title = _("Import keys") + message = ' '.join([ + _("To create a watching-only wallet, please enter your master public key (xpub), or a list of Bitcoin addresses."), + _("To create a spending wallet, please enter a master private key (xprv), or a list of Bitcoin private keys.") + ]) + else: + v = is_bip32_key + title = _("Master public or private key") + message = ' '.join([ + _("To create a watching-only wallet, please enter your master public key (xpub)."), + _("To create a spending wallet, please enter a master private key (xprv).") + ]) + self.restore_keys_dialog(title=title, message=message, run_next=self.on_restore, is_valid=v) def restore_2fa(self): self.storage.put('wallet_type', '2fa') t@@ -210,46 +231,46 @@ class BaseWizard(object): elif self.wallet_type == 'multisig': self.storage.put('wallet_type', self.multisig_type) self.wallet = Multisig_Wallet(self.storage) - self.wallet.add_seed(text, password) - self.wallet.create_master_keys(password) - self.run_wallet() - - def add_cosigners(self): - xpub = self.wallet.master_public_keys.get('x1/') - self.show_xpub_dialog(run_next=lambda x: self.add_cosigner(), xpub=xpub) - - def add_cosigner(self): - def on_xpub(xpub): - self.wallet.add_cosigner(xpub) - i = self.wallet.get_missing_cosigner() - action = 'add_cosigner' if i else 'create_addresses' - self.run(action) + self.wallet.add_cosigner('x1/', text, password) + self.run('show_xpub_and_add_cosigners', (Wallet.is_xpub(text), password,)) + + def show_xpub_and_add_cosigners(self, is_xpub, password): + if not is_xpub: + xpub = self.wallet.master_public_keys.get('x1/') + self.show_xpub_dialog(run_next=lambda x: self.add_cosigners(password), xpub=xpub) + else: + self.add_cosigners(password) + + def add_cosigners(self, password): i = self.wallet.get_missing_cosigner() - title = _("Add Cosigner") + " %d"%(i-1) - message = _('Please paste your cosigners master public key, or scan it using the camera button.') - self.add_xpub_dialog(run_next=on_xpub, title=title, message=message, is_valid=Wallet.is_any) + self.add_cosigner_dialog(run_next=lambda x: self.on_cosigner(x, password), index=(i-1), is_valid=Wallet.is_xpub) + + def on_cosigner(self, text, password): + i = self.wallet.get_missing_cosigner() + self.wallet.add_cosigner('x%d/'%i, text, password) + i = self.wallet.get_missing_cosigner() + if i: + self.run('add_cosigners', (password,)) + else: + self.create_addresses() def create_addresses(self): def task(): self.wallet.create_main_account() self.wallet.synchronize() self.terminate() - msg= _("Electrum is generating your addresses, please wait.") + msg = _("Electrum is generating your addresses, please wait.") self.waiting_dialog(task, msg) def create_seed(self): from electrum.wallet import BIP32_Wallet seed = BIP32_Wallet.make_seed() - msg = _("If you forget your PIN or lose your device, your seed phrase will be the " - "only way to recover your funds.") - self.show_seed_dialog(run_next=self.confirm_seed, message=msg, seed_text=seed) + self.show_seed_dialog(run_next=self.confirm_seed, seed_text=seed) def confirm_seed(self, seed): assert Wallet.is_seed(seed) - title = _('Confirm Seed') - msg = _('Please retype your seed phrase, to confirm that you properly saved it') - self.enter_seed_dialog(run_next=self.add_password, title=title, message=msg, is_valid=lambda x: x==seed) + self.confirm_seed_dialog(run_next=self.add_password, is_valid=lambda x: x==seed) - def add_password(self, seed): - f = lambda x: self.create_wallet(seed, x) + def add_password(self, text): + f = lambda x: self.create_wallet(text, x) self.request_password(run_next=f) DIR diff --git a/lib/wallet.py b/lib/wallet.py t@@ -1920,14 +1920,24 @@ class Multisig_Wallet(BIP32_RD_Wallet, Mnemonic): if self.master_public_keys.get("x%d/"%(i+1)) is None: return i+1 - def add_cosigner(self, xpub): - i = self.get_missing_cosigner() - self.add_master_public_key("x%d/" % i, xpub) + def add_cosigner(self, name, text, password): + if Wallet.is_xprv(text): + xpub = bitcoin.xpub_from_xprv(text) + self.add_master_public_key(name, xpub) + self.add_master_private_key(name, text, password) + elif Wallet.is_xpub(text): + self.add_master_public_key(name, text) + if Wallet.is_seed(text): + if name == 'x1/': + self.add_seed(text, password) + self.create_master_keys(password) + else: + self.add_xprv_from_seed(text, name, password) def get_action(self): i = self.get_missing_cosigner() if i is not None: - return 'create_seed' if i == 1 else 'add_cosigners' + return 'create_seed' if i == 1 else 'show_xpub_and_add_cosigners' if not self.accounts: return 'create_main_account' t@@ -2191,30 +2201,6 @@ class Wallet(object): return w @staticmethod - def from_multisig(key_list, password, storage, wallet_type): - storage.put('wallet_type', wallet_type) - wallet = Multisig_Wallet(storage) - key_list = sorted(key_list, key = Wallet.is_xpub) - for i, text in enumerate(key_list): - name = "x%d/" % (i+1) - if Wallet.is_xprv(text): - xpub = bitcoin.xpub_from_xprv(text) - wallet.add_master_public_key(name, xpub) - wallet.add_master_private_key(name, text, password) - elif Wallet.is_xpub(text): - wallet.add_master_public_key(name, text) - elif Wallet.is_seed(text): - if name == 'x1/': - wallet.add_seed(text, password) - wallet.create_master_keys(password) - else: - wallet.add_xprv_from_seed(text, name, password) - else: - raise RunTimeError("Cannot handle text for multisig") - wallet.set_use_encryption(password is not None) - return wallet - - @staticmethod def from_text(text, password, storage): if Wallet.is_xprv(text): wallet = Wallet.from_xprv(text, password, storage)