Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: check if class hash exists for given network #49

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -106,53 +107,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 @@ -198,6 +152,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
Loading