Is there a way in web3.py to create a wallet of accounts similar to web3.js to sign the transactions locally without building a raw transaction?

web3.js code:

var provider = new Web3.providers.WebsocketProvider(conf.networkURL);
var web3 = new Web3(provider);

const user1 = web3.eth.accounts.privateKeyToAccount(conf.accounts.user1.privateKey);

const contract = new web3.eth.Contract(contractAbi, contractAddress, { gasPrice, gas });
var result = await contract.methods.myMethod().send({ from: user1.address, gas, value });

Is there a reason to not to be that functionality?


Web3.py 4.0 some example ocde, might not be 100% correct as it is out from my head:

    # Set transaction parameters
    tx_data = {}
    tx_data["nonce"] = 1  # You need to manually manage account nonce if you sign tx yourself
    tx_data["gas"] = 500000
    tx_data["gasPrice"] = 21000000000

    func = contract.transfer  # ERC-20 transfer 20 tokens
    unsigned_payload = func(**args).buildTransaction(tx_data)  # Construct raw tx payload
    signed = self.web3.eth.account.signTransaction(tx_data, self.private_key_hex)  # Sign with private key
    print(signed.hash.hex())  # Hex output

More examples here:


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.