|
1 | 1 | use anyhow::{Context, Result, anyhow};
|
2 |
| -use anyhow::{Ok, bail}; |
3 | 2 | use camino::Utf8PathBuf;
|
4 |
| -use clap::{Args, ValueEnum}; |
5 |
| -use promptly::prompt; |
6 | 3 | use reqwest::StatusCode;
|
7 |
| -use scarb_api::StarknetContractArtifacts; |
8 |
| -use serde::Serialize; |
9 | 4 | use sncast::Network;
|
10 | 5 | use sncast::response::structs::VerifyResponse;
|
11 | 6 | use starknet_types_core::felt::Felt;
|
12 |
| -use std::collections::HashMap; |
| 7 | +use std::env; |
13 | 8 | use std::ffi::OsStr;
|
14 |
| -use std::{env, fmt}; |
15 | 9 | use walkdir::WalkDir;
|
16 | 10 |
|
17 |
| -struct WalnutVerificationInterface { |
| 11 | +use super::explorer::{VerificationInterface, VerificationPayload}; |
| 12 | + |
| 13 | +pub struct WalnutVerificationInterface { |
18 | 14 | network: Network,
|
19 | 15 | workspace_dir: Utf8PathBuf,
|
20 | 16 | }
|
21 | 17 |
|
22 |
| -#[async_trait::async_trait] |
23 |
| -trait VerificationInterface { |
24 |
| - fn new(network: Network, workspace_dir: Utf8PathBuf) -> Self; |
25 |
| - async fn verify(&self, contract_address: Felt, contract_name: String) |
26 |
| - -> Result<VerifyResponse>; |
27 |
| - fn gen_explorer_url(&self) -> Result<String>; |
28 |
| -} |
29 |
| - |
30 | 18 | #[async_trait::async_trait]
|
31 | 19 | impl VerificationInterface for WalnutVerificationInterface {
|
32 | 20 | fn new(network: Network, workspace_dir: Utf8PathBuf) -> Self {
|
@@ -109,90 +97,3 @@ impl VerificationInterface for WalnutVerificationInterface {
|
109 | 97 | Ok(format!("{api_base_url}{path}"))
|
110 | 98 | }
|
111 | 99 | }
|
112 |
| - |
113 |
| -#[derive(Args)] |
114 |
| -#[command(about = "Verify a contract through a block explorer")] |
115 |
| -pub struct Verify { |
116 |
| - /// Address of a contract to be verified |
117 |
| - #[clap(short = 'd', long)] |
118 |
| - pub contract_address: Felt, |
119 |
| - |
120 |
| - /// Name of the contract that is being verified |
121 |
| - #[clap(short, long)] |
122 |
| - pub contract_name: String, |
123 |
| - |
124 |
| - /// Block explorer to use for the verification |
125 |
| - #[clap(short, long, value_enum, default_value_t = Verifier::Walnut)] |
126 |
| - pub verifier: Verifier, |
127 |
| - |
128 |
| - /// The network on which block explorer will do the verification |
129 |
| - #[clap(short, long, value_enum)] |
130 |
| - pub network: Network, |
131 |
| - |
132 |
| - /// Assume "yes" as answer to confirmation prompt and run non-interactively |
133 |
| - #[clap(long, default_value = "false")] |
134 |
| - pub confirm_verification: bool, |
135 |
| - |
136 |
| - /// Specifies scarb package to be used |
137 |
| - #[clap(long)] |
138 |
| - pub package: Option<String>, |
139 |
| -} |
140 |
| - |
141 |
| -#[derive(ValueEnum, Clone, Debug)] |
142 |
| -pub enum Verifier { |
143 |
| - Walnut, |
144 |
| -} |
145 |
| - |
146 |
| -impl fmt::Display for Verifier { |
147 |
| - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
148 |
| - match *self { |
149 |
| - Verifier::Walnut => write!(f, "walnut"), |
150 |
| - } |
151 |
| - } |
152 |
| -} |
153 |
| - |
154 |
| -#[derive(Serialize, Debug)] |
155 |
| -struct VerificationPayload { |
156 |
| - contract_name: String, |
157 |
| - contract_address: String, |
158 |
| - source_code: serde_json::Value, |
159 |
| -} |
160 |
| - |
161 |
| -pub async fn verify( |
162 |
| - contract_address: Felt, |
163 |
| - contract_name: String, |
164 |
| - verifier: Verifier, |
165 |
| - network: Network, |
166 |
| - confirm_verification: bool, |
167 |
| - manifest_path: &Utf8PathBuf, |
168 |
| - artifacts: &HashMap<String, StarknetContractArtifacts>, |
169 |
| -) -> Result<VerifyResponse> { |
170 |
| - // Let's ask confirmation |
171 |
| - if !confirm_verification { |
172 |
| - let prompt_text = format!( |
173 |
| - "\n\tYou are about to submit the entire workspace code to the third-party verifier at {verifier}.\n\n\tImportant: Make sure your project does not include sensitive information like private keys. The snfoundry.toml file will be uploaded. Keep the keystore outside the project to prevent it from being uploaded.\n\n\tAre you sure you want to proceed? (Y/n)" |
174 |
| - ); |
175 |
| - let input: String = prompt(prompt_text)?; |
176 |
| - |
177 |
| - if !input.starts_with('Y') { |
178 |
| - bail!("Verification aborted"); |
179 |
| - } |
180 |
| - } |
181 |
| - |
182 |
| - if !artifacts.contains_key(&contract_name) { |
183 |
| - return Err(anyhow!("Contract named '{contract_name}' was not found")); |
184 |
| - } |
185 |
| - |
186 |
| - // Build JSON Payload for the verification request |
187 |
| - // get the parent dir of the manifest path |
188 |
| - let workspace_dir = manifest_path |
189 |
| - .parent() |
190 |
| - .ok_or(anyhow!("Failed to obtain workspace dir"))?; |
191 |
| - |
192 |
| - match verifier { |
193 |
| - Verifier::Walnut => { |
194 |
| - let walnut = WalnutVerificationInterface::new(network, workspace_dir.to_path_buf()); |
195 |
| - walnut.verify(contract_address, contract_name).await |
196 |
| - } |
197 |
| - } |
198 |
| -} |
0 commit comments