1

I am new to front end Web3 and I am trying to have a simple SIGN button that will invoke metamask to sign a "Message". I can't seem to get it working. I am using pure HTML/JS without any frameworks such as React etc.

This code:

function sign(){
    window.web3 = new Web3(web3.currentProvider);
    console.log(web3.version);
    web3.eth.sign(("dinosaur"), owner, console.log);
}

returns:

Error: Returned error: cannot sign data; no private key

This other attempt:

function sign(){

    web3.eth.personal.sign('message to sign', owner)
    .then(signature => {
        // whatever
    });
}

returns:

Uncaught (in promise) Error: Returned error: Method personal_sign not supported.

I am using localhost ganache and metamask in Chrome connecting to ganache and initiating web3 using:

web3 = new Web3(new Web3.providers.HttpProvider(provider));

Any ideas?

5
  • Please follow MetaMask instructionsn how to set up a web3 object. Jan 8 at 12:49
  • You reply isn't useful. It doesn't contain any links or instructions Jan 8 at 19:31
  • It is not a reply, it is a comment. MetaMask has a tutorial how to use it and your code looks nothing like it should be. Jan 9 at 9:14
  • Alright I went there and the first line is: if (typeof window.ethereum !== 'undefined') { console.log('MetaMask is installed!'); } This one is turning out false for me. Even though my metamask is installed and I use it daily. Any ideas? Jan 9 at 10:19
  • Fixed my issue will answer below Jan 9 at 10:25
0

The issue I had is that I was serving the html and js files using the file:// in chrome. Metamask doesn't support as per here that so I had to install the HTTP-server using these steps:

1> Have Node.js installed in your system.

2> In CMD, run the command npm install http-server -g

3> Navigate to the specific path of your file folder in CMD and run the command http-server

4> Go to your browser and type localhost:8080. Your Application should run there.

After this Metamask was coming up

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.