3

I know there is a wallet called MyEtherWallet can sign with private key offline, and there seems to be javascript library that can also do this, but is there a python library or any code that can sign the raw transaction provided that I have private key string?

2 Answers 2

2

There are a few options. Account.signTransaction in web3.py is one such option: https://github.com/ethereum/web3.py/blob/master/docs/web3.eth.account.rst.

1
  • that's great help
    – xuanzhui
    Commented Jan 22, 2018 at 14:16
5

is there a python library or any code that can sign the raw transaction provided that I have private key string?

There is module called Account in Web3.py. This module has signTransaction method, which can be used like that:

transaction = {
        'to': '0xF0109fC8DF283027b6285cc889F5aA624EaC1F55',
        'value': 1000000000,
        'gas': 2000000,
        'gasPrice': 234567897654321,
        'nonce': 0,
        'chainId': 1
    }
key = '0x4c0883a69102937d6231471b5dbb6204fe5129617082792ae468d01a3f362318'
signed = w3.eth.account.signTransaction(transaction, key)

w3.eth.sendRawTransaction(signed.rawTransaction)
2
  • using this, I get this error: raise TypeError("Transaction had invalid fields: %r" % invalid) Commented Feb 8, 2019 at 15:12
  • @FedericoCaccia This appears to be an issue with addresses that are not checksummed github.com/ethereum/eth-account/issues/211
    – Ron
    Commented Jul 18, 2023 at 21:18

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.