How to return the address of my current account from Metamask? I have tried with web3.eth.getCoinbase() but this and metamask has two different address.

web3.eth.getCoinbase() returns me the first account from the list of accounts provided by Ganache.

What I need to do to resolve this?



I am following the tutorial from here and 2019 updated Github code is here.

The below code is the problem:

web3.eth.getCoinbase(function(err, account) {
      if (err === null) {
        App.account = account;
        $("#accountAddress").html("Your Account: " + account);

It returns null even if I have address of my account in metamask and yes I have imported an account given by Ganache onto Metamask.


If you switch off the privacy mode, which is under security and privacy in the metamask settings. Then refresh the page, that should display your current account address.

| improve this answer | |
  • Wow. Thank you very much. @HSK – faruk13 Apr 25 '19 at 19:44

To get current account selected in Metamask: web3.eth.accounts[0].

To make above code work with metamask, set coinbase on ganache, and then configure and use custom RPC url (provided by ganache ) in metamask.

| improve this answer | |
  • web3.eth.accounts[0] Isn't that outdated. – faruk13 Apr 18 '19 at 9:14
  • I can get first account from the list of address returned from web3.eth.getAccounts() but that address and the metamask account address are not same which should be same actually, shown in the tutorial. – faruk13 Apr 18 '19 at 9:17
  • web3.eth.getAccounts() will return accounts controlled by ethereum node, whereas metamask itself is a wallet with its own addresses, which are different from addresses created on node. However you could import ethereum node addresses in metamask manually with import account option. And, web3.eth.accounts[0] is not deprecated, Metamask uses web3 version 0.20.X. – kherwa Apr 18 '19 at 9:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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