snarkvm_console_program/owner/
serialize.rs

1// Copyright 2024 Aleo Network Foundation
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
18use snarkvm_utilities::DeserializeExt;
19
20impl<N: Network> Serialize for ProgramOwner<N> {
21    /// Serializes the program owner into string or bytes.
22    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
23        match serializer.is_human_readable() {
24            true => {
25                let mut owner = serializer.serialize_struct("ProgramOwner", 2)?;
26                owner.serialize_field("address", &self.address)?;
27                owner.serialize_field("signature", &self.signature)?;
28                owner.end()
29            }
30            false => ToBytesSerializer::serialize_with_size_encoding(self, serializer),
31        }
32    }
33}
34
35impl<'de, N: Network> Deserialize<'de> for ProgramOwner<N> {
36    /// Deserializes the owner from a string or bytes.
37    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
38        match deserializer.is_human_readable() {
39            true => {
40                // Parse the program owner from a string into a value.
41                let mut owner = serde_json::Value::deserialize(deserializer)?;
42
43                // Recover the program owner.
44                let owner = Self::from(
45                    // Retrieve the address.
46                    DeserializeExt::take_from_value::<D>(&mut owner, "address")?,
47                    // Retrieve the signature.
48                    DeserializeExt::take_from_value::<D>(&mut owner, "signature")?,
49                );
50
51                Ok(owner)
52            }
53            false => FromBytesDeserializer::<Self>::deserialize_with_size_encoding(deserializer, "program owner"),
54        }
55    }
56}
57
58#[cfg(test)]
59mod tests {
60    use super::*;
61
62    #[test]
63    fn test_serde_json() -> Result<()> {
64        // Sample the program owner.
65        let expected = test_helpers::sample_program_owner();
66
67        // Serialize
68        let expected_string = &expected.to_string();
69        let candidate_string = serde_json::to_string(&expected)?;
70        assert_eq!(expected, serde_json::from_str(&candidate_string)?);
71
72        // Deserialize
73        assert_eq!(expected, ProgramOwner::from_str(expected_string)?);
74        assert_eq!(expected, serde_json::from_str(&candidate_string)?);
75
76        Ok(())
77    }
78
79    #[test]
80    fn test_bincode() -> Result<()> {
81        // Sample the program owner.
82        let expected = test_helpers::sample_program_owner();
83
84        // Serialize
85        let expected_bytes = expected.to_bytes_le()?;
86        let expected_bytes_with_size_encoding = bincode::serialize(&expected)?;
87        assert_eq!(&expected_bytes[..], &expected_bytes_with_size_encoding[8..]);
88
89        // Deserialize
90        assert_eq!(expected, ProgramOwner::read_le(&expected_bytes[..])?);
91        assert_eq!(expected, bincode::deserialize(&expected_bytes_with_size_encoding[..])?);
92
93        Ok(())
94    }
95}