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

Add voyager verification to the verify command #2566

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 17 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
1 change: 1 addition & 0 deletions CHANGELOG.md
cwkang1998 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- You can skip `--name` flag when using `account import` - a default name will be generated.
- Addresses outputted when calling `sncast account create`, `sncast deploy` and `sncast declare` are now padded to 64 characters length and prefixed with `0x0`
- Voyager API support for sncast `verify` subcommand

#### Changed

Expand Down
4 changes: 3 additions & 1 deletion crates/sncast/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -623,11 +623,13 @@ async fn run_async_command(
)
.expect("Failed to build contract");
let result = starknet_commands::verify::verify(
verify.contract_address,
verify.contract_address_or_class_hash.contract_address,
verify.contract_address_or_class_hash.class_hash,
verify.contract_name,
verify.verifier,
verify.network,
verify.confirm_verification,
verify.custom_base_api_url,
&package_metadata.manifest_path,
&artifacts,
)
Expand Down
197 changes: 0 additions & 197 deletions crates/sncast/src/starknet_commands/verify.rs

This file was deleted.

95 changes: 95 additions & 0 deletions crates/sncast/src/starknet_commands/verify/base.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
use anyhow::{anyhow, Context, Result};
use async_trait::async_trait;
use camino::Utf8PathBuf;
use reqwest::StatusCode;
use serde::Serialize;
use sncast::response::structs::VerifyResponse;
use starknet::core::types::Felt;
use std::ffi::OsStr;
use walkdir::WalkDir;

fn read_workspace_files(
workspace_dir: Utf8PathBuf,
) -> Result<serde_json::Map<String, serde_json::Value>> {
// Read all files name along with their contents in a JSON format
// in the workspace dir recursively
// key is the file name and value is the file content
let mut file_data = serde_json::Map::new();

// Recursively read files and their contents in workspace directory
for entry in WalkDir::new(workspace_dir.clone()).follow_links(true) {
let entry = entry?;
let path = entry.path();
if path.is_file() {
if let Some(extension) = path.extension() {
if extension == OsStr::new("cairo") || extension == OsStr::new("toml") {
let relative_path = path.strip_prefix(workspace_dir.clone())?;
let file_content = std::fs::read_to_string(path)?;
file_data.insert(
relative_path.to_string_lossy().into_owned(),
serde_json::Value::String(file_content),
);
}
}
}
}
Ok(file_data)
}

async fn send_verification_request(
url: String,
payload: VerificationPayload,
) -> Result<VerifyResponse> {
let json_payload = serde_json::to_string(&payload)?;
let client = reqwest::Client::new();
let api_res = client
.post(url)
.header("Content-Type", "application/json")
.body(json_payload)
.send()
.await
.context("Failed to send request to verifier API")?;

if api_res.status() == StatusCode::OK {
let message = api_res
.text()
.await
.context("Failed to read verifier API response")?;
Ok(VerifyResponse { message })
} else {
let message = api_res.text().await.context("Failed to verify contract")?;
Err(anyhow!(message))
}
}

#[async_trait]
pub trait VerificationInterface {
fn explorer_url(&self) -> String;

async fn verify(
&self,
workspace_dir: Utf8PathBuf,
contract_address: Option<Felt>,
class_hash: Option<Felt>,
contract_name: String,
) -> Result<VerifyResponse> {
let file_data = read_workspace_files(workspace_dir)?;
let source_code = serde_json::Value::Object(file_data);
let payload = VerificationPayload {
contract_name,
contract_address,
class_hash,
source_code,
};
let url = self.explorer_url();
send_verification_request(url, payload).await
}
}

#[derive(Serialize, Debug)]
pub struct VerificationPayload {
pub contract_name: String,
pub contract_address: Option<Felt>,
pub class_hash: Option<Felt>,
pub source_code: serde_json::Value,
}
Loading
Loading