1

I'm trying to get the current wallet address of MetaMask on my Firefox 67 but I keep getting nothing (empty array), except it says my MetMask is locked. But on Chrome 74 it works. FYI, my MetaMask is NOT locked at all.

    window.addEventListener('load', function() {
    // Check if Web3 has been injected by the browser:
    if (typeof web3 !== 'undefined') {
        // You have a web3 browser! Continue below!
        web3 = new Web3(web3.currentProvider);

        var version = web3.version;

        //Get wallet info in the form of Javascript object
        var account = web3.eth.accounts;

        //Get the current MetaMask selected/active wallet
        walletAddress = account.givenProvider.selectedAddress;

        //Check if Metamask is locked
        if (!empty(walletAddress)){

            //Detect if the user changes the account on MetaMask
            window.ethereum.on('accountsChanged', function (accounts) {
                console.log("MetaMask account change. Reloading...");
                window.location.reload(); 
            })

            //If not locked, continue to run the app
            startApp(web3);

          } else {

            //If locked, alert user to unlock their MetaMask
            if (alert("<?php _e('Your MetaMask is locked. Please unlock with your password and then press OK below.', 'dc-woocommerce-multi-vendor'); ?>")){
            } else {
                window.location.reload(); 
            }
          }

    } else {
        if (alert("<?php _e('No MetaMask plugin detected. Please install MetaMask digital wallet at www.metamask.io', 'dc-woocommerce-multi-vendor'); ?>")){
        } else {
            window.location.reload(); 
        }
    }
});

I also tried the two options below but failed.

    var accounts = web3.eth.getAccounts(function(error, result) {
        if (error) {
            console.log(error);
        } else {
            console.log(result);
        }       
    });

var account = web3.eth.accounts[0];
        var accountInterval = setInterval(function() {
          if (web3.eth.accounts[0] !== account) {
            account = web3.eth.accounts[0];
            window.location.reload();
          }
        }, 100);
1
  • 1
    Check the privacy and security setting of metmask
    – Majd TL
    Jun 25, 2019 at 6:48

3 Answers 3

2

Ok, after a few tries by turning of Privacy Mode, it worked. I guess I wasn't aware of this Privacy Mode. It was turned off in my Chrome by default but on by default in my FireFox MetaMask 6.6.2. Read this, https://medium.com/metamask/https-medium-com-metamask-breaking-change-injecting-web3-7722797916a8

0

You can try with web3.eth.defaultAccount and it works great with multiple addresses on both chrome and firefox.

0
0

@warnerque

Exactly, now to access the accounts you need to ask the user for permission in the web browser.

For all future readers you can do a simple block of the code to access the account here:

const ethereum = window.ethereum;
const web3 = window.web3;
const web3Instance = new Web3(ethereum);
const enabledWeb3 = await ethereum.enable();
const account = await web3Instance.eth.getAccounts();
const accountAddress = await account[0];

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.