lbry-sdk/torba/tests/client_tests/unit/test_wallet.py

149 lines
5.9 KiB
Python
Raw Normal View History

import tempfile
2019-03-12 03:24:57 +01:00
from binascii import hexlify
2018-05-25 08:03:25 +02:00
from unittest import TestCase, mock
2018-11-04 06:55:50 +01:00
from torba.testcase import AsyncioTestCase
2018-10-15 06:45:21 +02:00
2018-06-14 02:57:57 +02:00
from torba.coin.bitcoinsegwit import MainNetLedger as BTCLedger
from torba.coin.bitcoincash import MainNetLedger as BCHLedger
2018-11-04 06:55:50 +01:00
from torba.client.basemanager import BaseWalletManager
from torba.client.wallet import Wallet, WalletStorage, TimestampedPreferences
2018-05-25 08:03:25 +02:00
2018-10-15 06:45:21 +02:00
class TestWalletCreation(AsyncioTestCase):
2018-05-25 08:03:25 +02:00
2018-10-15 06:45:21 +02:00
async def asyncSetUp(self):
self.manager = BaseWalletManager()
config = {'data_path': '/tmp/wallet'}
2018-06-14 02:57:57 +02:00
self.btc_ledger = self.manager.get_or_create_ledger(BTCLedger.get_id(), config)
self.bch_ledger = self.manager.get_or_create_ledger(BCHLedger.get_id(), config)
2018-05-25 08:03:25 +02:00
def test_create_wallet_and_accounts(self):
wallet = Wallet()
self.assertEqual(wallet.name, 'Wallet')
self.assertListEqual(wallet.accounts, [])
2018-05-25 08:03:25 +02:00
account1 = wallet.generate_account(self.btc_ledger)
2018-06-14 02:57:57 +02:00
wallet.generate_account(self.btc_ledger)
wallet.generate_account(self.bch_ledger)
2018-05-25 08:03:25 +02:00
self.assertEqual(wallet.default_account, account1)
self.assertEqual(len(wallet.accounts), 3)
def test_load_and_save_wallet(self):
wallet_dict = {
'version': 1,
2018-05-25 08:03:25 +02:00
'name': 'Main Wallet',
'preferences': {},
2018-05-25 08:03:25 +02:00
'accounts': [
{
'name': 'An Account',
2018-06-14 02:57:57 +02:00
'ledger': 'btc_mainnet',
2019-03-12 03:24:57 +01:00
'modified_on': 123.456,
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':
'xprv9s21ZrQH143K3TsAz5efNV8K93g3Ms3FXcjaWB9fVUsMwAoE3Z'
'T4vYymkp5BxKKfnpz8J6sHDFriX1SnpvjNkzcks8XBnxjGLS83BTyfpna',
2018-05-25 08:03:25 +02:00
'public_key':
'xpub661MyMwAqRbcFwwe67Bfjd53h5WXmKm6tqfBJZZH3pQLoy8Nb6'
'mKUMJFc7UbpVNzmwFPN2evn3YHnig1pkKVYcvCV8owTd2yAcEkJfCX53g',
2018-07-29 19:13:40 +02:00
'address_generator': {
'name': 'deterministic-chain',
'receiving': {'gap': 17, 'maximum_uses_per_address': 3},
'change': {'gap': 10, 'maximum_uses_per_address': 3}
}
2018-05-25 08:03:25 +02:00
}
]
}
storage = WalletStorage(default=wallet_dict)
wallet = Wallet.from_storage(storage, self.manager)
self.assertEqual(wallet.name, 'Main Wallet')
2019-03-12 03:24:57 +01:00
self.assertEqual(
hexlify(wallet.hash), b'1bd61fbe18875cb7828c466022af576104ed861c8a1fdb1dadf5e39417a68483'
2019-03-12 03:24:57 +01:00
)
2018-05-25 08:03:25 +02:00
self.assertEqual(len(wallet.accounts), 1)
account = wallet.default_account
2018-06-14 02:57:57 +02:00
self.assertIsInstance(account, BTCLedger.account_class)
self.maxDiff = None
2018-05-25 08:03:25 +02:00
self.assertDictEqual(wallet_dict, wallet.to_dict())
2019-03-12 03:24:57 +01:00
encrypted = wallet.pack('password')
decrypted = Wallet.unpack('password', encrypted)
self.assertEqual(decrypted['accounts'][0]['name'], 'An Account')
def test_read_write(self):
manager = BaseWalletManager()
config = {'data_path': '/tmp/wallet'}
ledger = manager.get_or_create_ledger(BTCLedger.get_id(), config)
with tempfile.NamedTemporaryFile(suffix='.json') as wallet_file:
wallet_file.write(b'{"version": 1}')
wallet_file.seek(0)
# create and write wallet to a file
2018-07-05 03:17:45 +02:00
wallet = manager.import_wallet(wallet_file.name)
account = wallet.generate_account(ledger)
wallet.save()
# read wallet from file
wallet_storage = WalletStorage(wallet_file.name)
wallet = Wallet.from_storage(wallet_storage, manager)
self.assertEqual(account.public_key.address, wallet.default_account.public_key.address)
def test_merge(self):
wallet1 = Wallet()
wallet1.preferences['one'] = 1
wallet1.preferences['conflict'] = 1
wallet1.generate_account(self.btc_ledger)
wallet2 = Wallet()
wallet2.preferences['two'] = 2
wallet2.preferences['conflict'] = 2 # will be more recent
wallet2.generate_account(self.btc_ledger)
self.assertEqual(len(wallet1.accounts), 1)
self.assertEqual(wallet1.preferences, {'one': 1, 'conflict': 1})
added = wallet1.merge(self.manager, 'password', wallet2.pack('password'))
self.assertEqual(added[0].id, wallet2.default_account.id)
self.assertEqual(len(wallet1.accounts), 2)
self.assertEqual(wallet1.accounts[1].id, wallet2.default_account.id)
self.assertEqual(wallet1.preferences, {'one': 1, 'two': 2, 'conflict': 2})
class TestTimestampedPreferences(TestCase):
def test_hash(self):
p = TimestampedPreferences()
self.assertEqual(
hexlify(p.hash), b'44136fa355b3678a1146ad16f7e8649e94fb4fc21fe77e8310c060f61caaff8a'
)
with mock.patch('time.time', mock.Mock(return_value=12345)):
p['one'] = 1
self.assertEqual(
hexlify(p.hash), b'c9e82bf4cb099dd0125f78fa381b21a8131af601917eb531e1f5f980f8f3da66'
)
def test_merge(self):
p1 = TimestampedPreferences()
p2 = TimestampedPreferences()
with mock.patch('time.time', mock.Mock(return_value=10)):
p1['one'] = 1
p1['conflict'] = 1
with mock.patch('time.time', mock.Mock(return_value=20)):
p2['two'] = 2
p2['conflict'] = 2
# conflict in p2 overrides conflict in p1
p1.merge(p2.data)
self.assertEqual(p1, {'one': 1, 'two': 2, 'conflict': 2})
# have a newer conflict in p1 so it is not overridden this time
with mock.patch('time.time', mock.Mock(return_value=21)):
p1['conflict'] = 1
p1.merge(p2.data)
self.assertEqual(p1, {'one': 1, 'two': 2, 'conflict': 1})