Web3.js: How do I use the unlockAccount method?

Created on 16 Aug 2017  Â·  20Comments  Â·  Source: ChainSafe/web3.js

the doc is ....
// TODO
getAccounts, unlockAccount, lockAccount, sendTransaction, ecRecover

Most helpful comment

@Suhail The process is kind of tricky for now as per my knowledge
object = web3.eth.accounts.create() will create an account object but it will not get imported into your geth node.
To do that you need to do web3.eth.personal.importRawKey(privatekey, passphrase)
you will get the privatekey from object.privatekey
Now you can see your account from your geth console.
Note you need to give the exact passphrase at the time of unlocking the account.
object.address will return you the address.
@alexpriceonline I find this way of creating an account. Is this ok ?

All 20 comments

in stackexchange they says web3.personal.unlockAccount("0x..", "", 1000); Please test and report if this works with web3 1.0 beta .18 . I am also facing this issue.
I also asked something like this

I've tested this on web3 1.0-beta 18 and can confirm it works with the following code. The only difference is the call chaining, it's web3.eth.personal instead of web3.personal.

web3.eth.personal.unlockAccount(address, password, 600)
    .then((response) => {
        console.log(response);
    }).catch((error) => {
        console.log(error);
    });

the previous code throws an error for me for "web3": "^1.0.0-beta.20":

Error: Returned error: The method personal_unlockAccount does not exist/is not available
    at Object.ErrorResponse (/Users/serge/repos/learning/learning_npm_web3/node_modules/web3/packages/web3-core-helpers/src/errors.js:29:16)
    at /Users/serge/repos/learning/learning_npm_web3/node_modules/web3/packages/web3-core-requestmanager/src/index.js:137:36
    at XMLHttpRequest.request.onreadystatechange (/Users/serge/repos/learning/learning_npm_web3/node_modules/web3/packages/web3-providers-http/src/index.js:64:13)
    at XMLHttpRequestEventTarget.dispatchEvent (/Users/serge/repos/learning/learning_npm_web3/node_modules/web3/packages/web3-providers-http/node_modules/xhr2/lib/xhr2.js:64:18)

@serge-nikitin

Make sure "personal" is enabled in the RPC/IPC API options when starting geth.

https://github.com/ethereum/go-ethereum/wiki/Command-Line-Options

Thanks, I have to solve it

web3.eth.personal.newAccount only returns true or false then how can I get the address of the created account using web3?

@sidharthaa https://github.com/ethereum/web3.js/pull/1157 is going to resolve that.

When you manually create an account, I get: "Error: Returned error: Error: could not unlock signer account"

How do you do this if you did web3.eth.accounts.create()? You get a account object back but you're not setting a password when you call that function.

@Suhail The process is kind of tricky for now as per my knowledge
object = web3.eth.accounts.create() will create an account object but it will not get imported into your geth node.
To do that you need to do web3.eth.personal.importRawKey(privatekey, passphrase)
you will get the privatekey from object.privatekey
Now you can see your account from your geth console.
Note you need to give the exact passphrase at the time of unlocking the account.
object.address will return you the address.
@alexpriceonline I find this way of creating an account. Is this ok ?

Hi all, I have a similar situation with creating new accounts and unlocking them.
Over IPC i call web3.personal.newAccount('qwerty123'), after it i have an address, private key and new file in keystore directory of geth so i suppose that it already have been imported to node.
After it i call method web3.personal.unlockAccount('0x602039bb4b8ec6bf9c49716f640edb9a47383483', 'qwerty123') and get error: Error: could not decrypt key with given passphrase What i can do wrong?
unlock.txt
create_address.txt

P.S. All this through nodejs
P.P.S. I am newbie in blockchain/ thanks.

While starting the ethereum blockchain, make sure "personal" is enabled in the RPC/IPC API options

geth --datadir . console --rpc --rpcapi web3, personal,eth,network,db

HrishikeshFC Thanks for reply, i switch to rpc and php, and work well for now. Thanks again.

web3.eth.personal.newAccount('!@superpassword')
.then(console.log);

this is hard to believe that create not working... hard!

@jbarros35 web3.eth.accounts.create() is working fine with that web3.eth.personal.importRawKey(privatekey, passphrase) method
And
web3.eth.personal.newAccount('!@superpassword').then((response) => {console.log(response)})
also working with no issue

Hi, I'm using web3 beta 33. Now I think its fine the issue is when we at first time install web3 it won't intall the beta.. they install 0.2.2

@VikkiUser You said "Thanks for reply, i switch to rpc and php, and work well for now. Thanks again." Can you please tell me how you handled unlocking and re-locking accounts using RPC?

Uncaught TypeError: Cannot read property 'unlockAccount' of undefined
When I remove eth part in chain - I get:
inpage.js:1 Uncaught Error: The MetaMask Web3 object does not support synchronous methods like personal_unlockAccount without a callback parameter. See <link to web3.js documentation>
And of course the suggested link is totally useless.

Where can I see the full example with login into MetaMask? All the tutorials contain random chunks of code which look like normal but become a hell of nested code together.
Any description about how to use it with promises?

Hi @stone212
$this->rpc->request('personal_unlockAccount', [$from, $psf])
$from - eth address
$psf - pass phrase

$this->rpc->request('personal_newAccount', [$psf])
$psf - pass phrase
All this using geth - latest version and CURL.
Hope this helps.

This demo is about the use of the unlockAccount method. The documentation is in Chinese, but the code is easy to understand。
https://github.com/WenHou/eth-web3-server

Still confused by this..
In web3j

NewAccountIdentifier ac = node.personalNewAccount("secret").send();
String address = ac.getAccountId();

This works and a new account[6] is created on the node having address
However in java script console

personal.unlockAccount(eth.accounts[6],"secret",1000)
Error: could not decrypt key with given passphrase

Second connected issue when trying to transaction from created account.
webj3

Transfer.sendFunds( .... Credentials ..  ) requires credentials object
Credentials.create("< NEEDS PRIVATE KEY ??? ");

What is the relationship between personalNewAccount("secret") and privateKey as they appear to not be the same thing ? or how to generate privateKey from "secret" after account has already been created using the API with secret. once NewAccountIdentifier ac is not longer accessible ?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

zamoore picture zamoore  Â·  3Comments

mishell-trickster picture mishell-trickster  Â·  3Comments

webersson picture webersson  Â·  3Comments

baxy picture baxy  Â·  3Comments

oktapodia picture oktapodia  Â·  3Comments