[Ethereum] Unlocking accounts and making transactions in web3.js


This question is related to this one and this one. It also has a partial answer here and here.

In the third link the account is being unlocked for a period of time using web3.js:

web3.personal.unlockAccount("0x..", "<passs>", 1000);

The unlock period parameter is optional.

How is it possible to check if accounts are locked (prior to making transactions)?

Are there any functionality consequences of unlocking an already unlocked account?

Best Answer

There currently isn't a method for checking whether an account is locked or not in the web3 JS API.

However, from the code, if an account is already unlocked, then there's no problem calling unlock again.

// If the account address is already unlocked for a duration, TimedUnlock extends or
// shortens the active unlock timeout. If the address was previously unlocked
// indefinitely the timeout is not altered.
func (am *Manager) TimedUnlock(a Account, passphrase string, timeout time.Duration) error {

Why are we only looking at TimedUnlock(), which presumably only gets invoked if we pass the third parameter?

As you noted, the unlock period parameter is optional. If it isn't provided, then we call Unlock(), which is a wrapper around TimedUnlock(), but which passes 0 as the time period. Hence we only need consider the TimedUnlock() function in the explanation.

Related Topic