3

How do you specify connection with Coinbase Wallet and Metamask separately?

Right now when using window.ethereum.enable() both the Metamask and Coinbase Wallet extensions popup. I would like two separate buttons, one for Metamask and the other for Coinbase Wallet.

My code:

const { createAlchemyWeb3 } = require("@alch/alchemy-web3");
const web3 = createAlchemyWeb3(alchemyKey);

export const connectWallet = async () => {
    if (window.ethereum) { //check if Metamask is installed 
      try {
            const address = await window.ethereum.enable(); //connect Metamask
      }
   }
})
cormacncheese
  • 1,251
  • 1
  • 13
  • 27

2 Answers2

4

From this discussion, it seems like you may be able to use a different Metamask request method to force connection selection:

  const connectWallet = async () => {
    if (window.ethereum) { //check if Metamask is installed 
      try {
        const address = await window.ethereum.request({
          method: 'wallet_requestPermissions',
          params: [{ eth_accounts: {}}]
        }); 
      } catch (error) {
        console.log(error);
      }
    }
  }

The wallet_requestPermissions seems to do the trick.

thatguyintech
  • 501
  • 3
  • 14
0

What worked for me was finding MetaMask provider and setting it as window.ethereum.selectedProvider - the solution taken from this answer.

const {ethereum} = window;
const metamaskProvider = ethereum?.providers?.find(p => p.isMetaMask);

if (metamaskProvider && typeof ethereum.setSelectedProvider === 'function') {
  ethereum.setSelectedProvider(metamaskProvider);
}

Setting ethereum.selectedProvider = metamaskProvider directly doesn't work properly.

Note that providers and selectedProvider are there only with both Metamask and Coinbase Wallet installed, with either of them only installed it's different.

Dmitry Yudakov
  • 15,364
  • 4
  • 49
  • 53