Skip to content

Commit

Permalink
feat(eip): make 7702 auth recovery fallible (#1082)
Browse files Browse the repository at this point in the history
* ake 7702 auth recovery fallible

* Update auth_list.rs

* Update auth_list.rs

* clippy

* clippy

* Update auth_list.rs

* Update crates/eips/src/eip7702/auth_list.rs

Co-authored-by: Matthias Seitz <matthias.seitz@outlook.de>

* Update auth_list.rs

* Update crates/eips/src/eip7702/auth_list.rs

Co-authored-by: Oliver <onbjerg@users.noreply.github.com>

* Update crates/eips/src/eip7702/auth_list.rs

Co-authored-by: Oliver <onbjerg@users.noreply.github.com>

* chore: add is fns

---------

Co-authored-by: Matthias Seitz <matthias.seitz@outlook.de>
Co-authored-by: Oliver <onbjerg@users.noreply.github.com>
  • Loading branch information
3 people authored Jul 23, 2024
1 parent 67f024a commit 7d67e9c
Showing 1 changed file with 54 additions and 16 deletions.
70 changes: 54 additions & 16 deletions crates/eips/src/eip7702/auth_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,37 @@ 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).
#[derive(Debug, Clone, Hash, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum RecoveredAuthority {
/// 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,
}
}

/// Returns true if the authority is valid.
pub const fn is_valid(&self) -> bool {
matches!(self, Self::Valid(_))
}

/// Returns true if the authority is invalid.
pub const fn is_invalid(&self) -> bool {
matches!(self, Self::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 @@ -158,11 +189,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 into_recovered(self) -> RecoveredAuthorization {
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 @@ -205,35 +237,41 @@ 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> {
self.authority.address()
}

/// 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)
}
}

#[cfg(feature = "k256")]
impl TryFrom<SignedAuthorization> for RecoveredAuthorization {
type Error = alloy_primitives::SignatureError;

fn try_from(value: SignedAuthorization) -> Result<Self, Self::Error> {
value.try_into_recovered()
impl From<SignedAuthorization> for RecoveredAuthority {
fn from(value: SignedAuthorization) -> Self {
value.into_recovered().authority
}
}

#[cfg(feature = "k256")]
impl From<SignedAuthorization> for RecoveredAuthorization {
fn from(value: SignedAuthorization) -> Self {
value.into_recovered()
}
}
impl Deref for RecoveredAuthorization {
type Target = Authorization;

Expand Down

0 comments on commit 7d67e9c

Please sign in to comment.