Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add has_eip155_value convenience function to signature #791

Merged
merged 1 commit into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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