1

I need to call a (state-changing) function on my contract.

Here is how I am currently executing this transaction:

let transaction = contract.methods.myFunc();
await web3.eth.personal.unlockAccount(ACCOUNT_ADDRESS, ACCOUNT_PASSWORD);
let gas = await transaction.estimateGas({from: ACCOUNT_ADDRESS, gasPrice: GAS_PRICE});
await transaction.send({from: ACCOUNT_ADDRESS, gasPrice: GAS_PRICE, gas: gas});

However, to my understanding, unlocking the account on my Ethereum client leaves a lot of security holes to deal with.

Is there any way for me to pass my credentials (account address and password) when I execute the transaction, instead of unlocking the account beforehand?

Documentation for unlockAccount is here.

Documentation for send is here.

Thank you very much!

2

You can just sign the transaction on your side and send a raw transaction, you will be doing this on python or java or another environment but not on your Ethereum client directly. So the risk is minimized.

Example:

var Tx = require('ethereumjs-tx');
dataTx = myContract.methods.myFunc(params).encodeABI(); //The encoded ABI of the method

var rawTx = {
  to: 'yourcantract address', 
  data:dataTx 
}

var tx = new Tx(rawTx);
tx.sign('yourprivateKey');

var serializedTx = tx.serialize();
web3.eth.sendSignedTransaction('0x' + serializedTx.toString('hex'))
.on('receipt', console.log);

Hope this helps.

  • Thank you. So I'm looking at functions sign and signTransaction here. But neither one of them takes the private key / passphrase / password as input. So how exactly can it possibly replace unlockAccount? – goodvibration Jun 16 '18 at 9:18
  • Oh, I think I got it. I need to use function web3.eth.personal.signTransaction (not function web3.eth.signTransaction). – goodvibration Jun 16 '18 at 9:27
  • yes! see here – Jaime Jun 16 '18 at 9:29
  • If this answered your question please accept the answer. – Jaime Jun 16 '18 at 9:30
  • Sorry, but it did not yet answered my question, because there are still things left to be solved and I have not been able to make it work as of yet. Would be great if you could add a coding example (or fix the one that I posted). Otherwise, I will continue investigate, and accept your answer when done. Thanks. – goodvibration Jun 16 '18 at 9:37

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.