mirror of
https://github.com/LBRYFoundation/LBRY-Vault.git
synced 2025-08-31 17:31:36 +00:00
daemon: make 'wallets' dict private
especially as keys (paths) need to be standardized, this should not be exposed
This commit is contained in:
parent
befa8ea771
commit
ef5a5151e3
2 changed files with 23 additions and 22 deletions
|
@ -34,7 +34,7 @@ import operator
|
||||||
import asyncio
|
import asyncio
|
||||||
from functools import wraps, partial
|
from functools import wraps, partial
|
||||||
from decimal import Decimal
|
from decimal import Decimal
|
||||||
from typing import Optional, TYPE_CHECKING
|
from typing import Optional, TYPE_CHECKING, Dict
|
||||||
|
|
||||||
from .import util, ecc
|
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
|
from .util import bfh, bh2u, format_satoshis, json_decode, json_encode, is_hash256_str, is_hex_str, to_bytes, timestamp_to_datetime
|
||||||
|
@ -61,7 +61,7 @@ if TYPE_CHECKING:
|
||||||
from .daemon import Daemon
|
from .daemon import Daemon
|
||||||
|
|
||||||
|
|
||||||
known_commands = {}
|
known_commands = {} # type: Dict[str, Command]
|
||||||
|
|
||||||
|
|
||||||
def satoshis(amount):
|
def satoshis(amount):
|
||||||
|
@ -96,8 +96,8 @@ def command(s):
|
||||||
known_commands[name] = Command(func, s)
|
known_commands[name] = Command(func, s)
|
||||||
@wraps(func)
|
@wraps(func)
|
||||||
async def func_wrapper(*args, **kwargs):
|
async def func_wrapper(*args, **kwargs):
|
||||||
cmd_runner = args[0]
|
cmd_runner = args[0] # type: Commands
|
||||||
cmd = known_commands[func.__name__]
|
cmd = known_commands[func.__name__] # type: Command
|
||||||
password = kwargs.get('password')
|
password = kwargs.get('password')
|
||||||
daemon = cmd_runner.daemon
|
daemon = cmd_runner.daemon
|
||||||
if daemon:
|
if daemon:
|
||||||
|
@ -105,8 +105,7 @@ def command(s):
|
||||||
kwargs['wallet_path'] = daemon.config.get_wallet_path()
|
kwargs['wallet_path'] = daemon.config.get_wallet_path()
|
||||||
if cmd.requires_wallet:
|
if cmd.requires_wallet:
|
||||||
wallet_path = kwargs.pop('wallet_path')
|
wallet_path = kwargs.pop('wallet_path')
|
||||||
wallet_path = standardize_path(wallet_path)
|
wallet = daemon.get_wallet(wallet_path)
|
||||||
wallet = daemon.wallets.get(wallet_path)
|
|
||||||
if wallet is None:
|
if wallet is None:
|
||||||
raise Exception('wallet not loaded')
|
raise Exception('wallet not loaded')
|
||||||
kwargs['wallet'] = wallet
|
kwargs['wallet'] = wallet
|
||||||
|
@ -185,7 +184,8 @@ class Commands:
|
||||||
@command('n')
|
@command('n')
|
||||||
async def list_wallets(self):
|
async def list_wallets(self):
|
||||||
"""List wallets open in daemon"""
|
"""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')
|
@command('n')
|
||||||
async def load_wallet(self, wallet_path=None):
|
async def load_wallet(self, wallet_path=None):
|
||||||
|
|
|
@ -174,7 +174,7 @@ class WatchTowerServer(Logger):
|
||||||
|
|
||||||
class HttpServer(Logger):
|
class HttpServer(Logger):
|
||||||
|
|
||||||
def __init__(self, daemon):
|
def __init__(self, daemon: 'Daemon'):
|
||||||
Logger.__init__(self)
|
Logger.__init__(self)
|
||||||
self.daemon = daemon
|
self.daemon = daemon
|
||||||
self.config = daemon.config
|
self.config = daemon.config
|
||||||
|
@ -287,7 +287,7 @@ class Daemon(Logger):
|
||||||
self.fx = FxThread(config, self.network)
|
self.fx = FxThread(config, self.network)
|
||||||
self.gui_object = None
|
self.gui_object = None
|
||||||
# path -> wallet; make sure path is standardized.
|
# 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]
|
jobs = [self.fx.run]
|
||||||
# Setup JSONRPC server
|
# Setup JSONRPC server
|
||||||
if listen_jsonrpc:
|
if listen_jsonrpc:
|
||||||
|
@ -379,8 +379,8 @@ class Daemon(Logger):
|
||||||
def load_wallet(self, path, password) -> Optional[Abstract_Wallet]:
|
def load_wallet(self, path, password) -> Optional[Abstract_Wallet]:
|
||||||
path = standardize_path(path)
|
path = standardize_path(path)
|
||||||
# wizard will be launched if we return
|
# wizard will be launched if we return
|
||||||
if path in self.wallets:
|
if path in self._wallets:
|
||||||
wallet = self.wallets[path]
|
wallet = self._wallets[path]
|
||||||
return wallet
|
return wallet
|
||||||
storage = WalletStorage(path, manual_upgrades=True)
|
storage = WalletStorage(path, manual_upgrades=True)
|
||||||
if not storage.file_exists():
|
if not storage.file_exists():
|
||||||
|
@ -397,38 +397,39 @@ class Daemon(Logger):
|
||||||
return
|
return
|
||||||
wallet = Wallet(storage)
|
wallet = Wallet(storage)
|
||||||
wallet.start_network(self.network)
|
wallet.start_network(self.network)
|
||||||
self.wallets[path] = wallet
|
self._wallets[path] = wallet
|
||||||
self.wallet = wallet
|
self.wallet = wallet
|
||||||
return wallet
|
return wallet
|
||||||
|
|
||||||
def add_wallet(self, wallet: Abstract_Wallet):
|
def add_wallet(self, wallet: Abstract_Wallet) -> None:
|
||||||
path = wallet.storage.path
|
path = wallet.storage.path
|
||||||
path = standardize_path(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)
|
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)
|
self.stop_wallet(path)
|
||||||
if os.path.exists(path):
|
if os.path.exists(path):
|
||||||
os.unlink(path)
|
os.unlink(path)
|
||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def stop_wallet(self, path) -> bool:
|
def stop_wallet(self, path: str) -> bool:
|
||||||
"""Returns True iff a wallet was found."""
|
"""Returns True iff a wallet was found."""
|
||||||
path = standardize_path(path)
|
path = standardize_path(path)
|
||||||
wallet = self.wallets.pop(path, None)
|
wallet = self._wallets.pop(path, None)
|
||||||
if not wallet:
|
if not wallet:
|
||||||
return False
|
return False
|
||||||
wallet.stop_threads()
|
wallet.stop_threads()
|
||||||
return True
|
return True
|
||||||
|
|
||||||
async def run_cmdline(self, config_options):
|
async def run_cmdline(self, config_options):
|
||||||
password = config_options.get('password')
|
|
||||||
new_password = config_options.get('new_password')
|
|
||||||
config = SimpleConfig(config_options)
|
config = SimpleConfig(config_options)
|
||||||
# FIXME this is ugly...
|
# FIXME this is ugly...
|
||||||
config.fee_estimates = self.network.config.fee_estimates.copy()
|
config.fee_estimates = self.network.config.fee_estimates.copy()
|
||||||
|
@ -474,7 +475,7 @@ class Daemon(Logger):
|
||||||
if self.gui_object:
|
if self.gui_object:
|
||||||
self.gui_object.stop()
|
self.gui_object.stop()
|
||||||
# stop network/wallets
|
# stop network/wallets
|
||||||
for k, wallet in self.wallets.items():
|
for k, wallet in self._wallets.items():
|
||||||
wallet.stop_threads()
|
wallet.stop_threads()
|
||||||
if self.network:
|
if self.network:
|
||||||
self.logger.info("shutting down network")
|
self.logger.info("shutting down network")
|
||||||
|
|
Loading…
Add table
Reference in a new issue