Browse Source

Android: unify password as soon as we know it is possible

patch-4
ThomasV 4 years ago
parent
commit
cd6a468fb9
  1. 4
      electrum/gui/kivy/main_window.py
  2. 24
      electrum/wallet.py

4
electrum/gui/kivy/main_window.py

@ -12,7 +12,7 @@ from typing import TYPE_CHECKING, Optional, Union, Callable, Sequence
from electrum.storage import WalletStorage, StorageReadWriteError from electrum.storage import WalletStorage, StorageReadWriteError
from electrum.wallet_db import WalletDB from electrum.wallet_db import WalletDB
from electrum.wallet import Wallet, InternalAddressCorruption, Abstract_Wallet from electrum.wallet import Wallet, InternalAddressCorruption, Abstract_Wallet
from electrum.wallet import check_password_for_directory, update_password_for_directory from electrum.wallet import update_password_for_directory
from electrum.plugin import run_hook from electrum.plugin import run_hook
from electrum import util from electrum import util
@ -660,7 +660,7 @@ class ElectrumWindow(App, Logger):
def on_wizard_success(self, storage, db, password): def on_wizard_success(self, storage, db, password):
self.password = password self.password = password
if self.electrum_config.get('single_password'): if self.electrum_config.get('single_password'):
self._use_single_password = check_password_for_directory(self.electrum_config, password) self._use_single_password = update_password_for_directory(self.electrum_config, password, password)
self.logger.info(f'use single password: {self._use_single_password}') self.logger.info(f'use single password: {self._use_single_password}')
wallet = Wallet(db, storage, config=self.electrum_config) wallet = Wallet(db, storage, config=self.electrum_config)
wallet.start_network(self.daemon.network) wallet.start_network(self.daemon.network)

24
electrum/wallet.py

@ -3201,12 +3201,13 @@ def restore_wallet_from_text(text, *, path, config: SimpleConfig,
return {'wallet': wallet, 'msg': msg} return {'wallet': wallet, 'msg': msg}
def check_password_for_directory(config: SimpleConfig, old_password, new_password=None) -> bool: def check_password_for_directory(config: SimpleConfig, old_password, new_password=None) -> Tuple[bool, bool]:
"""Checks password against all wallets and returns True if they can all be updated. """Checks password against all wallets, returns whether they can be unified and whether they are already.
If new_password is not None, update all wallet passwords to new_password. If new_password is not None, update all wallet passwords to new_password.
""" """
dirname = os.path.dirname(config.get_wallet_path()) dirname = os.path.dirname(config.get_wallet_path())
failed = [] failed = []
is_unified = True
for filename in os.listdir(dirname): for filename in os.listdir(dirname):
path = os.path.join(dirname, filename) path = os.path.join(dirname, filename)
if not os.path.isfile(path): if not os.path.isfile(path):
@ -3214,6 +3215,7 @@ def check_password_for_directory(config: SimpleConfig, old_password, new_passwor
basename = os.path.basename(path) basename = os.path.basename(path)
storage = WalletStorage(path) storage = WalletStorage(path)
if not storage.is_encrypted(): if not storage.is_encrypted():
is_unified = False
# it is a bit wasteful load the wallet here, but that is fine # it is a bit wasteful load the wallet here, but that is fine
# because we are progressively enforcing storage encryption. # because we are progressively enforcing storage encryption.
db = WalletDB(storage.read(), manual_upgrades=False) db = WalletDB(storage.read(), manual_upgrades=False)
@ -3247,10 +3249,20 @@ def check_password_for_directory(config: SimpleConfig, old_password, new_passwor
continue continue
if new_password: if new_password:
wallet.update_password(old_password, new_password) wallet.update_password(old_password, new_password)
return failed == [] can_be_unified = failed == []
is_unified = can_be_unified and is_unified
return can_be_unified, is_unified
def update_password_for_directory(config: SimpleConfig, old_password, new_password) -> bool: def update_password_for_directory(config: SimpleConfig, old_password, new_password) -> bool:
assert new_password is not None " returns whether password is unified "
assert check_password_for_directory(config, old_password, None) if new_password is None:
return check_password_for_directory(config, old_password, new_password) # we opened a non-encrypted wallet
return False
can_be_unified, is_unified = check_password_for_directory(config, old_password, None)
if not can_be_unified:
return False
if is_unified and old_password == new_password:
return True
check_password_for_directory(config, old_password, new_password)
return True

Loading…
Cancel
Save