From 11e50227c6976fce4800107184363497dd073895 Mon Sep 17 00:00:00 2001 From: tesseract <146037313+DoTheBestToGetTheBest@users.noreply.github.com> Date: Fri, 19 Jul 2024 06:00:05 -0700 Subject: [PATCH 01/11] ake 7702 auth recovery fallible --- crates/eips/src/eip7702/auth_list.rs | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/crates/eips/src/eip7702/auth_list.rs b/crates/eips/src/eip7702/auth_list.rs index d7ef4142639..75c7bf3d3dd 100644 --- a/crates/eips/src/eip7702/auth_list.rs +++ b/crates/eips/src/eip7702/auth_list.rs @@ -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). +#[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, +} + /// An unsigned EIP-7702 authorization. #[derive(Debug, Clone, Hash, RlpEncodable, RlpDecodable, Eq, PartialEq)] #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] @@ -200,22 +211,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() } /// 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) } } @@ -307,7 +320,6 @@ impl Deref for OptionalNonce { mod tests { use super::*; use alloy_primitives::{hex, Signature}; - use arbitrary::Arbitrary; use core::str::FromStr; fn test_encode_decode_roundtrip(auth: Authorization) { From dd4b211890047457910e858ed42d6eef14f26d33 Mon Sep 17 00:00:00 2001 From: tesseract <146037313+DoTheBestToGetTheBest@users.noreply.github.com> Date: Fri, 19 Jul 2024 06:08:38 -0700 Subject: [PATCH 02/11] Update auth_list.rs --- crates/eips/src/eip7702/auth_list.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/crates/eips/src/eip7702/auth_list.rs b/crates/eips/src/eip7702/auth_list.rs index 75c7bf3d3dd..dcc505df98c 100644 --- a/crates/eips/src/eip7702/auth_list.rs +++ b/crates/eips/src/eip7702/auth_list.rs @@ -173,7 +173,10 @@ impl SignedAuthorization { self, ) -> Result { let authority = self.recover_authority()?; - Ok(RecoveredAuthorization { inner: self.inner, authority }) + Ok(RecoveredAuthorization { + inner: self.inner, + authority: RecoveredAuthority::Valid(authority), + }) } } @@ -320,6 +323,7 @@ impl Deref for OptionalNonce { mod tests { use super::*; use alloy_primitives::{hex, Signature}; + use arbitrary::Arbitrary; use core::str::FromStr; fn test_encode_decode_roundtrip(auth: Authorization) { From 581336ed4d54fbfba7da12bd5b1def9f67d8e2b2 Mon Sep 17 00:00:00 2001 From: tesseract <146037313+DoTheBestToGetTheBest@users.noreply.github.com> Date: Sat, 20 Jul 2024 07:11:51 -0700 Subject: [PATCH 03/11] Update auth_list.rs --- crates/eips/src/eip7702/auth_list.rs | 40 +++++++++++++++++++--------- 1 file changed, 28 insertions(+), 12 deletions(-) diff --git a/crates/eips/src/eip7702/auth_list.rs b/crates/eips/src/eip7702/auth_list.rs index dcc505df98c..ebf4bb0b754 100644 --- a/crates/eips/src/eip7702/auth_list.rs +++ b/crates/eips/src/eip7702/auth_list.rs @@ -20,6 +20,18 @@ pub enum RecoveredAuthority { Invalid, } +impl RecoveredAuthority { + /// Returns the contained `Address` as an `Option`. + /// - Returns `Some(Address)` if the recovery was successful. + /// - Returns `None` if the recovery failed. + pub fn address(&self) -> Option
{ + match self { + RecoveredAuthority::Valid(address) => Some(*address), + RecoveredAuthority::Invalid => None, + } + } +} + /// An unsigned EIP-7702 authorization. #[derive(Debug, Clone, Hash, RlpEncodable, RlpDecodable, Eq, PartialEq)] #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] @@ -169,14 +181,15 @@ impl SignedAuthorization { /// Recover the authority and transform the signed authorization into a /// [`RecoveredAuthorization`]. - pub fn try_into_recovered( - self, - ) -> Result { - let authority = self.recover_authority()?; - Ok(RecoveredAuthorization { - inner: self.inner, - authority: RecoveredAuthority::Valid(authority), - }) + pub fn try_into_recovered(self) -> RecoveredAuthorization { + let authority_result = self.recover_authority(); + + let authority = match authority_result { + Ok(addr) => RecoveredAuthority::Valid(addr), + Err(_) => RecoveredAuthority::Invalid, + }; + + RecoveredAuthorization { inner: self.inner, authority } } } @@ -225,9 +238,12 @@ impl RecoveredAuthorization { Self { inner, authority } } - /// Get the `authority` for the authorization. - pub fn authority(&self) -> RecoveredAuthority { - self.authority.clone() + /// Returns an optional address based on the current state of the authority. + pub fn authority(&self) -> Option
{ + match &self.authority { + RecoveredAuthority::Valid(address) => Some(address.clone()), + RecoveredAuthority::Invalid => None, + } } /// Splits the authorization into parts. @@ -241,7 +257,7 @@ impl TryFrom for RecoveredAuthorization { type Error = alloy_primitives::SignatureError; fn try_from(value: SignedAuthorization) -> Result { - value.try_into_recovered() + Ok(value.try_into_recovered()) } } From 5d65c27bdb9f24aca549c09829b96454d7b5c465 Mon Sep 17 00:00:00 2001 From: tesseract <146037313+DoTheBestToGetTheBest@users.noreply.github.com> Date: Sat, 20 Jul 2024 07:16:30 -0700 Subject: [PATCH 04/11] clippy --- crates/eips/src/eip7702/auth_list.rs | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/crates/eips/src/eip7702/auth_list.rs b/crates/eips/src/eip7702/auth_list.rs index ebf4bb0b754..94dd1ccd545 100644 --- a/crates/eips/src/eip7702/auth_list.rs +++ b/crates/eips/src/eip7702/auth_list.rs @@ -21,13 +21,11 @@ pub enum RecoveredAuthority { } impl RecoveredAuthority { - /// Returns the contained `Address` as an `Option`. - /// - Returns `Some(Address)` if the recovery was successful. - /// - Returns `None` if the recovery failed. - pub fn address(&self) -> Option
{ - match self { - RecoveredAuthority::Valid(address) => Some(*address), - RecoveredAuthority::Invalid => None, + /// Returns an optional address if valid. + pub const fn address(&self) -> Option
{ + match *self { + Self::Valid(address) => Some(address), + Self::Invalid => None, } } } @@ -183,11 +181,8 @@ impl SignedAuthorization { /// [`RecoveredAuthorization`]. pub fn try_into_recovered(self) -> RecoveredAuthorization { let authority_result = self.recover_authority(); - - let authority = match authority_result { - Ok(addr) => RecoveredAuthority::Valid(addr), - Err(_) => RecoveredAuthority::Invalid, - }; + let authority = + authority_result.map_or(RecoveredAuthority::Invalid, RecoveredAuthority::Valid); RecoveredAuthorization { inner: self.inner, authority } } From 92c2fdb4206568f014ff46488e214c4ded4d5b5f Mon Sep 17 00:00:00 2001 From: tesseract <146037313+DoTheBestToGetTheBest@users.noreply.github.com> Date: Sat, 20 Jul 2024 07:19:41 -0700 Subject: [PATCH 05/11] clippy --- crates/eips/src/eip7702/auth_list.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/eips/src/eip7702/auth_list.rs b/crates/eips/src/eip7702/auth_list.rs index 94dd1ccd545..3e35e0fbd2a 100644 --- a/crates/eips/src/eip7702/auth_list.rs +++ b/crates/eips/src/eip7702/auth_list.rs @@ -236,7 +236,7 @@ impl RecoveredAuthorization { /// Returns an optional address based on the current state of the authority. pub fn authority(&self) -> Option
{ match &self.authority { - RecoveredAuthority::Valid(address) => Some(address.clone()), + RecoveredAuthority::Valid(address) => Some(*address), RecoveredAuthority::Invalid => None, } } From 1be964f9d84fffd04268abdc449f3efd43805885 Mon Sep 17 00:00:00 2001 From: tesseract <146037313+DoTheBestToGetTheBest@users.noreply.github.com> Date: Sat, 20 Jul 2024 07:23:49 -0700 Subject: [PATCH 06/11] Update auth_list.rs --- crates/eips/src/eip7702/auth_list.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/eips/src/eip7702/auth_list.rs b/crates/eips/src/eip7702/auth_list.rs index 3e35e0fbd2a..570206fe6ac 100644 --- a/crates/eips/src/eip7702/auth_list.rs +++ b/crates/eips/src/eip7702/auth_list.rs @@ -234,7 +234,7 @@ impl RecoveredAuthorization { } /// Returns an optional address based on the current state of the authority. - pub fn authority(&self) -> Option
{ + pub const fn authority(&self) -> Option
{ match &self.authority { RecoveredAuthority::Valid(address) => Some(*address), RecoveredAuthority::Invalid => None, From 8a0fa03cbbabb49660ec736ec8b411565951673e Mon Sep 17 00:00:00 2001 From: tesseract <146037313+DoTheBestToGetTheBest@users.noreply.github.com> Date: Sun, 21 Jul 2024 11:18:17 -0700 Subject: [PATCH 07/11] Update crates/eips/src/eip7702/auth_list.rs Co-authored-by: Matthias Seitz --- crates/eips/src/eip7702/auth_list.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/eips/src/eip7702/auth_list.rs b/crates/eips/src/eip7702/auth_list.rs index 570206fe6ac..68d188e10cb 100644 --- a/crates/eips/src/eip7702/auth_list.rs +++ b/crates/eips/src/eip7702/auth_list.rs @@ -179,7 +179,7 @@ impl SignedAuthorization { /// Recover the authority and transform the signed authorization into a /// [`RecoveredAuthorization`]. - pub fn try_into_recovered(self) -> RecoveredAuthorization { + pub fn into_recovered(self) -> RecoveredAuthorization { let authority_result = self.recover_authority(); let authority = authority_result.map_or(RecoveredAuthority::Invalid, RecoveredAuthority::Valid); From cb2e88de523ebff2eed1986ac46836023f0534ac Mon Sep 17 00:00:00 2001 From: tesseract <146037313+DoTheBestToGetTheBest@users.noreply.github.com> Date: Sun, 21 Jul 2024 11:23:59 -0700 Subject: [PATCH 08/11] Update auth_list.rs --- crates/eips/src/eip7702/auth_list.rs | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/crates/eips/src/eip7702/auth_list.rs b/crates/eips/src/eip7702/auth_list.rs index 68d188e10cb..58a6a4a12c0 100644 --- a/crates/eips/src/eip7702/auth_list.rs +++ b/crates/eips/src/eip7702/auth_list.rs @@ -235,10 +235,7 @@ impl RecoveredAuthorization { /// Returns an optional address based on the current state of the authority. pub const fn authority(&self) -> Option
{ - match &self.authority { - RecoveredAuthority::Valid(address) => Some(*address), - RecoveredAuthority::Invalid => None, - } + self.authority.address() } /// Splits the authorization into parts. @@ -248,14 +245,17 @@ impl RecoveredAuthorization { } #[cfg(feature = "k256")] -impl TryFrom for RecoveredAuthorization { - type Error = alloy_primitives::SignatureError; - - fn try_from(value: SignedAuthorization) -> Result { - Ok(value.try_into_recovered()) +impl From for RecoveredAuthority { + fn from(value: SignedAuthorization) -> Self { + value.into_recovered().authority + } +} +#[cfg(feature = "k256")] +impl From for RecoveredAuthorization { + fn from(value: SignedAuthorization) -> Self { + value.into_recovered() } } - impl Deref for RecoveredAuthorization { type Target = Authorization; From 855ad82f9bcbe07e6c299d570ae4eaf0e8fb63e0 Mon Sep 17 00:00:00 2001 From: tesseract <146037313+DoTheBestToGetTheBest@users.noreply.github.com> Date: Mon, 22 Jul 2024 14:35:54 -0700 Subject: [PATCH 09/11] Update crates/eips/src/eip7702/auth_list.rs Co-authored-by: Oliver --- crates/eips/src/eip7702/auth_list.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/eips/src/eip7702/auth_list.rs b/crates/eips/src/eip7702/auth_list.rs index 58a6a4a12c0..94877d03496 100644 --- a/crates/eips/src/eip7702/auth_list.rs +++ b/crates/eips/src/eip7702/auth_list.rs @@ -10,7 +10,7 @@ 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). +/// 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 { From 5dad60d4f6f871c4ec939204db3384d47cb2fc3d Mon Sep 17 00:00:00 2001 From: tesseract <146037313+DoTheBestToGetTheBest@users.noreply.github.com> Date: Mon, 22 Jul 2024 14:36:01 -0700 Subject: [PATCH 10/11] Update crates/eips/src/eip7702/auth_list.rs Co-authored-by: Oliver --- crates/eips/src/eip7702/auth_list.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/crates/eips/src/eip7702/auth_list.rs b/crates/eips/src/eip7702/auth_list.rs index 94877d03496..eca306c63a8 100644 --- a/crates/eips/src/eip7702/auth_list.rs +++ b/crates/eips/src/eip7702/auth_list.rs @@ -250,6 +250,7 @@ impl From for RecoveredAuthority { value.into_recovered().authority } } + #[cfg(feature = "k256")] impl From for RecoveredAuthorization { fn from(value: SignedAuthorization) -> Self { From d0680c9078917bf3787e260c281661357a31ca90 Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Tue, 23 Jul 2024 10:28:43 +0200 Subject: [PATCH 11/11] chore: add is fns --- crates/eips/src/eip7702/auth_list.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/crates/eips/src/eip7702/auth_list.rs b/crates/eips/src/eip7702/auth_list.rs index eca306c63a8..a9c170ac157 100644 --- a/crates/eips/src/eip7702/auth_list.rs +++ b/crates/eips/src/eip7702/auth_list.rs @@ -28,6 +28,16 @@ impl RecoveredAuthority { 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.