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?

    Is 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)`

    No, just a password used to encrypt the private key (that is later used in transactions).

    Note that the personal API is legacy and no wallet in practice use this. Different wallets provide different providers to sign transactions. More information here: https://ethereum.stackexchange.com/questions/82531/connecting-the-wallet-of-a-user-to-your-dapp-web3-js-project/82532#82532

  • 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).

    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).

License under CC-BY-SA with attribution


Content dated before 7/24/2021 11:53 AM