tdaemon: make 'wallets' dict private - electrum - Electrum Bitcoin wallet HTML git clone https://git.parazyd.org/electrum DIR Log DIR Files DIR Refs DIR Submodules --- DIR commit ef5a5151e3d121d28e180d8e27772e1de3ed2aa9 DIR parent befa8ea771a6ed3494cc9ba9061d8fbaa9e0807d HTML Author: SomberNight <somber.night@protonmail.com> Date: Mon, 9 Sep 2019 22:15:11 +0200 daemon: make 'wallets' dict private especially as keys (paths) need to be standardized, this should not be exposed Diffstat: M electrum/commands.py | 14 +++++++------- M electrum/daemon.py | 31 ++++++++++++++++--------------- 2 files changed, 23 insertions(+), 22 deletions(-) --- DIR diff --git a/electrum/commands.py b/electrum/commands.py t@@ -34,7 +34,7 @@ import operator import asyncio from functools import wraps, partial from decimal import Decimal -from typing import Optional, TYPE_CHECKING +from typing import Optional, TYPE_CHECKING, Dict from .import util, ecc from .util import bfh, bh2u, format_satoshis, json_decode, json_encode, is_hash256_str, is_hex_str, to_bytes, timestamp_to_datetime t@@ -61,7 +61,7 @@ if TYPE_CHECKING: from .daemon import Daemon -known_commands = {} +known_commands = {} # type: Dict[str, Command] def satoshis(amount): t@@ -96,8 +96,8 @@ def command(s): known_commands[name] = Command(func, s) @wraps(func) async def func_wrapper(*args, **kwargs): - cmd_runner = args[0] - cmd = known_commands[func.__name__] + cmd_runner = args[0] # type: Commands + cmd = known_commands[func.__name__] # type: Command password = kwargs.get('password') daemon = cmd_runner.daemon if daemon: t@@ -105,8 +105,7 @@ def command(s): kwargs['wallet_path'] = daemon.config.get_wallet_path() if cmd.requires_wallet: wallet_path = kwargs.pop('wallet_path') - wallet_path = standardize_path(wallet_path) - wallet = daemon.wallets.get(wallet_path) + wallet = daemon.get_wallet(wallet_path) if wallet is None: raise Exception('wallet not loaded') kwargs['wallet'] = wallet t@@ -185,7 +184,8 @@ class Commands: @command('n') async def list_wallets(self): """List wallets open in daemon""" - return [{'path':k, 'synchronized':w.is_up_to_date()} for k, w in self.daemon.wallets.items()] + return [{'path': path, 'synchronized': w.is_up_to_date()} + for path, w in self.daemon.get_wallets().items()] @command('n') async def load_wallet(self, wallet_path=None): DIR diff --git a/electrum/daemon.py b/electrum/daemon.py t@@ -174,7 +174,7 @@ class WatchTowerServer(Logger): class HttpServer(Logger): - def __init__(self, daemon): + def __init__(self, daemon: 'Daemon'): Logger.__init__(self) self.daemon = daemon self.config = daemon.config t@@ -287,7 +287,7 @@ class Daemon(Logger): self.fx = FxThread(config, self.network) self.gui_object = None # path -> wallet; make sure path is standardized. - self.wallets = {} # type: Dict[str, Abstract_Wallet] + self._wallets = {} # type: Dict[str, Abstract_Wallet] jobs = [self.fx.run] # Setup JSONRPC server if listen_jsonrpc: t@@ -379,8 +379,8 @@ class Daemon(Logger): def load_wallet(self, path, password) -> Optional[Abstract_Wallet]: path = standardize_path(path) # wizard will be launched if we return - if path in self.wallets: - wallet = self.wallets[path] + if path in self._wallets: + wallet = self._wallets[path] return wallet storage = WalletStorage(path, manual_upgrades=True) if not storage.file_exists(): t@@ -397,38 +397,39 @@ class Daemon(Logger): return wallet = Wallet(storage) wallet.start_network(self.network) - self.wallets[path] = wallet + self._wallets[path] = wallet self.wallet = wallet return wallet - def add_wallet(self, wallet: Abstract_Wallet): + def add_wallet(self, wallet: Abstract_Wallet) -> None: path = wallet.storage.path path = standardize_path(path) - self.wallets[path] = wallet + self._wallets[path] = wallet - def get_wallet(self, path): + def get_wallet(self, path: str) -> Abstract_Wallet: path = standardize_path(path) - return self.wallets.get(path) + return self._wallets.get(path) - def delete_wallet(self, path): + def get_wallets(self) -> Dict[str, Abstract_Wallet]: + return dict(self._wallets) # copy + + def delete_wallet(self, path: str) -> bool: self.stop_wallet(path) if os.path.exists(path): os.unlink(path) return True return False - def stop_wallet(self, path) -> bool: + def stop_wallet(self, path: str) -> bool: """Returns True iff a wallet was found.""" path = standardize_path(path) - wallet = self.wallets.pop(path, None) + wallet = self._wallets.pop(path, None) if not wallet: return False wallet.stop_threads() return True async def run_cmdline(self, config_options): - password = config_options.get('password') - new_password = config_options.get('new_password') config = SimpleConfig(config_options) # FIXME this is ugly... config.fee_estimates = self.network.config.fee_estimates.copy() t@@ -474,7 +475,7 @@ class Daemon(Logger): if self.gui_object: self.gui_object.stop() # stop network/wallets - for k, wallet in self.wallets.items(): + for k, wallet in self._wallets.items(): wallet.stop_threads() if self.network: self.logger.info("shutting down network")