2018-11-04 07:24:41 +01:00
|
|
|
from torba.testcase import AsyncioTestCase
|
|
|
|
from torba.client.wallet import Wallet
|
2018-04-30 09:04:52 +02:00
|
|
|
|
2019-06-21 03:02:58 +02:00
|
|
|
from lbry.wallet.ledger import MainNetLedger, WalletDatabase
|
|
|
|
from lbry.wallet.header import Headers
|
|
|
|
from lbry.wallet.account import Account
|
2018-04-30 09:04:52 +02:00
|
|
|
|
|
|
|
|
2018-10-15 23:16:43 +02:00
|
|
|
class TestAccount(AsyncioTestCase):
|
2018-04-30 09:04:52 +02:00
|
|
|
|
2018-10-15 23:16:43 +02:00
|
|
|
async def asyncSetUp(self):
|
2018-08-16 17:48:50 +02:00
|
|
|
self.ledger = MainNetLedger({
|
|
|
|
'db': WalletDatabase(':memory:'),
|
|
|
|
'headers': Headers(':memory:')
|
|
|
|
})
|
2018-10-15 23:16:43 +02:00
|
|
|
await self.ledger.db.open()
|
|
|
|
|
|
|
|
async def asyncTearDown(self):
|
|
|
|
await self.ledger.db.close()
|
2018-04-30 09:04:52 +02:00
|
|
|
|
2018-10-15 23:16:43 +02:00
|
|
|
async def test_generate_account(self):
|
2018-08-09 02:41:29 +02:00
|
|
|
account = Account.generate(self.ledger, Wallet(), 'lbryum')
|
2018-07-09 23:04:59 +02:00
|
|
|
self.assertEqual(account.ledger, self.ledger)
|
2018-04-30 09:04:52 +02:00
|
|
|
self.assertIsNotNone(account.seed)
|
2018-07-09 23:04:59 +02:00
|
|
|
self.assertEqual(account.public_key.ledger, self.ledger)
|
2018-04-30 09:04:52 +02:00
|
|
|
self.assertEqual(account.private_key.public_key, account.public_key)
|
|
|
|
|
2018-07-09 23:04:59 +02:00
|
|
|
self.assertEqual(account.public_key.ledger, self.ledger)
|
|
|
|
self.assertEqual(account.private_key.public_key, account.public_key)
|
|
|
|
|
2018-10-15 23:16:43 +02:00
|
|
|
addresses = await account.receiving.get_addresses()
|
2018-07-09 23:04:59 +02:00
|
|
|
self.assertEqual(len(addresses), 0)
|
2018-10-15 23:16:43 +02:00
|
|
|
addresses = await account.change.get_addresses()
|
2018-07-09 23:04:59 +02:00
|
|
|
self.assertEqual(len(addresses), 0)
|
|
|
|
|
2018-10-15 23:16:43 +02:00
|
|
|
await account.ensure_address_gap()
|
2018-04-30 09:04:52 +02:00
|
|
|
|
2018-10-15 23:16:43 +02:00
|
|
|
addresses = await account.receiving.get_addresses()
|
2018-07-09 23:04:59 +02:00
|
|
|
self.assertEqual(len(addresses), 20)
|
2018-10-15 23:16:43 +02:00
|
|
|
addresses = await account.change.get_addresses()
|
2018-07-09 23:04:59 +02:00
|
|
|
self.assertEqual(len(addresses), 6)
|
2018-04-30 09:04:52 +02:00
|
|
|
|
2018-10-15 23:16:43 +02:00
|
|
|
async def test_generate_account_from_seed(self):
|
2018-08-06 09:05:32 +02:00
|
|
|
account = Account.from_dict(
|
2018-08-09 02:41:29 +02:00
|
|
|
self.ledger, Wallet(), {
|
2018-08-06 09:05:32 +02:00
|
|
|
"seed":
|
|
|
|
"carbon smart garage balance margin twelve chest sword toas"
|
|
|
|
"t envelope bottom stomach absent"
|
|
|
|
}
|
2018-04-30 09:04:52 +02:00
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
account.private_key.extended_key_string(),
|
2018-07-17 05:32:37 +02:00
|
|
|
'xprv9s21ZrQH143K42ovpZygnjfHdAqSd9jo7zceDfPRogM7bkkoNVv7DRNLEoB8'
|
|
|
|
'HoirMgH969NrgL8jNzLEegqFzPRWM37GXd4uE8uuRkx4LAe'
|
2018-04-30 09:04:52 +02:00
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
account.public_key.extended_key_string(),
|
2018-07-17 05:32:37 +02:00
|
|
|
'xpub661MyMwAqRbcGWtPvbWh9sc2BCfw2cTeVDYF23o3N1t6UZ5wv3EMmDgp66FxH'
|
|
|
|
'uDtWdft3B5eL5xQtyzAtkdmhhC95gjRjLzSTdkho95asu9'
|
2018-04-30 09:04:52 +02:00
|
|
|
)
|
2018-10-15 23:16:43 +02:00
|
|
|
address = await account.receiving.ensure_address_gap()
|
2018-07-17 05:32:37 +02:00
|
|
|
self.assertEqual(address[0], 'bCqJrLHdoiRqEZ1whFZ3WHNb33bP34SuGx')
|
2018-07-09 23:04:59 +02:00
|
|
|
|
2019-09-20 06:05:37 +02:00
|
|
|
private_key = await self.ledger.get_private_key_for_address(
|
|
|
|
account.wallet, 'bCqJrLHdoiRqEZ1whFZ3WHNb33bP34SuGx'
|
|
|
|
)
|
2018-04-30 09:04:52 +02:00
|
|
|
self.assertEqual(
|
|
|
|
private_key.extended_key_string(),
|
2018-07-17 05:32:37 +02:00
|
|
|
'xprv9vwXVierUTT4hmoe3dtTeBfbNv1ph2mm8RWXARU6HsZjBaAoFaS2FRQu4fptR'
|
|
|
|
'AyJWhJW42dmsEaC1nKnVKKTMhq3TVEHsNj1ca3ciZMKktT'
|
2018-04-30 09:04:52 +02:00
|
|
|
)
|
2019-09-20 06:05:37 +02:00
|
|
|
private_key = await self.ledger.get_private_key_for_address(
|
|
|
|
account.wallet, 'BcQjRlhDOIrQez1WHfz3whnB33Bp34sUgX'
|
|
|
|
)
|
2018-07-09 23:04:59 +02:00
|
|
|
self.assertIsNone(private_key)
|
2018-04-30 09:04:52 +02:00
|
|
|
|
|
|
|
def test_load_and_save_account(self):
|
|
|
|
account_data = {
|
2018-07-17 05:32:37 +02:00
|
|
|
'name': 'Main Account',
|
2019-03-12 18:00:43 +01:00
|
|
|
'modified_on': 123.456,
|
2018-04-30 09:04:52 +02:00
|
|
|
'seed':
|
|
|
|
"carbon smart garage balance margin twelve chest sword toast envelope bottom stomac"
|
|
|
|
"h absent",
|
|
|
|
'encrypted': False,
|
|
|
|
'private_key':
|
2018-07-09 23:04:59 +02:00
|
|
|
'xprv9s21ZrQH143K42ovpZygnjfHdAqSd9jo7zceDfPRogM7bkkoNVv7DRNLEoB8'
|
|
|
|
'HoirMgH969NrgL8jNzLEegqFzPRWM37GXd4uE8uuRkx4LAe',
|
2018-04-30 09:04:52 +02:00
|
|
|
'public_key':
|
2018-07-09 23:04:59 +02:00
|
|
|
'xpub661MyMwAqRbcGWtPvbWh9sc2BCfw2cTeVDYF23o3N1t6UZ5wv3EMmDgp66FxH'
|
|
|
|
'uDtWdft3B5eL5xQtyzAtkdmhhC95gjRjLzSTdkho95asu9',
|
2018-07-17 05:32:37 +02:00
|
|
|
'certificates': {},
|
2018-07-30 01:15:06 +02:00
|
|
|
'address_generator': {
|
|
|
|
'name': 'deterministic-chain',
|
|
|
|
'receiving': {'gap': 17, 'maximum_uses_per_address': 2},
|
|
|
|
'change': {'gap': 10, 'maximum_uses_per_address': 2}
|
|
|
|
}
|
2018-04-30 09:04:52 +02:00
|
|
|
}
|
|
|
|
|
2018-08-09 02:41:29 +02:00
|
|
|
account = Account.from_dict(self.ledger, Wallet(), account_data)
|
2018-07-09 23:04:59 +02:00
|
|
|
account_data['ledger'] = 'lbc_mainnet'
|
2018-04-30 09:04:52 +02:00
|
|
|
self.assertDictEqual(account_data, account.to_dict())
|
2019-05-04 23:22:32 +02:00
|
|
|
|
|
|
|
async def test_save_max_gap(self):
|
|
|
|
account = Account.generate(
|
|
|
|
self.ledger, Wallet(), 'lbryum', {
|
|
|
|
'name': 'deterministic-chain',
|
|
|
|
'receiving': {'gap': 3, 'maximum_uses_per_address': 2},
|
|
|
|
'change': {'gap': 4, 'maximum_uses_per_address': 2}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
self.assertEqual(account.receiving.gap, 3)
|
|
|
|
self.assertEqual(account.change.gap, 4)
|
|
|
|
await account.save_max_gap()
|
|
|
|
self.assertEqual(account.receiving.gap, 20)
|
|
|
|
self.assertEqual(account.change.gap, 6)
|
|
|
|
# doesn't fail for single-address account
|
|
|
|
account2 = Account.generate(self.ledger, Wallet(), 'lbryum', {'name': 'single-address'})
|
|
|
|
await account2.save_max_gap()
|