In web3 APIs I could not see an option to specify private key for sending a transaction in doc. How this is done then ? My understanding is that we need private key of a particular account to send transaction.

  • Have you tried personal.unlockAccount()? web3py.readthedocs.io/en/latest/… You can unlock the account that you specified in from field then the client will use the corresponding private key for signing. Commented Nov 14, 2017 at 11:09

2 Answers 2


web3 sends transactions using unlocked wallets, which requires that you unlock your account using the keyfile and the password prior to sending a transaction. web3 is then able to access the the private key from the unlocked wallet and sign the transaction.

If you want to avoid unlocking the wallet, you need to sign the transaction yourself and send a raw transaction using eth.sendRawTransaction.

Unfortunately, web3 does not support signing of transactions. However, you can sign a transactrion using ethereumjs-tx. To obtain a hex string serializedTx containing the signed transaction use

const tx = new EthereumTx(txParams); 
let serializedTx = "0x" + tx.serialize().toString('hex');

serializedTx can be used with eth.sendRawTransaction.

For signing the transactions, the private key privateKeyis needed. If you don't have the private key, you can use myetherwallet.com to obtain it from a keyfile and the passphrase, or you can optain the private key programatically from a keyfile and the password using

let privateKey = keythereum.recover(password, keyObject);

from keyethereum. keyObject is the content of the keyfile, which is a JSON string.

  • Thanks @gisdev_p for the reply. I got the concept now. One query is that How can I sign the smart contract transactions ? If I am moving with the ethereumjs-tx way other than web3 pass phrase way. Commented Nov 14, 2017 at 12:20
  • Actually, there is no difference if you make a transaction to a normal account or a contract. The handling of the transaction and the signing ist the same. That you will typically have a data part with a transaction to a contract, but most likely not with a transaction to a normal account, does not have any influence on the signing. You just add the data part to the transation and sign it.
    – gisdev_p
    Commented Nov 14, 2017 at 12:52
  • The process is documented in full here Commented Jun 5, 2018 at 4:23

The signing of the transaction is handled by the Provider set on the web3 object.

For example, if the user is interacting with a Dapp through a browser then it is likely that the provider is MetaMask and this will handle the user authorising the transactions.

  • I am actually trying from server. From my python Django application. How can I do it then with web3.py ? Commented Nov 14, 2017 at 10:31
  • I'm curious based-on what you just said, if I'm using built-in provider e.g. web3.providers.HttpProvider does it do transaction signing?
    – c.sokun
    Commented Nov 14, 2017 at 10:37
  • @c.sokun, if you have a local Ethereum node running and the RPC API exposed then that node will handle the TX signing
    – SteveJaxon
    Commented Nov 15, 2017 at 11:25

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.