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) wallet.generate_account(self.btc_ledger) wallet.generate_account(self.bch_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', 'accounts': [ { 'name': 'An Account', 'ledger': 'btc_mainnet', 'seed': "carbon smart garage balance margin twelve chest sword toast envelope bottom stomac" "h absent", 'encrypted': False, 'private_key': 'xprv9s21ZrQH143K3TsAz5efNV8K93g3Ms3FXcjaWB9fVUsMwAoE3Z' 'T4vYymkp5BxKKfnpz8J6sHDFriX1SnpvjNkzcks8XBnxjGLS83BTyfpna', 'public_key': 'xpub661MyMwAqRbcFwwe67Bfjd53h5WXmKm6tqfBJZZH3pQLoy8Nb6' 'mKUMJFc7UbpVNzmwFPN2evn3YHnig1pkKVYcvCV8owTd2yAcEkJfCX53g', '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(len(wallet.accounts), 1) account = wallet.default_account self.assertIsInstance(account, BTCLedger.account_class) self.maxDiff = None self.assertDictEqual(wallet_dict, wallet.to_dict())
def test_sign(self): account = self.ledger.account_class.from_dict( self.ledger, Wallet(), { "seed": "carbon smart garage balance margin twelve chest sword " "toast envelope bottom stomach absent" }) yield account.ensure_address_gap() address1, address2 = yield account.receiving.get_addresses(2) pubkey_hash1 = self.ledger.address_to_hash160(address1) pubkey_hash2 = self.ledger.address_to_hash160(address2) tx_class = ledger_class.transaction_class tx = tx_class() \ .add_inputs([tx_class.input_class.spend(get_output(2*COIN, pubkey_hash1))]) \ .add_outputs([tx_class.output_class.pay_pubkey_hash(int(1.9*COIN), pubkey_hash2)]) \ yield tx.sign([account]) print(hexlify(tx.inputs[0].script.values['signature'])) self.assertEqual( hexlify(tx.inputs[0].script.values['signature']), b'304402205a1df8cd5d2d2fa5934b756883d6c07e4f83e1350c740992d47a12422' b'226aaa202200098ac8675827aea2b0d6f0e49566143a95d523e311d342172cd99e2021e47cb01' )
def test_load_and_save_account(self): account_data = { 'name': 'Main Account', 'seed': "carbon smart garage balance margin twelve chest sword toast envelope bottom stomac" "h absent", 'encrypted': False, 'private_key': 'xprv9s21ZrQH143K42ovpZygnjfHdAqSd9jo7zceDfPRogM7bkkoNVv7DRNLEoB8' 'HoirMgH969NrgL8jNzLEegqFzPRWM37GXd4uE8uuRkx4LAe', 'public_key': 'xpub661MyMwAqRbcGWtPvbWh9sc2BCfw2cTeVDYF23o3N1t6UZ5wv3EMmDgp66FxH' 'uDtWdft3B5eL5xQtyzAtkdmhhC95gjRjLzSTdkho95asu9', 'certificates': {}, 'address_generator': { 'name': 'deterministic-chain', 'receiving': { 'gap': 17, 'maximum_uses_per_address': 2 }, 'change': { 'gap': 10, 'maximum_uses_per_address': 2 } } } account = Account.from_dict(self.ledger, Wallet(), account_data) account_data['ledger'] = 'lbc_mainnet' self.assertDictEqual(account_data, account.to_dict())
async def test_generate_account_from_seed(self): account = Account.from_dict( self.ledger, Wallet(), { "seed": "carbon smart garage balance margin twelve chest sword toas" "t envelope bottom stomach absent" }) self.assertEqual( account.private_key.extended_key_string(), 'xprv9s21ZrQH143K42ovpZygnjfHdAqSd9jo7zceDfPRogM7bkkoNVv7DRNLEoB8' 'HoirMgH969NrgL8jNzLEegqFzPRWM37GXd4uE8uuRkx4LAe') self.assertEqual( account.public_key.extended_key_string(), 'xpub661MyMwAqRbcGWtPvbWh9sc2BCfw2cTeVDYF23o3N1t6UZ5wv3EMmDgp66FxH' 'uDtWdft3B5eL5xQtyzAtkdmhhC95gjRjLzSTdkho95asu9') address = await account.receiving.ensure_address_gap() self.assertEqual(address[0], 'bCqJrLHdoiRqEZ1whFZ3WHNb33bP34SuGx') private_key = await self.ledger.get_private_key_for_address( 'bCqJrLHdoiRqEZ1whFZ3WHNb33bP34SuGx') self.assertEqual( private_key.extended_key_string(), 'xprv9vwXVierUTT4hmoe3dtTeBfbNv1ph2mm8RWXARU6HsZjBaAoFaS2FRQu4fptR' 'AyJWhJW42dmsEaC1nKnVKKTMhq3TVEHsNj1ca3ciZMKktT') private_key = await self.ledger.get_private_key_for_address( 'BcQjRlhDOIrQez1WHfz3whnB33Bp34sUgX') self.assertIsNone(private_key)
async def asyncSetUp(self): self.ledger = MainNetLedger({ 'db': MainNetLedger.database_class(':memory:'), 'headers': MainNetLedger.headers_class(':memory:') }) self.account = Account.generate(self.ledger, Wallet(), "lbryum") await self.ledger.db.open()
def from_config(cls, config: dict) -> 'BaseWalletManager': manager = cls() for ledger_id, ledger_config in config.get('ledgers', {}).items(): manager.get_or_create_ledger(ledger_id, ledger_config) for wallet_path in config.get('wallets', []): wallet_storage = WalletStorage(wallet_path) wallet = Wallet.from_storage(wallet_storage, manager) manager.wallets.append(wallet) return manager
def test_update_history(self): account = self.ledger.account_class.generate(self.ledger, Wallet(), "torba") address = yield account.receiving.get_or_create_usable_address() address_details = yield self.ledger.db.get_address(address) self.assertEqual(address_details['history'], None) self.add_header(block_height=0, merkle_root=b'abcd04') self.add_header(block_height=1, merkle_root=b'abcd04') self.add_header(block_height=2, merkle_root=b'abcd04') self.add_header(block_height=3, merkle_root=b'abcd04') self.ledger.network = MockNetwork( [ { 'tx_hash': 'abcd01', 'height': 0 }, { 'tx_hash': 'abcd02', 'height': 1 }, { 'tx_hash': 'abcd03', 'height': 2 }, ], { 'abcd01': hexlify(get_transaction(get_output(1)).raw), 'abcd02': hexlify(get_transaction(get_output(2)).raw), 'abcd03': hexlify(get_transaction(get_output(3)).raw), }) yield self.ledger.update_history(address) self.assertEqual(self.ledger.network.get_history_called, [address]) self.assertEqual(self.ledger.network.get_transaction_called, ['abcd01', 'abcd02', 'abcd03']) address_details = yield self.ledger.db.get_address(address) self.assertEqual(address_details['history'], 'abcd01:0:abcd02:1:abcd03:2:') self.ledger.network.get_history_called = [] self.ledger.network.get_transaction_called = [] yield self.ledger.update_history(address) self.assertEqual(self.ledger.network.get_history_called, [address]) self.assertEqual(self.ledger.network.get_transaction_called, []) self.ledger.network.history.append({'tx_hash': 'abcd04', 'height': 3}) self.ledger.network.transaction['abcd04'] = hexlify( get_transaction(get_output(4)).raw) self.ledger.network.get_history_called = [] self.ledger.network.get_transaction_called = [] yield self.ledger.update_history(address) self.assertEqual(self.ledger.network.get_history_called, [address]) self.assertEqual(self.ledger.network.get_transaction_called, ['abcd04']) address_details = yield self.ledger.db.get_address(address) self.assertEqual(address_details['history'], 'abcd01:0:abcd02:1:abcd03:2:abcd04:3:')
def from_config(cls, config): # type: (Dict) -> WalletManager wallets = [] manager = cls(wallets) for coin_id, ledger_config in config.get('ledgers', {}).items(): manager.get_or_create_ledger(coin_id, ledger_config) for wallet_path in config.get('wallets', []): wallet_storage = WalletStorage(wallet_path) wallet = Wallet.from_storage(wallet_storage, manager) wallets.append(wallet) return manager
def setUp(self): super().setUp() self.ledger = MainNetLedger({ 'db': MainNetLedger.database_class(':memory:'), 'headers': MainNetLedger.headers_class(':memory:') }) self.account = Account.generate(self.ledger, Wallet(), "lbryum") return self.ledger.db.open()
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)
def setUp(self): self.ledger = ledger_class({ 'db': ledger_class.database_class(':memory:'), 'headers': ledger_class.headers_class(':memory:'), }) yield self.ledger.db.open() self.account = self.ledger.account_class.from_dict( self.ledger, Wallet(), { "seed": "carbon smart garage balance margin twelve chest sword " "toast envelope bottom stomach absent" }) addresses = yield self.account.ensure_address_gap() self.pubkey_hash = [ self.ledger.address_to_hash160(a) for a in addresses ] self.hash_cycler = cycle(self.pubkey_hash)
async def test_generate_account(self): account = Account.generate(self.ledger, Wallet(), 'lbryum') 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) self.assertEqual(account.public_key.ledger, self.ledger) self.assertEqual(account.private_key.public_key, account.public_key) addresses = await account.receiving.get_addresses() self.assertEqual(len(addresses), 0) addresses = await account.change.get_addresses() self.assertEqual(len(addresses), 0) await account.ensure_address_gap() addresses = await account.receiving.get_addresses() self.assertEqual(len(addresses), 20) addresses = await account.change.get_addresses() self.assertEqual(len(addresses), 6)
async def test_sign(self): account = self.ledger.account_class.from_dict( self.ledger, Wallet(), { "seed": "carbon smart garage balance margin twelve chest sword toas" "t envelope bottom stomach absent" }) await account.ensure_address_gap() address1, address2 = await account.receiving.get_addresses(limit=2) pubkey_hash1 = self.ledger.address_to_hash160(address1) pubkey_hash2 = self.ledger.address_to_hash160(address2) tx = Transaction() \ .add_inputs([Input.spend(get_output(int(2*COIN), pubkey_hash1))]) \ .add_outputs([Output.pay_pubkey_hash(int(1.9*COIN), pubkey_hash2)]) await tx.sign([account]) self.assertEqual( hexlify(tx.inputs[0].script.values['signature']), b'304402200dafa26ad7cf38c5a971c8a25ce7d85a076235f146126762296b1223c42ae21e022020ef9eeb8' b'398327891008c5c0be4357683f12cb22346691ff23914f457bf679601')
def test_load_and_save_wallet(self): wallet_dict = { 'name': 'Main Wallet', 'accounts': [{ 'ledger': 'btc_mainnet', 'seed': "carbon smart garage balance margin twelve chest sword toast envelope bottom stomac" "h absent", 'encrypted': False, 'private_key': 'xprv9s21ZrQH143K2dyhK7SevfRG72bYDRNv25yKPWWm6dqApNxm1Zb1m5gGcBWYfbsPjTr2v5joit8Af2Zp5P' '6yz3jMbycrLrRMpeAJxR8qDg8', 'public_key': 'xpub661MyMwAqRbcF84AR8yfHoMzf4S2ct6mPJtvBtvNeyN9hBHuZ6uGJszkTSn5fQUCdz3XU17eBzFeAUwV6f' 'iW44g14WF52fYC5J483wqQ5ZP', 'receiving_gap': 10, 'receiving_maximum_use_per_address': 2, 'change_gap': 10, 'change_maximum_use_per_address': 2, }] } 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 self.assertIsInstance(account, BTCLedger.account_class) self.maxDiff = None self.assertDictEqual(wallet_dict, wallet.to_dict())
def create_wallet(self, path): storage = WalletStorage(path) wallet = Wallet.from_storage(storage, self) self.wallets.append(wallet) return wallet