eest_types/
authorization.rs

1use cfx_types::{Address, U256};
2
3/// An unsigned EIP-7702 authorization.
4#[derive(Debug, Clone, Hash, Eq, PartialEq)]
5#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
6#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
7pub struct Authorization {
8    /// The chain ID of the authorization.
9    pub chain_id: U256,
10    /// The address of the authorization.
11    pub address: Address,
12    /// The nonce for the authorization.
13    #[cfg_attr(feature = "serde", serde(with = "quantity"))]
14    pub nonce: u64,
15}
16
17/// A signed EIP-7702 authorization.
18#[derive(Debug, Clone, Eq, PartialEq)]
19#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
20pub struct SignedAuthorization {
21    /// Inner authorization.
22    #[cfg_attr(feature = "serde", serde(flatten))]
23    inner: Authorization,
24    /// Signature parity value. We allow any [`U8`] here, however, the only
25    /// valid values are `0` and `1` and anything else will result in error
26    /// during recovery.
27    #[cfg_attr(
28        feature = "serde",
29        serde(rename = "yParity", alias = "v", with = "quantity")
30    )]
31    y_parity: u64,
32    /// Signature `r` value.
33    r: U256,
34    /// Signature `s` value.
35    s: U256,
36}
37
38impl SignedAuthorization {
39    /// Returns the inner [`Authorization`].
40    pub const fn strip_signature(self) -> Authorization { self.inner }
41
42    /// Returns the inner [`Authorization`].
43    pub const fn inner(&self) -> &Authorization { &self.inner }
44
45    /// Returns the signature parity value.
46    pub fn y_parity(&self) -> u8 { self.y_parity as u8 }
47
48    /// Returns the signature `r` value.
49    pub const fn r(&self) -> U256 { self.r }
50
51    /// Returns the signature `s` value.
52    pub const fn s(&self) -> U256 { self.s }
53}
54
55#[cfg(feature = "serde")]
56mod quantity {
57    use cfx_types::U64;
58    use serde::{Deserialize, Deserializer, Serialize, Serializer};
59
60    /// Serializes a primitive number as a "quantity" hex string.
61    pub(crate) fn serialize<S>(
62        value: &u64, serializer: S,
63    ) -> Result<S::Ok, S::Error>
64    where S: Serializer {
65        U64::from(*value).serialize(serializer)
66    }
67
68    /// Deserializes a primitive number from a "quantity" hex string.
69    pub(crate) fn deserialize<'de, D>(
70        deserializer: D,
71    ) -> Result<u64, D::Error>
72    where D: Deserializer<'de> {
73        U64::deserialize(deserializer).map(|value| value.as_u64())
74    }
75}