Web3.js: How to unlock account with private key

Created on 6 Apr 2018  路  8Comments  路  Source: ChainSafe/web3.js

I need some clarifications let say when deploying contract with node js we have this line

 try {
    web3.personal.unlockAccount(web3.eth.coinbase, password);
} catch(e) {
    console.log(e);
    return;
}

which is unlocking account to make transaction for contract creation .

What if account is created with this way

web3.eth.accounts.create([entropy]);

And I have private key encyrpted somewhere on the system how I shall use it to unlock (or jsut use account) account let say after a week from it's creation ?

Also can u give details on what does entropy do ?

Is it like password in case of personal account ?

Thanks and sorry if questions are similar , there are not much resources to get more detailed info

Most helpful comment

If you have a pre-existing private key and want to use that with web3, this can help:

const privateKey = 'e0f34403.................................29c8c861937';
const account = web3.eth.accounts.privateKeyToAccount('0x' + privateKey);
web3.eth.accounts.wallet.add(account);
web3.eth.defaultAccount = account.address;

All 8 comments

@hrachbkweb

You can create an account (on the client side) from a private key using web3.eth.accounts.privateKeyToAccount('0xyourprivatekey')

The 'entropy' parameter in this case is essentially a random string that is used to further increase the entropy used to generate the private key for security reasons. you can find more info here

@iurimatias Thanks for response.
But I already have created account with .
web3.eth.create()
which gives me all account info as an object.
Am I missing something?

I was in a similar situation. One way to achieve what you want is to sign the transactions yourself using web3 before sending the transaction.

I found this helpful: https://ethereum.stackexchange.com/questions/36358/how-to-properly-create-a-raw-transaction-and-sign-it-using-web3-in-browser

If you have a pre-existing private key and want to use that with web3, this can help:

const privateKey = 'e0f34403.................................29c8c861937';
const account = web3.eth.accounts.privateKeyToAccount('0x' + privateKey);
web3.eth.accounts.wallet.add(account);
web3.eth.defaultAccount = account.address;

@grimsa when I run that I get the following error
vue.esm.js?a026:1741 TypeError: Cannot read property 'fromRed' of null
at Point.getX (short.js?3300:416)
at Point._encode (base.js?ea53:294)
at Point.encode (base.js?ea53:303)
at KeyPair.getPublic (key.js?bb34:67)
at Object.fromPrivate (account.js?0c3d:31)
at Accounts.privateKeyToAccount (index.js?222f:129)
at VueComponent.created (TransactionAddress.vue?f18e:43)
at callHook (vue.esm.js?a026:2921)
at VueComponent.Vue._init (vue.esm.js?a026:4630)
at new VueComponent (vue.esm.js?a026:4798)

@mastashake08 This might be related to JS minification, see here: https://github.com/indutny/bn.js/issues/133
The version of web3 that worked for me was unminified 1.0.0-beta.34, which uses v4.11.6 of bn.js

This issue should be reopened. With version 1.0.0-beta.36 (latest at the moment of writing) there's the same problem as @mastashake08 specified:

TypeError: Cannot read property 'fromRed' of null

If we think that downgrading to web3 1.0.0-beta.34 that @grimsa suggested was suitable solution that allowed for closing this issue, then something is not okay.

Can this be fixed in latest release? Thanks.

cc @nivida

I tried to install web3 today, and got the same error mentioned above. I saw that bn.js is version 4.11.8, according to my package-lock.json. Is there an easy fix?

Was this page helpful?
0 / 5 - 0 ratings