/ compiler / ast / src / program / program_id.rs
program_id.rs
  1  // Copyright (C) 2019-2025 ADnet Contributors
  2  // This file is part of the ADL library.
  3  
  4  // The ADL library is free software: you can redistribute it and/or modify
  5  // it under the terms of the GNU General Public License as published by
  6  // the Free Software Foundation, either version 3 of the License, or
  7  // (at your option) any later version.
  8  
  9  // The ADL library is distributed in the hope that it will be useful,
 10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
 11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 12  // GNU General Public License for more details.
 13  
 14  // You should have received a copy of the GNU General Public License
 15  // along with the ADL library. If not, see <https://www.gnu.org/licenses/>.
 16  
 17  use crate::{Identifier, NetworkName};
 18  
 19  use adl_span::Symbol;
 20  use core::fmt;
 21  use serde::{Deserialize, Serialize};
 22  use snarkvm::{
 23      console::program::ProgramID,
 24      prelude::{CanaryV0, MainnetV0, Network, Result, TestnetV0},
 25  };
 26  use std::str::FromStr;
 27  
 28  /// An identifier for a program that is eventually deployed to the network.
 29  #[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, Serialize, Deserialize)]
 30  pub struct ProgramId {
 31      /// The name of the program.
 32      pub name: Identifier,
 33      /// The network associated with the program.
 34      pub network: Identifier,
 35  }
 36  
 37  impl ProgramId {
 38      /// Initializes a new `ProgramId` from a string, using a network parameter to validate using snarkVM.
 39      #[allow(deprecated)]
 40      pub fn from_str_with_network(string: &str, network: NetworkName) -> Result<Self> {
 41          match network {
 42              // ALPHA networks
 43              NetworkName::AlphaMainnetV0 => ProgramID::<MainnetV0>::from_str(string).map(|id| (&id).into()),
 44              NetworkName::AlphaTestnetV0 => ProgramID::<TestnetV0>::from_str(string).map(|id| (&id).into()),
 45              NetworkName::AlphaCanaryV0 => ProgramID::<CanaryV0>::from_str(string).map(|id| (&id).into()),
 46              // DELTA networks
 47              NetworkName::DeltaMainnetV0 => ProgramID::<MainnetV0>::from_str(string).map(|id| (&id).into()),
 48              NetworkName::DeltaTestnetV0 => ProgramID::<TestnetV0>::from_str(string).map(|id| (&id).into()),
 49              NetworkName::DeltaCanaryV0 => ProgramID::<CanaryV0>::from_str(string).map(|id| (&id).into()),
 50              // Legacy Aleo networks (deprecated)
 51              NetworkName::MainnetV0 => ProgramID::<MainnetV0>::from_str(string).map(|id| (&id).into()),
 52              NetworkName::TestnetV0 => ProgramID::<TestnetV0>::from_str(string).map(|id| (&id).into()),
 53              NetworkName::CanaryV0 => ProgramID::<CanaryV0>::from_str(string).map(|id| (&id).into()),
 54          }
 55      }
 56  
 57      /// Converts the `ProgramId` into an address string.
 58      #[allow(deprecated)]
 59      pub fn to_address_string(&self, network: NetworkName) -> Result<String> {
 60          match network {
 61              // ALPHA networks
 62              NetworkName::AlphaMainnetV0 => {
 63                  ProgramID::<MainnetV0>::from_str(&self.to_string())?.to_address().map(|addr| addr.to_string())
 64              }
 65              NetworkName::AlphaTestnetV0 => {
 66                  ProgramID::<TestnetV0>::from_str(&self.to_string())?.to_address().map(|addr| addr.to_string())
 67              }
 68              NetworkName::AlphaCanaryV0 => {
 69                  ProgramID::<CanaryV0>::from_str(&self.to_string())?.to_address().map(|addr| addr.to_string())
 70              }
 71              // DELTA networks
 72              NetworkName::DeltaMainnetV0 => {
 73                  ProgramID::<MainnetV0>::from_str(&self.to_string())?.to_address().map(|addr| addr.to_string())
 74              }
 75              NetworkName::DeltaTestnetV0 => {
 76                  ProgramID::<TestnetV0>::from_str(&self.to_string())?.to_address().map(|addr| addr.to_string())
 77              }
 78              NetworkName::DeltaCanaryV0 => {
 79                  ProgramID::<CanaryV0>::from_str(&self.to_string())?.to_address().map(|addr| addr.to_string())
 80              }
 81              // Legacy Aleo networks (deprecated)
 82              NetworkName::MainnetV0 => {
 83                  ProgramID::<MainnetV0>::from_str(&self.to_string())?.to_address().map(|addr| addr.to_string())
 84              }
 85              NetworkName::TestnetV0 => {
 86                  ProgramID::<TestnetV0>::from_str(&self.to_string())?.to_address().map(|addr| addr.to_string())
 87              }
 88              NetworkName::CanaryV0 => {
 89                  ProgramID::<CanaryV0>::from_str(&self.to_string())?.to_address().map(|addr| addr.to_string())
 90              }
 91          }
 92      }
 93  }
 94  
 95  impl fmt::Display for ProgramId {
 96      fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
 97          write!(f, "{}.{}", self.name, self.network)
 98      }
 99  }
100  
101  impl<N: Network> From<&ProgramID<N>> for ProgramId {
102      fn from(program: &ProgramID<N>) -> Self {
103          Self { name: Identifier::from(program.name()), network: Identifier::from(program.network()) }
104      }
105  }
106  
107  impl From<Identifier> for ProgramId {
108      fn from(name: Identifier) -> Self {
109          Self {
110              name,
111              network: Identifier { name: Symbol::intern("alpha"), span: Default::default(), id: Default::default() },
112          }
113      }
114  }