forked from LBRYCommunity/lbry-sdk
split wallet creation into wallet_add and wallet_create
This commit is contained in:
parent
1ac3b6c173
commit
f287483397
3 changed files with 58 additions and 36 deletions
lbry/lbry
|
@ -485,6 +485,10 @@ class Config(CLIConfig):
|
||||||
"Directory containing a 'wallets' subdirectory with 'default_wallet' file.",
|
"Directory containing a 'wallets' subdirectory with 'default_wallet' file.",
|
||||||
previous_names=['lbryum_wallet_dir'], metavar='DIR'
|
previous_names=['lbryum_wallet_dir'], metavar='DIR'
|
||||||
)
|
)
|
||||||
|
wallets = Strings(
|
||||||
|
"Wallet files in 'wallet_dir' to load at startup.",
|
||||||
|
['default_wallet']
|
||||||
|
)
|
||||||
|
|
||||||
# network
|
# network
|
||||||
use_upnp = Toggle(
|
use_upnp = Toggle(
|
||||||
|
|
|
@ -1064,51 +1064,66 @@ class Daemon(metaclass=JSONRPCServerType):
|
||||||
return self.wallet_manager.wallets
|
return self.wallet_manager.wallets
|
||||||
|
|
||||||
@requires("wallet")
|
@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:
|
Usage:
|
||||||
wallet_balance [<wallet_id> | --wallet_id=<wallet_id>]
|
wallet_create (<wallet_id> | --wallet_id=<wallet_id>) [--skip_on_startup]
|
||||||
|
[--create_account] [--single_key_account]
|
||||||
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]
|
|
||||||
|
|
||||||
Options:
|
Options:
|
||||||
--wallet_id=<wallet_id> : (str) wallet file name
|
--wallet_id=<wallet_id> : (str) wallet file name
|
||||||
--create_wallet : (bool) create wallet if file doesn't exist
|
--skip_on_startup : (bool) don't add wallet to daemon_settings.yml
|
||||||
--create_account : (bool) generate default account if wallet is empty
|
--create_account : (bool) generates the default account
|
||||||
|
--single_key : (bool) used with --create_account, creates single-key account
|
||||||
|
|
||||||
Returns: {Wallet}
|
Returns: {Wallet}
|
||||||
"""
|
"""
|
||||||
wallet_path = os.path.join(self.conf.wallet_dir, 'wallets', wallet_id)
|
wallet_path = os.path.join(self.conf.wallet_dir, 'wallets', wallet_id)
|
||||||
if not os.path.exists(wallet_path) and not create_wallet:
|
for wallet in self.wallet_manager.wallets:
|
||||||
raise Exception(f"Wallet at path '{wallet_path}' does not exist and '--create_wallet' not passed.")
|
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)
|
wallet = self.wallet_manager.import_wallet(wallet_path)
|
||||||
if not wallet.accounts and create_account:
|
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:
|
if self.ledger.network.is_connected:
|
||||||
await self.ledger.subscribe_account(account)
|
await self.ledger.subscribe_account(account)
|
||||||
wallet.save()
|
wallet.save()
|
||||||
|
if not skip_on_startup:
|
||||||
|
with self.conf.update_config() as c:
|
||||||
|
c.wallets += [wallet_id]
|
||||||
return wallet
|
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")
|
@requires("wallet")
|
||||||
def jsonrpc_wallet_remove(self, wallet_id):
|
def jsonrpc_wallet_remove(self, wallet_id):
|
||||||
"""
|
"""
|
||||||
|
|
|
@ -138,20 +138,23 @@ class LbryWalletManager(BaseWalletManager):
|
||||||
if not os.path.exists(wallets_directory):
|
if not os.path.exists(wallets_directory):
|
||||||
os.mkdir(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(
|
||||||
|
os.path.join(wallets_directory, 'default_wallet')
|
||||||
receiving_addresses, change_addresses = cls.migrate_lbryum_to_torba(wallet_file_path)
|
)
|
||||||
|
|
||||||
manager = cls.from_config({
|
manager = cls.from_config({
|
||||||
'ledgers': {ledger_id: ledger_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 = manager.get_or_create_ledger(ledger_id)
|
||||||
ledger.coin_selection_strategy = settings.coin_selection_strategy
|
ledger.coin_selection_strategy = settings.coin_selection_strategy
|
||||||
if manager.default_account is None:
|
default_wallet = manager.default_wallet
|
||||||
log.info('Wallet at %s is empty, generating a default account.', wallet_file_path)
|
if default_wallet.default_account is None:
|
||||||
manager.default_wallet.generate_account(ledger)
|
log.info('Wallet at %s is empty, generating a default account.', default_wallet.id)
|
||||||
manager.default_wallet.save()
|
default_wallet.generate_account(ledger)
|
||||||
|
default_wallet.save()
|
||||||
if receiving_addresses or change_addresses:
|
if receiving_addresses or change_addresses:
|
||||||
if not os.path.exists(ledger.path):
|
if not os.path.exists(ledger.path):
|
||||||
os.mkdir(ledger.path)
|
os.mkdir(ledger.path)
|
||||||
|
|
Loading…
Add table
Reference in a new issue