The parameters for ecrecover() are:

  • msgHash
  • v
  • r
  • s
  • chainId

The first four are pretty straight forward to me but the last one - chainId.... what's the point of that one?

In looking at the source code I see that if chainId is 0 then 27 is subtracted from the last byte of the signature and if the result isn't 0 or 1 then ecrecover throws an exception. I guess the whole 27 thing is part of what the eth_sign RPC method discussed in https://eth.wiki/json-rpc/API#eth_sign but that doesn't really give me any insight...

Any ideas?


1 Answer 1


The ChainId is used as a simple replay attack protection. After the London fork the chainId is mandatory and must be used to sign a transaction otherwise the nodes will not accept it.

This EIP-155 describes how the chainId is added to the signature and why.

the v of the signature MUST be set to {0,1} + CHAIN_ID * 2 + 35 where {0,1} is the parity of the y value of the curve point for which r is the x-value in the secp256k1 signing process

Your Answer

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

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