2

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

  • Please share your code. – user19510 Aug 29 '18 at 13:51
  • Did you find a solution for this? – Vindberg Sep 3 '18 at 9:29
  • @Vindberg i have tried method provided in here (ethereum.stackexchange.com/questions/12621/…) and seems to be working in test suit, but i need to check in actual application when using metamask, I will confirm once i have checked with metamask – Pram Sep 3 '18 at 9:46
  • @smarx Please find the code which i had earlier, web3.eth.sign(account, web3.eth.sha3(challenge), (err, signature)=>{}) 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'); if(('0x'+address) === client_address) – Pram Sep 3 '18 at 9:54
  • Now my metamask implementation is broken. I reverted back to my old code and have tried installing different version of web3 ie.0.20.3 same as metamasks web3 version. but still no use, test case not working. – Pram Sep 4 '18 at 14:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.