2

In the DAPP I am building I want to let users log in with their wallet of choice. Currently I have two wallets, Metamask and coinbase wallet. The function window.ethereum.enable() injects web3 globally and therefore both the chrome extensions pop up. How can I choose which wallet to pop up on the click of the user and there on do transactions with that wallet only

2 Answers 2

0

You should be able to show a modal with multiple wallets, and then let the user choose. You can check web3Modal as it makes it way easier to do what you want, of course, if you are making a web Dapp.

Also, there is a window.ethereum.isMetamask variable that can differentiate between metamask and other wallets. But this is always in the hands of the wallet your user is using, as a wallet can create that variable is set it to true even if it's not metamsk.

4
  • Yes at the moment I am showing a modal with the two wallet names. Bu the thing is to open both of them the same function window.ethereum.enable() is used. Therefore both the modals pop up. Any solution to that? Jan 16, 2022 at 14:40
  • @Sol_lover i mentioned the window.ethereum.isMetamask variable, that you can use to differentiate between metamask and other wallets Jan 16, 2022 at 15:06
  • According to the documentation window.ethereum.isMetamask returns true when metamask is installed. But my use case here is different. I present two buttons to the user to connect their wallet to the dapp either using metamask or CoinBase. So when the relevant button is clicked how can only that relevant wallet pop up? ethereum.enable() makes both the installed wallets pop up. Jan 16, 2022 at 17:18
  • If both wallets use the same enable() method, then it shouldn't be your issue. you can't take into consideration any wallet, especially if anybody can create a wallet and make to seem like metamask and implement their popup the same way metamask does. Jan 24, 2022 at 10:42
0

EIP-6963 ("Multi Injected Provider Discovery") was created in May 2023 to solve this problem. Both MetaMask and Coinbase Wallet (and most other major wallets) support EIP-6963.

Basically, on page load you should dispatch an eip6963:requestProvider event. Then all wallets that support EIP-6963 will dispatch a eip6963:announceProvider event with their EIP-1193 provider object (equivalent to window.ethereum) as well as some metadata to identify the wallet. So, you can collect each wallet in a separate object:

// Collect all wallets here
const wallets = [];
window.addEventListener('eip6963:announceProvider', e => wallets.push(e.detail));

// Request all EIP-6963 wallets announce themselves
window.dispatchEvent(new Event("eip6963:requestProvider"));

Then you can use this metadata to show a modal with only the wallets the user has installed, and use the provider object corresponding to the wallet selected by the user.

for (const wallet of wallets) {
  console.log({
    name: wallet.info.name,
    provider: wallet.provider // EIP-1193 provider, equivalent to window.ethereum
  });
}

// Console logs:
// {name: 'Coinbase Wallet', provider: i}
// {name: 'MetaMask', provider: Proxy(f)}

Your Answer

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

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