-
Notifications
You must be signed in to change notification settings - Fork 518
/
authorization_list.rs
159 lines (135 loc) · 5.09 KB
/
authorization_list.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
pub use alloy_eips::eip7702::{Authorization, SignedAuthorization};
pub use alloy_primitives::{Parity, Signature};
use super::SECP256K1N_HALF;
use crate::Address;
use core::{fmt, ops::Deref};
use std::{boxed::Box, vec::Vec};
/// Authorization list for EIP-7702 transaction type.
#[derive(Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum AuthorizationList {
Signed(Vec<SignedAuthorization>),
Recovered(Vec<RecoveredAuthorization>),
}
impl From<Vec<SignedAuthorization>> for AuthorizationList {
fn from(signed: Vec<SignedAuthorization>) -> Self {
Self::Signed(signed)
}
}
impl From<Vec<RecoveredAuthorization>> for AuthorizationList {
fn from(recovered: Vec<RecoveredAuthorization>) -> Self {
Self::Recovered(recovered)
}
}
impl AuthorizationList {
/// Returns length of the authorization list.
pub fn len(&self) -> usize {
match self {
Self::Signed(signed) => signed.len(),
Self::Recovered(recovered) => recovered.len(),
}
}
/// Returns true if the authorization list is valid.
pub fn is_valid(&self, _chain_id: u64) -> Result<(), InvalidAuthorization> {
let validate = |auth: &SignedAuthorization| -> Result<(), InvalidAuthorization> {
// TODO Eip7702. Check chain_id
// Pending: https://github.com/ethereum/EIPs/pull/8833/files
// let auth_chain_id: u64 = auth.chain_id().try_into().unwrap_or(u64::MAX);
// if auth_chain_id != 0 && auth_chain_id != chain_id {
// return Err(InvalidAuthorization::InvalidChainId);
// }
// Check y_parity, Parity::Parity means that it was 0 or 1.
if !matches!(auth.signature().v(), Parity::Parity(_)) {
return Err(InvalidAuthorization::InvalidYParity);
}
// Check s-value
if auth.signature().s() > SECP256K1N_HALF {
return Err(InvalidAuthorization::Eip2InvalidSValue);
}
Ok(())
};
match self {
Self::Signed(signed) => signed.iter().try_for_each(validate)?,
Self::Recovered(recovered) => recovered
.iter()
.map(|recovered| &recovered.inner)
.try_for_each(validate)?,
};
Ok(())
}
/// Return empty authorization list.
pub fn empty() -> Self {
Self::Recovered(Vec::new())
}
/// Returns true if the authorization list is empty.
pub fn is_empty(&self) -> bool {
self.len() == 0
}
/// Returns iterator of recovered Authorizations.
pub fn recovered_iter<'a>(&'a self) -> Box<dyn Iterator<Item = RecoveredAuthorization> + 'a> {
match self {
Self::Signed(signed) => Box::new(signed.iter().map(|signed| signed.clone().into())),
Self::Recovered(recovered) => Box::new(recovered.clone().into_iter()),
}
}
/// Returns recovered authorizations list.
pub fn into_recovered(self) -> Self {
let Self::Signed(signed) = self else {
return self;
};
Self::Recovered(signed.into_iter().map(|signed| signed.into()).collect())
}
}
/// A recovered authorization.
#[derive(Debug, Clone, Hash, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct RecoveredAuthorization {
#[cfg_attr(feature = "serde", serde(flatten))]
inner: SignedAuthorization,
authority: Option<Address>,
}
impl RecoveredAuthorization {
/// Instantiate without performing recovery. This should be used carefully.
pub const fn new_unchecked(inner: SignedAuthorization, authority: Option<Address>) -> Self {
Self { inner, authority }
}
/// Get the `authority` for the authorization.
///
/// If this is `None`, then the authority could not be recovered.
pub const fn authority(&self) -> Option<Address> {
self.authority
}
/// Splits the authorization into parts.
pub const fn into_parts(self) -> (SignedAuthorization, Option<Address>) {
(self.inner, self.authority)
}
}
impl From<SignedAuthorization> for RecoveredAuthorization {
fn from(signed_auth: SignedAuthorization) -> Self {
let authority = signed_auth.recover_authority().ok();
Self::new_unchecked(signed_auth, authority)
}
}
impl Deref for RecoveredAuthorization {
type Target = Authorization;
fn deref(&self) -> &Self::Target {
&self.inner
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum InvalidAuthorization {
InvalidChainId,
InvalidYParity,
Eip2InvalidSValue,
}
impl fmt::Display for InvalidAuthorization {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let s = match self {
Self::InvalidChainId => "Invalid chain_id, Expect chain's ID or zero",
Self::InvalidYParity => "Invalid y_parity, Expect 0 or 1.",
Self::Eip2InvalidSValue => "Invalid signature s-value.",
};
f.write_str(s)
}
}