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
// Copyright (C) 2019-2023 Aleo Systems Inc.
// This file is part of the snarkVM library.

// The snarkVM library is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// The snarkVM library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with the snarkVM library. If not, see <https://www.gnu.org/licenses/>.

use super::*;

use snarkvm_utilities::DeserializeExt;

impl<N: Network> Serialize for InputID<N> {
    /// Serializes the input ID into string or bytes.
    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        match serializer.is_human_readable() {
            true => match self {
                Self::Constant(id) => {
                    let mut input = serializer.serialize_struct("InputID", 2)?;
                    input.serialize_field("type", "constant")?;
                    input.serialize_field("id", &id)?;
                    input.end()
                }
                Self::Public(id) => {
                    let mut input = serializer.serialize_struct("InputID", 2)?;
                    input.serialize_field("type", "public")?;
                    input.serialize_field("id", &id)?;
                    input.end()
                }
                Self::Private(id) => {
                    let mut input = serializer.serialize_struct("InputID", 2)?;
                    input.serialize_field("type", "private")?;
                    input.serialize_field("id", &id)?;
                    input.end()
                }
                Self::Record(commitment, gamma, serial_number, tag) => {
                    let mut input = serializer.serialize_struct("InputID", 5)?;
                    input.serialize_field("type", "record")?;
                    input.serialize_field("commitment", &commitment)?;
                    input.serialize_field("gamma", &gamma)?;
                    input.serialize_field("serial_number", &serial_number)?;
                    input.serialize_field("tag", &tag)?;
                    input.end()
                }
                Self::ExternalRecord(id) => {
                    let mut input = serializer.serialize_struct("InputID", 2)?;
                    input.serialize_field("type", "external_record")?;
                    input.serialize_field("id", &id)?;
                    input.end()
                }
            },
            false => ToBytesSerializer::serialize_with_size_encoding(self, serializer),
        }
    }
}

impl<'de, N: Network> Deserialize<'de> for InputID<N> {
    /// Deserializes the input ID from a string or bytes.
    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        match deserializer.is_human_readable() {
            true => {
                // Parse the input ID from a string into a value.
                let mut input = serde_json::Value::deserialize(deserializer)?;
                // Recover the input.
                let input_id = match input.get("type").and_then(|t| t.as_str()) {
                    Some("constant") => InputID::Constant(DeserializeExt::take_from_value::<D>(&mut input, "id")?),
                    Some("public") => InputID::Public(DeserializeExt::take_from_value::<D>(&mut input, "id")?),
                    Some("private") => InputID::Private(DeserializeExt::take_from_value::<D>(&mut input, "id")?),
                    Some("record") => InputID::Record(
                        DeserializeExt::take_from_value::<D>(&mut input, "commitment")?,
                        DeserializeExt::take_from_value::<D>(&mut input, "gamma")?,
                        DeserializeExt::take_from_value::<D>(&mut input, "serial_number")?,
                        DeserializeExt::take_from_value::<D>(&mut input, "tag")?,
                    ),
                    Some("external_record") => {
                        InputID::ExternalRecord(DeserializeExt::take_from_value::<D>(&mut input, "id")?)
                    }
                    _ => return Err(de::Error::custom("Invalid input type")),
                };
                Ok(input_id)
            }
            false => FromBytesDeserializer::<Self>::deserialize_with_size_encoding(deserializer, "input ID"),
        }
    }
}

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

    type CurrentNetwork = Testnet3;

    /// Add test cases here to be checked for serialization.
    const TEST_CASES: &[&str] = &[
        "{\"type\":\"constant\",\"id\":\"5field\"}",
        "{\"type\":\"public\",\"id\":\"0field\"}",
        "{\"type\":\"private\",\"id\":\"123field\"}",
        "{\"type\":\"record\",\"commitment\":\"123123field\",\"tag\":\"0field\",\"serial_number\":\"123456789field\",\"gamma\":\"0group\"}",
        "{\"type\":\"external_record\",\"id\":\"123456789field\"}",
    ];

    fn check_serde_json<
        T: Serialize + for<'a> Deserialize<'a> + Debug + Display + PartialEq + Eq + FromStr + ToBytes + FromBytes,
    >(
        expected: T,
    ) {
        // Serialize
        let expected_string = expected.to_string();
        let candidate_string = serde_json::to_string(&expected).unwrap();
        let candidate = serde_json::from_str::<T>(&candidate_string).unwrap();
        assert_eq!(expected, candidate);
        assert_eq!(expected_string, candidate_string);
        assert_eq!(expected_string, candidate.to_string());

        // Deserialize
        assert_eq!(expected, T::from_str(&expected_string).unwrap_or_else(|_| panic!("FromStr: {expected_string}")));
        assert_eq!(expected, serde_json::from_str(&candidate_string).unwrap());
    }

    fn check_bincode<
        T: Serialize + for<'a> Deserialize<'a> + Debug + Display + PartialEq + Eq + FromStr + ToBytes + FromBytes,
    >(
        expected: T,
    ) {
        // Serialize
        let expected_bytes = expected.to_bytes_le().unwrap();
        let expected_bytes_with_size_encoding = bincode::serialize(&expected).unwrap();
        assert_eq!(&expected_bytes[..], &expected_bytes_with_size_encoding[8..]);

        // Deserialize
        assert_eq!(expected, T::read_le(&expected_bytes[..]).unwrap());
        assert_eq!(expected, bincode::deserialize(&expected_bytes_with_size_encoding[..]).unwrap());
    }

    #[test]
    fn test_serde_json() {
        for case in TEST_CASES.iter() {
            check_serde_json(InputID::<CurrentNetwork>::from_str(case).unwrap());
        }
    }

    #[test]
    fn test_bincode() {
        for case in TEST_CASES.iter() {
            check_bincode(InputID::<CurrentNetwork>::from_str(case).unwrap());
        }
    }
}