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 ProgramOwner<N> { 22 /// Reads the program owner from a buffer. 23 fn read_le<R: Read>(mut reader: R) -> IoResult<Self> { 24 // Read the version. 25 let version = u8::read_le(&mut reader)?; 26 // Ensure the version is valid. 27 if version != 1 { 28 return Err(error("Invalid program owner version")); 29 } 30 31 // Read the address. 32 let address = Address::read_le(&mut reader)?; 33 // Read the signature. 34 let signature = Signature::read_le(&mut reader)?; 35 36 // Return the program owner. 37 Ok(Self::from(address, signature)) 38 } 39 } 40 41 impl<N: Network> ToBytes for ProgramOwner<N> { 42 /// Writes the program owner to a buffer. 43 fn write_le<W: Write>(&self, mut writer: W) -> IoResult<()> { 44 // Write the version. 45 1u8.write_le(&mut writer)?; 46 // Write the address. 47 self.address.write_le(&mut writer)?; 48 // Write the signature. 49 self.signature.write_le(&mut writer) 50 } 51 } 52 53 #[cfg(test)] 54 mod tests { 55 use super::*; 56 use alphavm_console_network::MainnetV0; 57 58 type CurrentNetwork = MainnetV0; 59 60 #[test] 61 fn test_bytes() -> Result<()> { 62 // Construct a new program owner. 63 let expected = test_helpers::sample_program_owner(); 64 65 // Check the byte representation. 66 let expected_bytes = expected.to_bytes_le()?; 67 assert_eq!(expected, ProgramOwner::read_le(&expected_bytes[..])?); 68 assert!(ProgramOwner::<CurrentNetwork>::read_le(&expected_bytes[1..]).is_err()); 69 Ok(()) 70 } 71 }