getSignatureV static method
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:
- MessageException if the input recovery id is invalid or out of range.
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;
}