Metamask: Ethers.js and Metamask, how to get the currently selected account?

Getting the Selected Account in Metamask

Metamask is a popular cryptocurrency wallet that allows users to store, send, and receive cryptocurrency. When you connect an account to your website using MetaMask’s eth_requestAccounts() method, it only returns the currently selected Ethereum account. However, when you switch accounts while the first one is still connected to your website, the eth_requestAccounts() method may return an incorrect result.

The Problem

When a user switches accounts in Metamask, the wallet creates a new blockchain session for each connection. As a result, all MetaMask connections are stored on the local machine’s Ethereum node. This means that even if you switch to another account while the first one is still connected, your wallet will only store the information for the newly selected account.

Solution

To work around this issue, you can use the eth_requestAccounts() method in a callback function with an additional argument specifying the current blockchain session ID (SSID). The SSID is generated when you create a new connection to the Ethereum network. You can then compare these SSIDs to determine which account is currently selected.

Sample Code

Here is an example of how to get the currently selected account using eth_requestAccounts() with an additional argument for the current SSID:

async function getCurrentlySelectedAccount() {

const [selectedAccount, accounts] = await MetaMask.eth.request({

method: 'eth_requestAccounts',

params: [],

});

if (accounts.length > 0) {

const sessionId = accounts[0].ssid;

// Use the SSID to determine which account is currently selected

// For example:

const selectedAccountAddress = accounts.find((account) => account.address === selectedAccount).address;

console.log(Currently selected account: ${selectedAccountAddress});

} else {

console.log('No accounts are connected.');

}

}

Using the callback function

To use this approach, you can modify your eth_requestAccounts() call to pass an additional argument specifying the current SSID:

async function getCurrentlySelectedAccount() {

const [selectedAccount, accounts] = await MetaMask.eth.request({

method: 'eth_requestAccounts',

params: [

{

ssid: 'your-ssid-here', // replace with your actual SSID

},

],

});

if (accounts.length > 0) {

const sessionId = accounts[0].ssid;

const selectedAccountAddress = accounts.find((account) => account.address === selectedAccount).address;

console.log(Currently selected account: ${selectedAccountAddress});

} else {

console.log('No accounts are connected.');

}

}

Conclusion

By using the eth_requestAccounts() method in a callback function with an additional argument for the current SSID, you can determine which account is currently selected when switching between multiple connections. This approach ensures accurate information about your Ethereum accounts across all MetaMask connections.

Note: Make sure to replace "your-ssid-here" with your actual SSID value.

Leave a Comment

Your email address will not be published. Required fields are marked *