aboutsummaryrefslogtreecommitdiff
path: root/src/serialization.rs
blob: 466b75955c3e351a5d92a4009a5c7137b8a9b25a (plain)
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
//! Module with JSON serialization helpers.

use serde_json::{Map, Value};

/// A JSON object.
pub type JsonObject = Map<String, Value>;

/// `U256` serialization methods.
pub mod u256 {
    use ethnum::{AsU256 as _, U256};
    use serde::de::{self, Deserializer, Visitor};
    use std::fmt::{self, Formatter};

    struct U256Visitor;

    impl<'de> Visitor<'de> for U256Visitor {
        type Value = U256;

        fn expecting(&self, f: &mut Formatter) -> fmt::Result {
            f.write_str("number, decimal string or '0x-' prefixed hexadecimal string")
        }

        fn visit_u64<E>(self, v: u64) -> Result<Self::Value, E>
        where
            E: de::Error,
        {
            Ok(v.as_u256())
        }

        fn visit_u128<E>(self, v: u128) -> Result<Self::Value, E>
        where
            E: de::Error,
        {
            Ok(v.as_u256())
        }

        fn visit_f64<E>(self, v: f64) -> Result<Self::Value, E>
        where
            E: de::Error,
        {
            if v.fract() != 0. {
                return Err(de::Error::custom(
                    "invalid conversion from floating point number \
                     with fractional part to 256-bit integer",
                ));
            }
            if !(0.0..(1_u64 << 53) as _).contains(&v) {
                return Err(de::Error::custom(
                    "invalid conversion from floating point number \
                     outside of valid integer range [0, 2^53)",
                ));
            }

            Ok(v.as_u256())
        }

        fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
        where
            E: de::Error,
        {
            let (src, radix) = match v.strip_prefix("0x") {
                Some(v) => (v, 16),
                None => (v, 10),
            };
            U256::from_str_radix(src, radix).map_err(de::Error::custom)
        }
    }

    pub fn deserialize<'de, D>(deserializer: D) -> Result<U256, D::Error>
    where
        D: Deserializer<'de>,
    {
        deserializer.deserialize_any(U256Visitor)
    }

    pub mod option {
        use super::*;
        use serde::Deserialize;

        #[derive(Deserialize)]
        #[serde(transparent)]
        struct Helper(#[serde(with = "super")] U256);

        pub fn deserialize<'de, D>(deserializer: D) -> Result<Option<U256>, D::Error>
        where
            D: Deserializer<'de>,
        {
            let option = Option::deserialize(deserializer)?;
            Ok(option.map(|Helper(v)| v))
        }
    }
}

/// Dynamic byte array serialization methods.
pub mod bytes {
    use serde::{
        de::{self, Deserializer},
        Deserialize as _,
    };
    use std::borrow::Cow;

    pub fn deserialize<'de, D>(deserializer: D) -> Result<Vec<u8>, D::Error>
    where
        D: Deserializer<'de>,
    {
        let s = Cow::<str>::deserialize(deserializer)?;
        let s = s
            .strip_prefix("0x")
            .ok_or_else(|| de::Error::custom("storage slot missing '0x' prefix"))?;
        hex::decode(s).map_err(de::Error::custom)
    }
}

/// Fixed byte array serialization methods.
pub mod bytearray {
    use serde::{
        de::{self, Deserializer},
        Deserialize as _,
    };
    use std::borrow::Cow;

    pub fn deserialize<'de, D, const N: usize>(deserializer: D) -> Result<[u8; N], D::Error>
    where
        D: Deserializer<'de>,
    {
        let mut value = [0_u8; N];
        let s = Cow::<str>::deserialize(deserializer)?;
        let s = s
            .strip_prefix("0x")
            .ok_or_else(|| de::Error::custom("storage slot missing '0x' prefix"))?;
        hex::decode_to_slice(s, &mut value).map_err(de::Error::custom)?;
        Ok(value)
    }
}