Skip to content

Commit

Permalink
feat: add has_eip155_value convenience function to signature (#791)
Browse files Browse the repository at this point in the history
  • Loading branch information
prestwich authored Oct 30, 2024
1 parent 0cc2710 commit 2f75f2c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
15 changes: 14 additions & 1 deletion crates/primitives/src/signature/parity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@ impl TryFrom<u64> 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<ChainId> {
match *self {
Self::Eip155(mut v @ 35..) => {
Expand All @@ -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 {
Expand Down
19 changes: 19 additions & 0 deletions crates/primitives/src/signature/sig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u64> {
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
Expand Down

0 comments on commit 2f75f2c

Please sign in to comment.