snarkvm_ledger_block/transaction/deployment/
serialize.rs

1// Copyright (c) 2019-2025 Provable Inc.
2// This file is part of the snarkVM library.
3
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at:
7
8// http://www.apache.org/licenses/LICENSE-2.0
9
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16use super::*;
17
18impl<N: Network> Serialize for Deployment<N> {
19    /// Serializes the deployment into string or bytes.
20    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
21        match serializer.is_human_readable() {
22            true => {
23                let mut deployment = serializer.serialize_struct("Deployment", 3)?;
24                deployment.serialize_field("edition", &self.edition)?;
25                deployment.serialize_field("program", &self.program)?;
26                deployment.serialize_field("verifying_keys", &self.verifying_keys)?;
27                deployment.end()
28            }
29            false => ToBytesSerializer::serialize_with_size_encoding(self, serializer),
30        }
31    }
32}
33
34impl<'de, N: Network> Deserialize<'de> for Deployment<N> {
35    /// Deserializes the deployment from a string or bytes.
36    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
37        match deserializer.is_human_readable() {
38            true => {
39                // Parse the deployment from a string into a value.
40                let mut deployment = serde_json::Value::deserialize(deserializer)?;
41
42                // Recover the deployment.
43                let deployment = Self::new(
44                    // Retrieve the edition.
45                    DeserializeExt::take_from_value::<D>(&mut deployment, "edition")?,
46                    // Retrieve the program.
47                    DeserializeExt::take_from_value::<D>(&mut deployment, "program")?,
48                    // Retrieve the verifying keys.
49                    DeserializeExt::take_from_value::<D>(&mut deployment, "verifying_keys")?,
50                )
51                .map_err(de::Error::custom)?;
52
53                Ok(deployment)
54            }
55            false => FromBytesDeserializer::<Self>::deserialize_with_size_encoding(deserializer, "deployment"),
56        }
57    }
58}
59
60#[cfg(test)]
61mod tests {
62    use super::*;
63
64    #[test]
65    fn test_serde_json() -> Result<()> {
66        let rng = &mut TestRng::default();
67
68        // Sample the deployment.
69        let expected = test_helpers::sample_deployment(rng);
70
71        // Serialize
72        let expected_string = &expected.to_string();
73        let candidate_string = serde_json::to_string(&expected)?;
74        assert_eq!(expected, serde_json::from_str(&candidate_string)?);
75
76        // Deserialize
77        assert_eq!(expected, Deployment::from_str(expected_string)?);
78        assert_eq!(expected, serde_json::from_str(&candidate_string)?);
79
80        Ok(())
81    }
82
83    #[test]
84    fn test_bincode() -> Result<()> {
85        let rng = &mut TestRng::default();
86
87        // Sample the deployment.
88        let expected = test_helpers::sample_deployment(rng);
89
90        // Serialize
91        let expected_bytes = expected.to_bytes_le()?;
92        let expected_bytes_with_size_encoding = bincode::serialize(&expected)?;
93        assert_eq!(&expected_bytes[..], &expected_bytes_with_size_encoding[8..]);
94
95        // Deserialize
96        assert_eq!(expected, Deployment::read_le(&expected_bytes[..])?);
97        assert_eq!(expected, bincode::deserialize(&expected_bytes_with_size_encoding[..])?);
98
99        Ok(())
100    }
101}