cfx_rpc_eth_types/
authorization.rs

1use cfx_types::{Address, U256, U64};
2use primitives::transaction::AuthorizationListItem;
3
4#[derive(
5    Debug, Default, PartialEq, Eq, serde::Deserialize, serde::Serialize, Clone,
6)]
7#[serde(rename_all = "camelCase")]
8pub struct Authorization {
9    /// The chain ID of the authorization.
10    pub chain_id: U256,
11    /// The address of the authorization.
12    pub address: Address,
13    /// The nonce for the authorization.
14    pub nonce: U64,
15}
16
17#[derive(
18    Debug, Default, PartialEq, Eq, serde::Deserialize, serde::Serialize, Clone,
19)]
20#[serde(rename_all = "camelCase")]
21pub struct SignedAuthorization {
22    /// Inner authorization.
23    #[serde(flatten)]
24    inner: Authorization,
25    /// Signature parity value. We allow any [`U64`] here, however, the only
26    /// valid values are `0` and `1` and anything else will result in error
27    /// during recovery.
28    pub y_parity: U64,
29    /// Signature `r` value.
30    pub r: U256,
31    /// Signature `s` value.
32    pub s: U256,
33}
34
35impl SignedAuthorization {
36    /// Returns the inner authorization.
37    pub const fn inner(&self) -> &Authorization { &self.inner }
38
39    /// Returns the signature parity value.
40    pub fn y_parity(&self) -> u8 { self.y_parity.as_u32() as u8 }
41
42    /// Returns the signature `r` value.
43    pub const fn r(&self) -> U256 { self.r }
44
45    /// Returns the signature `s` value.
46    pub const fn s(&self) -> U256 { self.s }
47}
48
49impl From<AuthorizationListItem> for SignedAuthorization {
50    fn from(item: AuthorizationListItem) -> Self {
51        Self {
52            inner: Authorization {
53                chain_id: item.chain_id.into(),
54                address: item.address.into(),
55                nonce: item.nonce.into(),
56            },
57            y_parity: item.y_parity.into(),
58            r: item.r,
59            s: item.s,
60        }
61    }
62}
63
64impl Into<AuthorizationListItem> for SignedAuthorization {
65    fn into(self) -> AuthorizationListItem {
66        AuthorizationListItem {
67            chain_id: self.inner.chain_id,
68            address: self.inner.address,
69            nonce: self.inner.nonce.as_u64(),
70            y_parity: self.y_parity(),
71            r: self.r,
72            s: self.s,
73        }
74    }
75}