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 mands 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 fix this issue?
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 mands 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 fix this issue?
Share Improve this question edited Aug 19, 2022 at 20:45 TylerH 21.1k77 gold badges79 silver badges112 bronze badges asked Oct 20, 2017 at 17:00 centriqcentriq 211 silver badge4 bronze badges2 Answers
Reset to default 7After messing around with this for a few hours I finally found a working solution.
Instead of using web3.eth.personal.ecRecover use web3.eth.accounts.recover
const Web3 = require('web3');
const web3 = new Web3(new Web3.providers.HttpProvider('http://node.expanse.tech'));
let test = {
address: web3.eth.accounts[0],
msg: "this is a test",
sig: "0x689f9d5718e3a4ecb643d27742c60bde7bee66a596a4593ae00ca853b8c6ea5d1ffb5b72112b053ccdcb31c4640f90bfb47b543e644437c6b23cd5f24f4e83b41b",
version: "2"
}
let key = web3.eth.accounts.recover(test.msg, test.sig);
console.log(key)
You should use web3.personal.sign
instead of web3.eth.sign
https://web3js.readthedocs.io/en/1.0/web3-eth-personal.html#sign