1

I know that I can sign a message using web3js, but I don't know how I can do that, then how to verify that signature using ecrecover()?

1 Answer 1

1

You can use metamask to sign transaction see their example

https://docs.metamask.io/guide/signing-data.html#sign-typed-data-v4

Basically the steps are,

const keep_it_hidden = process.env.Key; // this is a private key

const CHAINID = 1;
const hash_data =  web3.utils.soliditySha3(CHAINID, accounts[0] , "some data" ); // any data you want the func to hash
const sign = await web3.eth.accounts.sign(hash_data, keep_it_hidden )['signature'] // sign the hashed data

Now in the smart contract import the Elliptic Curve Digital Signature Algorithm (ECDSA) operations from OpenZeppelin, you have sign as signature you can run using e.g recover method and toEthSignedMessageHash for hash the message in the contract

https://github.com/OpenZeppelin/openzeppelin-contracts/blob/6af4ed6169ee4165ce86457f98c0a568bd9d7491/contracts/utils/cryptography/ECDSA.sol#L99

I hope this is clear

2
  • where I can put a java script code
    – Asma
    Sep 22, 2021 at 6:19
  • where I can put a java script code
    – Asma
    Sep 22, 2021 at 6:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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