1

Does any know know how does metamask lock function works? In certain Dapps, you can disconnect the wallet from the Dapp itself. But all they are doing is that changing the account state to empty string.

  const [currentAccount, setCurrentAccount] = useState("");
  const disconnectWallet = () => {
    try {
      setCurrentAccount("");
      window.location.reload(false);
    } catch (error) {
      console.log(error);
    }
  };

Something like this. But In reality, it is just changing the account address to empty so the Dapp could no longer can detect the account.

You can recognize similar behaviour in Uniswap too. First time when you are connecting the Dapp, it will ask password. And next time when you disconnect and connect from Dapp, it will just directly connect because uniswap already present in connected sites.

In my case, I want to know how metamask "lock" works and is it possible to imitate the lock from Dapp.

enter code here

1 Answer 1

1

What MetaMask is doing when you click the Lock button is to locking its keyring controller and resetting its state.

Check this line: https://github.com/MetaMask/metamask-extension/blob/develop/app/scripts/metamask-controller.js#L4331

That line is calling keyringController.setLocked(), which you can see here what it's doing: https://github.com/MetaMask/KeyringController/blob/main/index.js#L146

MetaMask uses the eth-keyring-controller library to manage the user accounts, lock, unlock them with a password, etc. So, when it calls setLocked function of the eth-keyring-controller library, the library resets all the information it has in memory, like the password, keyrings, etc. And MetaMask updates its UI accordingly.

Notice that for this, MetaMask really needs the user's password and seed phrase to be able to unlock and manage the accounts. So, when you are talking about 'imitating' the lock function of MetaMask in your Dapp, are you referring to kind of managing the user's keyring from your Dapp and being able to unlock and lock them? I don't think it will be a good idea to try to manage the user's secrets from a Dapp.

Here is the code to unlock the account with a password: https://github.com/MetaMask/metamask-extension/blob/develop/app/scripts/metamask-controller.js#L2340

When a Dapp connects to MetaMask, MetaMask keeps a mapping of the URL of the site and to which accounts it has access. When you Lock MetaMask, it also resets this mapping and forgets about the sites it was connected to.

1
  • 1
    Thank you. When I mentioned imitation, I was refering to reset the metamask mapping from the Dapp itself.
    – Ad-h0c
    Sep 27, 2022 at 15:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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