getSignatureV static method

int getSignatureV(
  1. int v
)

Gets the canonicalized version of the signature recovery id 'v' in Ethereum signatures.

Ethereum signatures include a recovery id 'v' that can be 0 or 1 for Ethereum mainnet or 27 or 28 for testnets. This method ensures that the recovery id is canonicalized to 27 or 28, throwing an exception for invalid values.

Parameters:

  • v: The recovery id value extracted from an Ethereum signature.

Returns:

  • The canonicalized recovery id (27 or 28).

Throws:

Implementation

static int getSignatureV(int v) {
  if (v == 0 || v == 27) {
    return 27;
  }
  if (v == 1 || v == 28) {
    return 28;
  }
  if (v < 35) {
    throw MessageException("Invalid signature recovery id",
        details: {"input": v});
  }
  return (v & 1) != 0 ? 27 : 28;
}