From d46fd8f9fdf237955e35bb8790918224b7b642b9 Mon Sep 17 00:00:00 2001 From: James Date: Tue, 29 Oct 2024 13:29:57 -0400 Subject: [PATCH] feat: add has_eip155_value convenience function to signature --- crates/primitives/src/signature/parity.rs | 15 ++++++++++++++- crates/primitives/src/signature/sig.rs | 19 +++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/crates/primitives/src/signature/parity.rs b/crates/primitives/src/signature/parity.rs index 1fc44cf35..b16011b0d 100644 --- a/crates/primitives/src/signature/parity.rs +++ b/crates/primitives/src/signature/parity.rs @@ -62,7 +62,10 @@ impl TryFrom for Parity { } impl Parity { - /// Get the chain_id of the V value, if any. + /// Returns the chain ID associated with the V value, if this signature is + /// replay-protected by [EIP-155]. + /// + /// [EIP-155]: https://eips.ethereum.org/EIPS/eip-155 pub const fn chain_id(&self) -> Option { match *self { Self::Eip155(mut v @ 35..) => { @@ -76,6 +79,16 @@ impl Parity { } } + /// Returns true if the signature is replay-protected by [EIP-155]. + /// + /// This is true if the V value is 35 or greater. Values less than 35 are + /// either not replay protected (27/28), or are invalid. + /// + /// [EIP-155]: https://eips.ethereum.org/EIPS/eip-155 + pub const fn has_eip155_value(&self) -> bool { + self.chain_id().is_some() + } + /// Return the y-parity as a boolean. pub const fn y_parity(&self) -> bool { match self { diff --git a/crates/primitives/src/signature/sig.rs b/crates/primitives/src/signature/sig.rs index d2de0ad66..f121c4730 100644 --- a/crates/primitives/src/signature/sig.rs +++ b/crates/primitives/src/signature/sig.rs @@ -301,6 +301,25 @@ impl Signature { self.v } + /// Returns the chain ID associated with the V value, if this signature is + /// replay-protected by [EIP-155]. + /// + /// [EIP-155]: https://eips.ethereum.org/EIPS/eip-155 + pub const fn chain_id(&self) -> Option { + self.v.chain_id() + } + + /// Returns true if the signature is replay-protected by [EIP-155]. + /// + /// This is true if the V value is 35 or greater. Values less than 35 are + /// either not replay protected (27/28), or are invalid. + /// + /// [EIP-155]: https://eips.ethereum.org/EIPS/eip-155 + #[inline] + pub const fn has_eip155_value(&self) -> bool { + self.v().has_eip155_value() + } + /// Returns the byte-array representation of this signature. /// /// The first 32 bytes are the `r` value, the second 32 bytes the `s` value