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 helper to eip155 variant #774

Closed
wants to merge 1 commit into from
Closed
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
19 changes: 19 additions & 0 deletions crates/primitives/src/signature/parity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,25 @@ impl Parity {
}
}

/// Applies a check to the [`Parity::Eip155`] v value if this instance is a [`Parity::Eip155`].
///
/// Returns [`Ok`] if the instance is not [`Parity::Eip155`]
pub fn ensure_eip155<F, E>(&self, f: F) -> Result<(), E>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is too specific, doesn't make sense to have as a method, is_ and as_ are enough

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm this would make things like:

            // Check y_parity
            if let Parity::Eip155(parity) = auth.signature().v() {
                if parity > u8::MAX as u64 {
                    return Err(InvalidAuthorization::InvalidYParity);
                }
            }
            Ok(())

slightly easier imo

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the same is achievable with as_eip().is_some_and(), again this is too specific

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

still think this is nice to have and doesn't hurt really, but closing because can be solved with some extra calls as well

where
F: FnOnce(u64) -> Result<(), E>,
{
if let Self::Eip155(v) = self {
f(*v)
} else {
Ok(())
}
}

/// Returns true if this instance is a [`Parity::Eip155`]
pub const fn is_eip155(&self) -> bool {
matches!(self, Self::Eip155(_))
}

/// Return the y-parity as a boolean.
pub const fn y_parity(&self) -> bool {
match self {
Expand Down