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(eip): make 7702 auth recovery fallible #1082

Merged
28 changes: 22 additions & 6 deletions crates/eips/src/eip7702/auth_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,17 @@ use alloy_rlp::{
};
use core::hash::{Hash, Hasher};

/// Represents the outcome of an attempt to recover the authority from an authorization.
/// It can either be valid (containing an Address) or invalid (indicating recovery failure).
DoTheBestToGetTheBest marked this conversation as resolved.
Show resolved Hide resolved
#[derive(Debug, Clone, Hash, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum RecoveredAuthority {
DoTheBestToGetTheBest marked this conversation as resolved.
Show resolved Hide resolved
/// Indicates a successfully recovered authority address.
Valid(Address),
/// Indicates a failed recovery attempt where no valid address could be recovered.
Invalid,
}

/// An unsigned EIP-7702 authorization.
#[derive(Debug, Clone, Hash, RlpEncodable, RlpDecodable, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
Expand Down Expand Up @@ -162,7 +173,10 @@ impl SignedAuthorization {
self,
) -> Result<RecoveredAuthorization, alloy_primitives::SignatureError> {
DoTheBestToGetTheBest marked this conversation as resolved.
Show resolved Hide resolved
let authority = self.recover_authority()?;
Ok(RecoveredAuthorization { inner: self.inner, authority })
Ok(RecoveredAuthorization {
inner: self.inner,
authority: RecoveredAuthority::Valid(authority),
})
}
}

Expand Down Expand Up @@ -200,22 +214,24 @@ impl<'a> arbitrary::Arbitrary<'a> for SignedAuthorization {
pub struct RecoveredAuthorization {
#[cfg_attr(feature = "serde", serde(flatten))]
inner: Authorization,
authority: Address,
/// The result of the authority recovery process, which can either be a valid address or
/// indicate a failure.
authority: RecoveredAuthority,
}

impl RecoveredAuthorization {
/// Instantiate without performing recovery. This should be used carefully.
pub const fn new_unchecked(inner: Authorization, authority: Address) -> Self {
pub const fn new_unchecked(inner: Authorization, authority: RecoveredAuthority) -> Self {
Self { inner, authority }
}

/// Get the `authority` for the authorization.
pub const fn authority(&self) -> Address {
self.authority
pub fn authority(&self) -> RecoveredAuthority {
self.authority.clone()
}
DoTheBestToGetTheBest marked this conversation as resolved.
Show resolved Hide resolved

/// Splits the authorization into parts.
pub const fn into_parts(self) -> (Authorization, Address) {
pub const fn into_parts(self) -> (Authorization, RecoveredAuthority) {
(self.inner, self.authority)
}
}
Expand Down