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
web3.eth.personal
is undefined, which means thatweb3.eth
doesn't have apersonal
property. My guess is that you should possibly just remove theeth.
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.web3.eth.personal.sign
is well supported in v1.x, so you could not have possibly got an error which impliesweb3.eth.personal == undefined
!