Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into release/2.4.0
Browse files Browse the repository at this point in the history
  • Loading branch information
cwkang1998 committed Jul 18, 2024
2 parents 6aec475 + 91c9ad7 commit 6c42166
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 61 deletions.
115 changes: 67 additions & 48 deletions crates/cli/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ mod utils;
mod validation;
mod verify;

use crate::api::{does_class_exist, Network};
use crate::license::LicenseType;
use crate::utils::detect_local_tools;
use camino::Utf8PathBuf;
Expand All @@ -13,7 +14,7 @@ use dialoguer::{theme::ColorfulTheme, Confirm, Input, Select};
use dirs::home_dir;
use indicatif::{HumanDuration, ProgressStyle};
use regex::Regex;
use std::{env, time::Instant};
use std::{env, str::FromStr, time::Instant};
use strum::IntoEnumIterator;
use validation::is_class_hash_valid;
use verify::VerifyProjectArgs;
Expand Down Expand Up @@ -111,53 +112,6 @@ fn main() -> anyhow::Result<()> {
style("[3/4]").bold().dim(),
Emoji("🔍 ", "")
);
let re = Regex::new(r"^0x[a-fA-F0-9]{64}$").unwrap();
let class_hash: String = Input::with_theme(&ColorfulTheme::default())
.with_prompt("Input class hash to verify : ")
.validate_with(|input: &String| -> Result<(), &str> {
if is_class_hash_valid(input) {
Ok(())
} else {
Err("This is not a class hash")
}
})
.interact()?;

// Get name that you want to use for the contract
let class_name: String = Input::with_theme(&ColorfulTheme::default())
.with_prompt("Enter your desired class name: ")
.validate_with(|input: &String| -> Result<(), &str> {
if input.len() > 50 {
Err("Given name is too long")
} else {
Ok(())
}
})
.interact_text()
.expect("Aborted at class name input, terminating...")
.trim()
.to_string();

// Check if account contract
// TODO: Is there a way to detect this automatically?
// println!(
// "{} {} Checking if account contract...",
// style("[x/x]").bold().dim(),
// Emoji("📃 ", "")
// );
let is_account_contract: bool = Confirm::with_theme(&ColorfulTheme::default())
.with_prompt("Is this an Account Class?")
.interact()?;

// Set license for your contract code
let licenses: Vec<LicenseType> = LicenseType::iter().collect();
let license_index = Select::with_theme(&ColorfulTheme::default())
.with_prompt("Select license you'd like to verify under :")
.items(&licenses)
.default(0)
.interact_opt()
.expect("Aborted at license version selection, terminating...")
.expect("Aborted at license version selection, terminating...");

// -- Network selection --

Expand Down Expand Up @@ -203,6 +157,71 @@ fn main() -> anyhow::Result<()> {
"custom"
};

let network_enum = Network::from_str(selected_network)?;
let mut class_hash: String;
loop {
class_hash = Input::with_theme(&ColorfulTheme::default())
.with_prompt("Input class hash to verify : ")
.validate_with(|input: &String| -> Result<(), &str> {
if is_class_hash_valid(input) {
Ok(())
} else {
Err("This is not a class hash.")
}
})
.interact()?;

// Check if the class exists on the network
match does_class_exist(network_enum.clone(), &class_hash) {
Ok(true) => break,
Ok(false) => {
println!("This class hash does not exist for the given network. Please try again.")
}
Err(e) => {
return Err(anyhow::anyhow!(
"Error while checking if class exists: {}",
e
))
}
}
}

// Get name that you want to use for the contract
let class_name: String = Input::with_theme(&ColorfulTheme::default())
.with_prompt("Enter your desired class name: ")
.validate_with(|input: &String| -> Result<(), &str> {
if input.len() > 50 {
Err("Given name is too long")
} else {
Ok(())
}
})
.interact_text()
.expect("Aborted at class name input, terminating...")
.trim()
.to_string();

// Check if account contract
// TODO: Is there a way to detect this automatically?
// println!(
// "{} {} Checking if account contract...",
// style("[x/x]").bold().dim(),
// Emoji("📃 ", "")
// );
let is_account_contract: bool = Confirm::with_theme(&ColorfulTheme::default())
.with_prompt("Is this an Account Class?")
.interact()?;

// Set license for your contract code
let licenses: Vec<LicenseType> = LicenseType::iter().collect();
let license_index = Select::with_theme(&ColorfulTheme::default())
.with_prompt("Select license you'd like to verify under :")
.items(&licenses)
.default(0)
.interact_opt()
.expect("Aborted at license version selection, terminating...")
.expect("Aborted at license version selection, terminating...");

let verification_start = Instant::now();
println!(
"{} {} Verifying project...",
Expand Down
15 changes: 2 additions & 13 deletions crates/cli/src/verify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ use dyn_compiler::dyn_compiler::SupportedCairoVersions;

use crate::{
api::{
dispatch_class_verification_job, does_class_exist, poll_verification_status, FileInfo,
Network, ProjectMetadataInfo,
dispatch_class_verification_job, poll_verification_status, FileInfo, Network,
ProjectMetadataInfo,
},
license::LicenseType,
resolver::get_dynamic_compiler,
Expand Down Expand Up @@ -56,18 +56,7 @@ pub fn verify_project(
metadata: ProjectMetadataInfo,
files: Vec<FileInfo>,
) -> Result<()> {
// Check if the class exists on the network
let network_enum = Network::from_str(args.network.as_str())?;
match does_class_exist(network_enum.clone(), &args.hash) {
Ok(true) => (),
Ok(false) => return Err(anyhow::anyhow!("Class does not exist on the network")),
Err(e) => {
return Err(anyhow::anyhow!(
"Error while checking if class exists: {}",
e
))
}
}

let dispatch_response = dispatch_class_verification_job(
args.api_key.as_str(),
Expand Down

0 comments on commit 6c42166

Please sign in to comment.