async def asyncSetUp(self): self.manager = WalletManager() config = {'data_path': '/tmp/wallet'} self.main_ledger = self.manager.get_or_create_ledger( Ledger.get_id(), config) self.test_ledger = self.manager.get_or_create_ledger( RegTestLedger.get_id(), config)
async def get_mock_wallet(sd_hash, storage, balance=10.0, fee=None): claim = Claim() if fee: if fee['currency'] == 'LBC': claim.stream.fee.lbc = Decimal(fee['amount']) elif fee['currency'] == 'USD': claim.stream.fee.usd = Decimal(fee['amount']) claim.stream.title = "33rpm" claim.stream.languages.append("en") claim.stream.source.sd_hash = sd_hash claim.stream.source.media_type = "image/png" tx = get_claim_transaction("33rpm", claim.to_bytes()) tx.height = 514081 txo = tx.outputs[0] txo.meta.update({ "permanent_url": "33rpm#c49566d631226492317d06ad7fdbe1ed32925124", }) class FakeHeaders: def estimated_timestamp(self, height): return 1984 def __init__(self, height): self.height = height def __getitem__(self, item): return {'timestamp': 1984} wallet = Wallet() ledger = Ledger({ 'db': Database(':memory:'), 'headers': FakeHeaders(514082) }) await ledger.db.open() wallet.generate_account(ledger) manager = WalletManager() manager.config = Config() manager.wallets.append(wallet) manager.ledgers[Ledger] = ledger manager.ledger.network.client = ClientSession( network=manager.ledger.network, server=('fakespv.lbry.com', 50001) ) async def mock_resolve(*args, **kwargs): result = {txo.meta['permanent_url']: txo} claims = [ StreamManager._convert_to_old_resolve_output(manager, result)[txo.meta['permanent_url']] ] await storage.save_claims(claims) return result manager.ledger.resolve = mock_resolve async def get_balance(*_): return balance manager.get_balance = get_balance return manager, txo.meta['permanent_url']
def test_read_write(self): manager = WalletManager() config = {'data_path': '/tmp/wallet'} ledger = manager.get_or_create_ledger(Ledger.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 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)
class TestWalletCreation(AsyncioTestCase): async def asyncSetUp(self): self.manager = WalletManager() config = {'data_path': '/tmp/wallet'} self.main_ledger = self.manager.get_or_create_ledger( Ledger.get_id(), config) self.test_ledger = self.manager.get_or_create_ledger( RegTestLedger.get_id(), config) def test_create_wallet_and_accounts(self): wallet = Wallet() self.assertEqual(wallet.name, 'Wallet') self.assertListEqual(wallet.accounts, []) account1 = wallet.generate_account(self.main_ledger) wallet.generate_account(self.main_ledger) wallet.generate_account(self.test_ledger) self.assertEqual(wallet.default_account, account1) self.assertEqual(len(wallet.accounts), 3) def test_load_and_save_wallet(self): wallet_dict = { 'version': 1, 'name': 'Main Wallet', 'preferences': {}, 'accounts': [{ 'certificates': {}, 'name': 'An Account', 'ledger': 'lbc_mainnet', 'modified_on': 123, 'seed': "carbon smart garage balance margin twelve chest sword toast envelope bottom stomac" "h absent", 'encrypted': False, 'private_key': 'xprv9s21ZrQH143K42ovpZygnjfHdAqSd9jo7zceDfPRogM7bkkoNVv7' 'DRNLEoB8HoirMgH969NrgL8jNzLEegqFzPRWM37GXd4uE8uuRkx4LAe', 'public_key': 'xpub661MyMwAqRbcGWtPvbWh9sc2BCfw2cTeVDYF23o3N1t6UZ5wv3EMm' 'Dgp66FxHuDtWdft3B5eL5xQtyzAtkdmhhC95gjRjLzSTdkho95asu9', 'address_generator': { 'name': 'deterministic-chain', 'receiving': { 'gap': 17, 'maximum_uses_per_address': 3 }, 'change': { 'gap': 10, 'maximum_uses_per_address': 3 } } }] } storage = WalletStorage(default=wallet_dict) wallet = Wallet.from_storage(storage, self.manager) self.assertEqual(wallet.name, 'Main Wallet') self.assertEqual( hexlify(wallet.hash), b'869acc4660dde0f13784ed743796adf89562cdf79fdfc9e5c6dbea98d62ccf90' ) self.assertEqual(len(wallet.accounts), 1) account = wallet.default_account self.assertIsInstance(account, Account) self.maxDiff = None self.assertDictEqual(wallet_dict, wallet.to_dict()) encrypted = wallet.pack('password') decrypted = Wallet.unpack('password', encrypted) self.assertEqual(decrypted['accounts'][0]['name'], 'An Account') def test_read_write(self): manager = WalletManager() config = {'data_path': '/tmp/wallet'} ledger = manager.get_or_create_ledger(Ledger.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 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.main_ledger) wallet2 = Wallet() wallet2.preferences['two'] = 2 wallet2.preferences['conflict'] = 2 # will be more recent wallet2.generate_account(self.main_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 })