2018-08-06 06:15:19 +02:00
|
|
|
import logging
|
2018-07-29 02:52:54 +02:00
|
|
|
from typing import Type, MutableSequence, MutableMapping
|
2018-05-25 08:03:25 +02:00
|
|
|
from twisted.internet import defer
|
|
|
|
|
2018-06-12 16:02:04 +02:00
|
|
|
from torba.baseledger import BaseLedger, LedgerRegistry
|
2018-05-25 08:03:25 +02:00
|
|
|
from torba.wallet import Wallet, WalletStorage
|
2018-07-15 03:34:07 +02:00
|
|
|
from torba.constants import COIN
|
2018-05-25 08:03:25 +02:00
|
|
|
|
2018-08-06 06:15:19 +02:00
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
2018-05-25 08:03:25 +02:00
|
|
|
|
2018-07-29 02:52:54 +02:00
|
|
|
class BaseWalletManager:
|
2018-05-25 08:03:25 +02:00
|
|
|
|
2018-07-29 02:52:54 +02:00
|
|
|
def __init__(self, wallets: MutableSequence[Wallet] = None,
|
|
|
|
ledgers: MutableMapping[Type[BaseLedger], BaseLedger] = None) -> None:
|
2018-06-14 21:17:59 +02:00
|
|
|
self.wallets = wallets or []
|
|
|
|
self.ledgers = ledgers or {}
|
2018-05-25 08:03:25 +02:00
|
|
|
self.running = False
|
|
|
|
|
|
|
|
@classmethod
|
2018-07-29 02:52:54 +02:00
|
|
|
def from_config(cls, config: dict) -> 'BaseWalletManager':
|
2018-07-01 23:20:17 +02:00
|
|
|
manager = cls()
|
|
|
|
for ledger_id, ledger_config in config.get('ledgers', {}).items():
|
|
|
|
manager.get_or_create_ledger(ledger_id, ledger_config)
|
2018-05-25 08:03:25 +02:00
|
|
|
for wallet_path in config.get('wallets', []):
|
|
|
|
wallet_storage = WalletStorage(wallet_path)
|
|
|
|
wallet = Wallet.from_storage(wallet_storage, manager)
|
2018-08-06 06:15:19 +02:00
|
|
|
if wallet.default_account is None:
|
|
|
|
ledger = manager.get_or_create_ledger('lbc_mainnet')
|
|
|
|
log.info('Wallet at %s is empty, generating a default account.', wallet_path)
|
|
|
|
wallet.generate_account(ledger)
|
|
|
|
wallet.save()
|
2018-07-01 23:20:17 +02:00
|
|
|
manager.wallets.append(wallet)
|
2018-05-25 08:03:25 +02:00
|
|
|
return manager
|
|
|
|
|
2018-06-12 16:02:04 +02:00
|
|
|
def get_or_create_ledger(self, ledger_id, ledger_config=None):
|
|
|
|
ledger_class = LedgerRegistry.get_ledger_class(ledger_id)
|
2018-05-25 08:03:25 +02:00
|
|
|
ledger = self.ledgers.get(ledger_class)
|
|
|
|
if ledger is None:
|
2018-06-14 21:17:59 +02:00
|
|
|
ledger = ledger_class(ledger_config or {})
|
2018-05-25 08:03:25 +02:00
|
|
|
self.ledgers[ledger_class] = ledger
|
|
|
|
return ledger
|
|
|
|
|
2018-07-05 03:17:45 +02:00
|
|
|
def import_wallet(self, path):
|
2018-06-14 21:17:59 +02:00
|
|
|
storage = WalletStorage(path)
|
|
|
|
wallet = Wallet.from_storage(storage, self)
|
|
|
|
self.wallets.append(wallet)
|
|
|
|
return wallet
|
2018-06-08 05:47:46 +02:00
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
2018-07-15 03:34:07 +02:00
|
|
|
def get_balances(self, confirmations=6):
|
2018-06-08 05:47:46 +02:00
|
|
|
balances = {}
|
2018-07-15 03:34:07 +02:00
|
|
|
for i, ledger in enumerate(self.ledgers.values()):
|
|
|
|
ledger_balances = balances[ledger.get_id()] = []
|
|
|
|
for j, account in enumerate(ledger.accounts):
|
|
|
|
satoshis = yield account.get_balance(confirmations)
|
|
|
|
ledger_balances.append({
|
|
|
|
'account': account.name,
|
2018-07-21 20:39:53 +02:00
|
|
|
'coins': round(satoshis/COIN, 2),
|
2018-07-15 03:34:07 +02:00
|
|
|
'satoshis': satoshis,
|
2018-07-25 18:05:25 +02:00
|
|
|
'is_default_account': i == j == 0,
|
|
|
|
'id': account.public_key.address
|
2018-07-15 03:34:07 +02:00
|
|
|
})
|
2018-06-08 05:47:46 +02:00
|
|
|
defer.returnValue(balances)
|
|
|
|
|
2018-05-25 08:03:25 +02:00
|
|
|
@property
|
|
|
|
def default_wallet(self):
|
|
|
|
for wallet in self.wallets:
|
|
|
|
return wallet
|
|
|
|
|
|
|
|
@property
|
|
|
|
def default_account(self):
|
|
|
|
for wallet in self.wallets:
|
|
|
|
return wallet.default_account
|
|
|
|
|
2018-07-17 05:58:29 +02:00
|
|
|
@property
|
|
|
|
def accounts(self):
|
|
|
|
for wallet in self.wallets:
|
|
|
|
for account in wallet.accounts:
|
|
|
|
yield account
|
|
|
|
|
2018-05-25 08:03:25 +02:00
|
|
|
@defer.inlineCallbacks
|
2018-06-08 05:47:46 +02:00
|
|
|
def start(self):
|
2018-05-25 08:03:25 +02:00
|
|
|
self.running = True
|
|
|
|
yield defer.DeferredList([
|
|
|
|
l.start() for l in self.ledgers.values()
|
|
|
|
])
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
2018-06-08 05:47:46 +02:00
|
|
|
def stop(self):
|
2018-05-25 08:03:25 +02:00
|
|
|
yield defer.DeferredList([
|
|
|
|
l.stop() for l in self.ledgers.values()
|
|
|
|
])
|
|
|
|
self.running = False
|