def get_icontract(address, contract_name, contract_address=None): if contract_address is None: contract_address = get_status(address, contract_name)[2].strip() if not contract_exists(contract_name): raise FileNotFoundError("challenges/" + contract_name + ".py") file_name = constants.CHALLENGE_DIR + contract_name + ".py" # Load the file loader = importlib.machinery.SourceFileLoader("contract", file_name) module = types.ModuleType(loader.name) loader.exec_module(module) # Setup / reconstruct the EasyWeb3 object for this contract contract = module.Contract() contract.contract_address = contract_address contract.user_address = address contract.eweb3 = easyweb3.EasyWeb3() contract.eweb3._deployed_address = contract_address contract.eweb3._status = "deployed" bytecode, abi = get_bytecode_abi(contract_name) contract.contract_object = contract.eweb3._web3.eth.contract( abi=abi, address=contract_address) return contract
def test_contract_deployment(self): constants.GETH_DATADIR = "/tmp" constants.DEPLOY_FROM_ADDRESS = prefundedAddr w3handle = easyweb3.EasyWeb3() cbase = w3handle.balance(w3handle._web3.eth.defaultAccount) self.assertIsNotNone(cbase) cAddr = w3handle._deploy_solidity_contract( "01_naive_programmer", w3handle._web3.eth.defaultAccount) self.assertIsNotNone(cAddr)
def get_deploy_thread_status(address, contract): global deployers deploy_key = (address, contract) if not deploy_key in deployers: web3_instance = easyweb3.EasyWeb3() deployers[deploy_key] = web3_instance web3_instance.deploy_named_solidity_contract(contract, address) else: web3_instance = deployers[deploy_key] return web3_instance.deploy_status()
def get_grade_thread_status(address, contract_name): global graders grade_key = (address, contract_name) if not grade_key in graders: web3_instance = easyweb3.EasyWeb3() graders[grade_key] = web3_instance contract_addr = util.get_deployed_contract_address_for_challenge( address, util.get_contract_number(contract_name)) web3_instance.grade_challenge(contract_name, address, contract_addr) else: web3_instance = graders[grade_key] return web3_instance.deploy_status()
def test_challenges_01_naive_programmer(self): w3handle = easyweb3.EasyWeb3() cbase = w3handle._web3.eth.getBalance( w3handle._web3.eth.defaultAccount) self.assertIsNotNone(cbase) # Fund user account so they have money too try: w3handle._web3.personal.importRawKey(self.MOCK_PRIVATE_KEY, "") except ValueError as accountAlreadyExists: print("skipping importing the key for the mock user twice") w3handle._web3.personal.unlockAccount(self.MOCK_USER, "") fund_tx = w3handle._web3.eth.sendTransaction({ "from": prefundedAddr, "to": self.MOCK_USER, "value": FIVE_ETHER }) w3handle._web3.eth.waitForTransactionReceipt(fund_tx) self.assertGreaterEqual(w3handle._web3.eth.getBalance(self.MOCK_USER), FIVE_ETHER) cAddr = w3handle._deploy_solidity_contract( "01_naive_programmer", w3handle._web3.eth.defaultAccount) self.assertIsNotNone(cAddr) icontract = util.get_icontract(self.MOCK_USER, "01_naive_programmer", contract_address=cAddr) _, reconstructed_abi = util.get_bytecode_abi("01_naive_programmer") reconstructed_contract_handle = w3handle._web3.eth.contract( abi=reconstructed_abi, address=cAddr) self.assertFalse(icontract.has_been_hacked()) # solve the challenge solution_tx_hash = reconstructed_contract_handle.functions.SourceTrixAreFor1337( 21, "Bob", "Alice").transact({ "from": self.MOCK_USER, "gas": 500000 }) solution_tx_receipt = w3handle._web3.eth.waitForTransactionReceipt( solution_tx_hash) time.sleep(30) self.assertTrue(icontract.has_been_hacked())
def test_challenges_00_hello_world(self): w3handle = easyweb3.EasyWeb3() cbase = w3handle._web3.eth.getBalance( w3handle._web3.eth.defaultAccount) self.assertIsNotNone(cbase) # Fund user account so they have money too try: w3handle._web3.personal.importRawKey(self.MOCK_PRIVATE_KEY, "") except ValueError as accountAlreadyExists: print("skipping importing the key for the mock user twice") w3handle._web3.personal.unlockAccount(self.MOCK_USER, "") fund_tx = w3handle._web3.eth.sendTransaction({ "from": prefundedAddr, "to": self.MOCK_USER, "value": FIVE_ETHER }) w3handle._web3.eth.waitForTransactionReceipt(fund_tx) self.assertGreaterEqual(w3handle._web3.eth.getBalance(self.MOCK_USER), FIVE_ETHER) cAddr = w3handle._deploy_solidity_contract( "00_hello_world", w3handle._web3.eth.defaultAccount) self.assertIsNotNone(cAddr) icontract = util.get_icontract(self.MOCK_USER, "00_hello_world", contract_address=cAddr) _, reconstructed_abi = util.get_bytecode_abi("00_hello_world") reconstructed_contract_handle = w3handle._web3.eth.contract( abi=reconstructed_abi, address=cAddr) self.assertFalse(icontract.has_been_hacked()) # solve the challenge instring = self.MOCK_USER + "a very useless salt" solution_tx_hash = reconstructed_contract_handle.functions.helloworld( "hello", w3handle._web3.sha3(text=instring)).transact({ "from": self.MOCK_USER, "gas": 500000 }) solution_tx_receipt = w3handle._web3.eth.waitForTransactionReceipt( solution_tx_hash) self.assertTrue(icontract.has_been_hacked())
def wrapped(*args, **kwargs): # Because the address is sometimes in args and sometimes in kwargs address = None web3_handle = easyweb3.EasyWeb3() position = None if 'address' in kwargs: address = kwargs['address'] position = True else: address = args[0] position = False if web3_handle._web3.isAddress(address): if not web3_handle._web3.isChecksumAddress(address): newaddress = web3_handle._web3.toChecksumAddress(address) if position: kwargs['address'] = newaddress else: args[0] = newaddress else: return render_template("error.html") return fn(*args, **kwargs)