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-07-29 19:13:40 +02:00
|
|
|
from torba.baseaccount import HierarchicalDeterministic, SingleKey
|
2018-05-25 08:03:25 +02:00
|
|
|
|
|
|
|
|
2018-07-29 19:13:40 +02:00
|
|
|
class TestHierarchicalDeterministicAccount(unittest.TestCase):
|
2018-06-12 16:02:04 +02:00
|
|
|
|
2018-07-14 23:47:18 +02:00
|
|
|
@defer.inlineCallbacks
|
2018-06-12 16:02:04 +02:00
|
|
|
def setUp(self):
|
2018-07-12 04:47:44 +02:00
|
|
|
self.ledger = MainNetLedger({'db': MainNetLedger.database_class(':memory:')})
|
2018-07-14 23:47:18 +02:00
|
|
|
yield self.ledger.db.start()
|
|
|
|
self.account = self.ledger.account_class.generate(self.ledger, u"torba")
|
2018-06-12 16:02:04 +02:00
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
2018-07-14 23:47:18 +02:00
|
|
|
def test_generate_account(self):
|
|
|
|
account = self.account
|
|
|
|
|
|
|
|
self.assertEqual(account.ledger, self.ledger)
|
|
|
|
self.assertIsNotNone(account.seed)
|
|
|
|
self.assertEqual(account.public_key.ledger, self.ledger)
|
|
|
|
self.assertEqual(account.private_key.public_key, account.public_key)
|
|
|
|
|
|
|
|
addresses = yield account.receiving.get_addresses()
|
|
|
|
self.assertEqual(len(addresses), 0)
|
|
|
|
addresses = yield account.change.get_addresses()
|
|
|
|
self.assertEqual(len(addresses), 0)
|
|
|
|
|
|
|
|
yield account.ensure_address_gap()
|
|
|
|
|
|
|
|
addresses = yield account.receiving.get_addresses()
|
|
|
|
self.assertEqual(len(addresses), 20)
|
|
|
|
addresses = yield account.change.get_addresses()
|
|
|
|
self.assertEqual(len(addresses), 6)
|
|
|
|
|
|
|
|
addresses = yield account.get_addresses()
|
|
|
|
self.assertEqual(len(addresses), 26)
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def test_ensure_address_gap(self):
|
|
|
|
account = self.account
|
|
|
|
|
2018-07-29 19:13:40 +02:00
|
|
|
self.assertIsInstance(account.receiving, HierarchicalDeterministic)
|
2018-06-12 16:02:04 +02:00
|
|
|
|
|
|
|
yield account.receiving.generate_keys(4, 7)
|
|
|
|
yield account.receiving.generate_keys(0, 3)
|
|
|
|
yield account.receiving.generate_keys(8, 11)
|
2018-07-14 23:47:18 +02:00
|
|
|
records = yield account.receiving.get_address_records()
|
2018-06-12 16:02:04 +02:00
|
|
|
self.assertEqual(
|
2018-07-14 23:47:18 +02:00
|
|
|
[r['position'] for r in records],
|
|
|
|
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
|
2018-06-12 16:02:04 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
# we have 12, but default gap is 20
|
|
|
|
new_keys = yield account.receiving.ensure_address_gap()
|
|
|
|
self.assertEqual(len(new_keys), 8)
|
2018-07-14 23:47:18 +02:00
|
|
|
records = yield account.receiving.get_address_records()
|
2018-06-12 16:02:04 +02:00
|
|
|
self.assertEqual(
|
2018-07-14 23:47:18 +02:00
|
|
|
[r['position'] for r in records],
|
|
|
|
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
|
2018-06-12 16:02:04 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
# 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
|
2018-07-14 23:47:18 +02:00
|
|
|
records = yield account.receiving.get_address_records()
|
|
|
|
yield self.ledger.db.set_address_history(records[0]['address'], '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
|
2018-07-14 23:47:18 +02:00
|
|
|
yield self.ledger.db.set_address_history(new_keys[0], '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
|
2018-07-14 23:47:18 +02:00
|
|
|
def test_get_or_create_usable_address(self):
|
|
|
|
account = self.account
|
2018-06-12 16:02:04 +02:00
|
|
|
|
2018-07-14 23:47:18 +02:00
|
|
|
keys = yield account.receiving.get_addresses()
|
2018-06-12 16:02:04 +02:00
|
|
|
self.assertEqual(len(keys), 0)
|
|
|
|
|
|
|
|
address = yield account.receiving.get_or_create_usable_address()
|
|
|
|
self.assertIsNotNone(address)
|
|
|
|
|
2018-07-14 23:47:18 +02:00
|
|
|
keys = yield account.receiving.get_addresses()
|
2018-06-12 16:02:04 +02:00
|
|
|
self.assertEqual(len(keys), 20)
|
|
|
|
|
2018-07-14 23:47:18 +02:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def test_generate_account_from_seed(self):
|
2018-08-06 08:52:52 +02:00
|
|
|
account = self.ledger.account_class.from_dict(
|
|
|
|
self.ledger, {
|
|
|
|
"seed": "carbon smart garage balance margin twelve chest sword "
|
|
|
|
"toast envelope bottom stomach absent",
|
|
|
|
"address_generator": {
|
|
|
|
'name': 'deterministic-chain',
|
|
|
|
'receiving': {'gap': 3, 'maximum_uses_per_address': 1},
|
|
|
|
'change': {'gap': 2, 'maximum_uses_per_address': 1}
|
|
|
|
}
|
|
|
|
}
|
2018-07-14 23:47:18 +02:00
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
account.private_key.extended_key_string(),
|
2018-08-06 08:52:52 +02:00
|
|
|
'xprv9s21ZrQH143K3TsAz5efNV8K93g3Ms3FXcjaWB9fVUsMwAoE3ZT4vYymkp5BxK'
|
|
|
|
'Kfnpz8J6sHDFriX1SnpvjNkzcks8XBnxjGLS83BTyfpna'
|
2018-07-14 23:47:18 +02:00
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
account.public_key.extended_key_string(),
|
2018-08-06 08:52:52 +02:00
|
|
|
'xpub661MyMwAqRbcFwwe67Bfjd53h5WXmKm6tqfBJZZH3pQLoy8Nb6mKUMJFc7UbpV'
|
|
|
|
'NzmwFPN2evn3YHnig1pkKVYcvCV8owTd2yAcEkJfCX53g'
|
2018-07-14 23:47:18 +02:00
|
|
|
)
|
|
|
|
address = yield account.receiving.ensure_address_gap()
|
2018-08-06 08:52:52 +02:00
|
|
|
self.assertEqual(address[0], '1CDLuMfwmPqJiNk5C2Bvew6tpgjAGgUk8J')
|
2018-07-14 23:47:18 +02:00
|
|
|
|
2018-08-06 08:52:52 +02:00
|
|
|
private_key = yield self.ledger.get_private_key_for_address('1CDLuMfwmPqJiNk5C2Bvew6tpgjAGgUk8J')
|
2018-07-14 23:47:18 +02:00
|
|
|
self.assertEqual(
|
|
|
|
private_key.extended_key_string(),
|
2018-08-06 08:52:52 +02:00
|
|
|
'xprv9xV7rhbg6M4yWrdTeLorz3Q1GrQb4aQzzGWboP3du7W7UUztzNTUrEYTnDfz7o'
|
|
|
|
'ptBygDxXYRppyiuenJpoBTgYP2C26E1Ah5FEALM24CsWi'
|
2018-07-14 23:47:18 +02:00
|
|
|
)
|
2018-06-12 16:02:04 +02:00
|
|
|
|
2018-07-15 03:34:07 +02:00
|
|
|
invalid_key = yield self.ledger.get_private_key_for_address('BcQjRlhDOIrQez1WHfz3whnB33Bp34sUgX')
|
2018-07-14 23:47:18 +02:00
|
|
|
self.assertIsNone(invalid_key)
|
2018-05-25 08:03:25 +02:00
|
|
|
|
2018-07-14 23:47:18 +02:00
|
|
|
self.assertEqual(
|
|
|
|
hexlify(private_key.wif()),
|
2018-08-06 08:52:52 +02:00
|
|
|
b'1c01ae1e4c7d89e39f6d3aa7792c097a30ca7d40be249b6de52c81ec8cf9aab48b01'
|
2018-07-14 23:47:18 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def test_load_and_save_account(self):
|
|
|
|
account_data = {
|
|
|
|
'name': 'My Account',
|
|
|
|
'seed':
|
|
|
|
"carbon smart garage balance margin twelve chest sword toast envelope bottom stomac"
|
|
|
|
"h absent",
|
|
|
|
'encrypted': False,
|
|
|
|
'private_key':
|
2018-08-06 08:52:52 +02:00
|
|
|
'xprv9s21ZrQH143K3TsAz5efNV8K93g3Ms3FXcjaWB9fVUsMwAoE3ZT4vYymkp'
|
|
|
|
'5BxKKfnpz8J6sHDFriX1SnpvjNkzcks8XBnxjGLS83BTyfpna',
|
2018-07-14 23:47:18 +02:00
|
|
|
'public_key':
|
2018-08-06 08:52:52 +02:00
|
|
|
'xpub661MyMwAqRbcFwwe67Bfjd53h5WXmKm6tqfBJZZH3pQLoy8Nb6mKUMJFc7'
|
|
|
|
'UbpVNzmwFPN2evn3YHnig1pkKVYcvCV8owTd2yAcEkJfCX53g',
|
2018-07-29 19:13:40 +02:00
|
|
|
'address_generator': {
|
|
|
|
'name': 'deterministic-chain',
|
|
|
|
'receiving': {'gap': 5, 'maximum_uses_per_address': 2},
|
|
|
|
'change': {'gap': 5, 'maximum_uses_per_address': 2}
|
|
|
|
}
|
2018-07-14 23:47:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
account = self.ledger.account_class.from_dict(self.ledger, account_data)
|
|
|
|
|
|
|
|
yield account.ensure_address_gap()
|
|
|
|
|
|
|
|
addresses = yield account.receiving.get_addresses()
|
|
|
|
self.assertEqual(len(addresses), 5)
|
|
|
|
addresses = yield account.change.get_addresses()
|
|
|
|
self.assertEqual(len(addresses), 5)
|
|
|
|
|
|
|
|
self.maxDiff = None
|
|
|
|
account_data['ledger'] = 'btc_mainnet'
|
|
|
|
self.assertDictEqual(account_data, account.to_dict())
|
|
|
|
|
|
|
|
|
|
|
|
class TestSingleKeyAccount(unittest.TestCase):
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
2018-05-25 08:03:25 +02:00
|
|
|
def setUp(self):
|
2018-07-12 04:47:44 +02:00
|
|
|
self.ledger = MainNetLedger({'db': MainNetLedger.database_class(':memory:')})
|
2018-07-14 23:47:18 +02:00
|
|
|
yield self.ledger.db.start()
|
2018-07-29 19:13:40 +02:00
|
|
|
self.account = self.ledger.account_class.generate(self.ledger, u"torba", {'name': 'single-address'})
|
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-07-14 23:47:18 +02:00
|
|
|
account = self.account
|
|
|
|
|
2018-06-11 15:33:32 +02:00
|
|
|
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()
|
2018-07-14 23:47:18 +02:00
|
|
|
self.assertEqual(len(addresses), 1)
|
|
|
|
self.assertEqual(addresses[0], account.public_key.address)
|
2018-06-11 15:33:32 +02:00
|
|
|
addresses = yield account.change.get_addresses()
|
2018-07-14 23:47:18 +02:00
|
|
|
self.assertEqual(len(addresses), 1)
|
|
|
|
self.assertEqual(addresses[0], account.public_key.address)
|
|
|
|
|
|
|
|
addresses = yield account.get_addresses()
|
|
|
|
self.assertEqual(len(addresses), 1)
|
|
|
|
self.assertEqual(addresses[0], account.public_key.address)
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def test_ensure_address_gap(self):
|
|
|
|
account = self.account
|
|
|
|
|
|
|
|
self.assertIsInstance(account.receiving, SingleKey)
|
|
|
|
addresses = yield account.receiving.get_addresses()
|
|
|
|
self.assertEqual(addresses, [])
|
|
|
|
|
|
|
|
# we have 12, but default gap is 20
|
|
|
|
new_keys = yield account.receiving.ensure_address_gap()
|
|
|
|
self.assertEqual(len(new_keys), 1)
|
|
|
|
self.assertEqual(new_keys[0], account.public_key.address)
|
|
|
|
records = yield account.receiving.get_address_records()
|
|
|
|
self.assertEqual(records, [{
|
|
|
|
'position': 0, 'address': account.public_key.address, 'used_times': 0
|
|
|
|
}])
|
|
|
|
|
|
|
|
# case #1: no new addresses needed
|
|
|
|
empty = yield account.receiving.ensure_address_gap()
|
|
|
|
self.assertEqual(len(empty), 0)
|
|
|
|
|
|
|
|
# case #2: after use, still no new address needed
|
|
|
|
records = yield account.receiving.get_address_records()
|
|
|
|
yield self.ledger.db.set_address_history(records[0]['address'], 'a:1:')
|
|
|
|
empty = yield account.receiving.ensure_address_gap()
|
|
|
|
self.assertEqual(len(empty), 0)
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def test_get_or_create_usable_address(self):
|
|
|
|
account = self.account
|
|
|
|
|
|
|
|
addresses = yield account.receiving.get_addresses()
|
|
|
|
self.assertEqual(len(addresses), 0)
|
|
|
|
|
|
|
|
address1 = yield account.receiving.get_or_create_usable_address()
|
|
|
|
self.assertIsNotNone(address1)
|
|
|
|
|
|
|
|
yield self.ledger.db.set_address_history(address1, 'a:1:b:2:c:3:')
|
|
|
|
records = yield account.receiving.get_address_records()
|
|
|
|
self.assertEqual(records[0]['used_times'], 3)
|
|
|
|
|
|
|
|
address2 = yield account.receiving.get_or_create_usable_address()
|
|
|
|
self.assertEqual(address1, address2)
|
|
|
|
|
|
|
|
keys = yield account.receiving.get_addresses()
|
|
|
|
self.assertEqual(len(keys), 1)
|
2018-06-11 15:33:32 +02:00
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
2018-05-25 08:03:25 +02:00
|
|
|
def test_generate_account_from_seed(self):
|
2018-08-06 08:52:52 +02:00
|
|
|
account = self.ledger.account_class.from_dict(
|
|
|
|
self.ledger, {
|
|
|
|
"seed":
|
|
|
|
"carbon smart garage balance margin twelve chest sword toas"
|
|
|
|
"t envelope bottom stomach absent",
|
|
|
|
'address_generator': {'name': 'single-address'}
|
|
|
|
}
|
2018-05-25 08:03:25 +02:00
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
account.private_key.extended_key_string(),
|
2018-08-06 08:52:52 +02:00
|
|
|
'xprv9s21ZrQH143K3TsAz5efNV8K93g3Ms3FXcjaWB9fVUsMwAoE3ZT4vYymkp'
|
|
|
|
'5BxKKfnpz8J6sHDFriX1SnpvjNkzcks8XBnxjGLS83BTyfpna',
|
2018-05-25 08:03:25 +02:00
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
account.public_key.extended_key_string(),
|
2018-08-06 08:52:52 +02:00
|
|
|
'xpub661MyMwAqRbcFwwe67Bfjd53h5WXmKm6tqfBJZZH3pQLoy8Nb6mKUMJFc7'
|
|
|
|
'UbpVNzmwFPN2evn3YHnig1pkKVYcvCV8owTd2yAcEkJfCX53g',
|
2018-05-25 08:03:25 +02:00
|
|
|
)
|
2018-06-12 16:02:04 +02:00
|
|
|
address = yield account.receiving.ensure_address_gap()
|
2018-07-14 23:47:18 +02:00
|
|
|
self.assertEqual(address[0], account.public_key.address)
|
2018-06-12 16:02:04 +02:00
|
|
|
|
2018-07-14 23:47:18 +02:00
|
|
|
private_key = yield self.ledger.get_private_key_for_address(address[0])
|
2018-05-25 08:03:25 +02:00
|
|
|
self.assertEqual(
|
|
|
|
private_key.extended_key_string(),
|
2018-08-06 08:52:52 +02:00
|
|
|
'xprv9s21ZrQH143K3TsAz5efNV8K93g3Ms3FXcjaWB9fVUsMwAoE3ZT4vYymkp'
|
|
|
|
'5BxKKfnpz8J6sHDFriX1SnpvjNkzcks8XBnxjGLS83BTyfpna',
|
2018-05-25 08:03:25 +02:00
|
|
|
)
|
2018-06-12 16:02:04 +02:00
|
|
|
|
2018-07-15 03:34:07 +02:00
|
|
|
invalid_key = yield self.ledger.get_private_key_for_address('BcQjRlhDOIrQez1WHfz3whnB33Bp34sUgX')
|
2018-06-11 15:33:32 +02:00
|
|
|
self.assertIsNone(invalid_key)
|
2018-05-25 08:03:25 +02:00
|
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
hexlify(private_key.wif()),
|
2018-08-06 08:52:52 +02:00
|
|
|
b'1c92caa0ef99bfd5e2ceb73b66da8cd726a9370be8c368d448a322f3c5b23aaab901'
|
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 = {
|
2018-07-14 23:47:18 +02:00
|
|
|
'name': 'My Account',
|
2018-05-25 08:03:25 +02:00
|
|
|
'seed':
|
|
|
|
"carbon smart garage balance margin twelve chest sword toast envelope bottom stomac"
|
|
|
|
"h absent",
|
|
|
|
'encrypted': False,
|
|
|
|
'private_key':
|
2018-08-06 08:52:52 +02:00
|
|
|
'xprv9s21ZrQH143K3TsAz5efNV8K93g3Ms3FXcjaWB9fVUsMwAoE3ZT4vYymkp'
|
|
|
|
'5BxKKfnpz8J6sHDFriX1SnpvjNkzcks8XBnxjGLS83BTyfpna',
|
2018-05-25 08:03:25 +02:00
|
|
|
'public_key':
|
2018-08-06 08:52:52 +02:00
|
|
|
'xpub661MyMwAqRbcFwwe67Bfjd53h5WXmKm6tqfBJZZH3pQLoy8Nb6mKUMJFc7'
|
|
|
|
'UbpVNzmwFPN2evn3YHnig1pkKVYcvCV8owTd2yAcEkJfCX53g',
|
2018-07-29 19:13:40 +02:00
|
|
|
'address_generator': {'name': 'single-address'}
|
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()
|
2018-07-14 23:47:18 +02:00
|
|
|
self.assertEqual(len(addresses), 1)
|
2018-06-11 15:33:32 +02:00
|
|
|
addresses = yield account.change.get_addresses()
|
2018-07-14 23:47:18 +02:00
|
|
|
self.assertEqual(len(addresses), 1)
|
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())
|