use cfx_types::{Address, U256, U64};
use primitives::transaction::AuthorizationListItem;
#[derive(
Debug, Default, PartialEq, Eq, serde::Deserialize, serde::Serialize, Clone,
)]
#[serde(rename_all = "camelCase")]
pub struct Authorization {
pub chain_id: U256,
pub address: Address,
pub nonce: U64,
}
#[derive(
Debug, Default, PartialEq, Eq, serde::Deserialize, serde::Serialize, Clone,
)]
#[serde(rename_all = "camelCase")]
pub struct SignedAuthorization {
#[serde(flatten)]
inner: Authorization,
pub y_parity: U64,
pub r: U256,
pub s: U256,
}
impl SignedAuthorization {
pub const fn inner(&self) -> &Authorization { &self.inner }
pub fn y_parity(&self) -> u8 { self.y_parity.as_u32() as u8 }
pub const fn r(&self) -> U256 { self.r }
pub const fn s(&self) -> U256 { self.s }
}
impl From<AuthorizationListItem> for SignedAuthorization {
fn from(item: AuthorizationListItem) -> Self {
Self {
inner: Authorization {
chain_id: item.chain_id.into(),
address: item.address.into(),
nonce: item.nonce.into(),
},
y_parity: item.y_parity.into(),
r: item.r,
s: item.s,
}
}
}
impl Into<AuthorizationListItem> for SignedAuthorization {
fn into(self) -> AuthorizationListItem {
AuthorizationListItem {
chain_id: self.inner.chain_id,
address: self.inner.address,
nonce: self.inner.nonce.as_u64(),
y_parity: self.y_parity(),
r: self.r,
s: self.s,
}
}
}