refactored address_list API to be more detailed

This commit is contained in:
Lex Berezhny 2019-09-17 01:19:57 -04:00
parent 5c151a3a09
commit 81f16d7061
4 changed files with 153 additions and 115 deletions

File diff suppressed because one or more lines are too long

View file

@ -1520,26 +1520,33 @@ class Daemon(metaclass=JSONRPCServerType):
return False
@requires(WALLET_COMPONENT)
def jsonrpc_address_list(self, account_id=None, page=None, page_size=None):
def jsonrpc_address_list(self, address=None, account_id=None, page=None, page_size=None):
"""
List account addresses
List account addresses or details of single address.
Usage:
address_list [<account_id> | --account_id=<account_id>]
address_list [--address=<address>] [--account_id=<account_id>]
[--page=<page>] [--page_size=<page_size>]
Options:
--address=<address> : (str) just show details for single address
--account_id=<account_id> : (str) id of the account to use
--page=<page> : (int) page to return during paginating
--page_size=<page_size> : (int) number of items on page during pagination
Returns: {Paginated[Address]}
"""
account = self.get_account_or_default(account_id)
constraints = {
'cols': ('address', 'account', 'used_times')
}
if address:
constraints['address'] = address
if account_id:
constraints['accounts'] = [self.get_account_or_error(account_id)]
return maybe_paginate(
account.get_addresses,
account.get_address_count,
page, page_size
self.ledger.get_addresses,
self.ledger.get_address_count,
page, page_size, **constraints
)
@requires(WALLET_COMPONENT)

View file

@ -0,0 +1,12 @@
from lbry.testcase import CommandTestCase
class AddressManagement(CommandTestCase):
async def test_address_list(self):
addresses = await self.daemon.jsonrpc_address_list()
self.assertEqual(27, len(addresses))
single = await self.daemon.jsonrpc_address_list(addresses[11]['address'])
self.assertEqual(1, len(single))
self.assertEqual(single[0], addresses[11])

View file

@ -19,6 +19,7 @@ from torba.client.coinselection import CoinSelector
from torba.client.constants import COIN, NULL_HASH32
from torba.stream import StreamController
from torba.client.hash import hash160, double_sha256, sha256, Base58
from torba.client.bip32 import PubKey, PrivateKey
log = logging.getLogger(__name__)
@ -185,13 +186,13 @@ class BaseLedger(metaclass=LedgerRegistry):
if match['account'] == account.public_key.address:
return account, match
async def get_private_key_for_address(self, address):
async def get_private_key_for_address(self, address) -> Optional[PrivateKey]:
match = await self._get_account_and_address_info_for_address(address)
if match:
account, address_info = match
return account.get_private_key(address_info['chain'], address_info['position'])
async def get_public_key_for_address(self, address):
async def get_public_key_for_address(self, address) -> Optional[PubKey]:
match = await self._get_account_and_address_info_for_address(address)
if match:
account, address_info = match
@ -210,6 +211,18 @@ class BaseLedger(metaclass=LedgerRegistry):
estimators.append(utxo.get_estimator(self))
return estimators
async def get_addresses(self, **constraints):
self.constraint_account_or_all(constraints)
addresses = await self.db.get_addresses(**constraints)
for address in addresses:
public_key = await self.get_public_key_for_address(address['address'])
address['public_key'] = public_key.extended_key_string()
return addresses
def get_address_count(self, **constraints):
self.constraint_account_or_all(constraints)
return self.db.get_address_count(**constraints)
async def get_spendable_utxos(self, amount: int, funding_accounts):
async with self._utxo_reservation_lock:
txos = await self.get_effective_amount_estimators(funding_accounts)