0

This question already has an answer here:

I need to unlock accounts using web3js with infura.io, but it seems that infura does not support web3.personal modules.

In this case, I think options are 1.using geth or parity for client node not infura or 2.signing accounts locally before being sent to infura.

If the option 2 is possible, how to do that?

And also, I have user account addresses, public keys, and private keys for use.

marked as duplicate by Ismael, shane, Achala Dissanayake, Briomkez, Nicolas Massart Mar 29 at 16:44

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • In option 2, you mean ""signing transactions", not "signing accounts". – goodvibration Mar 27 at 12:24
0

With web3.js v1.0.0-beta.34, you can use these 3 functions:

async sendTransaction(transaction, value = 0) {
    return await signAndSendTransaction(transaction._parent._address, transaction.encodeABI(), value);
}

async sendEtherToContract(contract, value) {
    return await signAndSendTransaction(contract._address, "", value);
}

async sendEtherToAccount(account, value) {
    return await signAndSendTransaction(account, "", value, 21000);
}

All of which relying on this function:

async function signAndSendTransaction(to, data, value, gas = 0) {
    const options = {
        to   : to,
        data : data,
        value: value,
        gas  : gas ? gas : (await web3.eth.getBlock("latest")).gasLimit;
    };
    const signedTransaction  = await web3.eth.accounts.signTransaction(options, YOUR_PRIVATE_KEY);
    const transactionReceipt = await web3.eth.sendSignedTransaction(signedTransaction.rawTransaction);
    return transactionReceipt;
}
  • Thanks! What should I do in case of calling erc20 contract methods? – shuNjj Mar 27 at 12:36
  • @shuNjj: Something like await sendTransaction(yourContract.methods.yourFunction(arg1, arg2), value). If yourFunction is not payable, then you should pass 0 for value (or omit this argument altogether, since it's default value is 0). – goodvibration Mar 27 at 12:37
0

For option 2 you need to use web3.eth.accounts.signTransaction to sign your transaction locally, and then web3.eth.sendSignedTransaction to publish it.

If you need to call contract methods, you need to use yourContract.methods.yourMethod.encodeABI in order to prepare transaction data, then sign locally and publish transaction as usual.

  • Thanks! What should I do in case of calling erc20 contract methods? – shuNjj Mar 27 at 12:34
  • Added to the answer. – Mikhail Vladimirov Mar 27 at 12:36

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