forked from LBRYCommunity/lbry-sdk
gave accounts reference to wallet so that they can save themselves after being modified
This commit is contained in:
parent
d4107600a5
commit
e2537e43b5
5 changed files with 30 additions and 26 deletions
|
@ -4,6 +4,7 @@ from twisted.internet import defer
|
|||
|
||||
from torba.coin.bitcoinsegwit import MainNetLedger
|
||||
from torba.baseaccount import HierarchicalDeterministic, SingleKey
|
||||
from torba.wallet import Wallet
|
||||
|
||||
|
||||
class TestHierarchicalDeterministicAccount(unittest.TestCase):
|
||||
|
@ -12,7 +13,7 @@ class TestHierarchicalDeterministicAccount(unittest.TestCase):
|
|||
def setUp(self):
|
||||
self.ledger = MainNetLedger({'db': MainNetLedger.database_class(':memory:')})
|
||||
yield self.ledger.db.start()
|
||||
self.account = self.ledger.account_class.generate(self.ledger, u"torba")
|
||||
self.account = self.ledger.account_class.generate(self.ledger, Wallet(), "torba")
|
||||
|
||||
@defer.inlineCallbacks
|
||||
def test_generate_account(self):
|
||||
|
@ -93,7 +94,7 @@ class TestHierarchicalDeterministicAccount(unittest.TestCase):
|
|||
@defer.inlineCallbacks
|
||||
def test_generate_account_from_seed(self):
|
||||
account = self.ledger.account_class.from_dict(
|
||||
self.ledger, {
|
||||
self.ledger, Wallet(), {
|
||||
"seed": "carbon smart garage balance margin twelve chest sword "
|
||||
"toast envelope bottom stomach absent",
|
||||
"address_generator": {
|
||||
|
@ -152,7 +153,7 @@ class TestHierarchicalDeterministicAccount(unittest.TestCase):
|
|||
}
|
||||
}
|
||||
|
||||
account = self.ledger.account_class.from_dict(self.ledger, account_data)
|
||||
account = self.ledger.account_class.from_dict(self.ledger, Wallet(), account_data)
|
||||
|
||||
yield account.ensure_address_gap()
|
||||
|
||||
|
@ -172,7 +173,8 @@ class TestSingleKeyAccount(unittest.TestCase):
|
|||
def setUp(self):
|
||||
self.ledger = MainNetLedger({'db': MainNetLedger.database_class(':memory:')})
|
||||
yield self.ledger.db.start()
|
||||
self.account = self.ledger.account_class.generate(self.ledger, u"torba", {'name': 'single-address'})
|
||||
self.account = self.ledger.account_class.generate(
|
||||
self.ledger, Wallet(), "torba", {'name': 'single-address'})
|
||||
|
||||
@defer.inlineCallbacks
|
||||
def test_generate_account(self):
|
||||
|
@ -251,7 +253,7 @@ class TestSingleKeyAccount(unittest.TestCase):
|
|||
@defer.inlineCallbacks
|
||||
def test_generate_account_from_seed(self):
|
||||
account = self.ledger.account_class.from_dict(
|
||||
self.ledger, {
|
||||
self.ledger, Wallet(), {
|
||||
"seed":
|
||||
"carbon smart garage balance margin twelve chest sword toas"
|
||||
"t envelope bottom stomach absent",
|
||||
|
@ -303,7 +305,7 @@ class TestSingleKeyAccount(unittest.TestCase):
|
|||
'address_generator': {'name': 'single-address'}
|
||||
}
|
||||
|
||||
account = self.ledger.account_class.from_dict(self.ledger, account_data)
|
||||
account = self.ledger.account_class.from_dict(self.ledger, Wallet(), account_data)
|
||||
|
||||
yield account.ensure_address_gap()
|
||||
|
||||
|
|
|
@ -3,6 +3,7 @@ from twisted.trial import unittest
|
|||
from twisted.internet import defer
|
||||
|
||||
from torba.coin.bitcoinsegwit import MainNetLedger
|
||||
from torba.wallet import Wallet
|
||||
|
||||
from .test_transaction import get_transaction, get_output
|
||||
|
||||
|
@ -63,7 +64,7 @@ class TestSynchronization(LedgerTestCase):
|
|||
|
||||
@defer.inlineCallbacks
|
||||
def test_update_history(self):
|
||||
account = self.ledger.account_class.generate(self.ledger, u"torba")
|
||||
account = self.ledger.account_class.generate(self.ledger, Wallet(), "torba")
|
||||
address = yield account.receiving.get_or_create_usable_address()
|
||||
address_details = yield self.ledger.db.get_address(address)
|
||||
self.assertEqual(address_details['history'], None)
|
||||
|
|
|
@ -4,6 +4,7 @@ from twisted.trial import unittest
|
|||
from twisted.internet import defer
|
||||
|
||||
from torba.coin.bitcoinsegwit import MainNetLedger as ledger_class
|
||||
from torba.wallet import Wallet
|
||||
from torba.constants import CENT, COIN
|
||||
|
||||
|
||||
|
@ -142,7 +143,7 @@ class TestTransactionSigning(unittest.TestCase):
|
|||
@defer.inlineCallbacks
|
||||
def test_sign(self):
|
||||
account = self.ledger.account_class.from_dict(
|
||||
self.ledger, {
|
||||
self.ledger, Wallet(), {
|
||||
"seed": "carbon smart garage balance margin twelve chest sword "
|
||||
"toast envelope bottom stomach absent"
|
||||
|
||||
|
@ -177,7 +178,7 @@ class TransactionIOBalancing(unittest.TestCase):
|
|||
self.ledger = ledger_class({'db': ledger_class.database_class(':memory:')})
|
||||
yield self.ledger.db.start()
|
||||
self.account = self.ledger.account_class.from_dict(
|
||||
self.ledger, {
|
||||
self.ledger, Wallet(), {
|
||||
"seed": "carbon smart garage balance margin twelve chest sword "
|
||||
"toast envelope bottom stomach absent"
|
||||
}
|
||||
|
|
|
@ -195,10 +195,11 @@ class BaseAccount:
|
|||
HierarchicalDeterministic.name: HierarchicalDeterministic,
|
||||
}
|
||||
|
||||
def __init__(self, ledger: 'baseledger.BaseLedger', name: str, seed: str, encrypted: bool,
|
||||
def __init__(self, ledger: 'baseledger.BaseLedger', wallet, name: str, seed: str, encrypted: bool,
|
||||
private_key: PrivateKey, public_key: PubKey, address_generator: dict
|
||||
) -> None:
|
||||
self.ledger = ledger
|
||||
self.wallet = wallet
|
||||
self.name = name
|
||||
self.seed = seed
|
||||
self.encrypted = encrypted
|
||||
|
@ -209,10 +210,11 @@ class BaseAccount:
|
|||
self.receiving, self.change = self.address_generator.from_dict(self, address_generator)
|
||||
self.address_managers = {self.receiving, self.change}
|
||||
ledger.add_account(self)
|
||||
wallet.add_account(self)
|
||||
|
||||
@classmethod
|
||||
def generate(cls, ledger: 'baseledger.BaseLedger', name: str = None, address_generator: dict = None):
|
||||
return cls.from_dict(ledger, {
|
||||
def generate(cls, ledger: 'baseledger.BaseLedger', wallet, name: str = None, address_generator: dict = None):
|
||||
return cls.from_dict(ledger, wallet, {
|
||||
'name': name,
|
||||
'seed': cls.mnemonic_class().make_seed(),
|
||||
'address_generator': address_generator or {}
|
||||
|
@ -225,7 +227,7 @@ class BaseAccount:
|
|||
)
|
||||
|
||||
@classmethod
|
||||
def from_dict(cls, ledger: 'baseledger.BaseLedger', d: dict):
|
||||
def from_dict(cls, ledger: 'baseledger.BaseLedger', wallet, d: dict):
|
||||
seed = d.get('seed', '')
|
||||
private_key = d.get('private_key', '')
|
||||
public_key = None
|
||||
|
@ -244,6 +246,7 @@ class BaseAccount:
|
|||
name = 'Account #{}'.format(public_key.address)
|
||||
return cls(
|
||||
ledger=ledger,
|
||||
wallet=wallet,
|
||||
name=name,
|
||||
seed=seed,
|
||||
encrypted=encrypted,
|
||||
|
|
|
@ -23,27 +23,24 @@ class Wallet:
|
|||
self.accounts = accounts or []
|
||||
self.storage = storage or WalletStorage()
|
||||
|
||||
def generate_account(self, ledger: 'baseledger.BaseLedger') -> 'baseaccount.BaseAccount':
|
||||
account = ledger.account_class.generate(ledger, u'torba', {})
|
||||
def add_account(self, account):
|
||||
self.accounts.append(account)
|
||||
return account
|
||||
|
||||
def generate_account(self, ledger: 'baseledger.BaseLedger') -> 'baseaccount.BaseAccount':
|
||||
return ledger.account_class.generate(ledger, self)
|
||||
|
||||
@classmethod
|
||||
def from_storage(cls, storage: 'WalletStorage', manager: 'basemanager.BaseWalletManager') -> 'Wallet':
|
||||
json_dict = storage.read()
|
||||
|
||||
accounts = []
|
||||
wallet = cls(
|
||||
name=json_dict.get('name', 'Wallet'),
|
||||
storage=storage
|
||||
)
|
||||
account_dicts: Sequence[dict] = json_dict.get('accounts', [])
|
||||
for account_dict in account_dicts:
|
||||
ledger = manager.get_or_create_ledger(account_dict['ledger'])
|
||||
account = ledger.account_class.from_dict(ledger, account_dict)
|
||||
accounts.append(account)
|
||||
|
||||
return cls(
|
||||
name=json_dict.get('name', 'Wallet'),
|
||||
accounts=accounts,
|
||||
storage=storage
|
||||
)
|
||||
ledger.account_class.from_dict(ledger, wallet, account_dict)
|
||||
return wallet
|
||||
|
||||
def to_dict(self):
|
||||
return {
|
||||
|
|
Loading…
Reference in a new issue