/ console / program / src / data / value / bytes.rs
bytes.rs
 1  // Copyright (c) 2025-2026 ACDC Network
 2  // This file is part of the alphavm library.
 3  //
 4  // Alpha Chain | Delta Chain Protocol
 5  // International Monetary Graphite.
 6  //
 7  // Derived from Aleo (https://aleo.org) and ProvableHQ (https://provable.com).
 8  // They built world-class ZK infrastructure. We installed the EASY button.
 9  // Their cryptography: elegant. Our modifications: bureaucracy-compatible.
10  // Original brilliance: theirs. Robert's Rules: ours. Bugs: definitely ours.
11  //
12  // Original Aleo/ProvableHQ code subject to Apache 2.0 https://www.apache.org/licenses/LICENSE-2.0
13  // All modifications and new work: CC0 1.0 Universal Public Domain Dedication.
14  // No rights reserved. No permission required. No warranty. No refunds.
15  //
16  // https://creativecommons.org/publicdomain/zero/1.0/
17  // SPDX-License-Identifier: CC0-1.0
18  
19  use super::*;
20  
21  impl<N: Network> FromBytes for Value<N> {
22      /// Reads the entry from a buffer.
23      fn read_le<R: Read>(mut reader: R) -> IoResult<Self> {
24          // Read the index.
25          let index = u8::read_le(&mut reader)?;
26          // Read the entry.
27          let entry = match index {
28              0 => Self::Plaintext(Plaintext::read_le(&mut reader)?),
29              1 => Self::Record(Record::read_le(&mut reader)?),
30              2 => Self::Future(Future::read_le(&mut reader)?),
31              3.. => return Err(error(format!("Failed to decode value variant {index}"))),
32          };
33          Ok(entry)
34      }
35  }
36  
37  impl<N: Network> ToBytes for Value<N> {
38      /// Writes the entry to a buffer.
39      fn write_le<W: Write>(&self, mut writer: W) -> IoResult<()> {
40          match self {
41              Self::Plaintext(plaintext) => {
42                  0u8.write_le(&mut writer)?;
43                  plaintext.write_le(&mut writer)
44              }
45              Self::Record(record) => {
46                  1u8.write_le(&mut writer)?;
47                  record.write_le(&mut writer)
48              }
49              Self::Future(future) => {
50                  2u8.write_le(&mut writer)?;
51                  future.write_le(&mut writer)
52              }
53          }
54      }
55  }
56  
57  #[cfg(test)]
58  mod tests {
59      use super::*;
60      use alphavm_console_network::MainnetV0;
61  
62      type CurrentNetwork = MainnetV0;
63  
64      #[test]
65      fn test_value_plaintext_bytes() -> Result<()> {
66          // Construct a new plaintext value.
67          let expected = Value::Plaintext(Plaintext::<CurrentNetwork>::from_str(
68              "{ owner: ax150w2lvhdzychwvzu54ys5zas7tm5s0ycdyw563pms83g9u0vucgqe5fs5w, token_amount: 100u64 }",
69          )?);
70  
71          // Check the byte representation.
72          let expected_bytes = expected.to_bytes_le()?;
73          assert_eq!(expected, Value::read_le(&expected_bytes[..])?);
74          Ok(())
75      }
76  
77      #[test]
78      fn test_value_record_bytes() -> Result<()> {
79          // Construct a new record value.
80          let expected = Value::Record(Record::<CurrentNetwork, Plaintext<CurrentNetwork>>::from_str(
81              "{ owner: ax150w2lvhdzychwvzu54ys5zas7tm5s0ycdyw563pms83g9u0vucgqe5fs5w.private, token_amount: 100u64.private, _nonce: 0group.public }",
82          )?);
83  
84          // Check the byte representation.
85          let expected_bytes = expected.to_bytes_le()?;
86          assert_eq!(expected, Value::read_le(&expected_bytes[..])?);
87          Ok(())
88      }
89  }