From b5434521ca333008be7bea92e788afb5f6e6b32c Mon Sep 17 00:00:00 2001 From: liam Date: Mon, 15 Jul 2024 11:14:35 -0400 Subject: [PATCH 1/4] fix: check class hash --- crates/cli/src/cli.rs | 129 ++++++++++++++++++++++----------------- crates/cli/src/verify.rs | 11 ---- 2 files changed, 74 insertions(+), 66 deletions(-) diff --git a/crates/cli/src/cli.rs b/crates/cli/src/cli.rs index a6e9b4d..5afd2b9 100644 --- a/crates/cli/src/cli.rs +++ b/crates/cli/src/cli.rs @@ -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; @@ -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; @@ -95,6 +96,50 @@ fn main() -> anyhow::Result<()> { } }; + // -- Network selection -- + + // Custom network selection + let custom_internal_api_endpoint_url = env::var("CUSTOM_INTERNAL_API_ENDPOINT_URL"); + let custom_public_api_endpoint_url = env::var("CUSTOM_PUBLIC_API_ENDPOINT_URL"); + let is_custom_network = + custom_internal_api_endpoint_url.is_ok() && custom_public_api_endpoint_url.is_ok(); + + // Only show local if debug network option is up. + let is_debug_network = env::var("DEBUG_NETWORK").is_ok(); + let network_items = if is_debug_network { + vec!["Mainnet", "Sepolia", "Integration", "Local"] + } else { + vec!["Mainnet", "Sepolia"] + }; + + // defaults to the first item. + let selected_network = if !is_custom_network { + let network_index = Select::with_theme(&ColorfulTheme::default()) + .items(&network_items) + .with_prompt("Which network would you like to verify on : ") + .default(0) + .interact_opt() + .expect("Aborted at network selection, terminating...") + .expect("Aborted at network selection, terminating..."); + + network_items[network_index] + } else { + println!( + "🔔 {}", + style("Custom verification endpoint provided:").bold() + ); + println!( + "Internal endpoint url: {}", + custom_internal_api_endpoint_url.unwrap_or("".to_string()) + ); + println!( + "Public endpoint url: {}", + custom_public_api_endpoint_url.unwrap_or("".to_string()) + ); + + "custom" + }; + // TODO: try to calculate the class hash automatically later after contract selection? // println!( // "{} {} Calculating class hash...", @@ -106,17 +151,35 @@ 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") + + 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.") } - }) - .interact()?; + 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()) @@ -154,50 +217,6 @@ fn main() -> anyhow::Result<()> { .expect("Aborted at license version selection, terminating...") .expect("Aborted at license version selection, terminating..."); - // -- Network selection -- - - // Custom network selection - let custom_internal_api_endpoint_url = env::var("CUSTOM_INTERNAL_API_ENDPOINT_URL"); - let custom_public_api_endpoint_url = env::var("CUSTOM_PUBLIC_API_ENDPOINT_URL"); - let is_custom_network = - custom_internal_api_endpoint_url.is_ok() && custom_public_api_endpoint_url.is_ok(); - - // Only show local if debug network option is up. - let is_debug_network = env::var("DEBUG_NETWORK").is_ok(); - let network_items = if is_debug_network { - vec!["Mainnet", "Sepolia", "Integration", "Local"] - } else { - vec!["Mainnet", "Sepolia"] - }; - - // defaults to the first item. - let selected_network = if !is_custom_network { - let network_index = Select::with_theme(&ColorfulTheme::default()) - .items(&network_items) - .with_prompt("Which network would you like to verify on : ") - .default(0) - .interact_opt() - .expect("Aborted at network selection, terminating...") - .expect("Aborted at network selection, terminating..."); - - network_items[network_index] - } else { - println!( - "🔔 {}", - style("Custom verification endpoint provided:").bold() - ); - println!( - "Internal endpoint url: {}", - custom_internal_api_endpoint_url.unwrap_or("".to_string()) - ); - println!( - "Public endpoint url: {}", - custom_public_api_endpoint_url.unwrap_or("".to_string()) - ); - - "custom" - }; - let verification_start = Instant::now(); println!( "{} {} Verifying project...", diff --git a/crates/cli/src/verify.rs b/crates/cli/src/verify.rs index ce7d3b5..e40d4e5 100644 --- a/crates/cli/src/verify.rs +++ b/crates/cli/src/verify.rs @@ -56,18 +56,7 @@ pub fn verify_project( metadata: ProjectMetadataInfo, files: Vec, ) -> 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(), From e43cccca896857cac7200d867ec27c8168d6c5e2 Mon Sep 17 00:00:00 2001 From: liam Date: Mon, 15 Jul 2024 12:57:11 -0400 Subject: [PATCH 2/4] unneeded imports --- crates/cli/src/verify.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/cli/src/verify.rs b/crates/cli/src/verify.rs index e40d4e5..c7fd3f3 100644 --- a/crates/cli/src/verify.rs +++ b/crates/cli/src/verify.rs @@ -8,7 +8,7 @@ use dyn_compiler::dyn_compiler::SupportedCairoVersions; use crate::{ api::{ - dispatch_class_verification_job, does_class_exist, poll_verification_status, FileInfo, + dispatch_class_verification_job, poll_verification_status, FileInfo, Network, ProjectMetadataInfo, }, license::LicenseType, From f54655e0f45893e5a3ac4e0794fa59890d8bcace Mon Sep 17 00:00:00 2001 From: liam Date: Mon, 15 Jul 2024 12:57:34 -0400 Subject: [PATCH 3/4] fmt --- crates/cli/src/verify.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/cli/src/verify.rs b/crates/cli/src/verify.rs index c7fd3f3..4ad2373 100644 --- a/crates/cli/src/verify.rs +++ b/crates/cli/src/verify.rs @@ -8,8 +8,8 @@ use dyn_compiler::dyn_compiler::SupportedCairoVersions; use crate::{ api::{ - dispatch_class_verification_job, poll_verification_status, FileInfo, - Network, ProjectMetadataInfo, + dispatch_class_verification_job, poll_verification_status, FileInfo, Network, + ProjectMetadataInfo, }, license::LicenseType, resolver::get_dynamic_compiler, From 531a480637f13c760a675b11eb6cd6ce4752ab3c Mon Sep 17 00:00:00 2001 From: liam Date: Thu, 18 Jul 2024 07:47:07 -0400 Subject: [PATCH 4/4] clean --- crates/cli/src/cli.rs | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/crates/cli/src/cli.rs b/crates/cli/src/cli.rs index 5afd2b9..933e676 100644 --- a/crates/cli/src/cli.rs +++ b/crates/cli/src/cli.rs @@ -96,6 +96,18 @@ fn main() -> anyhow::Result<()> { } }; + // TODO: try to calculate the class hash automatically later after contract selection? + // println!( + // "{} {} Calculating class hash...", + // style("[x/x]").bold().dim(), + // Emoji("🔍 ", "") + // ); + println!( + "{} {} Getting verification information...", + style("[3/4]").bold().dim(), + Emoji("🔍 ", "") + ); + // -- Network selection -- // Custom network selection @@ -140,18 +152,6 @@ fn main() -> anyhow::Result<()> { "custom" }; - // TODO: try to calculate the class hash automatically later after contract selection? - // println!( - // "{} {} Calculating class hash...", - // style("[x/x]").bold().dim(), - // Emoji("🔍 ", "") - // ); - println!( - "{} {} Getting verification information...", - style("[3/4]").bold().dim(), - Emoji("🔍 ", "") - ); - let network_enum = Network::from_str(selected_network)?; let mut class_hash: String; loop {