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

Pram
  • 121
  • 7
  • Please share your code. – user19510 Aug 29 '18 at 13:51
  • Did you find a solution for this? – Vindberg Sep 03 '18 at 09:29
  • @Vindberg i have tried method provided in here (https://ethereum.stackexchange.com/questions/12621/getting-the-wrong-address-back-from-ecrecover/12684#12684) 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 03 '18 at 09: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 03 '18 at 09: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 04 '18 at 14:09
  • Hi Pram, please add your code to the original question. It seems that Metamask doesn't prefix the transaction with "\x19Ethereum Signed Message:\n32"? – Vindberg Sep 10 '18 at 11:16
  • @Vindberg i have added code. – Pram Sep 10 '18 at 11:40
  • I use Web3Js for my automated tests, sorry cant help with the ethereumjs lib. – Vindberg Nov 26 '18 at 14:36

0 Answers0