1

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);
web3.eth.accounts.wallet.add(user1);

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?

1

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
    self.web3.eth.sendRawTransaction(signed.rawTransaction)

More examples here:

https://github.com/TokenMarketNet/sto/blob/master/sto/ethereum/txservice.py#L331

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.