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 3 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
129 changes: 74 additions & 55 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 @@ -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");
cwkang1998 marked this conversation as resolved.
Show resolved Hide resolved
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...",
Expand All @@ -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())
Expand Down Expand Up @@ -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...",
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