split wallet creation into wallet_add and wallet_create

This commit is contained in:
Lex Berezhny 2019-09-23 12:47:50 -04:00
parent 1ac3b6c173
commit f287483397
3 changed files with 58 additions and 36 deletions

View file

@ -485,6 +485,10 @@ class Config(CLIConfig):
"Directory containing a 'wallets' subdirectory with 'default_wallet' file.",
previous_names=['lbryum_wallet_dir'], metavar='DIR'
)
wallets = Strings(
"Wallet files in 'wallet_dir' to load at startup.",
['default_wallet']
)
# network
use_upnp = Toggle(

View file

@ -1064,51 +1064,66 @@ class Daemon(metaclass=JSONRPCServerType):
return self.wallet_manager.wallets
@requires("wallet")
async def jsonrpc_wallet_balance(self, wallet_id=None):
async def jsonrpc_wallet_create(
self, wallet_id, skip_on_startup=False, create_account=False, single_key=False):
"""
Return the balance of an entire wallet
Create a new wallet.
Usage:
wallet_balance [<wallet_id> | --wallet_id=<wallet_id>]
Options:
--wallet_id=<wallet_id> : (str) balance for specific wallet
Returns:
(decimal) amount of lbry credits in wallet
"""
wallet = self.wallet_manager.get_wallet_or_default(wallet_id)
balance = 0
for account in wallet.accounts:
balance += await account.get_balance()
return dewies_to_lbc(balance)
@requires("wallet")
async def jsonrpc_wallet_add(self, wallet_id, create_wallet=False, create_account=False):
"""
Add wallet.
Usage:
wallet_add (<wallet_id> | --wallet_id=<wallet_id>) [--create_wallet] [--create_account]
wallet_create (<wallet_id> | --wallet_id=<wallet_id>) [--skip_on_startup]
[--create_account] [--single_key_account]
Options:
--wallet_id=<wallet_id> : (str) wallet file name
--create_wallet : (bool) create wallet if file doesn't exist
--create_account : (bool) generate default account if wallet is empty
--skip_on_startup : (bool) don't add wallet to daemon_settings.yml
--create_account : (bool) generates the default account
--single_key : (bool) used with --create_account, creates single-key account
Returns: {Wallet}
"""
wallet_path = os.path.join(self.conf.wallet_dir, 'wallets', wallet_id)
if not os.path.exists(wallet_path) and not create_wallet:
raise Exception(f"Wallet at path '{wallet_path}' does not exist and '--create_wallet' not passed.")
for wallet in self.wallet_manager.wallets:
if wallet.id == wallet_id:
raise Exception(f"Wallet at path '{wallet_path}' already exists and is loaded.")
if os.path.exists(wallet_path):
raise Exception(f"Wallet at path '{wallet_path}' already exists, use 'wallet_add' to load wallet.")
wallet = self.wallet_manager.import_wallet(wallet_path)
if not wallet.accounts and create_account:
account = wallet.generate_account(self.ledger)
account = LBCAccount.generate(
self.ledger, wallet, address_generator={
'name': SingleKey.name if single_key else HierarchicalDeterministic.name
}
)
if self.ledger.network.is_connected:
await self.ledger.subscribe_account(account)
wallet.save()
if not skip_on_startup:
with self.conf.update_config() as c:
c.wallets += [wallet_id]
return wallet
@requires("wallet")
async def jsonrpc_wallet_add(self, wallet_id):
"""
Add existing wallet.
Usage:
wallet_add (<wallet_id> | --wallet_id=<wallet_id>)
Options:
--wallet_id=<wallet_id> : (str) wallet file name
Returns: {Wallet}
"""
wallet_path = os.path.join(self.conf.wallet_dir, 'wallets', wallet_id)
for wallet in self.wallet_manager.wallets:
if wallet.id == wallet_id:
raise Exception(f"Wallet at path '{wallet_path}' is already loaded.")
if not os.path.exists(wallet_path):
raise Exception(f"Wallet at path '{wallet_path}' was not found.")
return self.wallet_manager.import_wallet(wallet_path)
@requires("wallet")
def jsonrpc_wallet_remove(self, wallet_id):
"""

View file

@ -138,20 +138,23 @@ class LbryWalletManager(BaseWalletManager):
if not os.path.exists(wallets_directory):
os.mkdir(wallets_directory)
wallet_file_path = os.path.join(wallets_directory, 'default_wallet')
receiving_addresses, change_addresses = cls.migrate_lbryum_to_torba(wallet_file_path)
receiving_addresses, change_addresses = cls.migrate_lbryum_to_torba(
os.path.join(wallets_directory, 'default_wallet')
)
manager = cls.from_config({
'ledgers': {ledger_id: ledger_config},
'wallets': [wallet_file_path]
'wallets': [
os.path.join(wallets_directory, wallet_file) for wallet_file in settings.wallets
]
})
ledger = manager.get_or_create_ledger(ledger_id)
ledger.coin_selection_strategy = settings.coin_selection_strategy
if manager.default_account is None:
log.info('Wallet at %s is empty, generating a default account.', wallet_file_path)
manager.default_wallet.generate_account(ledger)
manager.default_wallet.save()
default_wallet = manager.default_wallet
if default_wallet.default_account is None:
log.info('Wallet at %s is empty, generating a default account.', default_wallet.id)
default_wallet.generate_account(ledger)
default_wallet.save()
if receiving_addresses or change_addresses:
if not os.path.exists(ledger.path):
os.mkdir(ledger.path)