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
51 changes: 39 additions & 12 deletions crates/eips/src/eip7702/auth_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,27 @@ 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,
}

impl RecoveredAuthority {
/// Returns an optional address if valid.
pub const fn address(&self) -> Option<Address> {
match *self {
Self::Valid(address) => Some(address),
Self::Invalid => None,
}
}
}

/// 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 @@ -158,11 +179,12 @@ impl SignedAuthorization {

/// Recover the authority and transform the signed authorization into a
/// [`RecoveredAuthorization`].
pub fn try_into_recovered(
self,
) -> Result<RecoveredAuthorization, alloy_primitives::SignatureError> {
let authority = self.recover_authority()?;
Ok(RecoveredAuthorization { inner: self.inner, authority })
pub fn try_into_recovered(self) -> RecoveredAuthorization {
DoTheBestToGetTheBest marked this conversation as resolved.
Show resolved Hide resolved
let authority_result = self.recover_authority();
let authority =
authority_result.map_or(RecoveredAuthority::Invalid, RecoveredAuthority::Valid);

RecoveredAuthorization { inner: self.inner, authority }
}
}

Expand Down Expand Up @@ -200,22 +222,27 @@ 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
/// Returns an optional address based on the current state of the authority.
pub const fn authority(&self) -> Option<Address> {
match &self.authority {
RecoveredAuthority::Valid(address) => Some(*address),
RecoveredAuthority::Invalid => None,
}
}

/// 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 All @@ -225,7 +252,7 @@ impl TryFrom<SignedAuthorization> for RecoveredAuthorization {
type Error = alloy_primitives::SignatureError;

fn try_from(value: SignedAuthorization) -> Result<Self, Self::Error> {
value.try_into_recovered()
Ok(value.try_into_recovered())
DoTheBestToGetTheBest marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down