wallet: don't leave a stale 'use_encryption' flag on enable/disable
What changed, and why it matters
This commit fixes a bug in the Electrum Bitcoin wallet where disabling and re-enabling an encrypted keystore could leave the wallet's seed (the secret that controls the coins) stored on disk without a password, while the wallet still falsely claimed the keystore was password-protected. The patch makes the wallet correctly clear the encryption flag when no encryptable keystore remains, and warns the user that disabling will remove the wallet password. A new test also handles wallets that were already affected by the old bug.
Users who previously disabled and re-enabled a keystore in a standard wallet without storage encryption should create a new wallet and move funds, because the seed may have been stored on disk without password protection despite the UI indicating keystore encryption. Update to the patched version.
Security signals we found
Plaintext storage of cryptographic seed under certain user actions
Inconsistent encryption state flag leading to false security indication
Regression tests added for disable/enable keystore encryption transitions
User-facing warning added about password removal when disabling keystore
Evidence from the diff
The bug involved the use_encryption / set_keystore_encryption flag. When the only password-protectable keystore of a standard wallet was disabled, the flag stayed True even though has_password() became False (no storage encryption). Re-enabling the keystore then encrypted the new keystore with None as password (effectively plaintext) while has_keystore_encryption() still returned True. The fix updates disable_keystore() to call set_keystore_encryption(False) when no keystore can be encrypted, and enable_keystore() to set the flag consistently based on whether the wallet actually has a password. A GUI note and regression tests are added.
Changed components
electrum/wallet.py - Abstract_Wallet.enable_keystore / disable_keystoreelectrum/gui/qt/wallet_info_dialog.py - disable_keystore confirmation dialogtests/test_wizard.py - new unit tests for keystore enable/disable encryption statesInspect captured patch +70 / −4
### electrum/gui/qt/wallet_info_dialog.py
@@ -187,6 +187,13 @@ def disable_keystore(self, keystore):
msg = _('Disable keystore? This will make the keystore watching-only.')
if self.wallet.storage.is_encrypted_with_hw_device():
msg += '\n\n' + _('Note that this will disable wallet file encryption, because it uses your hardware wallet device.')
+ other_keystores = [k for k in self.wallet.get_keystores() if k is not keystore]
+ if (self.wallet.has_keystore_encryption() and not self.wallet.storage.is_encrypted_with_user_pw()
+ and not any(k.may_have_password() for k in other_keystores)):
+ msg += '\n\n' + ' '.join([
+ _('Note that this will remove the wallet password, because it only protects this keystore.'),
+ _('If you enable the keystore again later, set a new password to protect it.'),
+ ])
if not self.window.question(msg):
return
self.accept()
### electrum/wallet.py
@@ -4353,10 +4353,14 @@ def enable_keystore(self, keystore: KeyStore, is_hardware_keystore: bool, passwo
if not target.is_watching_only():
# e.g. replacing a keystore that has a seed would remove the seed from the wallet file
raise Exception("keystore to be replaced is not watching-only")
- if not is_hardware_keystore and (self.storage.is_encrypted_with_user_pw() or self.has_keystore_encryption()):
- self.check_password(password)
- keystore.update_password(None, password)
- self.db.put('use_encryption', True)
+ if not is_hardware_keystore:
+ if self.storage.is_encrypted_with_user_pw() or self.has_keystore_encryption():
+ self.check_password(password)
+ keystore.update_password(None, password)
+ self.db.set_keystore_encryption(True)
+ else:
+ # the wallet has no password.
+ self.db.set_keystore_encryption(False)
self._update_keystore(keystore)
self.save_db()
@@ -4371,6 +4375,12 @@ def disable_keystore(self, keystore: KeyStore) -> None:
self.update_password(password, None, encrypt_storage=False)
new = keystore.watching_only_keystore()
self._update_keystore(new)
+ if not self.can_have_keystore_encryption():
+ # no keystore left that can be encrypted. Without storage encryption,
+ # the wallet no longer has a password.
+ self.db.set_keystore_encryption(False)
+ if not self.has_password():
+ self.lock_wallet() # clear in memory password
# save changes. force full rewrite to rm remnants of the seed
if self.storage and self.storage.file_exists():
self.db.write_and_force_consolidation()
### tests/test_wizard.py
@@ -306,6 +306,55 @@ async def test_disable_keystore__seed_removed_from_disk(self):
wallet2 = Daemon._load_wallet(self.wallet_path, password=None, config=self.config)
self.assertTrue(wallet2.get_keystore().is_watching_only())
+ async def test_disable_enable_keystore__keystore_encryption_without_storage_encryption(self):
+ myseed = 'bitter grass shiver impose acquire brush forget axis eager alone wine silver'
+ wallet = restore_wallet_from_text(
+ myseed, path=self.wallet_path, config=self.config, password='1234', encrypt_file=False)['wallet']
+ self.assertTrue(wallet.has_keystore_encryption())
+ self.assertFalse(wallet.has_storage_encryption())
+
+ # the password only protected the keystore, so after disabling it, the wallet has no password
+ wallet.disable_keystore(wallet.get_keystore())
+ self.assertFalse(wallet.has_password())
+ self.assertFalse(wallet.db.get('use_encryption'))
+
+ ks = keystore.from_seed(myseed, passphrase='', for_multisig=False)
+ wallet.enable_keystore(ks, False, None)
+ self.assertFalse(wallet.has_keystore_encryption())
+ wallet.check_password(None)
+ self.assertEqual(myseed, wallet.get_keystore().get_seed(None))
+
+ async def test_disable_enable_keystore__storage_encryption(self):
+ myseed = 'bitter grass shiver impose acquire brush forget axis eager alone wine silver'
+ wallet = restore_wallet_from_text(
+ myseed, path=self.wallet_path, config=self.config, password='1234', encrypt_file=True)['wallet']
+ self.assertTrue(wallet.has_keystore_encryption())
+
+ wallet.disable_keystore(wallet.get_keystore())
+ self.assertFalse(wallet.has_keystore_encryption())
+ self.assertTrue(wallet.has_password()) # storage encryption
+
+ ks = keystore.from_seed(myseed, passphrase='', for_multisig=False)
+ wallet.enable_keystore(ks, False, '1234')
+ self.assertTrue(wallet.has_keystore_encryption())
+ wallet.check_password('1234')
+ self.assertEqual(myseed, wallet.get_keystore().get_seed('1234'))
+
+ async def test_enable_keystore__stale_use_encryption_flag(self):
+ """older versions left 'use_encryption' set when disabling the only keystore that was encrypted"""
+ myseed = 'bitter grass shiver impose acquire brush forget axis eager alone wine silver'
+ wallet = restore_wallet_from_text(myseed, path=self.wallet_path, config=self.config)['wallet']
+ wallet.disable_keystore(wallet.get_keystore())
+ wallet.db.put('use_encryption', True)
+ self.assertFalse(wallet.has_password())
+
+ ks = keystore.from_seed(myseed, passphrase='', for_multisig=False)
+ wallet.enable_keystore(ks, False, None)
+ # the seed is not encrypted, so the wallet must not claim keystore encryption
+ self.assertFalse(wallet.has_keystore_encryption())
+ wallet.check_password(None)
+ self.assertEqual(myseed, wallet.get_keystore().get_seed(None))
+
async def test_haveseed_electrum__2fa_seed(self):
"""unlike NewWalletWizard, KeystoreWizard cannot redirect to the 2fa flow, so 2fa seeds are invalid"""
w, v = self._wizard_for()Why this scored 69/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.