19

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?

  • Is <pass> the private key? E.g. let account = self.web3.eth.accounts.privateKeyToAccount(self.config.walletPrivateKey); & self.web3.eth.personal.unlockAccount(account.address, account.privateKey, 200) – Andi Giga Nov 29 '17 at 8:58
  • No, just a password used to encrypt the private key (that is later used in transactions). – Sebi Nov 30 '17 at 11:10
10

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.

  • I have a design that may change and do not know yet which choice is best. I'll go with no time unlock for now (this would normally keep the account unlocked indefinitely). – Sebi Jul 9 '16 at 21:28
  • I'd argue that there is a problem in unlocking an account over and over again and that is that the unlocking takes quite some time (~3s for me using latest geth). – Validity Labs - Sebastian Jan 19 '17 at 13:04
8

Here is a workaround to check if account is unlocked. It is not very beautiful but in most cases it works. Use it only for development purposes on testnet or private net!!! Because "this sends out an actual transaction for which you have to pay gas" Unlocking accounts and making transactions in web3.js

function isAccountLocked(account) {
    try {
        web3.eth.sendTransaction({
            from: account,
            to: account,
            value: 0
        });
        return false;
    } catch (err) {
        return (err.message == "authentication needed: password or unlock");
    }
}

function unlockAccountsIfNeeded(accounts, passwords, unlock_duration_sec) {
    if (typeof(unlock_duration_sec)==='undefined') unlock_duration_sec = 300;

    for (let i = 0; i < accounts.length; i++) {
        if (isAccountLocked(accounts[i])) {
            console.log("Account " + accounts[i] + " is locked. Unlocking")
            web3.personal.unlockAccount(accounts[i], passwords[i], unlock_duration_sec);
        }
    }
}
  • 4
    This is indeed not very beautiful because this sends out an actual transaction for which you have to pay gas. – Validity Labs - Sebastian Feb 24 '17 at 14:18
  • 1
    @AlexeyBarsuk nice script, i suggest to add unlock duration (0 unlocked as geth is running) web3.personal.unlockAccount(accounts[i], passwords[i], Duration); – Badr Bellaj Nov 2 '17 at 22:35
4

You can use web3.eth.sign and check if it fails.

async function isUnlocked (web3, address) {
    try {
        await web3.eth.sign("", address);
    } catch (e) {
        return false;
    }
    return true;
}
  • Please give a little more detail in your answer. Providing a link without much context may not help the OP. – Malone Nov 10 '17 at 13:46
  • @Malone web3.eth.sign will throw "authentication needed: password or unlock" in case you are not unlocked – kroe Jul 4 '18 at 4:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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