Skip to content
Advertisement

How to authenticate and send contract method using web3.js 1.0

I am confused about how I should be executing a contract’s method using the web3 1.0 library.

This code works (so long as I manually unlock the account first):

JavaScript

I get this error (if I don’t unlock manually first):

Returned error: authentication needed: password or unlock

The above code is an API endpoint in node.js, so I want it to unlock or authenticate programmatically.

There is no method in web3.js 1.0 to unlock the account.

I also don’t think this is necessary (at least that’s what I am confused about). Since I am managing accounts, I know what the private key is.

I am thinking the transaction needs to be signed with the private key?? Is this correct? Is this effectively the same thing as “unlocking the account”?

I tried doing this:

JavaScript

I get this error:

Returned error: The method net_version does not exist/is not available

What is the easiest way to authenticate and submit a transaction?

Ideally, I want to use the first approach in my code sample, as it is the cleanest.

Advertisement

Answer

This code allows me to sign a transaction server-side (node.js) using the privateKey from the account I created (using web3.eth.accounts.create()), and send the signed transaction to the network without having to unlock the account.

I am using Geth 1.7.1

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement