eest_types/
test_authorization.rs

1use super::SignedAuthorization;
2use serde::{de::Error, Deserialize, Deserializer, Serialize};
3
4/// Struct for test authorization
5#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
6#[serde(rename_all = "camelCase")]
7pub struct TestAuthorization {
8    #[serde(flatten)]
9    inner: SignedAuthorization,
10}
11
12impl From<TestAuthorization> for SignedAuthorization {
13    fn from(auth: TestAuthorization) -> Self { auth.inner }
14}
15
16impl<'de> Deserialize<'de> for TestAuthorization {
17    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
18    where D: Deserializer<'de> {
19        // This is a hack to remove duplicate yParity and v fields which can be
20        // used by the test files for cross client compat
21        let mut value: serde_json::Value =
22            Deserialize::deserialize(deserializer)?;
23        if let Some(val) = value.as_object_mut() {
24            if val.contains_key("v") && val.contains_key("yParity") {
25                val.remove("v");
26            }
27        }
28        let inner: SignedAuthorization =
29            serde_json::from_value(value).map_err(D::Error::custom)?;
30        Ok(Self { inner })
31    }
32}
33
34#[cfg(test)]
35mod tests {
36    use super::*;
37
38    #[test]
39    fn recover_auth() {
40        // Test named:
41        // tests/prague/eip7702_set_code_tx/test_gas.
42        // py::test_account_warming[fork_Prague-state_test-single_valid_authorization_single_signer-check_delegated_account_first_True]
43
44        let auth = r#"{
45            "chainId": "0x00",
46            "address": "0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b",
47            "nonce": "0x00",
48            "v": "0x01",
49            "r": "0x5a8cac98fd240d8ef83c22db4a061ffa0facb1801245283cc05fc809d8b92837",
50            "s": "0x1c3162fe11d91bc24d4fa00fb19ca34531e0eacdf8142c804be44058d5b8244f",
51            "signer": "0x6389e7f33ce3b1e94e4325ef02829cd12297ef71"
52        }"#;
53
54        let auth: TestAuthorization = serde_json::from_str(auth).unwrap();
55        println!("{:?}", auth);
56    }
57
58    #[test]
59    fn recover_auth_duplicate_v_yparity() {
60        let auth = r#"{
61            "chainId": "0x00",
62            "address": "0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b",
63            "nonce": "0x00",
64            "v": "0x01",
65            "yParity": "0x01",
66            "r": "0x5a8cac98fd240d8ef83c22db4a061ffa0facb1801245283cc05fc809d8b92837",
67            "s": "0x1c3162fe11d91bc24d4fa00fb19ca34531e0eacdf8142c804be44058d5b8244f",
68            "signer": "0x6389e7f33ce3b1e94e4325ef02829cd12297ef71"
69        }"#;
70
71        let _: TestAuthorization = serde_json::from_str(auth).unwrap();
72    }
73}