ethereumjs – Understanding the Purpose of chainId Parameter in ethereumjs’s ecrecover Method

ecrecoverethereumjssignature

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?

Best Answer

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

Related Topic