2018-11-19 04:54:00 +01:00
|
|
|
import asyncio
|
2018-10-09 19:13:46 +02:00
|
|
|
import random
|
2018-07-29 02:52:54 +02:00
|
|
|
import typing
|
2018-11-19 04:54:00 +01:00
|
|
|
from typing import Dict, Tuple, Type, Optional, Any, List
|
2018-06-11 15:33:32 +02:00
|
|
|
|
2018-11-04 06:55:50 +01:00
|
|
|
from torba.client.mnemonic import Mnemonic
|
|
|
|
from torba.client.bip32 import PrivateKey, PubKey, from_extended_key_string
|
|
|
|
from torba.client.hash import aes_encrypt, aes_decrypt
|
|
|
|
from torba.client.constants import COIN
|
2018-06-11 15:33:32 +02:00
|
|
|
|
2018-07-29 02:52:54 +02:00
|
|
|
if typing.TYPE_CHECKING:
|
2018-11-04 06:55:50 +01:00
|
|
|
from torba.client import baseledger, wallet as basewallet
|
2018-06-11 15:33:32 +02:00
|
|
|
|
2018-07-29 02:52:54 +02:00
|
|
|
|
2018-07-29 19:13:40 +02:00
|
|
|
class AddressManager:
|
|
|
|
|
|
|
|
name: str
|
2018-06-11 15:33:32 +02:00
|
|
|
|
2018-11-19 04:54:00 +01:00
|
|
|
__slots__ = 'account', 'public_key', 'chain_number', 'address_generator_lock'
|
2018-07-14 23:47:18 +02:00
|
|
|
|
|
|
|
def __init__(self, account, public_key, chain_number):
|
2018-06-11 15:33:32 +02:00
|
|
|
self.account = account
|
2018-07-14 23:47:18 +02:00
|
|
|
self.public_key = public_key
|
2018-06-11 15:33:32 +02:00
|
|
|
self.chain_number = chain_number
|
2018-11-19 04:54:00 +01:00
|
|
|
self.address_generator_lock = asyncio.Lock()
|
2018-06-11 15:33:32 +02:00
|
|
|
|
2018-07-29 19:13:40 +02:00
|
|
|
@classmethod
|
|
|
|
def from_dict(cls, account: 'BaseAccount', d: dict) \
|
|
|
|
-> Tuple['AddressManager', 'AddressManager']:
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
@classmethod
|
2018-07-29 20:34:56 +02:00
|
|
|
def to_dict(cls, receiving: 'AddressManager', change: 'AddressManager') -> Dict:
|
|
|
|
d: Dict[str, Any] = {'name': cls.name}
|
|
|
|
receiving_dict = receiving.to_dict_instance()
|
|
|
|
if receiving_dict:
|
|
|
|
d['receiving'] = receiving_dict
|
|
|
|
change_dict = change.to_dict_instance()
|
|
|
|
if change_dict:
|
|
|
|
d['change'] = change_dict
|
|
|
|
return d
|
|
|
|
|
|
|
|
def to_dict_instance(self) -> Optional[dict]:
|
|
|
|
raise NotImplementedError
|
2018-07-29 19:13:40 +02:00
|
|
|
|
2018-10-07 20:53:44 +02:00
|
|
|
def _query_addresses(self, **constraints):
|
2018-10-15 04:16:51 +02:00
|
|
|
return self.account.ledger.db.get_addresses(
|
2018-10-07 20:53:44 +02:00
|
|
|
account=self.account,
|
|
|
|
chain=self.chain_number,
|
|
|
|
**constraints
|
2018-06-11 15:33:32 +02:00
|
|
|
)
|
|
|
|
|
2018-07-29 19:13:40 +02:00
|
|
|
def get_private_key(self, index: int) -> PrivateKey:
|
|
|
|
raise NotImplementedError
|
|
|
|
|
2018-10-15 04:16:51 +02:00
|
|
|
async def get_max_gap(self):
|
2018-07-26 05:29:41 +02:00
|
|
|
raise NotImplementedError
|
|
|
|
|
2018-10-15 04:16:51 +02:00
|
|
|
async def ensure_address_gap(self):
|
2018-07-14 23:47:18 +02:00
|
|
|
raise NotImplementedError
|
|
|
|
|
2018-10-15 04:16:51 +02:00
|
|
|
def get_address_records(self, only_usable: bool = False, **constraints):
|
2018-07-14 23:47:18 +02:00
|
|
|
raise NotImplementedError
|
|
|
|
|
2018-11-19 04:54:00 +01:00
|
|
|
async def get_addresses(self, only_usable: bool = False, **constraints) -> List[str]:
|
2018-10-15 04:16:51 +02:00
|
|
|
records = await self.get_address_records(only_usable=only_usable, **constraints)
|
2018-08-30 17:50:11 +02:00
|
|
|
return [r['address'] for r in records]
|
2018-07-14 23:47:18 +02:00
|
|
|
|
2018-11-19 04:54:00 +01:00
|
|
|
async def get_or_create_usable_address(self) -> str:
|
2018-10-15 04:16:51 +02:00
|
|
|
addresses = await self.get_addresses(only_usable=True, limit=10)
|
2018-07-14 23:47:18 +02:00
|
|
|
if addresses:
|
2018-10-09 19:13:46 +02:00
|
|
|
return random.choice(addresses)
|
2018-10-15 04:16:51 +02:00
|
|
|
addresses = await self.ensure_address_gap()
|
2018-08-30 17:50:11 +02:00
|
|
|
return addresses[0]
|
2018-07-14 23:47:18 +02:00
|
|
|
|
|
|
|
|
2018-07-29 19:13:40 +02:00
|
|
|
class HierarchicalDeterministic(AddressManager):
|
2018-07-14 23:47:18 +02:00
|
|
|
""" Implements simple version of Bitcoin Hierarchical Deterministic key management. """
|
|
|
|
|
2018-07-29 19:13:40 +02:00
|
|
|
name = "deterministic-chain"
|
|
|
|
|
2018-07-14 23:47:18 +02:00
|
|
|
__slots__ = 'gap', 'maximum_uses_per_address'
|
|
|
|
|
2018-07-29 19:13:40 +02:00
|
|
|
def __init__(self, account: 'BaseAccount', chain: int, gap: int, maximum_uses_per_address: int) -> None:
|
|
|
|
super().__init__(account, account.public_key.child(chain), chain)
|
2018-07-14 23:47:18 +02:00
|
|
|
self.gap = gap
|
|
|
|
self.maximum_uses_per_address = maximum_uses_per_address
|
|
|
|
|
2018-07-29 19:13:40 +02:00
|
|
|
@classmethod
|
|
|
|
def from_dict(cls, account: 'BaseAccount', d: dict) -> Tuple[AddressManager, AddressManager]:
|
|
|
|
return (
|
2018-11-19 04:54:00 +01:00
|
|
|
cls(account, 0, **d.get('receiving', {'gap': 20, 'maximum_uses_per_address': 1})),
|
|
|
|
cls(account, 1, **d.get('change', {'gap': 6, 'maximum_uses_per_address': 1}))
|
2018-07-29 19:13:40 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
def to_dict_instance(self):
|
|
|
|
return {'gap': self.gap, 'maximum_uses_per_address': self.maximum_uses_per_address}
|
|
|
|
|
|
|
|
def get_private_key(self, index: int) -> PrivateKey:
|
|
|
|
return self.account.private_key.child(self.chain_number).child(index)
|
|
|
|
|
2018-11-19 04:54:00 +01:00
|
|
|
async def get_max_gap(self) -> int:
|
2018-10-15 04:16:51 +02:00
|
|
|
addresses = await self._query_addresses(order_by="position ASC")
|
2018-07-26 05:29:41 +02:00
|
|
|
max_gap = 0
|
|
|
|
current_gap = 0
|
|
|
|
for address in addresses:
|
|
|
|
if address['used_times'] == 0:
|
|
|
|
current_gap += 1
|
|
|
|
else:
|
|
|
|
max_gap = max(max_gap, current_gap)
|
|
|
|
current_gap = 0
|
2018-08-30 17:50:11 +02:00
|
|
|
return max_gap
|
2018-07-26 05:29:41 +02:00
|
|
|
|
2018-11-19 04:54:00 +01:00
|
|
|
async def ensure_address_gap(self) -> List[str]:
|
|
|
|
async with self.address_generator_lock:
|
|
|
|
addresses = await self._query_addresses(limit=self.gap, order_by="position DESC")
|
|
|
|
|
|
|
|
existing_gap = 0
|
|
|
|
for address in addresses:
|
|
|
|
if address['used_times'] == 0:
|
|
|
|
existing_gap += 1
|
|
|
|
else:
|
|
|
|
break
|
|
|
|
|
|
|
|
if existing_gap == self.gap:
|
|
|
|
return []
|
|
|
|
|
|
|
|
start = addresses[0]['position']+1 if addresses else 0
|
|
|
|
end = start + (self.gap - existing_gap)
|
|
|
|
new_keys = await self._generate_keys(start, end-1)
|
2018-11-20 00:04:07 +01:00
|
|
|
await self.account.ledger.announce_addresses(self, new_keys)
|
2018-11-19 04:54:00 +01:00
|
|
|
return new_keys
|
|
|
|
|
|
|
|
async def _generate_keys(self, start: int, end: int) -> List[str]:
|
|
|
|
if not self.address_generator_lock.locked():
|
|
|
|
raise RuntimeError('Should not be called outside of address_generator_lock.')
|
|
|
|
keys_batch, final_keys = [], []
|
|
|
|
for index in range(start, end+1):
|
|
|
|
keys_batch.append((index, self.public_key.child(index)))
|
|
|
|
if index % 180 == 0 or index == end:
|
|
|
|
await self.account.ledger.db.add_keys(
|
|
|
|
self.account, self.chain_number, keys_batch
|
|
|
|
)
|
|
|
|
final_keys.extend(keys_batch)
|
|
|
|
keys_batch.clear()
|
|
|
|
return [key[1].address for key in final_keys]
|
2018-06-11 15:33:32 +02:00
|
|
|
|
2018-10-07 20:53:44 +02:00
|
|
|
def get_address_records(self, only_usable: bool = False, **constraints):
|
|
|
|
if only_usable:
|
2018-11-19 04:54:00 +01:00
|
|
|
constraints['used_times__lt'] = self.maximum_uses_per_address
|
2018-11-20 00:04:07 +01:00
|
|
|
if 'order_by' not in constraints:
|
|
|
|
constraints['order_by'] = "used_times ASC, position ASC"
|
|
|
|
return self._query_addresses(**constraints)
|
2018-07-14 23:47:18 +02:00
|
|
|
|
|
|
|
|
2018-07-29 19:13:40 +02:00
|
|
|
class SingleKey(AddressManager):
|
|
|
|
""" Single Key address manager always returns the same address for all operations. """
|
|
|
|
|
|
|
|
name = "single-address"
|
2018-07-14 23:47:18 +02:00
|
|
|
|
|
|
|
__slots__ = ()
|
|
|
|
|
2018-07-29 19:13:40 +02:00
|
|
|
@classmethod
|
|
|
|
def from_dict(cls, account: 'BaseAccount', d: dict)\
|
|
|
|
-> Tuple[AddressManager, AddressManager]:
|
|
|
|
same_address_manager = cls(account, account.public_key, 0)
|
|
|
|
return same_address_manager, same_address_manager
|
|
|
|
|
2018-07-29 20:34:56 +02:00
|
|
|
def to_dict_instance(self):
|
|
|
|
return None
|
|
|
|
|
2018-07-29 19:13:40 +02:00
|
|
|
def get_private_key(self, index: int) -> PrivateKey:
|
|
|
|
return self.account.private_key
|
|
|
|
|
2018-11-19 04:54:00 +01:00
|
|
|
async def get_max_gap(self) -> int:
|
2018-10-15 04:16:51 +02:00
|
|
|
return 0
|
2018-07-26 05:29:41 +02:00
|
|
|
|
2018-11-19 04:54:00 +01:00
|
|
|
async def ensure_address_gap(self) -> List[str]:
|
|
|
|
async with self.address_generator_lock:
|
|
|
|
exists = await self.get_address_records()
|
|
|
|
if not exists:
|
|
|
|
await self.account.ledger.db.add_keys(
|
|
|
|
self.account, self.chain_number, [(0, self.public_key)]
|
|
|
|
)
|
|
|
|
new_keys = [self.public_key.address]
|
2018-11-20 00:04:07 +01:00
|
|
|
await self.account.ledger.announce_addresses(self, new_keys)
|
2018-11-19 04:54:00 +01:00
|
|
|
return new_keys
|
|
|
|
return []
|
2018-07-14 23:47:18 +02:00
|
|
|
|
2018-10-15 04:16:51 +02:00
|
|
|
def get_address_records(self, only_usable: bool = False, **constraints):
|
2018-10-07 20:53:44 +02:00
|
|
|
return self._query_addresses(**constraints)
|
2018-06-11 15:33:32 +02:00
|
|
|
|
|
|
|
|
2018-07-29 02:52:54 +02:00
|
|
|
class BaseAccount:
|
2018-06-11 15:33:32 +02:00
|
|
|
|
|
|
|
mnemonic_class = Mnemonic
|
|
|
|
private_key_class = PrivateKey
|
|
|
|
public_key_class = PubKey
|
2018-07-29 20:34:56 +02:00
|
|
|
address_generators: Dict[str, Type[AddressManager]] = {
|
2018-07-29 19:13:40 +02:00
|
|
|
SingleKey.name: SingleKey,
|
|
|
|
HierarchicalDeterministic.name: HierarchicalDeterministic,
|
|
|
|
}
|
2018-06-11 15:33:32 +02:00
|
|
|
|
2018-08-08 03:47:30 +02:00
|
|
|
def __init__(self, ledger: 'baseledger.BaseLedger', wallet: 'basewallet.Wallet', name: str,
|
2018-09-25 05:12:46 +02:00
|
|
|
seed: str, private_key_string: str, encrypted: bool,
|
|
|
|
private_key: Optional[PrivateKey], public_key: PubKey,
|
|
|
|
address_generator: dict) -> None:
|
2018-06-12 16:02:04 +02:00
|
|
|
self.ledger = ledger
|
2018-08-08 03:31:29 +02:00
|
|
|
self.wallet = wallet
|
2018-08-30 17:50:11 +02:00
|
|
|
self.id = public_key.address
|
2018-07-14 23:47:18 +02:00
|
|
|
self.name = name
|
2018-06-12 16:02:04 +02:00
|
|
|
self.seed = seed
|
2018-09-25 05:12:46 +02:00
|
|
|
self.private_key_string = private_key_string
|
|
|
|
self.password: Optional[str] = None
|
2018-11-19 19:51:25 +01:00
|
|
|
self.private_key_encryption_init_vector: Optional[bytes] = None
|
|
|
|
self.seed_encryption_init_vector: Optional[bytes] = None
|
|
|
|
|
2018-06-12 16:02:04 +02:00
|
|
|
self.encrypted = encrypted
|
2018-09-21 20:49:16 +02:00
|
|
|
self.serialize_encrypted = encrypted
|
2018-09-25 05:12:46 +02:00
|
|
|
self.private_key = private_key
|
2018-06-12 16:02:04 +02:00
|
|
|
self.public_key = public_key
|
2018-07-29 19:13:40 +02:00
|
|
|
generator_name = address_generator.get('name', HierarchicalDeterministic.name)
|
2018-07-29 20:34:56 +02:00
|
|
|
self.address_generator = self.address_generators[generator_name]
|
2018-07-29 19:13:40 +02:00
|
|
|
self.receiving, self.change = self.address_generator.from_dict(self, address_generator)
|
2018-11-19 04:54:00 +01:00
|
|
|
self.address_managers = {am.chain_number: am for am in {self.receiving, self.change}}
|
2018-06-14 02:57:57 +02:00
|
|
|
ledger.add_account(self)
|
2018-08-08 03:31:29 +02:00
|
|
|
wallet.add_account(self)
|
2018-06-11 15:33:32 +02:00
|
|
|
|
|
|
|
@classmethod
|
2018-08-08 03:47:30 +02:00
|
|
|
def generate(cls, ledger: 'baseledger.BaseLedger', wallet: 'basewallet.Wallet',
|
2018-08-08 03:36:44 +02:00
|
|
|
name: str = None, address_generator: dict = None):
|
2018-08-08 03:31:29 +02:00
|
|
|
return cls.from_dict(ledger, wallet, {
|
2018-08-06 08:52:52 +02:00
|
|
|
'name': name,
|
|
|
|
'seed': cls.mnemonic_class().make_seed(),
|
|
|
|
'address_generator': address_generator or {}
|
|
|
|
})
|
2018-06-11 15:33:32 +02:00
|
|
|
|
|
|
|
@classmethod
|
2018-07-29 02:52:54 +02:00
|
|
|
def get_private_key_from_seed(cls, ledger: 'baseledger.BaseLedger', seed: str, password: str):
|
2018-06-11 15:33:32 +02:00
|
|
|
return cls.private_key_class.from_seed(
|
|
|
|
ledger, cls.mnemonic_class.mnemonic_to_seed(seed, password)
|
|
|
|
)
|
|
|
|
|
|
|
|
@classmethod
|
2018-08-08 03:47:30 +02:00
|
|
|
def from_dict(cls, ledger: 'baseledger.BaseLedger', wallet: 'basewallet.Wallet', d: dict):
|
2018-08-06 08:52:52 +02:00
|
|
|
seed = d.get('seed', '')
|
2018-09-25 05:12:46 +02:00
|
|
|
private_key_string = d.get('private_key', '')
|
|
|
|
private_key = None
|
2018-08-06 08:52:52 +02:00
|
|
|
public_key = None
|
|
|
|
encrypted = d.get('encrypted', False)
|
|
|
|
if not encrypted:
|
|
|
|
if seed:
|
|
|
|
private_key = cls.get_private_key_from_seed(ledger, seed, '')
|
|
|
|
public_key = private_key.public_key
|
|
|
|
elif private_key:
|
2018-09-25 05:12:46 +02:00
|
|
|
private_key = from_extended_key_string(ledger, private_key_string)
|
2018-08-06 08:52:52 +02:00
|
|
|
public_key = private_key.public_key
|
|
|
|
if public_key is None:
|
2018-06-11 15:33:32 +02:00
|
|
|
public_key = from_extended_key_string(ledger, d['public_key'])
|
2018-08-06 08:52:52 +02:00
|
|
|
name = d.get('name')
|
|
|
|
if not name:
|
|
|
|
name = 'Account #{}'.format(public_key.address)
|
2018-07-29 19:13:40 +02:00
|
|
|
return cls(
|
2018-06-11 15:33:32 +02:00
|
|
|
ledger=ledger,
|
2018-08-08 03:31:29 +02:00
|
|
|
wallet=wallet,
|
2018-08-06 08:52:52 +02:00
|
|
|
name=name,
|
|
|
|
seed=seed,
|
2018-09-25 05:12:46 +02:00
|
|
|
private_key_string=private_key_string,
|
2018-08-06 08:52:52 +02:00
|
|
|
encrypted=encrypted,
|
2018-06-11 15:33:32 +02:00
|
|
|
private_key=private_key,
|
|
|
|
public_key=public_key,
|
2018-08-06 08:52:52 +02:00
|
|
|
address_generator=d.get('address_generator', {})
|
2018-06-11 15:33:32 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
def to_dict(self):
|
2018-09-25 05:12:46 +02:00
|
|
|
private_key_string, seed = self.private_key_string, self.seed
|
2018-07-14 23:47:18 +02:00
|
|
|
if not self.encrypted and self.private_key:
|
2018-09-25 05:12:46 +02:00
|
|
|
private_key_string = self.private_key.extended_key_string()
|
2018-09-21 20:49:16 +02:00
|
|
|
if not self.encrypted and self.serialize_encrypted:
|
2018-11-19 19:51:25 +01:00
|
|
|
assert None not in [self.seed_encryption_init_vector, self.private_key_encryption_init_vector]
|
|
|
|
private_key_string = aes_encrypt(
|
|
|
|
self.password, private_key_string, self.private_key_encryption_init_vector
|
|
|
|
)
|
|
|
|
seed = aes_encrypt(self.password, self.seed, self.seed_encryption_init_vector)
|
2018-07-29 19:13:40 +02:00
|
|
|
return {
|
2018-06-11 15:33:32 +02:00
|
|
|
'ledger': self.ledger.get_id(),
|
2018-07-14 23:47:18 +02:00
|
|
|
'name': self.name,
|
2018-09-21 20:17:17 +02:00
|
|
|
'seed': seed,
|
2018-11-19 19:51:25 +01:00
|
|
|
'encrypted': self.serialize_encrypted,
|
2018-09-25 05:12:46 +02:00
|
|
|
'private_key': private_key_string,
|
2018-07-15 03:34:07 +02:00
|
|
|
'public_key': self.public_key.extended_key_string(),
|
2018-07-29 19:13:40 +02:00
|
|
|
'address_generator': self.address_generator.to_dict(self.receiving, self.change)
|
2018-06-11 15:33:32 +02:00
|
|
|
}
|
|
|
|
|
2018-10-15 04:16:51 +02:00
|
|
|
async def get_details(self, show_seed=False, **kwargs):
|
|
|
|
satoshis = await self.get_balance(**kwargs)
|
2018-08-30 17:50:11 +02:00
|
|
|
details = {
|
|
|
|
'id': self.id,
|
|
|
|
'name': self.name,
|
|
|
|
'coins': round(satoshis/COIN, 2),
|
|
|
|
'satoshis': satoshis,
|
|
|
|
'encrypted': self.encrypted,
|
|
|
|
'public_key': self.public_key.extended_key_string(),
|
|
|
|
'address_generator': self.address_generator.to_dict(self.receiving, self.change)
|
|
|
|
}
|
|
|
|
if show_seed:
|
|
|
|
details['seed'] = self.seed
|
|
|
|
return details
|
|
|
|
|
2018-09-21 20:49:16 +02:00
|
|
|
def decrypt(self, password: str) -> None:
|
2018-06-11 15:33:32 +02:00
|
|
|
assert self.encrypted, "Key is not encrypted."
|
2018-11-19 19:51:25 +01:00
|
|
|
self.seed, self.seed_encryption_init_vector = aes_decrypt(password, self.seed)
|
|
|
|
pk_string, self.private_key_encryption_init_vector = aes_decrypt(password, self.private_key_string)
|
2018-09-21 19:10:16 +02:00
|
|
|
self.private_key = from_extended_key_string(
|
2018-11-19 19:51:25 +01:00
|
|
|
self.ledger, pk_string
|
2018-09-21 19:10:16 +02:00
|
|
|
)
|
2018-09-21 20:17:17 +02:00
|
|
|
self.password = password
|
2018-06-11 15:33:32 +02:00
|
|
|
self.encrypted = False
|
|
|
|
|
2018-09-21 20:49:16 +02:00
|
|
|
def encrypt(self, password: str) -> None:
|
2018-06-11 15:33:32 +02:00
|
|
|
assert not self.encrypted, "Key is already encrypted."
|
2018-09-21 20:49:16 +02:00
|
|
|
assert isinstance(self.private_key, PrivateKey)
|
2018-11-19 19:51:25 +01:00
|
|
|
|
|
|
|
self.seed = aes_encrypt(password, self.seed, self.seed_encryption_init_vector)
|
2018-09-25 05:12:46 +02:00
|
|
|
self.private_key_string = aes_encrypt(
|
2018-11-19 19:51:25 +01:00
|
|
|
password, self.private_key.extended_key_string(), self.private_key_encryption_init_vector
|
2018-09-25 05:12:46 +02:00
|
|
|
)
|
|
|
|
self.private_key = None
|
2018-09-21 20:17:17 +02:00
|
|
|
self.password = None
|
2018-06-11 15:33:32 +02:00
|
|
|
self.encrypted = True
|
|
|
|
|
2018-10-15 04:16:51 +02:00
|
|
|
async def ensure_address_gap(self):
|
2018-06-11 15:33:32 +02:00
|
|
|
addresses = []
|
2018-11-19 04:54:00 +01:00
|
|
|
for address_manager in self.address_managers.values():
|
2018-10-15 04:16:51 +02:00
|
|
|
new_addresses = await address_manager.ensure_address_gap()
|
2018-06-11 15:33:32 +02:00
|
|
|
addresses.extend(new_addresses)
|
2018-08-30 17:50:11 +02:00
|
|
|
return addresses
|
2018-06-11 15:33:32 +02:00
|
|
|
|
2018-11-19 04:54:00 +01:00
|
|
|
async def get_addresses(self, **constraints) -> List[str]:
|
2018-10-15 04:16:51 +02:00
|
|
|
rows = await self.ledger.db.select_addresses('address', account=self, **constraints)
|
2018-10-07 20:53:44 +02:00
|
|
|
return [r[0] for r in rows]
|
2018-06-12 16:02:04 +02:00
|
|
|
|
2018-10-15 04:16:51 +02:00
|
|
|
def get_address_records(self, **constraints):
|
2018-10-07 20:53:44 +02:00
|
|
|
return self.ledger.db.get_addresses(account=self, **constraints)
|
2018-06-12 16:02:04 +02:00
|
|
|
|
2018-10-15 04:16:51 +02:00
|
|
|
def get_address_count(self, **constraints):
|
2018-10-10 01:29:20 +02:00
|
|
|
return self.ledger.db.get_address_count(account=self, **constraints)
|
|
|
|
|
2018-07-29 02:52:54 +02:00
|
|
|
def get_private_key(self, chain: int, index: int) -> PrivateKey:
|
2018-06-11 15:33:32 +02:00
|
|
|
assert not self.encrypted, "Cannot get private key on encrypted wallet account."
|
2018-11-19 04:54:00 +01:00
|
|
|
return self.address_managers[chain].get_private_key(index)
|
2018-06-11 15:33:32 +02:00
|
|
|
|
2018-10-07 20:53:44 +02:00
|
|
|
def get_balance(self, confirmations: int = 0, **constraints):
|
2018-07-17 05:58:29 +02:00
|
|
|
if confirmations > 0:
|
2018-07-14 23:47:18 +02:00
|
|
|
height = self.ledger.headers.height - (confirmations-1)
|
2018-07-23 04:52:21 +02:00
|
|
|
constraints.update({'height__lte': height, 'height__gt': 0})
|
2018-10-07 20:53:44 +02:00
|
|
|
return self.ledger.db.get_balance(account=self, **constraints)
|
2018-07-10 02:22:04 +02:00
|
|
|
|
2018-10-15 04:16:51 +02:00
|
|
|
async def get_max_gap(self):
|
|
|
|
change_gap = await self.change.get_max_gap()
|
|
|
|
receiving_gap = await self.receiving.get_max_gap()
|
2018-08-30 17:50:11 +02:00
|
|
|
return {
|
2018-07-26 05:29:41 +02:00
|
|
|
'max_change_gap': change_gap,
|
|
|
|
'max_receiving_gap': receiving_gap,
|
2018-08-30 17:50:11 +02:00
|
|
|
}
|
2018-07-26 05:29:41 +02:00
|
|
|
|
2018-10-07 20:53:44 +02:00
|
|
|
def get_utxos(self, **constraints):
|
2018-10-03 13:08:02 +02:00
|
|
|
return self.ledger.db.get_utxos(account=self, **constraints)
|
2018-08-06 06:15:19 +02:00
|
|
|
|
2018-10-10 01:29:20 +02:00
|
|
|
def get_utxo_count(self, **constraints):
|
|
|
|
return self.ledger.db.get_utxo_count(account=self, **constraints)
|
|
|
|
|
2018-10-16 17:56:53 +02:00
|
|
|
def get_transactions(self, **constraints):
|
2018-10-07 20:53:44 +02:00
|
|
|
return self.ledger.db.get_transactions(account=self, **constraints)
|
2018-09-21 15:47:31 +02:00
|
|
|
|
2018-10-10 01:29:20 +02:00
|
|
|
def get_transaction_count(self, **constraints):
|
|
|
|
return self.ledger.db.get_transaction_count(account=self, **constraints)
|
|
|
|
|
2018-10-15 04:16:51 +02:00
|
|
|
async def fund(self, to_account, amount=None, everything=False,
|
2018-10-15 06:04:25 +02:00
|
|
|
outputs=1, broadcast=False, **constraints):
|
2018-08-06 06:15:19 +02:00
|
|
|
assert self.ledger == to_account.ledger, 'Can only transfer between accounts of the same ledger.'
|
|
|
|
tx_class = self.ledger.transaction_class
|
|
|
|
if everything:
|
2018-10-15 04:16:51 +02:00
|
|
|
utxos = await self.get_utxos(**constraints)
|
|
|
|
await self.ledger.reserve_outputs(utxos)
|
|
|
|
tx = await tx_class.create(
|
2018-08-06 06:15:19 +02:00
|
|
|
inputs=[tx_class.input_class.spend(txo) for txo in utxos],
|
|
|
|
outputs=[],
|
|
|
|
funding_accounts=[self],
|
|
|
|
change_account=to_account
|
|
|
|
)
|
|
|
|
elif amount > 0:
|
2018-10-15 04:16:51 +02:00
|
|
|
to_address = await to_account.change.get_or_create_usable_address()
|
2018-08-06 06:15:19 +02:00
|
|
|
to_hash160 = to_account.ledger.address_to_hash160(to_address)
|
2018-10-15 04:16:51 +02:00
|
|
|
tx = await tx_class.create(
|
2018-08-06 06:15:19 +02:00
|
|
|
inputs=[],
|
|
|
|
outputs=[
|
|
|
|
tx_class.output_class.pay_pubkey_hash(amount//outputs, to_hash160)
|
|
|
|
for _ in range(outputs)
|
|
|
|
],
|
|
|
|
funding_accounts=[self],
|
|
|
|
change_account=self
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
raise ValueError('An amount is required.')
|
|
|
|
|
|
|
|
if broadcast:
|
2018-10-15 04:16:51 +02:00
|
|
|
await self.ledger.broadcast(tx)
|
2018-08-06 06:15:19 +02:00
|
|
|
else:
|
2018-10-15 04:16:51 +02:00
|
|
|
await self.ledger.release_outputs(
|
2018-08-06 06:15:19 +02:00
|
|
|
[txi.txo_ref.txo for txi in tx.inputs]
|
|
|
|
)
|
|
|
|
|
2018-08-30 17:50:11 +02:00
|
|
|
return tx
|