8545 solve

This commit is contained in:
2023-09-12 21:41:06 +02:00
parent c0dbb0e7a2
commit e913fc7a39
2 changed files with 156 additions and 0 deletions

View File

@@ -0,0 +1,78 @@
from web3 import *
from solcx import * # py-solc-x
from web3.middleware import geth_poa_middleware # some middleware magic
install_solc()
# Initialize Vars
node_url = "https://blockchain-eightfivefourfive-3e45438902cf45cf-eth.2023.ductf.dev:8545"
contract_address = "0xf22cB0Ca047e88AC996c17683Cee290518093574"
# Initialize the address calling the functions/signing transactions
player_address = "0x6BECafE7B0520Ad68E37c9A66Fd748b1204c1087"
private_key = "0xd660674d680ec9f24931ae685ebb065ebe89248f02caa91d02cb5db10ddefbdc" # To sign the transaction
src = compile_source("""
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.19;
contract EightFiveFourFive {
string private use_this;
bool public you_solved_it = false;
constructor(string memory some_string) {
use_this = some_string;
}
function readTheStringHere() external view returns (string memory) {
return use_this;
}
function solve_the_challenge(string memory answer) external {
you_solved_it = keccak256(bytes(answer)) == keccak256(bytes(use_this));
}
function isSolved() external view returns (bool) {
return you_solved_it;
}
}
""",output_values=['abi', 'bin'])
def get_nonce():
return web3.eth.get_transaction_count(player_address)
contract_id, contract_interface = src.popitem()
abi = contract_interface['abi']
# Create the node connection
web3 = Web3(Web3.HTTPProvider(node_url))
web3.middleware_onion.inject(geth_poa_middleware, layer=0)
# Verify if the connection is successful
if web3.is_connected():
print("-" * 50)
print("Connection Successful", web3.eth.chain_id)
print("-" * 50)
else:
print("Connection Failed")
# Initialize address nonce
nonce = web3.eth.get_transaction_count(player_address)
code = web3.eth.get_code(contract_address)
# Create smart contract instance
contract = web3.eth.contract(address=contract_address, abi=abi)
# initialize the chain id, we need it to build the transaction for replay protection
Chain_id = web3.eth.chain_id
answer = contract.functions.readTheStringHere().call()
print("answer = ", answer)
trx = contract.functions.solve_the_challenge(str(answer)).build_transaction({'from':player_address, 'nonce':get_nonce(), 'gasPrice':1})
print(trx)
strans = web3.eth.account.sign_transaction(trx, private_key)
web3.eth.send_raw_transaction(strans.rawTransaction)
print("isSolved = ", contract.functions.isSolved().call())
pass