snarkvm_circuit_program/id/
to_address.rs1use super::*;
17
18impl<A: Aleo> ProgramID<A> {
19 pub fn to_address(&self) -> Address<A> {
21 let group = A::hash_to_group_psd4(&[self.name().to_field(), self.network().to_field()]);
23 Address::from_group(group)
25 }
26}
27
28#[cfg(test)]
29mod tests {
30 use super::*;
31 use crate::{Circuit, data::identifier::tests::sample_lowercase_console_identifier_as_string};
32
33 use anyhow::Result;
34
35 const ITERATIONS: usize = 10;
36
37 fn check_to_address(
38 mode: Mode,
39 num_constants: u64,
40 num_public: u64,
41 num_private: u64,
42 num_constraints: u64,
43 ) -> Result<()> {
44 for i in 0..ITERATIONS {
45 let expected_name_string = sample_lowercase_console_identifier_as_string::<Circuit>()?;
47 let expected_program_id = console::ProgramID::<<Circuit as Environment>::Network>::from_str(&format!(
48 "{expected_name_string}.aleo"
49 ))?;
50 let expected = expected_program_id.to_address()?;
51
52 let program_id = match mode {
53 Mode::Constant => ProgramID::<Circuit>::constant(expected_program_id),
54 Mode::Public => ProgramID::<Circuit>::public(expected_program_id),
55 Mode::Private => panic!("ProgramID cannot be private"),
56 };
57
58 Circuit::scope(format!("{mode}"), || {
59 let candidate = program_id.to_address();
60 assert_eq!(expected, candidate.eject_value());
61 if i > 0 {
62 assert_scope!(num_constants, num_public, num_private, num_constraints);
63 }
64 });
65 Circuit::reset();
66 }
67 Ok(())
68 }
69
70 #[test]
71 fn test_to_address_constant() -> Result<()> {
72 check_to_address(Mode::Constant, 1059, 0, 0, 0)
73 }
74
75 #[test]
76 fn test_to_address_public() -> Result<()> {
77 check_to_address(Mode::Public, 529, 0, 2096, 2106)
78 }
79}