leo_package/
lib.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
17//! This crate deals with Leo packages on the file system and network.
18//!
19//! The main type is `Package`, which deals with Leo packages on the local filesystem.
20//! A Leo package directory is intended to have a structure like this:
21//! .
22//! ├── .env
23//! ├── program.json
24//! ├── build
25//! │   ├── imports
26//! │   │   └── credits.aleo
27//! │   └── main.aleo
28//! ├── outputs
29//! │   ├── program.TypeChecking.ast
30//! │   └── program.TypeChecking.json
31//! ├── src
32//! │   └── main.leo
33//! └── tests
34//!     └── test_something.leo
35//!
36//! The file `program.json` is a manifest containing the program name, version, description,
37//! and license, together with information about its dependencies.
38//!
39//! The file `.env` contains definitions for the environment variables NETWORK, PRIVATE_KEY,
40//! and ENDPOINT that may be used when deploying or executing the program.
41//!
42//! Such a directory structure, together with a `.gitignore` file, may be created
43//! on the file system using `Package::initialize`.
44//! ```no_run
45//! # use leo_package::{NetworkName, Package};
46//! let path = Package::initialize("my_package", "path/to/parent", NetworkName::TestnetV0, "http://localhost:3030").unwrap();
47//! ```
48//!
49//! `tests` is where unit test files may be placed.
50//!
51//! Given an existing directory with such a structure, a `Package` may be created from it with
52//! `Package::from_directory`:
53//! ```no_run
54//! # use leo_package::Package;
55//! let package = Package::from_directory("path/to/package", "/home/me/.aleo", false).unwrap();
56//! ```
57//! This will read the manifest and env file and keep their data in `package.manifest` and `package.env`.
58//! It will also process dependencies and store them in topological order in `package.programs`. This processing
59//! will involve fetching bytecode from the network for network dependencies.
60//! If the `no_cache` option (3rd parameter) is set to `true`, the package will not use the dependency cache.
61//!
62//! If you want to simply read the manifest and env file without processing dependencies, use
63//! `Package::from_directory_no_graph`.
64//!
65//! `Program` generally doesn't need to be created directly, as `Package` will create `Program`s
66//! for the main program and all dependencies. However, if you'd like to fetch bytecode for
67//! a program, you can use `Program::fetch`.
68
69#![forbid(unsafe_code)]
70
71use leo_errors::{PackageError, Result, UtilError};
72use leo_span::Symbol;
73
74use std::path::Path;
75
76mod dependency;
77pub use dependency::*;
78
79mod env;
80pub use env::*;
81
82mod location;
83pub use location::*;
84
85mod manifest;
86pub use manifest::*;
87
88mod network_name;
89pub use network_name::*;
90
91mod package;
92pub use package::*;
93
94mod program;
95pub use program::*;
96
97pub const SOURCE_DIRECTORY: &str = "src";
98
99pub const MAIN_FILENAME: &str = "main.leo";
100
101pub const IMPORTS_DIRECTORY: &str = "build/imports";
102
103pub const OUTPUTS_DIRECTORY: &str = "outputs";
104
105pub const BUILD_DIRECTORY: &str = "build";
106
107pub const TESTS_DIRECTORY: &str = "tests";
108
109pub const TEST_PRIVATE_KEY: &str = "APrivateKey1zkp8CZNn3yeCseEtxuVPbDCwSyhGW6yZKUYKfgXmcpoGPWH";
110
111fn symbol(name: &str) -> Result<Symbol> {
112    name.strip_suffix(".aleo").map(Symbol::intern).ok_or_else(|| PackageError::invalid_network_name(name).into())
113}
114
115/// Is this a valid name for an Aleo program?
116///
117/// Namely, it must be of the format "xxx.aleo" where `xxx` is nonempty,
118/// consist solely of ASCII alphanumeric characters and underscore, and
119/// begin with a letter.
120pub fn is_valid_aleo_name(name: &str) -> bool {
121    let Some(rest) = name.strip_suffix(".aleo") else {
122        return false;
123    };
124
125    // Check that the name is nonempty.
126    if rest.is_empty() {
127        tracing::error!("Aleo names must be nonempty");
128        return false;
129    }
130
131    let first = rest.chars().next().unwrap();
132
133    // Check that the first character is not an underscore.
134    if first == '_' {
135        tracing::error!("Aleo names cannot begin with an underscore");
136        return false;
137    }
138
139    // Check that the first character is not a number.
140    if first.is_numeric() {
141        tracing::error!("Aleo names cannot begin with a number");
142        return false;
143    }
144
145    // Iterate and check that the name is valid.
146    if rest.chars().any(|c| !c.is_ascii_alphanumeric() && c != '_') {
147        tracing::error!("Aleo names must can only contain ASCII alphanumeric characters and underscores.");
148        return false;
149    }
150
151    true
152}
153
154// Fetch the given endpoint url and return the sanitized response.
155pub fn fetch_from_network(url: &str) -> Result<String, UtilError> {
156    fetch_from_network_plain(url).map(|s| s.replace("\\n", "\n").replace('\"', ""))
157}
158
159pub fn fetch_from_network_plain(url: &str) -> Result<String, UtilError> {
160    let response = ureq::AgentBuilder::new()
161        .redirects(0)
162        .build()
163        .get(url)
164        .set("X-Leo-Version", env!("CARGO_PKG_VERSION"))
165        .call()
166        .map_err(UtilError::failed_to_retrieve_from_endpoint)?;
167    match response.status() {
168        200..=299 => Ok(response.into_string().unwrap()),
169        301 => Err(UtilError::endpoint_moved_error(url)),
170        _ => Err(UtilError::network_error(url, response.status())),
171    }
172}
173
174/// Fetch the given program from the network and return the program as a string.
175pub fn fetch_program_from_network(name: &str, endpoint: &str, network: NetworkName) -> Result<String, UtilError> {
176    let url = format!("{endpoint}/{network}/program/{name}");
177    let program = fetch_from_network(&url)?;
178    Ok(program)
179}
180
181// Verify that a fetched program is valid aleo instructions.
182pub fn verify_valid_program(name: &str, program: &str) -> Result<(), UtilError> {
183    use snarkvm::prelude::{Program, TestnetV0};
184    use std::str::FromStr as _;
185    match Program::<TestnetV0>::from_str(program) {
186        Ok(_) => Ok(()),
187        Err(_) => Err(UtilError::snarkvm_parsing_error(name)),
188    }
189}
190
191pub fn filename_no_leo_extension(path: &Path) -> Option<&str> {
192    filename_no_extension(path, ".leo")
193}
194
195pub fn filename_no_aleo_extension(path: &Path) -> Option<&str> {
196    filename_no_extension(path, ".aleo")
197}
198
199fn filename_no_extension<'a>(path: &'a Path, extension: &'static str) -> Option<&'a str> {
200    path.file_name().and_then(|os_str| os_str.to_str()).and_then(|s| s.strip_suffix(extension))
201}