[Ethereum] web3.personal.ecRecover doesn’t work

web3js

I'm trying to get web3.personal.ecRecover to work.
This is my setup:

  • Chrome using Remix IDE

  • MetaMask to inject web3.js

Now I'm trying the following commands in the Chrome Dev Console:

msg = web3.sha3('Schoolbus')

web3.eth.sign(web3.eth.accounts[0], msg , function(error, result){
     if(!error)
         console.log(result)
    else
        console.error(error);
})

this returns:

0xd030d9a04df643f62a1502b017f51c41a659268091abbd20e2de97b935724d7c

Now I set:

 signature = "0x36f32cbd6133ce6be7efa4cb73ff3f9ddf9b7db3ba15fa543ab0a93d04a96c102693739f946f2d89eca9030b4c8e01bb6fada1c23f05b6a4956dd63deaf187501b"

and try:

web3.personal.ecRecover(msg,signature, function(error, result){
    if(!error)
        console.log(result)
    else
        console.error(error);
}) 

and get:

0xcc3f70c6caa9fee58bab68f292bdf3132c3c9ae2

Obviously this is not the public address I used in (i.e web3.eth.accounts[0])

Any suggestions to improve this code?
Thanks!

S

Best Answer

This is working variant of code which return signer of the transaction

Script to sign your message :

let msg = web3.sha3('Schoolbus');
let signMsg;
web3.personal.sign(msg, web3.eth.accounts[0], function(error, result) {
   if(!error) {
      signMsg = result;
       console.log(result);
   } else {
       console.error(error);
   }
})

Script to get the signer

web3.personal.ecRecover(msg, signMsg, function(error, result) {
    if(!error) {
        console.log(result);
    }
    else {
        console.log("Error");
    }
 })