0

make a simple eth raw tx and self sign without web3

I only need generate a eth raw transaction and self sign , web3.py is bloated.

I generate lots of eth address using python like this:

def create_wallet():
    keccak = sha3.keccak_256()
    private_key = SigningKey.generate(curve=SECP256k1)
    public_key = private_key.get_verifying_key().to_string()

    keccak.update(public_key)

    address = '0x%s' % keccak.hexdigest()[24:]

    private_key = binascii.hexlify(private_key.to_string())
    public_key = binascii.hexlify(public_key)

    return {
        "private_key": private_key.decode(),
        "public_key": public_key.decode(),
        "address": address
    }

now i want make a simple raw transaction and sign it

1 Answer 1

2

eth-account is the component that handles transaction signing in web3.py. You can install it directly with pip install eth-account.

Then you can sign a transaction like so:

>>> from eth_account import Account

>>> transaction = {
        # Note that the address must be in checksum format, until this is resolved:
        # https://github.com/ethereum/eth-account/issues/32
        'to': '0xF0109fC8DF283027b6285cc889F5aA624EaC1F55',
        'value': 1000000000,
        'gas': 2000000,
        'gasPrice': 234567897654321,
        'nonce': 0,
        'chainId': 1
    }
>>> key = '0x4c0883a69102937d6231471b5dbb6204fe5129617082792ae468d01a3f362318'

>>> signed = Account.signTransaction(transaction, key)
{'hash': HexBytes('0x6893a6ee8df79b0f5d64a180cd1ef35d030f3e296a5361cf04d02ce720d32ec5'),
 'r': 4487286261793418179817841024889747115779324305375823110249149479905075174044,
 'rawTransaction': HexBytes('0xf86a8086d55698372431831e848094f0109fc8df283027b6285cc889f5aa624eac1f55843b9aca008025a009ebb6ca057a0535d6186462bc0b465b561c94a295bdb0621fc19208ab149a9ca0440ffd775ce91a833ab410777204d5341a6f9fa91216a6f3ee2c051fea6a0428'),  # noqa: E501
 's': 30785525769477805655994251009256770582792548537338581640010273753578382951464,
 'v': 37}

>>> w3.eth.sendRawTransaction(signed.rawTransaction)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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