This commit is contained in:
Pranav Gaddamadugu 2024-05-30 11:35:15 -07:00
parent 8964a194a4
commit 745d7268ae
11 changed files with 70 additions and 272 deletions

206
Cargo.lock generated
View File

@ -36,17 +36,6 @@ version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe"
[[package]]
name = "aes"
version = "0.8.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b169f7a6d4742236a0a00c541b845991d0ac43e546831af1249753ab4c3aa3a0"
dependencies = [
"cfg-if",
"cipher",
"cpufeatures",
]
[[package]]
name = "ahash"
version = "0.8.11"
@ -207,15 +196,6 @@ version = "1.0.86"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b3d1d046238990b9cf5bcde22a3fb3584ee5cf65fb2765f454ed428c7a0063da"
[[package]]
name = "arbitrary"
version = "1.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7d5a26814d8dcb93b0e5a0ff3c6d80a8843bafb21b39e8e18a6f05471870e110"
dependencies = [
"derive_arbitrary",
]
[[package]]
name = "arrayref"
version = "0.3.7"
@ -498,16 +478,6 @@ version = "1.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "514de17de45fdb8dc022b1a7975556c53c86f9f0aa5f534b98977b171857c2c9"
[[package]]
name = "bzip2"
version = "0.4.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bdb116a6ef3f6c3698828873ad02c3014b3c85cadb88496095628e3ef1e347f8"
dependencies = [
"bzip2-sys",
"libc",
]
[[package]]
name = "bzip2-sys"
version = "0.1.11+1.0.8"
@ -593,16 +563,6 @@ dependencies = [
"half",
]
[[package]]
name = "cipher"
version = "0.4.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "773f3b9af64447d2ce9850330c473515014aa235e6a783b02db81ff39e4a3dad"
dependencies = [
"crypto-common",
"inout",
]
[[package]]
name = "clang-sys"
version = "1.7.0"
@ -730,21 +690,6 @@ dependencies = [
"libc",
]
[[package]]
name = "crc"
version = "3.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "69e6e4d7b33a94f0991c26729976b10ebde1d34c3ee82408fb536164fa10d636"
dependencies = [
"crc-catalog",
]
[[package]]
name = "crc-catalog"
version = "2.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "19d374276b40fb8bbdee95aef7c7fa6b5316ec764510eb64b8dd0e2ed0d7e7f5"
[[package]]
name = "crc32fast"
version = "1.4.0"
@ -927,12 +872,6 @@ dependencies = [
"parking_lot_core",
]
[[package]]
name = "deflate64"
version = "0.1.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "83ace6c86376be0b6cdcf3fb41882e81d94b31587573d1cfa9d01cd06bba210d"
[[package]]
name = "der"
version = "0.7.9"
@ -963,17 +902,6 @@ dependencies = [
"syn 1.0.109",
]
[[package]]
name = "derive_arbitrary"
version = "1.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "67e77553c4162a157adbf834ebae5b415acbecbeafc7a74b0e886657506a7611"
dependencies = [
"proc-macro2",
"quote 1.0.36",
"syn 2.0.63",
]
[[package]]
name = "difflib"
version = "0.4.0"
@ -1032,17 +960,6 @@ dependencies = [
"windows-sys 0.48.0",
]
[[package]]
name = "displaydoc"
version = "0.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "487585f4d0c6655fe74905e2504d8ad6908e4db67f744eb140876906c2f3175d"
dependencies = [
"proc-macro2",
"quote 1.0.36",
"syn 2.0.63",
]
[[package]]
name = "doc-comment"
version = "0.3.3"
@ -1501,15 +1418,6 @@ version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70"
[[package]]
name = "hmac"
version = "0.12.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6c49c37c09c17a53d937dfbb742eb3a961d65a994e6bcdcf37e7399d0cc8ab5e"
dependencies = [
"digest",
]
[[package]]
name = "http"
version = "0.2.12"
@ -1724,15 +1632,6 @@ version = "2.0.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b248f5224d1d606005e02c97f5aa4e88eeb230488bcc03bc9ca4d7991399f2b5"
[[package]]
name = "inout"
version = "0.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a0c10553d664a4d0bcff9f4215d0aac67a639cc68ef660840afe309b807bc9f5"
dependencies = [
"generic-array",
]
[[package]]
name = "instant"
version = "0.1.12"
@ -1968,7 +1867,6 @@ dependencies = [
"tracing-subscriber",
"ureq",
"walkdir",
"zip 1.3.1",
]
[[package]]
@ -2149,12 +2047,6 @@ dependencies = [
"scopeguard",
]
[[package]]
name = "lockfree-object-pool"
version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9374ef4228402d4b7e403e5838cb880d9ee663314b0a900d5a6aabf0c213552e"
[[package]]
name = "log"
version = "0.4.21"
@ -2180,16 +2072,6 @@ dependencies = [
"libc",
]
[[package]]
name = "lzma-rs"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "297e814c836ae64db86b36cf2a557ba54368d03f6afcd7d947c266692f71115e"
dependencies = [
"byteorder",
"crc",
]
[[package]]
name = "matchers"
version = "0.1.0"
@ -2562,16 +2444,6 @@ version = "1.0.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "57c0d7b74b563b49d38dae00a0c37d4d6de9b432382b2892f0574ddcae73fd0a"
[[package]]
name = "pbkdf2"
version = "0.12.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f8ed6a7761f76e3b9f92dfb0a60a6a6477c61024b775147ff0973a02653abaf2"
dependencies = [
"digest",
"hmac",
]
[[package]]
name = "peeking_take_while"
version = "0.1.2"
@ -3302,7 +3174,7 @@ dependencies = [
"serde_json",
"tempfile",
"urlencoding",
"zip 0.6.6",
"zip",
"zipsign-api",
]
@ -3489,12 +3361,6 @@ dependencies = [
"rand_core",
]
[[package]]
name = "simd-adler32"
version = "0.3.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d66dc143e6b11c1eddc06d5c423cfc97062865baf299914ab64caa38182078fe"
[[package]]
name = "simple_asn1"
version = "0.6.2"
@ -5782,34 +5648,6 @@ dependencies = [
"time",
]
[[package]]
name = "zip"
version = "1.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1b7a5a9285bd4ee13bdeb3f8a4917eb46557e53f270c783849db8bef37b0ad00"
dependencies = [
"aes",
"arbitrary",
"bzip2",
"constant_time_eq",
"crc32fast",
"crossbeam-utils",
"deflate64",
"displaydoc",
"flate2",
"hmac",
"indexmap 2.2.6",
"lzma-rs",
"pbkdf2",
"rand",
"sha1",
"thiserror",
"time",
"zeroize",
"zopfli",
"zstd",
]
[[package]]
name = "zipsign-api"
version = "0.1.1"
@ -5819,45 +5657,3 @@ dependencies = [
"ed25519-dalek",
"thiserror",
]
[[package]]
name = "zopfli"
version = "0.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e5019f391bac5cf252e93bbcc53d039ffd62c7bfb7c150414d61369afe57e946"
dependencies = [
"bumpalo",
"crc32fast",
"lockfree-object-pool",
"log",
"once_cell",
"simd-adler32",
]
[[package]]
name = "zstd"
version = "0.13.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2d789b1514203a1120ad2429eae43a7bd32b90976a7bb8a05f7ec02fa88cc23a"
dependencies = [
"zstd-safe",
]
[[package]]
name = "zstd-safe"
version = "7.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1cd99b45c6bc03a018c8b8a86025678c87e55526064e38f9df301989dce7ec0a"
dependencies = [
"zstd-sys",
]
[[package]]
name = "zstd-sys"
version = "2.0.10+zstd.1.5.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c253a4914af5bafc8fa8c86ee400827e83cf6ec01195ec1f1ed8441bf00d65aa"
dependencies = [
"cc",
"pkg-config",
]

View File

@ -176,9 +176,6 @@ version = "0.1"
version = "0.3.18"
features = [ "fmt" ]
[dependencies.zip]
version = "^1.3"
[dependencies.crossterm]
version = "0.27.0"

View File

@ -27,7 +27,6 @@ use leo_errors::{
LeoWarning,
};
use leo_package::root::env::Env;
use leo_retriever::NetworkName;
use leo_span::source_map::FileName;
use leo_test_framework::{test::TestConfig, Test};
@ -137,16 +136,9 @@ pub fn setup_build_directory(
// Create the manifest file.
let _manifest_file = Manifest::create(&directory, &program_id).unwrap();
// Get the network name.
let network = match CurrentNetwork::ID {
MainnetV0::ID => NetworkName::MainnetV0,
TestnetV0::ID => NetworkName::TestnetV0,
_ => panic!("Invalid network"),
};
// Create the environment file.
Env::new(network).unwrap().write_to(&directory).unwrap();
if Env::exists_at(&directory) {
Env::<CurrentNetwork>::new().unwrap().write_to(&directory).unwrap();
if Env::<CurrentNetwork>::exists_at(&directory) {
println!(".env file created at {:?}", &directory);
}

View File

@ -324,9 +324,8 @@ pub(crate) fn verify_message<N: Network>(
// Write the network and private key to the .env file in project directory.
fn write_to_env_file<N: Network>(private_key: PrivateKey<N>, ctx: &Context) -> Result<()> {
let data = format!("PRIVATE_KEY={private_key}\n");
let program_dir = ctx.dir()?;
Env::from(data).write_to(&program_dir)?;
Env::<N>::from(private_key).write_to(&program_dir)?;
tracing::info!("✅ Private Key written to {}", program_dir.join(".env").display());
Ok(())
}

View File

@ -123,9 +123,7 @@ impl Command for Add {
manifest.license(),
Some(dependencies),
);
let new_manifest_data = serde_json::to_string_pretty(&new_manifest)
.map_err(|err| PackageError::failed_to_serialize_manifest_file(path.to_str().unwrap(), err))?;
std::fs::write(path.join("program.json"), new_manifest_data).map_err(PackageError::failed_to_write_manifest)?;
new_manifest.write_to_dir(path)?;
Ok(())
}

View File

@ -51,21 +51,10 @@ impl Command for New {
std::env::set_current_dir(&package_path)
.map_err(|err| PackageError::failed_to_set_cwd(package_path.display(), err))?;
// Open the program manifest.
// Note that this has the side effect of create the appropriate directories and files.
match network {
NetworkName::MainnetV0 => {
context.open_manifest::<MainnetV0>()?;
}
NetworkName::TestnetV0 => {
context.open_manifest::<TestnetV0>()?;
}
}
// Initialize the package.
match network {
NetworkName::MainnetV0 => Package::initialize(&self.name, network, &package_path),
NetworkName::TestnetV0 => Package::initialize(&self.name, network, &package_path),
NetworkName::MainnetV0 => Package::initialize::<MainnetV0>(&self.name, &package_path),
NetworkName::TestnetV0 => Package::initialize::<TestnetV0>(&self.name, &package_path),
}?;
Ok(())

View File

@ -104,9 +104,7 @@ impl Command for Remove {
manifest.license(),
Some(dependencies),
);
let new_manifest_data = serde_json::to_string_pretty(&new_manifest)
.map_err(|err| PackageError::failed_to_serialize_manifest_file(path.to_str().unwrap(), err))?;
std::fs::write(path.join("program.json"), new_manifest_data).map_err(PackageError::failed_to_write_manifest)?;
new_manifest.write_to_dir(path)?;
Ok(())
}

View File

@ -22,8 +22,9 @@ use crate::{
};
use leo_errors::{PackageError, Result};
use leo_retriever::NetworkName;
use leo_retriever::{Manifest, NetworkName};
use serde::Deserialize;
use snarkvm::prelude::Network;
use std::path::Path;
#[derive(Deserialize)]
@ -126,7 +127,7 @@ impl Package {
}
/// Creates a Leo package at the given path
pub fn initialize(package_name: &str, network: NetworkName, path: &Path) -> Result<()> {
pub fn initialize<N: Network>(package_name: &str, path: &Path) -> Result<()> {
// Verify that the .gitignore file does not exist.
if !Gitignore::exists_at(path) {
// Create the .gitignore file.
@ -134,11 +135,15 @@ impl Package {
}
// Verify that the .env file does not exist.
if !Env::exists_at(path) {
if !Env::<N>::exists_at(path) {
// Create the .env file.
Env::new(network)?.write_to(path)?;
Env::<N>::new()?.write_to(path)?;
}
// Create a manifest.
let manifest = Manifest::default(package_name);
manifest.write_to_dir(path.to_path_buf())?;
// Create the source directory.
SourceDirectory::create(path)?;
@ -158,11 +163,6 @@ impl Package {
Ok(())
}
//
// /// Removes the package at the given path
// pub fn remove_imported_package(package_name: &str, path: &Path) -> Result<()> {
// ImportsDirectory::remove_import(path, package_name)
// }
}
#[cfg(test)]

View File

@ -16,28 +16,31 @@
//! The `.env` file.
use leo_errors::{PackageError, Result};
use leo_retriever::NetworkName;
use snarkvm::console::account::PrivateKey;
use leo_retriever::NetworkName;
use snarkvm::prelude::{MainnetV0, Network, TestnetV0};
use serde::Deserialize;
use snarkvm::prelude::{MainnetV0, TestnetV0};
use std::{borrow::Cow, fs::File, io::Write, path::Path};
pub static ENV_FILENAME: &str = ".env";
// TODO: Should this be generic over network?
#[derive(Deserialize, Default)]
pub struct Env {
data: String,
#[derive(Deserialize)]
pub struct Env<N: Network> {
#[serde(bound(deserialize = ""))]
private_key: PrivateKey<N>,
}
impl Env {
pub fn new(network: NetworkName) -> Result<Self> {
Ok(Self { data: Self::template(network)? })
}
impl<N: Network> Env<N> {
pub fn new() -> Result<Self> {
// Initialize an RNG.
let rng = &mut rand::thread_rng();
pub fn from(data: String) -> Self {
Self { data }
// Generate a development private key.
let private_key = PrivateKey::<N>::new(rng)?;
Ok(Self { private_key })
}
pub fn exists_at(path: &Path) -> bool {
@ -55,20 +58,26 @@ impl Env {
}
let mut file = File::create(&path).map_err(PackageError::io_error_env_file)?;
file.write_all(self.data.as_bytes()).map_err(PackageError::io_error_env_file)?;
file.write_all(self.to_string().as_bytes()).map_err(PackageError::io_error_env_file)?;
Ok(())
}
}
fn template(network: NetworkName) -> Result<String> {
// Initialize an RNG.
let rng = &mut rand::thread_rng();
// Initialize a new development private key.
let private_key = match network {
NetworkName::MainnetV0 => PrivateKey::<MainnetV0>::new(rng)?.to_string(),
NetworkName::TestnetV0 => PrivateKey::<TestnetV0>::new(rng)?.to_string(),
};
Ok(format!("NETWORK={network}\nPRIVATE_KEY={private_key}\n"))
impl<N: Network> From<PrivateKey<N>> for Env<N> {
fn from(private_key: PrivateKey<N>) -> Self {
Self { private_key }
}
}
impl<N: Network> ToString for Env<N> {
fn to_string(&self) -> String {
// Get the network name.
let network = match N::ID {
MainnetV0::ID => NetworkName::MainnetV0,
TestnetV0::ID => NetworkName::TestnetV0,
_ => unimplemented!("Unsupported network"),
};
// Return the formatted string.
format!("NETWORK={network}\nPRIVATE_KEY={}\n", self.private_key)
}
}

View File

@ -15,7 +15,9 @@
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
use crate::Dependency;
use leo_errors::PackageError;
use serde::{Deserialize, Serialize};
use std::path::PathBuf;
// Struct representation of program's `program.json` specification
#[derive(Debug, Clone, Serialize, Deserialize)]
@ -44,6 +46,16 @@ impl Manifest {
}
}
pub fn default(program: &str) -> Self {
Self {
program: program.to_owned(),
version: "0.1.0".to_owned(),
description: "".to_owned(),
license: "MIT".to_owned(),
dependencies: None,
}
}
pub fn program(&self) -> &String {
&self.program
}
@ -63,4 +75,12 @@ impl Manifest {
pub fn dependencies(&self) -> &Option<Vec<Dependency>> {
&self.dependencies
}
pub fn write_to_dir(&self, path: PathBuf) -> Result<(), PackageError> {
// Serialize the manifest to a JSON string.
let contents = serde_json::to_string_pretty(&self)
.map_err(|err| PackageError::failed_to_serialize_manifest_file(path.to_str().unwrap(), err))?;
// Write the manifest to the file.
std::fs::write(path.join("program.json"), contents).map_err(PackageError::failed_to_write_manifest)
}
}