From e0a3efe5b520099394059f680f1a73f521843679 Mon Sep 17 00:00:00 2001 From: Sander van Grieken Date: Thu, 22 Sep 2022 14:41:29 +0200 Subject: [PATCH] move wallet creation from wizard to ui agnostic NewWalletWizard --- .../gui/qml/components/NewWalletWizard.qml | 2 +- electrum/gui/qml/qewizard.py | 70 ++----------------- electrum/gui/wizard.py | 64 +++++++++++++++++ 3 files changed, 70 insertions(+), 66 deletions(-) diff --git a/electrum/gui/qml/components/NewWalletWizard.qml b/electrum/gui/qml/components/NewWalletWizard.qml index b2ceac550..1f47eb7af 100644 --- a/electrum/gui/qml/components/NewWalletWizard.qml +++ b/electrum/gui/qml/components/NewWalletWizard.qml @@ -26,7 +26,7 @@ Wizard { onAccepted: { console.log('Finished new wallet wizard') - wiz.create_storage(wizard_data, Daemon.singlePasswordEnabled, Daemon.singlePassword) + wiz.createStorage(wizard_data, Daemon.singlePasswordEnabled, Daemon.singlePassword) } Connections { diff --git a/electrum/gui/qml/qewizard.py b/electrum/gui/qml/qewizard.py index 6ada4787b..f9749d3d3 100644 --- a/electrum/gui/qml/qewizard.py +++ b/electrum/gui/qml/qewizard.py @@ -6,11 +6,6 @@ from PyQt5.QtQml import QQmlApplicationEngine from electrum.logging import get_logger from electrum.gui.wizard import NewWalletWizard -from electrum.storage import WalletStorage, StorageEncryptionVersion -from electrum.wallet_db import WalletDB -from electrum.bip32 import normalize_bip32_derivation, xpub_type -from electrum import keystore - class QEAbstractWizard(QObject): _logger = get_logger(__name__) @@ -80,75 +75,20 @@ class QENewWalletWizard(NewWalletWizard, QEAbstractWizard): def last_if_single_password(self, view, wizard_data): return self._daemon.singlePasswordEnabled - @pyqtSlot('QJSValue',bool,str) - def create_storage(self, js_data, single_password_enabled, single_password): + @pyqtSlot('QJSValue', bool, str) + def createStorage(self, js_data, single_password_enabled, single_password): self._logger.info('Creating wallet from wizard data') data = js_data.toVariant() self._logger.debug(str(data)) - # only standard and 2fa wallets for now - assert data['wallet_type'] in ['standard', '2fa'] - if single_password_enabled and single_password: data['encrypt'] = True data['password'] = single_password + path = os.path.join(os.path.dirname(self._daemon.daemon.config.get_wallet_path()), data['wallet_name']) + try: - path = os.path.join(os.path.dirname(self._daemon.daemon.config.get_wallet_path()), data['wallet_name']) - if os.path.exists(path): - raise Exception('file already exists at path') - storage = WalletStorage(path) - - if data['keystore_type'] in ['createseed', 'haveseed']: - if data['seed_type'] in ['old', 'standard', 'segwit']: #2fa, 2fa-segwit - self._logger.debug('creating keystore from electrum seed') - k = keystore.from_seed(data['seed'], data['seed_extra_words'], data['wallet_type'] == 'multisig') - elif data['seed_type'] == 'bip39': - self._logger.debug('creating keystore from bip39 seed') - root_seed = keystore.bip39_to_seed(data['seed'], data['seed_extra_words']) - derivation = normalize_bip32_derivation(data['derivation_path']) - script = data['script_type'] if data['script_type'] != 'p2pkh' else 'standard' - k = keystore.from_bip43_rootseed(root_seed, derivation, xtype=script) - elif data['seed_type'] == '2fa_segwit': # TODO: legacy 2fa - self._logger.debug('creating keystore from 2fa seed') - k = keystore.from_xprv(data['x1/']['xprv']) - else: - raise Exception('unsupported/unknown seed_type %s' % data['seed_type']) - elif data['keystore_type'] == 'masterkey': - k = keystore.from_master_key(data['master_key']) - has_xpub = isinstance(k, keystore.Xpub) - assert has_xpub - t1 = xpub_type(k.xpub) - if t1 not in ['standard', 'p2wpkh', 'p2wpkh-p2sh']: - raise Exception('wrong key type %s' % t1) - else: - raise Exception('unsupported/unknown keystore_type %s' % data['keystore_type']) - - if data['encrypt']: - if k.may_have_password(): - k.update_password(None, data['password']) - storage.set_password(data['password'], enc_version=StorageEncryptionVersion.USER_PASSWORD) - - db = WalletDB('', manual_upgrades=False) - db.set_keystore_encryption(bool(data['password']) and data['encrypt']) - - db.put('wallet_type', data['wallet_type']) - if 'seed_type' in data: - db.put('seed_type', data['seed_type']) - - if data['wallet_type'] == 'standard': - db.put('keystore', k.dump()) - elif data['wallet_type'] == '2fa': - db.put('x1/', k.dump()) - db.put('x2/', data['x2/']) - db.put('x3/', data['x3/']) - db.put('use_trustedcoin', True) - - if k.can_have_deterministic_lightning_xprv(): - db.put('lightning_xprv', k.get_lightning_xprv(data['password'] if data['encrypt'] else None)) - - db.load_plugins() - db.write(storage) + self.create_storage(path, data) # minimally populate self after create self._password = data['password'] diff --git a/electrum/gui/wizard.py b/electrum/gui/wizard.py index a717dd3f2..01dbdfbdb 100644 --- a/electrum/gui/wizard.py +++ b/electrum/gui/wizard.py @@ -1,8 +1,13 @@ import copy +import os from typing import List, TYPE_CHECKING, Tuple, NamedTuple, Any, Dict, Optional, Union from electrum.logging import get_logger +from electrum.storage import WalletStorage, StorageEncryptionVersion +from electrum.wallet_db import WalletDB +from electrum.bip32 import normalize_bip32_derivation, xpub_type +from electrum import keystore class WizardViewState(NamedTuple): view: str @@ -197,3 +202,62 @@ class NewWalletWizard(AbstractWizard): def finished(self, wizard_data): self._logger.debug('finished') # override + + def create_storage(self, path, data): + # only standard and 2fa wallets for now + assert data['wallet_type'] in ['standard', '2fa'] + + if os.path.exists(path): + raise Exception('file already exists at path') + storage = WalletStorage(path) + + if data['keystore_type'] in ['createseed', 'haveseed']: + if data['seed_type'] in ['old', 'standard', 'segwit']: #2fa, 2fa-segwit + self._logger.debug('creating keystore from electrum seed') + k = keystore.from_seed(data['seed'], data['seed_extra_words'], data['wallet_type'] == 'multisig') + elif data['seed_type'] == 'bip39': + self._logger.debug('creating keystore from bip39 seed') + root_seed = keystore.bip39_to_seed(data['seed'], data['seed_extra_words']) + derivation = normalize_bip32_derivation(data['derivation_path']) + script = data['script_type'] if data['script_type'] != 'p2pkh' else 'standard' + k = keystore.from_bip43_rootseed(root_seed, derivation, xtype=script) + elif data['seed_type'] == '2fa_segwit': # TODO: legacy 2fa + self._logger.debug('creating keystore from 2fa seed') + k = keystore.from_xprv(data['x1/']['xprv']) + else: + raise Exception('unsupported/unknown seed_type %s' % data['seed_type']) + elif data['keystore_type'] == 'masterkey': + k = keystore.from_master_key(data['master_key']) + has_xpub = isinstance(k, keystore.Xpub) + assert has_xpub + t1 = xpub_type(k.xpub) + if t1 not in ['standard', 'p2wpkh', 'p2wpkh-p2sh']: + raise Exception('wrong key type %s' % t1) + else: + raise Exception('unsupported/unknown keystore_type %s' % data['keystore_type']) + + if data['encrypt']: + if k.may_have_password(): + k.update_password(None, data['password']) + storage.set_password(data['password'], enc_version=StorageEncryptionVersion.USER_PASSWORD) + + db = WalletDB('', manual_upgrades=False) + db.set_keystore_encryption(bool(data['password']) and data['encrypt']) + + db.put('wallet_type', data['wallet_type']) + if 'seed_type' in data: + db.put('seed_type', data['seed_type']) + + if data['wallet_type'] == 'standard': + db.put('keystore', k.dump()) + elif data['wallet_type'] == '2fa': + db.put('x1/', k.dump()) + db.put('x2/', data['x2/']) + db.put('x3/', data['x3/']) + db.put('use_trustedcoin', True) + + if k.can_have_deterministic_lightning_xprv(): + db.put('lightning_xprv', k.get_lightning_xprv(data['password'] if data['encrypt'] else None)) + + db.load_plugins() + db.write(storage)