Metamask-extension: ethereum.enable(console.log) always pending

Created on 13 Jan 2019  路  7Comments  路  Source: MetaMask/metamask-extension

Describe the bug
ethereum.enable(console.log);

returns promise that never resolves (in Chrom)

To Reproduce
Steps to reproduce the behavior:

  1. Go to https://8k8r0x68ll.codesandbox.io/#/bank
  2. Open Developers Tools
  3. find 8k8r0x68ll.codesandbox.io/src/app/services/api.js? [sm]
  4. Set breakpoint on lines 11 and 13
  5. Refreash

Expected behavior
Window asking permission should pop up

Screenshots
If applicable, add screenshots to help explain your problem.

Browser details (please complete the following information):

  • OS: Windows 8.1
  • Browser Chrome
  • MetaMask Version 5.3

Additional context
Project available under
https://codesandbox.io/s/8k8r0x68ll

problem might be related to usage of parceljs.org in a project

or to the fact that
https://*.codesandbox.io/ runs worker processes that may mess with requests

Also
window.addEventListener("message", function(n) { var o = n.data; o && o.type === t && (r && window.removeEventListener("message", e), e.apply(window, arguments)) })

the only time o.type is defined it has value

"ETHEREUM_ENABLE_PROVIDER"

while function expects
"ethereumprovider"

N02-needsReproduction

Most helpful comment

Having this very same problem. ethereum.enable() randomly hangs and does not resolve.

Using:

Chromium | 70.0.3538.67聽(Official Build)聽Built on Ubuntu , running on LinuxMint 19聽(64-bit)
-- | --
Revision | 9ab0cfab84ded083718d3a4ff830726efd38869f-refs/branch-heads/3538@{#1002}
OS | Linux
JavaScript | V8聽7.0.276.28
Flash | (Disabled)
User Agent | Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/70.0.3538.67 Chrome/70.0.3538.67 Safari/537.36
Command Line | /usr/lib/chromium-browser/chromium-browser --enable-pinch --flag-switches-begin --flag-switches-end
Executable Path | /usr/lib/chromium-browser/chromium-browser
Profile Path | /home/mad/.config/chromium/Default

Sample code is a react handler, called on a "Enable Wallet" button click. console.log(1) is never reached and promise is stuck in pending.

`
handleEnableWallet = () => {

console.log("3");

if (window.ethereum) {
  console.log("4");
  window.web3 = new Web3(window.ethereum);
  try {
    console.log("5");
    console.log(window.ethereum.enable()
      .then((data) => {
        console.log("1");
        const web3 = new Web3Wrapper().getWeb3();
        return web3;
      }, (reason) => {
        console.log("Permission to connect to Metamask was denied");
        NotificationManager.error("Permission to connect to Metamask was denied");
      })
      .then((web3) => {
        console.log("2");
        console.log(web3);
        if(web3)
          web3.eth.getAccounts()
            .then((accountData) => {
              if(accountData && web3.utils.isAddress(accountData[0])){
                this.props.loadWeb3(web3, accountData);
              }
            });
      }));
  } catch (error) {
      NotificationManager.error("App error");
      console.error(error)
  }
}else{
  NotificationManager.error("Need metamask for now");
}

}
`

All 7 comments

That page crashes for me with

Uncaught SyntaxError: Unexpected token {      main:1

How to handle ethereum.enable() thing to expose the metamask accounts. It takes time to resolve the promise and keeps showing errors related to accounts. Tried async/await and other hacks but it only works once (means able to see the contract details on React UI once), after hard refresh all account related info is gone.

@Abhilash04

As I've written I've tried the simplest possible code

ethereum.enable().then(console.log);
If You try it on this page in console (and have Metamask installed and running in privacy mode)
You will see

Promise(......

then window will popup and after confirm

You will see in console array with Your primary address

If You run this code on

https://8k8r0x68ll.codesandbox.io/#/bank
And write the same in console

You will see only
Promise(.....

there will be no popup and as a conseqence no promie resolution

Having this very same problem. ethereum.enable() randomly hangs and does not resolve.

Using:

Chromium | 70.0.3538.67聽(Official Build)聽Built on Ubuntu , running on LinuxMint 19聽(64-bit)
-- | --
Revision | 9ab0cfab84ded083718d3a4ff830726efd38869f-refs/branch-heads/3538@{#1002}
OS | Linux
JavaScript | V8聽7.0.276.28
Flash | (Disabled)
User Agent | Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/70.0.3538.67 Chrome/70.0.3538.67 Safari/537.36
Command Line | /usr/lib/chromium-browser/chromium-browser --enable-pinch --flag-switches-begin --flag-switches-end
Executable Path | /usr/lib/chromium-browser/chromium-browser
Profile Path | /home/mad/.config/chromium/Default

Sample code is a react handler, called on a "Enable Wallet" button click. console.log(1) is never reached and promise is stuck in pending.

`
handleEnableWallet = () => {

console.log("3");

if (window.ethereum) {
  console.log("4");
  window.web3 = new Web3(window.ethereum);
  try {
    console.log("5");
    console.log(window.ethereum.enable()
      .then((data) => {
        console.log("1");
        const web3 = new Web3Wrapper().getWeb3();
        return web3;
      }, (reason) => {
        console.log("Permission to connect to Metamask was denied");
        NotificationManager.error("Permission to connect to Metamask was denied");
      })
      .then((web3) => {
        console.log("2");
        console.log(web3);
        if(web3)
          web3.eth.getAccounts()
            .then((accountData) => {
              if(accountData && web3.utils.isAddress(accountData[0])){
                this.props.loadWeb3(web3, accountData);
              }
            });
      }));
  } catch (error) {
      NotificationManager.error("App error");
      console.error(error)
  }
}else{
  NotificationManager.error("Need metamask for now");
}

}
`

@adamskrodzki
@rrodriguezreche
@Abhilash04

I have tried reproduction steps and it has let me to believe there is an issue with implementation.
@adamskrodzki With your example ethereum is providing the object, but then I am unable to console.log the response. Clone of your repo https://codesandbox.io/s/2xwln3p9r, with a console.log(res)

@Abhilash04 Are you using web3.eth.accounts[0]? If you are it would be better to use web3.eth.getAccounts((err, accounts) => console.log(accounts[0]))

@rrodriguezreche In your snippet that I tried to reproduce, my console.log(1) was reached and data was provided.

All my reproduction steps can be seen here in an example dapp. https://codesandbox.io/s/ovo79jkok5.
In this example it doesn't matter if privacy mode is enabled.

@tmashuang
@adamskrodzki
@rrodriguezreche

Can you help me with this code? Am I doing something wrong here, that is causing the application to crash?

I'm trying to create a singleton web3 instance here.

web3 object creation -> web3.js file code

import Web3 from "web3";
let web3;

if (
  typeof window !== "undefined" &&
  typeof window.web3 !== "undefined" &&
  typeof window.ethereum !== "undefined"
) {
  //We are in the browser and metamask is running.

  // try {
  //   async function resolveEthereum() {
  //     const accounts = await ethereum.enable();
  //     console.log(accounts);
  //   }
  //   resolveEthereum();
  // } catch (error) {
  //   // Handle error. Likely the user rejected the login:
  //   console.log(reason === "User rejected provider access");
  // }

  ethereum
    .enable()
    .then(function(accounts) {
      console.log(accounts);
    })
    .catch(function(reason) {
      // Handle error. Likely the user rejected the login:
      console.log(reason === "User rejected provider access");
    });

  web3 = new Web3(window.ethereum);
} else {
  //We are on the browser OR the user is not running metamask
const provider = new Web3.providers.HttpProvider(
    "https://rinkeby.infura.io/v3/<your infura id>"
  );
  web3 = new Web3(provider);
}

export default web3;

This issue has been inactive for some time, and it looks like there were a variety of distinct problems encountered. I'm going to close it for now, but please feel free to create a new issue if you think there is an outstanding bug.

Please refer to the latest documentation for MetaMask: https://docs.metamask.io/

Was this page helpful?
0 / 5 - 0 ratings