diff --git a/electrum/base_wizard.py b/electrum/base_wizard.py index deb0291e7..81783d604 100644 --- a/electrum/base_wizard.py +++ b/electrum/base_wizard.py @@ -37,7 +37,7 @@ from .bip32 import is_bip32_derivation, xpub_type, normalize_bip32_derivation from .keystore import bip44_derivation, purpose48_derivation from .wallet import (Imported_Wallet, Standard_Wallet, Multisig_Wallet, wallet_types, Wallet, Abstract_Wallet) -from .storage import (WalletStorage, STO_EV_USER_PW, STO_EV_XPUB_PW, +from .storage import (WalletStorage, StorageEncryptionVersion, get_derivation_used_for_hw_device_encryption) from .i18n import _ from .util import UserCancelled, InvalidPassword, WalletFileException @@ -541,7 +541,7 @@ class BaseWizard(Logger): run_next=lambda encrypt_storage: self.on_password( password, encrypt_storage=encrypt_storage, - storage_enc_version=STO_EV_XPUB_PW, + storage_enc_version=StorageEncryptionVersion.XPUB_PASSWORD, encrypt_keystore=False)) else: # reset stack to disable 'back' button in password dialog @@ -551,12 +551,12 @@ class BaseWizard(Logger): run_next=lambda password, encrypt_storage: self.on_password( password, encrypt_storage=encrypt_storage, - storage_enc_version=STO_EV_USER_PW, + storage_enc_version=StorageEncryptionVersion.USER_PASSWORD, encrypt_keystore=encrypt_keystore), force_disable_encrypt_cb=not encrypt_keystore) def on_password(self, password, *, encrypt_storage, - storage_enc_version=STO_EV_USER_PW, encrypt_keystore): + storage_enc_version=StorageEncryptionVersion.USER_PASSWORD, encrypt_keystore): for k in self.keystores: if k.may_have_password(): k.update_password(None, password) diff --git a/electrum/gui/qt/main_window.py b/electrum/gui/qt/main_window.py index 29d61f590..cee254d07 100644 --- a/electrum/gui/qt/main_window.py +++ b/electrum/gui/qt/main_window.py @@ -2221,8 +2221,8 @@ class ElectrumWindow(QMainWindow, MessageBoxMixin, Logger): self.send_button.setVisible(not self.wallet.is_watching_only()) def change_password_dialog(self): - from electrum.storage import STO_EV_XPUB_PW - if self.wallet.get_available_storage_encryption_version() == STO_EV_XPUB_PW: + from electrum.storage import StorageEncryptionVersion + if self.wallet.get_available_storage_encryption_version() == StorageEncryptionVersion.XPUB_PASSWORD: from .password_dialog import ChangePasswordDialogForHW d = ChangePasswordDialogForHW(self, self.wallet) ok, encrypt_file = d.run() diff --git a/electrum/plugins/trustedcoin/trustedcoin.py b/electrum/plugins/trustedcoin/trustedcoin.py index 809893999..6c68774c0 100644 --- a/electrum/plugins/trustedcoin/trustedcoin.py +++ b/electrum/plugins/trustedcoin/trustedcoin.py @@ -45,7 +45,7 @@ from electrum.wallet import Multisig_Wallet, Deterministic_Wallet from electrum.i18n import _ from electrum.plugin import BasePlugin, hook from electrum.util import NotEnoughFunds, UserFacingException -from electrum.storage import STO_EV_USER_PW +from electrum.storage import StorageEncryptionVersion from electrum.network import Network from electrum.base_wizard import BaseWizard from electrum.logging import Logger @@ -594,7 +594,7 @@ class TrustedCoinPlugin(BasePlugin): k1.update_password(None, password) wizard.data['x1/'] = k1.dump() wizard.data['x2/'] = k2.dump() - wizard.pw_args = password, encrypt_storage, STO_EV_USER_PW + wizard.pw_args = password, encrypt_storage, StorageEncryptionVersion.USER_PASSWORD self.go_online_dialog(wizard) def restore_wallet(self, wizard): @@ -642,7 +642,7 @@ class TrustedCoinPlugin(BasePlugin): xpub3 = make_xpub(get_signing_xpub(xtype), long_user_id) k3 = keystore.from_xpub(xpub3) wizard.data['x3/'] = k3.dump() - wizard.pw_args = password, encrypt_storage, STO_EV_USER_PW + wizard.pw_args = password, encrypt_storage, StorageEncryptionVersion.USER_PASSWORD wizard.terminate() def create_remote_key(self, email, wizard): diff --git a/electrum/storage.py b/electrum/storage.py index 7a711bd88..42cc5377d 100644 --- a/electrum/storage.py +++ b/electrum/storage.py @@ -28,6 +28,7 @@ import stat import hashlib import base64 import zlib +from enum import IntEnum from . import ecc from .util import profiler, InvalidPassword, WalletFileException, bfh, standardize_path @@ -42,9 +43,11 @@ def get_derivation_used_for_hw_device_encryption(): "/4541509'" # ascii 'ELE' as decimal ("BIP43 purpose") "/1112098098'") # ascii 'BIE2' as decimal -# storage encryption version -STO_EV_PLAINTEXT, STO_EV_USER_PW, STO_EV_XPUB_PW = range(0, 3) +class StorageEncryptionVersion(IntEnum): + PLAINTEXT = 0 + USER_PASSWORD = 1 + XPUB_PASSWORD = 2 class WalletStorage(Logger): @@ -67,7 +70,7 @@ class WalletStorage(Logger): self.db = DB_Class(self.raw, manual_upgrades=manual_upgrades) self.load_plugins() else: - self._encryption_version = STO_EV_PLAINTEXT + self._encryption_version = StorageEncryptionVersion.PLAINTEXT # avoid new wallets getting 'upgraded' self.db = DB_Class('', manual_upgrades=False) @@ -128,13 +131,13 @@ class WalletStorage(Logger): def is_encrypted(self): """Return if storage encryption is currently enabled.""" - return self.get_encryption_version() != STO_EV_PLAINTEXT + return self.get_encryption_version() != StorageEncryptionVersion.PLAINTEXT def is_encrypted_with_user_pw(self): - return self.get_encryption_version() == STO_EV_USER_PW + return self.get_encryption_version() == StorageEncryptionVersion.USER_PASSWORD def is_encrypted_with_hw_device(self): - return self.get_encryption_version() == STO_EV_XPUB_PW + return self.get_encryption_version() == StorageEncryptionVersion.XPUB_PASSWORD def get_encryption_version(self): """Return the version of encryption used for this storage. @@ -151,13 +154,13 @@ class WalletStorage(Logger): try: magic = base64.b64decode(self.raw)[0:4] if magic == b'BIE1': - return STO_EV_USER_PW + return StorageEncryptionVersion.USER_PASSWORD elif magic == b'BIE2': - return STO_EV_XPUB_PW + return StorageEncryptionVersion.XPUB_PASSWORD else: - return STO_EV_PLAINTEXT + return StorageEncryptionVersion.PLAINTEXT except: - return STO_EV_PLAINTEXT + return StorageEncryptionVersion.PLAINTEXT @staticmethod def get_eckey_from_password(password): @@ -167,9 +170,9 @@ class WalletStorage(Logger): def _get_encryption_magic(self): v = self._encryption_version - if v == STO_EV_USER_PW: + if v == StorageEncryptionVersion.USER_PASSWORD: return b'BIE1' - elif v == STO_EV_XPUB_PW: + elif v == StorageEncryptionVersion.XPUB_PASSWORD: return b'BIE2' else: raise WalletFileException('no encryption magic for version: %s' % v) @@ -211,13 +214,13 @@ class WalletStorage(Logger): """Set a password to be used for encrypting this storage.""" if enc_version is None: enc_version = self._encryption_version - if password and enc_version != STO_EV_PLAINTEXT: + if password and enc_version != StorageEncryptionVersion.PLAINTEXT: ec_key = self.get_eckey_from_password(password) self.pubkey = ec_key.get_public_key_hex() self._encryption_version = enc_version else: self.pubkey = None - self._encryption_version = STO_EV_PLAINTEXT + self._encryption_version = StorageEncryptionVersion.PLAINTEXT # make sure next storage.write() saves changes self.db.set_modified(True) diff --git a/electrum/wallet.py b/electrum/wallet.py index aeef63275..012363fca 100644 --- a/electrum/wallet.py +++ b/electrum/wallet.py @@ -54,7 +54,7 @@ from .crypto import sha256d from . import keystore from .keystore import load_keystore, Hardware_KeyStore, KeyStore from .util import multisig_type -from .storage import STO_EV_PLAINTEXT, STO_EV_USER_PW, STO_EV_XPUB_PW, WalletStorage +from .storage import StorageEncryptionVersion, WalletStorage from . import transaction, bitcoin, coinchooser, paymentrequest, ecc, bip32 from .transaction import Transaction, TxOutput, TxOutputHwInfo from .plugin import run_hook @@ -1421,16 +1421,16 @@ class Abstract_Wallet(AddressSynchronizer): def can_have_keystore_encryption(self): return self.keystore and self.keystore.may_have_password() - def get_available_storage_encryption_version(self): + def get_available_storage_encryption_version(self) -> StorageEncryptionVersion: """Returns the type of storage encryption offered to the user. A wallet file (storage) is either encrypted with this version or is stored in plaintext. """ if isinstance(self.keystore, Hardware_KeyStore): - return STO_EV_XPUB_PW + return StorageEncryptionVersion.XPUB_PASSWORD else: - return STO_EV_USER_PW + return StorageEncryptionVersion.USER_PASSWORD def has_keystore_encryption(self): """Returns whether encryption is enabled for the keystore. @@ -1462,7 +1462,7 @@ class Abstract_Wallet(AddressSynchronizer): if encrypt_storage: enc_version = self.get_available_storage_encryption_version() else: - enc_version = STO_EV_PLAINTEXT + enc_version = StorageEncryptionVersion.PLAINTEXT self.storage.set_password(new_pw, enc_version) # note: Encrypting storage with a hw device is currently only @@ -2029,7 +2029,7 @@ class Multisig_Wallet(Deterministic_Wallet): def get_available_storage_encryption_version(self): # multisig wallets are not offered hw device encryption - return STO_EV_USER_PW + return StorageEncryptionVersion.USER_PASSWORD def has_seed(self): return self.keystore.has_seed()