lbry-sdk/tests/unit/test_account.py

162 lines
6.2 KiB
Python
Raw Normal View History

2018-05-25 08:03:25 +02:00
from binascii import hexlify
from twisted.trial import unittest
2018-06-11 15:33:32 +02:00
from twisted.internet import defer
2018-05-25 08:03:25 +02:00
2018-06-11 15:33:32 +02:00
from torba.coin.bitcoinsegwit import MainNetLedger
2018-05-25 08:03:25 +02:00
2018-06-12 16:02:04 +02:00
class TestKeyChain(unittest.TestCase):
def setUp(self):
self.ledger = MainNetLedger(db=':memory:')
return self.ledger.db.start()
@defer.inlineCallbacks
def test_address_gap_algorithm(self):
account = self.ledger.account_class.generate(self.ledger, u"torba")
# save records out of order to make sure we're really testing ORDER BY
# and not coincidentally getting records in the correct order
yield account.receiving.generate_keys(4, 7)
yield account.receiving.generate_keys(0, 3)
yield account.receiving.generate_keys(8, 11)
keys = yield account.receiving.get_addresses(None, True)
self.assertEqual(
[key['position'] for key in keys],
[11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
)
# we have 12, but default gap is 20
new_keys = yield account.receiving.ensure_address_gap()
self.assertEqual(len(new_keys), 8)
keys = yield account.receiving.get_addresses(None, True)
self.assertEqual(
[key['position'] for key in keys],
[19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
)
# case #1: no new addresses needed
empty = yield account.receiving.ensure_address_gap()
self.assertEqual(len(empty), 0)
# case #2: only one new addressed needed
keys = yield account.receiving.get_addresses(None, True)
2018-06-14 02:57:57 +02:00
yield self.ledger.db.set_address_history(keys[19]['address'], b'a:1:')
2018-06-12 16:02:04 +02:00
new_keys = yield account.receiving.ensure_address_gap()
self.assertEqual(len(new_keys), 1)
# case #3: 20 addresses needed
keys = yield account.receiving.get_addresses(None, True)
2018-06-14 02:57:57 +02:00
yield self.ledger.db.set_address_history(keys[0]['address'], b'a:1:')
2018-06-12 16:02:04 +02:00
new_keys = yield account.receiving.ensure_address_gap()
self.assertEqual(len(new_keys), 20)
@defer.inlineCallbacks
def test_create_usable_address(self):
account = self.ledger.account_class.generate(self.ledger, u"torba")
keys = yield account.receiving.get_addresses(None, True)
self.assertEqual(len(keys), 0)
address = yield account.receiving.get_or_create_usable_address()
self.assertIsNotNone(address)
keys = yield account.receiving.get_addresses(None, True)
self.assertEqual(len(keys), 20)
2018-05-25 08:03:25 +02:00
class TestAccount(unittest.TestCase):
def setUp(self):
2018-06-11 15:33:32 +02:00
self.ledger = MainNetLedger(db=':memory:')
return self.ledger.db.start()
2018-05-25 08:03:25 +02:00
2018-06-11 15:33:32 +02:00
@defer.inlineCallbacks
2018-05-25 08:03:25 +02:00
def test_generate_account(self):
2018-06-11 15:33:32 +02:00
account = self.ledger.account_class.generate(self.ledger, u"torba")
self.assertEqual(account.ledger, self.ledger)
2018-05-25 08:03:25 +02:00
self.assertIsNotNone(account.seed)
2018-06-11 15:33:32 +02:00
self.assertEqual(account.public_key.ledger, self.ledger)
2018-05-25 08:03:25 +02:00
self.assertEqual(account.private_key.public_key, account.public_key)
2018-06-11 15:33:32 +02:00
addresses = yield account.receiving.get_addresses()
self.assertEqual(len(addresses), 0)
addresses = yield account.change.get_addresses()
self.assertEqual(len(addresses), 0)
2018-05-25 08:03:25 +02:00
2018-06-12 16:02:04 +02:00
yield account.ensure_address_gap()
2018-05-25 08:03:25 +02:00
2018-06-11 15:33:32 +02:00
addresses = yield account.receiving.get_addresses()
self.assertEqual(len(addresses), 20)
addresses = yield account.change.get_addresses()
self.assertEqual(len(addresses), 6)
@defer.inlineCallbacks
2018-05-25 08:03:25 +02:00
def test_generate_account_from_seed(self):
2018-06-11 15:33:32 +02:00
account = self.ledger.account_class.from_seed(
self.ledger,
2018-05-25 08:03:25 +02:00
u"carbon smart garage balance margin twelve chest sword toast envelope bottom stomach ab"
u"sent",
u"torba"
)
self.assertEqual(
account.private_key.extended_key_string(),
2018-05-25 19:19:54 +02:00
b'xprv9s21ZrQH143K2dyhK7SevfRG72bYDRNv25yKPWWm6dqApNxm1Zb1m5gGcBWYfbsPjTr2v5joit8Af2Zp5P'
b'6yz3jMbycrLrRMpeAJxR8qDg8'
2018-05-25 08:03:25 +02:00
)
self.assertEqual(
account.public_key.extended_key_string(),
2018-05-25 19:19:54 +02:00
b'xpub661MyMwAqRbcF84AR8yfHoMzf4S2ct6mPJtvBtvNeyN9hBHuZ6uGJszkTSn5fQUCdz3XU17eBzFeAUwV6f'
b'iW44g14WF52fYC5J483wqQ5ZP'
2018-05-25 08:03:25 +02:00
)
2018-06-12 16:02:04 +02:00
address = yield account.receiving.ensure_address_gap()
self.assertEqual(address[0], b'1PmX9T3sCiDysNtWszJa44SkKcpGc2NaXP')
self.maxDiff = None
private_key = yield self.ledger.get_private_key_for_address(b'1PmX9T3sCiDysNtWszJa44SkKcpGc2NaXP')
2018-05-25 08:03:25 +02:00
self.assertEqual(
private_key.extended_key_string(),
2018-06-12 16:02:04 +02:00
b'xprv9xNEfQ296VTRaEUDZ8oKq74xw2U6kpj486vFUB4K1wT9U25GX4UwuzFgJN1YuRrqkQ5TTwCpkYnjNpSoH'
b'SBaEigNHPkoeYbuPMRo6mRUjxg'
2018-05-25 08:03:25 +02:00
)
2018-06-12 16:02:04 +02:00
2018-06-11 15:33:32 +02:00
invalid_key = yield self.ledger.get_private_key_for_address(b'BcQjRlhDOIrQez1WHfz3whnB33Bp34sUgX')
self.assertIsNone(invalid_key)
2018-05-25 08:03:25 +02:00
self.assertEqual(
hexlify(private_key.wif()),
2018-06-12 16:02:04 +02:00
b'1cc27be89ad47ef932562af80e95085eb0ab2ae3e5c019b1369b8b05ff2e94512f01'
2018-05-25 08:03:25 +02:00
)
2018-06-11 15:33:32 +02:00
@defer.inlineCallbacks
2018-05-25 08:03:25 +02:00
def test_load_and_save_account(self):
account_data = {
'seed':
"carbon smart garage balance margin twelve chest sword toast envelope bottom stomac"
"h absent",
'encrypted': False,
'private_key':
2018-06-12 16:02:04 +02:00
b'xprv9s21ZrQH143K2dyhK7SevfRG72bYDRNv25yKPWWm6dqApNxm1Zb1m5gGcBWYfbsPjTr2v5joit8Af2Zp5P'
b'6yz3jMbycrLrRMpeAJxR8qDg8',
2018-05-25 08:03:25 +02:00
'public_key':
2018-06-12 16:02:04 +02:00
b'xpub661MyMwAqRbcF84AR8yfHoMzf4S2ct6mPJtvBtvNeyN9hBHuZ6uGJszkTSn5fQUCdz3XU17eBzFeAUwV6f'
b'iW44g14WF52fYC5J483wqQ5ZP',
2018-05-25 08:03:25 +02:00
'receiving_gap': 10,
2018-06-12 16:02:04 +02:00
'receiving_maximum_use_per_address': 2,
2018-05-25 08:03:25 +02:00
'change_gap': 10,
2018-06-12 16:02:04 +02:00
'change_maximum_use_per_address': 2
2018-05-25 08:03:25 +02:00
}
2018-06-11 15:33:32 +02:00
account = self.ledger.account_class.from_dict(self.ledger, account_data)
2018-05-25 08:03:25 +02:00
2018-06-12 16:02:04 +02:00
yield account.ensure_address_gap()
2018-06-11 15:33:32 +02:00
addresses = yield account.receiving.get_addresses()
self.assertEqual(len(addresses), 10)
addresses = yield account.change.get_addresses()
self.assertEqual(len(addresses), 10)
2018-05-25 08:03:25 +02:00
self.maxDiff = None
2018-06-11 15:33:32 +02:00
account_data['ledger'] = 'btc_mainnet'
2018-05-25 08:03:25 +02:00
self.assertDictEqual(account_data, account.to_dict())