def create_blockchain_wallet_from_private_key( self, private_key, allow_existing=False, wei_target_balance=0, wei_topup_threshold=0, ): address = BlockchainWallet.address_from_private_key(private_key) existing_wallet = session.query(BlockchainWallet).filter_by( address=address).first() if existing_wallet: if allow_existing: return existing_wallet else: raise WalletExistsError( "Account for provided private key already exists") wallet = BlockchainWallet(private_key=private_key, wei_target_balance=wei_target_balance, wei_topup_threshold=wei_topup_threshold) session.add(wallet) session.commit() return wallet
def test_duplicate_wallets_cant_be_added(self, db_session): wallet_1 = BlockchainWallet(pk) wallet_2 = BlockchainWallet(pk) with pytest.raises(IntegrityError): db_session.add(wallet_1) db_session.add(wallet_2) db_session.commit()
def test_create_wallet_from_pk(self): pair = keypair() wallet = BlockchainWallet(pair['pk']) assert wallet.address == pair['address']
def dummy_wallet(db_session): from sql_persistence.models import BlockchainWallet w = BlockchainWallet() db_session.add(w) db_session.commit() return w
def test_create_wallet(self): wallet = BlockchainWallet() assert Web3.isChecksumAddress(wallet.address) pk = bytes.fromhex(wallet.private_key.replace('0x', '')) assert keys.PrivateKey( pk).public_key.to_checksum_address() == wallet.address assert wallet.encrypted_private_key != pk
def test_create_blockchain_transaction( self, db_session, persistence_module: SQLPersistenceInterface): wallet = BlockchainWallet() db_session.add(wallet) task = BlockchainTask(str_uuid()) task.signing_wallet = wallet db_session.add(task) trans = persistence_module.create_blockchain_transaction( task_uuid=task.uuid) assert trans.task.uuid == task.uuid
def test_deploy_contract_test(self, db_session, persistence_module: SQLPersistenceInterface): signing_wallet_obj = BlockchainWallet() db_session.add(signing_wallet_obj) uuid = str_uuid() contract_name = 'ERC20' args = [] kwargs = None gas_limit = None prior_tasks = None trans = persistence_module.create_deploy_contract_task( uuid, signing_wallet_obj, contract_name, args, kwargs, gas_limit, prior_tasks) assert trans.uuid == uuid assert trans.type == 'DEPLOY_CONTRACT'
def create_new_blockchain_wallet(self, wei_target_balance=0, wei_topup_threshold=0, private_key=None): if private_key: return self.create_blockchain_wallet_from_private_key( private_key, True, wei_target_balance, wei_topup_threshold, ) wallet = BlockchainWallet(wei_target_balance=wei_target_balance, wei_topup_threshold=wei_topup_threshold) self.session.add(wallet) self.session.commit() return wallet
def test_claim_transaction_nonce( self, db_session, persistence_module: SQLPersistenceInterface): def created_nonced_transaction(): t = BlockchainTransaction( first_block_hash=persistence_module.first_block_hash) t.signing_wallet = wallet db_session.add(t) db_session.commit() nonce = persistence_module.locked_claim_transaction_nonce( network_nonce=starting_nonce, signing_wallet_id=wallet.id, transaction_id=t.id) t.nonce_consumed = True return t, nonce wallet = BlockchainWallet() db_session.add(wallet) starting_nonce = 4 transactions = [] for i in range(0, 3): trans, nonce = created_nonced_transaction() transactions.append(trans) assert nonce == starting_nonce + i for t in transactions: t.nonce_consumed = False transactions[0].status = 'FAILED' trans, nonce = created_nonced_transaction() transactions.append(trans) assert trans.nonce == starting_nonce
def test_create_function_task(self, db_session, persistence_module: SQLPersistenceInterface): signing_wallet_obj = BlockchainWallet() db_session.add(signing_wallet_obj) uuid = str_uuid() contract_address = '0x1234' abi_type = "ERC20" # aka contract_type function_name = "transferFrom" args = ['0x1234', '0x2345', int(1e36)] kwargs = None signing_address = None encrypted_private_key = None gas_limit = None prior_tasks = None reserves_task = None trans = persistence_module.create_function_task( uuid, signing_wallet_obj, contract_address, abi_type, function_name, args, kwargs, gas_limit, prior_tasks, reserves_task) assert trans.uuid == uuid assert trans.type == 'FUNCTION'
def create_blockchain_wallet_from_encrypted_private_key( self, encrypted_private_key): private_key = BlockchainWallet.decrypt_private_key( encrypted_private_key) self.create_blockchain_wallet_from_private_key(private_key)
def test_wallets_are_random(self): wallet_1 = BlockchainWallet() wallet_2 = BlockchainWallet() assert wallet_1.private_key != wallet_2.private_key
def test_create_wallet_from_pk(self): wallet = BlockchainWallet(pk) assert wallet.address == address