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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
use std::{borrow::Cow, str::FromStr};

use super::{Ipv4, Ipv6, StakeCredential};
use cml_core::DeserializeError;
use cml_crypto::RawBytesEncoding;

impl StakeCredential {
    // we don't implement RawBytesEncoding as from_raw_bytes() would be unable to distinguish
    pub fn to_raw_bytes(&self) -> &[u8] {
        match self {
            Self::PubKey { hash, .. } => hash.to_raw_bytes(),
            Self::Script { hash, .. } => hash.to_raw_bytes(),
        }
    }
}

#[derive(Debug, thiserror::Error)]
pub enum IPStringParsingError {
    #[error("Invalid IPv4 Address String, expected period-separated bytes e.g. 0.0.0.0")]
    IPv4StringFormat,
    #[error("Invalid IPv6 Address String, expected colon-separated hextets e.g. 2001:0db8:0000:0000:0000:8a2e:0370:7334")]
    IPv6StringFormat,
    #[error("Deserializing from bytes: {0:?}")]
    DeserializeError(DeserializeError),
}

impl std::fmt::Display for Ipv4 {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "{}",
            self.inner
                .iter()
                .map(ToString::to_string)
                .collect::<Vec<String>>()
                .join(".")
        )
    }
}

impl FromStr for Ipv4 {
    type Err = IPStringParsingError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        s.split('.')
            .map(FromStr::from_str)
            .collect::<Result<Vec<u8>, _>>()
            .map_err(|_e| IPStringParsingError::IPv4StringFormat)
            .and_then(|bytes| Self::new(bytes).map_err(IPStringParsingError::DeserializeError))
    }
}

impl serde::Serialize for Ipv4 {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        serializer.serialize_str(&self.to_string())
    }
}

impl<'de> serde::de::Deserialize<'de> for Ipv4 {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::de::Deserializer<'de>,
    {
        let s = <String as serde::de::Deserialize>::deserialize(deserializer)?;
        Self::from_str(&s).map_err(|_e| {
            serde::de::Error::invalid_value(serde::de::Unexpected::Str(&s), &"invalid ipv4 address")
        })
    }
}

impl schemars::JsonSchema for Ipv4 {
    fn schema_name() -> String {
        String::from("Ipv4")
    }

    fn json_schema(gen: &mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema {
        String::json_schema(gen)
    }

    fn is_referenceable() -> bool {
        String::is_referenceable()
    }
}

impl Ipv6 {
    const LEN: usize = 16;

    pub fn hextets(&self) -> Vec<u16> {
        let mut ret = Vec::with_capacity(Self::LEN / 2);
        for i in (0..self.inner.len()).step_by(2) {
            ret.push(((self.inner[i + 1] as u16) << 8) | (self.inner[i] as u16));
        }
        ret
    }
}

impl std::fmt::Display for Ipv6 {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        // Using the canonical format for IPV6 in RFC5952
        // 4.1) Leading zeros MUST be suppressed.
        // 4.2.1) :: MUST shorten as much as possible
        // 4.2.2) :: MUST NOT be used for a single 0 field
        // 4.2.3) :: Ties are broken by choosing the location first in the string
        // 4.3) Hex chars MUST be lowercase
        // NOTE: we do NOT support IPv4-Mapped IPv6 special text representations (Section 5)
        //       this is only RECOMMENDED, not required, and only when the format is known
        //       e.g. specific prefixes are used
        let mut best_gap_len = 0;
        let mut best_gap_start = 0;
        // usize::MAX is fine since we're max 16 here
        const UNDEF: usize = usize::MAX;
        let mut cur_gap_start = UNDEF;
        let hextets = self.hextets();
        for (i, hextet) in hextets.iter().enumerate() {
            if *hextet == 0 {
                if cur_gap_start == UNDEF {
                    cur_gap_start = i;
                }
            } else {
                if cur_gap_start != UNDEF && (i - cur_gap_start) > best_gap_len {
                    best_gap_len = i - cur_gap_start;
                    best_gap_start = cur_gap_start;
                }
                cur_gap_start = UNDEF;
            }
        }
        if cur_gap_start != UNDEF && (hextets.len() - cur_gap_start) > best_gap_len {
            best_gap_len = hextets.len() - cur_gap_start;
            best_gap_start = cur_gap_start;
        }
        fn ipv6_substr(hextet_substr: &[u16]) -> String {
            hextet_substr
                .iter()
                .map(|hextet| {
                    let trimmed = hex::encode(hextet.to_le_bytes())
                        .trim_start_matches('0')
                        .to_owned();
                    if trimmed.is_empty() {
                        "0".to_owned()
                    } else {
                        trimmed
                    }
                })
                .collect::<Vec<String>>()
                .join(":")
        }
        let canonical_str_rep = if best_gap_len > 1 {
            format!(
                "{}::{}",
                ipv6_substr(&hextets[..best_gap_start]),
                ipv6_substr(&hextets[(best_gap_start + best_gap_len)..])
            )
        } else {
            ipv6_substr(&hextets)
        };
        write!(f, "{}", canonical_str_rep)
    }
}

impl FromStr for Ipv6 {
    type Err = IPStringParsingError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        fn ipv6_subbytes(substr: &str) -> Result<Vec<u8>, IPStringParsingError> {
            let mut bytes = Vec::new();
            for hextet_str in substr.split(':') {
                // hex::decode does not allow odd-length strings so pad it
                let padded_str = if hextet_str.len() % 2 == 0 {
                    Cow::Borrowed(hextet_str)
                } else {
                    Cow::Owned(format!("0{hextet_str}"))
                };
                let hextet_bytes = hex::decode(padded_str.as_bytes())
                    .map_err(|_e| IPStringParsingError::IPv6StringFormat)?;
                match hextet_bytes.len() {
                    0 => {
                        bytes.extend(&[0, 0]);
                    }
                    1 => {
                        bytes.push(0);
                        bytes.push(hextet_bytes[0]);
                    }
                    2 => {
                        bytes.extend(&hextet_bytes);
                    }
                    _ => return Err(IPStringParsingError::IPv6StringFormat),
                }
            }
            Ok(bytes)
        }
        let bytes = if let Some((left_str, right_str)) = s.split_once("::") {
            let mut bytes = ipv6_subbytes(left_str)?;
            let right_bytes = ipv6_subbytes(right_str)?;
            // pad middle with 0s
            bytes.resize(Self::LEN - right_bytes.len(), 0);
            bytes.extend(&right_bytes);
            bytes
        } else {
            ipv6_subbytes(s)?
        };
        Self::new(bytes).map_err(IPStringParsingError::DeserializeError)
    }
}

impl serde::Serialize for Ipv6 {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        serializer.serialize_str(&self.to_string())
    }
}

impl<'de> serde::de::Deserialize<'de> for Ipv6 {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::de::Deserializer<'de>,
    {
        let s = <String as serde::de::Deserialize>::deserialize(deserializer)?;
        Self::from_str(&s).map_err(|_e| {
            serde::de::Error::invalid_value(serde::de::Unexpected::Str(&s), &"invalid ipv6 address")
        })
    }
}

impl schemars::JsonSchema for Ipv6 {
    fn schema_name() -> String {
        String::from("Ipv6")
    }

    fn json_schema(gen: &mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema {
        String::json_schema(gen)
    }

    fn is_referenceable() -> bool {
        String::is_referenceable()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn ipv4_json() {
        let json_str_1 = "\"0.0.0.0\"";
        let from_json_1: Ipv4 = serde_json::from_str(json_str_1).unwrap();
        let to_json_1 = serde_json::to_string_pretty(&from_json_1).unwrap();
        assert_eq!(json_str_1, to_json_1);
        let json_str_2 = "\"255.255.255.255\"";
        let from_json_2: Ipv4 = serde_json::from_str(json_str_2).unwrap();
        let to_json_2 = serde_json::to_string_pretty(&from_json_2).unwrap();
        assert_eq!(json_str_2, to_json_2);
    }

    fn ipv6_json_testcase(long_form_json: &str, canonical_form_json: &str) {
        let from_long: Ipv6 = serde_json::from_str(long_form_json).unwrap();
        let to_json_1 = serde_json::to_string_pretty(&from_long).unwrap();
        assert_eq!(canonical_form_json, to_json_1);
        let from_canonical: Ipv6 = serde_json::from_str(canonical_form_json).unwrap();
        let to_json_2 = serde_json::to_string_pretty(&from_canonical).unwrap();
        assert_eq!(canonical_form_json, to_json_2);
        assert_eq!(from_long.inner, from_canonical.inner);
    }

    #[test]
    fn ipv6_json() {
        // This tests that we abide by RFC5952 for IPV6 Canonical text form
        // part of the implementation relies on the hex crate's behavior but
        // that is checked as part of this test (e.g. that lowercase is used + omit leading 0s)
        ipv6_json_testcase(
            "\"2001:0db8:0000:0000:0000:ff00:0042:8329\"",
            "\"2001:db8::ff00:42:8329\"",
        );
        // ties broken by first one
        ipv6_json_testcase(
            "\"2001:0db8:0000:0000:1111:0000:0000:8329\"",
            "\"2001:db8::1111:0:0:8329\"",
        );
        // min run not first
        ipv6_json_testcase(
            "\"0001:0000:0002:0000:0000:0000:0003:0000\"",
            "\"1:0:2::3:0\"",
        );
        // ends in min run
        ipv6_json_testcase("\"000a:000b:0000:0000:0000:0000:0000:0000\"", "\"a:b::\"");
        // starts with min run
        ipv6_json_testcase(
            "\"0000:0000:0000:0000:0000:0000:abcd:0000\"",
            "\"::abcd:0\"",
        );
        // don't use runs for single 0 hextets
        ipv6_json_testcase(
            "\"0000:000a:0000:000b:0000:000c:0000:000d\"",
            "\"0:a:0:b:0:c:0:d\"",
        );
    }
}