diff --git a/crates/primitives/src/signature/parity.rs b/crates/primitives/src/signature/parity.rs index 20d9a08d98..804118589c 100644 --- a/crates/primitives/src/signature/parity.rs +++ b/crates/primitives/src/signature/parity.rs @@ -106,7 +106,8 @@ impl Parity { match *self { Self::Parity(b) => Self::Parity(!b), Self::NonEip155(b) => Self::NonEip155(!b), - Self::Eip155(v @ 0..=34) => Self::Eip155(if v % 2 == 0 { v - 1 } else { v + 1 }), + Self::Eip155(0) => Self::Eip155(1), + Self::Eip155(v @ 1..=34) => Self::Eip155(if v % 2 == 0 { v - 1 } else { v + 1 }), Self::Eip155(v @ 35..) => Self::Eip155(v ^ 1), } } @@ -239,4 +240,22 @@ mod test { assert_eq!(p.with_chain_id(1), Parity::Eip155(37)); } + + #[test] + fn invert_parity() { + let p = Parity::Eip155(0); + assert_eq!(p.inverted(), Parity::Eip155(1)); + + let p = Parity::Eip155(22); + assert_eq!(p.inverted(), Parity::Eip155(21)); + + let p = Parity::Eip155(58); + assert_eq!(p.inverted(), Parity::Eip155(59)); + + let p = Parity::NonEip155(false); + assert_eq!(p.inverted(), Parity::NonEip155(true)); + + let p = Parity::Parity(true); + assert_eq!(p.inverted(), Parity::Parity(false)); + } }