2018-06-18 05:22:15 +02:00
|
|
|
import tempfile
|
2018-05-25 08:03:25 +02:00
|
|
|
from twisted.trial import unittest
|
|
|
|
|
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-06-11 15:33:32 +02:00
|
|
|
from torba.manager import WalletManager
|
2018-06-14 02:57:57 +02:00
|
|
|
from torba.wallet import Wallet, WalletStorage
|
2018-05-25 08:03:25 +02:00
|
|
|
|
|
|
|
|
|
|
|
class TestWalletCreation(unittest.TestCase):
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.manager = WalletManager()
|
2018-06-14 02:57:57 +02:00
|
|
|
config = {'wallet_path': '/tmp/wallet'}
|
|
|
|
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.assertEqual(wallet.accounts, [])
|
|
|
|
|
|
|
|
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 = {
|
|
|
|
'name': 'Main Wallet',
|
|
|
|
'accounts': [
|
|
|
|
{
|
2018-06-14 02:57:57 +02:00
|
|
|
'ledger': 'btc_mainnet',
|
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-06-18 05:22:15 +02:00
|
|
|
'xprv9s21ZrQH143K2dyhK7SevfRG72bYDRNv25yKPWWm6dqApNxm1Zb1m5gGcBWYfbsPjTr2v5joit8Af2Zp5P'
|
|
|
|
'6yz3jMbycrLrRMpeAJxR8qDg8',
|
2018-05-25 08:03:25 +02:00
|
|
|
'public_key':
|
2018-06-18 05:22:15 +02:00
|
|
|
'xpub661MyMwAqRbcF84AR8yfHoMzf4S2ct6mPJtvBtvNeyN9hBHuZ6uGJszkTSn5fQUCdz3XU17eBzFeAUwV6f'
|
|
|
|
'iW44g14WF52fYC5J483wqQ5ZP',
|
2018-05-25 08:03:25 +02:00
|
|
|
'receiving_gap': 10,
|
2018-06-14 02:57:57 +02:00
|
|
|
'receiving_maximum_use_per_address': 2,
|
2018-05-25 08:03:25 +02:00
|
|
|
'change_gap': 10,
|
2018-06-14 02:57:57 +02:00
|
|
|
'change_maximum_use_per_address': 2,
|
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')
|
|
|
|
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())
|
2018-06-18 05:22:15 +02:00
|
|
|
|
|
|
|
def test_read_write(self):
|
|
|
|
manager = WalletManager()
|
|
|
|
config = {'wallet_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'{}')
|
|
|
|
wallet_file.seek(0)
|
|
|
|
|
|
|
|
# create and write wallet to a file
|
|
|
|
wallet_storage = WalletStorage(wallet_file.name)
|
|
|
|
wallet = Wallet.from_storage(wallet_storage, manager)
|
|
|
|
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)
|