Error message
Invalid yParityOrV value
Also printed as
- Error: Invalid yParityOrV value
Behaviour differs between libraries: ethers 6 normalises EIP-155 style values (35 and above) and recovers the same signature without complaint (shown in the reproduction); for other unexpected values such as 5 it throws "invalid v". Code that moves from ethers to viem can therefore start failing on signatures that used to verify.
viem (TypeScript) — recoverAddress / recoverMessageAddress / recoverPublicKey
What it means
The last byte of the signature (the recovery identifier) is not one of the values viem accepts: 0 or 1 (`yParity`) or 27 or 28 (legacy `v`). Message and typed-data signatures always use one of those; other values come from transaction-signing conventions or from byte-order mistakes. viem stops before recovery.
Why it happens
EIP-155 transaction v used for a message
commonLegacy transactions encode the chain in v (`chainId * 2 + 35 + yParity`, e.g. 37/38 on mainnet). Code that reuses transaction-signing helpers for messages, or reads v from a transaction, produces values viem rejects.
Hardware wallet or library returns v differently
occasionalSome signer integrations return the recovery bit as 0/1 in a separate field, or add 27 twice. The concatenated signature then ends with an unexpected byte.
Components concatenated in the wrong order
occasionalBuilding the signature as v‖r‖s (as some APIs return it) instead of r‖s‖v puts a random byte of s in the v position.
How to fix it
- 1.
Normalise v before recovering
Map EIP-155 values and 0/1 to 27/28 (or to yParity 0/1), and make sure the signature is assembled as r‖s‖v.
ts import { hexToNumber, numberToHex, type Hex } from "viem"; // Message signatures carry v = 27/28 (or 0/1); map EIP-155 style values back. function normaliseV(signature: Hex): Hex { let v = hexToNumber(`0x${signature.slice(130)}`); if (v >= 35) v = ((v - 35) % 2) + 27; // EIP-155: chainId * 2 + 35 + yParity if (v === 0 || v === 1) v += 27; return `${signature.slice(0, 130)}${numberToHex(v).slice(2)}` as Hex; }