I am trying to write sign a challenge using web3.eth.sign, web3 uses ganache.provider(), But when i do fromRpcSig(signature) ecrecover(challenge) which results in different address. But when i sign using metamask web3 every thing works fine. I am writing test suit for signin using ganache-cli. Thanks in advance
//front end code using metamask injected web3
web3.eth.sign(web3.eth.defaultAccount, web3.sha3(challenge), async (err, signature) => { Here i will send signature to backend })
//Backend code
const eutil = require('ethereumjs-util');
const sig = eutil.fromRpcSig(signature);
const publicKey = eutil.ecrecover(eutil.sha3(challenge), sig.v, sig.r, sig.s);
const address = eutil.pubToAddress(publicKey).toString('hex');
// Test suit
same as front end but using ganache provider
Url for full code
https://github.com/pramodramdas/digital_healthcare
front end code: src->components->login.js->login()
back end code: routes->auth-routes.js
test suit: tests->check_flow.test.js->test case('Check Signin')
test case is not working with current implementation, but metamask implementation works as expected
web3.eth.sign(account, web3.eth.sha3(challenge), (err, signature)=>{})
const eutil = require('ethereumjs-util');
– Pram Sep 03 '18 at 09:54const sig = eutil.fromRpcSig(signature);
const publicKey = eutil.ecrecover(eutil.sha3(challenge), sig.v, sig.r, sig.s);
const address = eutil.pubToAddress(publicKey).toString('hex');
if(('0x'+address) === client_address)