0

I am doing a project with inserted Metamask to achieve on-click blockchain account login. When I am calling the signature function
web3.eth.sign("Hello world", "0x11f4d0A3c12e86B4b5F39B213F7E19D048276DAe") .then(console.log);, the browser reports the error telling me "Invalid Address".

I have checked the function reference on web3.eth website, pretty sure the format is correct. However, when I change to use "web3.eth.personal.sign()", the browser also reports error. This time I just used the sample code from web3.eth website like thisweb3.eth.personal.sign("Hello world", "0x11f4d0A3c12e86B4b5F39B213F7E19D048276DAe", "test password!") .then(console.log);

However, the browser still reports error says"Uncaught TypeError Cannot read property 'sign' of undefined."

Here are my full codes, I am struggling in solving this problem. Hope you guys can help me.

<!DOCTYPE html>

function send(){
        if(!window.web3){
            alert("Please install Metamask!");
        }//end if 
        //else{alert("You have installed Metamask");}

        if (!web3) {//5 check whether metamask is connected online
                web3 = new Web3(window.web3.currentProvider);
                const publicAddress = web3.eth.coinbase.toLowerCase();
                this.setState({ loading: true });
            }//else {
                     // set the provider you want from Web3.providers
              //  web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
                //  }//end else
       //ask for authorization
              if(window.ethereum){
                  try{
                      if(typeof window.ethereum.selectedAddress === 'undefined'){
                          window.ethereum.enable();
                          console.log('enable');
                          window.web3 = new web3(window.ethereum);
                      }else{//end if
                          console.log('window.ethereum.selectedAddress');
                          window.web3= new web3(window.ethereum);
                      }//end else
                  } catch (error){
                      console.log("The request is rejected");
                  }//end catch
                 }else{//end outer if
                    console.log("Please Use Metamask");
                 }
                    //get account 
                  if(typeof web3.eth.coinbase !== 'undefined'){
                alert(web3.eth.coinbase);
              }
              // sign the address
              var address = web3.eth.coinbase;
              web3.eth.personal.sign("Hello world", "0x11f4d0A3c12e86B4b5F39B213F7E19D048276DAe", "test password!").then(console.log);
           // alert(sign);
            }//end define function

  • 1
    Well, the error should immediately tell you that web3.eth.personal is undefined, which means that web3.eth doesn't have a personal property. My guess is that you should possibly just remove the eth. part, but since there have been API-breaking changes between web3.js v0 and web3.js v1, it really depends on the version that you're using, which you haven't bothered to specify. – goodvibration Aug 7 at 12:22
  • I am using web3.js 1.0 – Max Aug 7 at 13:18
  • That's what you think. But your browser is using a different version, probably 0.2.x (just an educated guess). Function web3.eth.personal.sign is well supported in v1.x, so you could not have possibly got an error which implies web3.eth.personal == undefined! – goodvibration Aug 7 at 13:30
  • Thank you, I have checked the version is 0.20.7. Are there any ways to solve this problem? I am just a beginner, may need your help incerely. – Max Aug 7 at 14:37
  • I've seen several similar questions on this website in the past. Look for "import web3 on browser" or something similar. It seems to be an issue that many have already experienced. – goodvibration Aug 7 at 16:43
0

You are using the web3.js version 1.0 API, but the injected web3 version is 0.2x.

If your project is an npm project you can use web3js v1.0 by installing it with npm install web3. source

You can get web3.js v 1.0 directly in an HTML file from a CDN here and then set web3 to use the given provider.

This should give you access to web3.eth.sign

  • Wow, thanks for your advice. However, I have tried your methods inserting the web3.js 1.0 in HTML file, but the browser still tells errors. They are "Uncaught Error: Invalid JSON RPC response: undefined" and " Uncaught TypeError: e is not a function." It seems that my codes conflict the inserted codes some where,,, – Max Aug 8 at 6:08

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.