Javascript side:

id = 128
uri = "xxx://xxx"
hash = web3.utils.soliditySha3(id,uri)
sign = web3.eth.accounts.sign(hash1, privateKey)

Solidity side:

function(uint256 id, string memory uri,bytes v, bytes32 r, bytes32 s){
  digest = keccak256(abi.encodePacked(id, uri));
  signer = ecrecover(digest, v, r, s);

signer always mismatch with the prviateKey i use to sign the message. What's the correct way to work with signature using soliditySha3?


You don’t need to use solidity for that you can use directly web3 like: web3.eth.accounts.recover(hash, v, r, s); (to get the signer address).

See https://web3js.readthedocs.io/en/v1.2.0/web3-eth-accounts.html?highlight=recover#recover

But if you still want to use solidity you need to deploy the contract with the ecrecover function and call it with web3.

To extract v, r, s form the signed message you can do this:

const Transaction = require('ethereumjs-tx')
const tx = web3.eth.getTransaction(txHash) // to get tx.v tx.r tx.s

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.