leo_ast/program/
program_id.rs

1// Copyright (C) 2019-2025 Provable Inc.
2// This file is part of the Leo library.
3
4// The Leo 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 Leo 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 Leo library. If not, see <https://www.gnu.org/licenses/>.
16
17use crate::{Identifier, NetworkName};
18
19use core::fmt;
20use leo_span::Symbol;
21use serde::{Deserialize, Serialize};
22use snarkvm::{
23    console::program::ProgramID,
24    prelude::{CanaryV0, MainnetV0, Network, Result, TestnetV0},
25};
26use 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)]
30pub 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
37impl ProgramId {
38    /// Initializes a new `ProgramId` from a string, using a network parameter to validate using snarkVM.
39    pub fn from_str_with_network(string: &str, network: NetworkName) -> Result<Self> {
40        match network {
41            NetworkName::MainnetV0 => ProgramID::<MainnetV0>::from_str(string).map(|id| (&id).into()),
42            NetworkName::TestnetV0 => ProgramID::<TestnetV0>::from_str(string).map(|id| (&id).into()),
43            NetworkName::CanaryV0 => ProgramID::<CanaryV0>::from_str(string).map(|id| (&id).into()),
44        }
45    }
46
47    /// Converts the `ProgramId` into an address string.
48    pub fn to_address_string(&self, network: NetworkName) -> Result<String> {
49        match network {
50            NetworkName::MainnetV0 => {
51                ProgramID::<MainnetV0>::from_str(&self.to_string())?.to_address().map(|addr| addr.to_string())
52            }
53            NetworkName::TestnetV0 => {
54                ProgramID::<TestnetV0>::from_str(&self.to_string())?.to_address().map(|addr| addr.to_string())
55            }
56            NetworkName::CanaryV0 => {
57                ProgramID::<CanaryV0>::from_str(&self.to_string())?.to_address().map(|addr| addr.to_string())
58            }
59        }
60    }
61}
62
63impl fmt::Display for ProgramId {
64    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
65        write!(f, "{}.{}", self.name, self.network)
66    }
67}
68
69impl<N: Network> From<&ProgramID<N>> for ProgramId {
70    fn from(program: &ProgramID<N>) -> Self {
71        Self { name: Identifier::from(program.name()), network: Identifier::from(program.network()) }
72    }
73}
74
75impl From<Identifier> for ProgramId {
76    fn from(name: Identifier) -> Self {
77        Self {
78            name,
79            network: Identifier { name: Symbol::intern("aleo"), span: Default::default(), id: Default::default() },
80        }
81    }
82}