1use crate::{
4 RetryArgs,
5 etherscan::EtherscanVerificationProvider,
6 provider::{
7 ExternalVerificationContext, VerificationContext, VerificationProvider,
8 VerificationProviderType,
9 },
10 sourcify::SourcifyVerificationProvider,
11 utils::wrap_verifier_url_error,
12};
13use alloy_primitives::{Address, TxHash, map::HashSet};
14use alloy_provider::Provider;
15use clap::{Parser, ValueEnum, ValueHint};
16use eyre::{Context, Result};
17use foundry_cli::{
18 opts::{EtherscanOpts, RpcOpts},
19 utils::{self, LoadConfig},
20};
21use foundry_common::{ContractsByArtifact, compile::ProjectCompiler};
22use foundry_compilers::{artifacts::EvmVersion, info::ContractInfo};
23use foundry_config::{
24 Chain, Config, NamedChain,
25 figment::{
26 Error, Metadata, Profile, Provider as FigmentProvider,
27 value::{Dict, Map, Value},
28 },
29 impl_figment_convert, impl_figment_convert_cast,
30};
31use itertools::Itertools;
32use reqwest::{Client, StatusCode, Url};
33use semver::BuildMetadata;
34use serde::Deserialize;
35use std::{path::PathBuf, time::Duration};
36
37#[derive(Clone, Copy, Debug, Eq, PartialEq)]
38enum VerifierCredentialProbe {
39 Accepted,
40 InvalidApiKey,
41 Inconclusive,
42}
43
44#[derive(Debug, Deserialize)]
45struct EtherscanProbeResponse {
46 status: String,
47 result: Option<serde_json::Value>,
48}
49
50fn verifier_credential_probe_query(api_key: Option<&str>) -> Vec<(&'static str, String)> {
51 let mut query = vec![
52 ("module", "contract".to_string()),
53 ("action", "getabi".to_string()),
54 ("address", Address::ZERO.to_string()),
55 ];
56 if let Some(api_key) = api_key {
57 query.push(("apikey", api_key.to_string()));
58 }
59 query
60}
61
62fn classify_verifier_credential_response(
63 status: StatusCode,
64 body: &str,
65) -> VerifierCredentialProbe {
66 let lower = body.to_lowercase();
67 if lower.contains("invalid api key") || lower.contains("invalid_api_key") {
68 return VerifierCredentialProbe::InvalidApiKey;
69 }
70
71 if lower.contains("contract source code not verified")
72 || lower.contains("contract not found")
73 || lower.contains("contract was not found")
74 {
75 return VerifierCredentialProbe::Accepted;
76 }
77
78 if status == StatusCode::UNAUTHORIZED {
79 return VerifierCredentialProbe::InvalidApiKey;
80 }
81
82 if !status.is_success()
83 || lower.contains("max rate limit reached")
84 || lower.contains("sorry, you have been blocked")
85 || lower.contains("checking if the site connection is secure")
86 {
87 return VerifierCredentialProbe::Inconclusive;
88 }
89
90 match serde_json::from_str::<EtherscanProbeResponse>(body) {
91 Ok(resp) if resp.status == "1" => VerifierCredentialProbe::Accepted,
92 Ok(resp) => resp
93 .result
94 .and_then(|result| result.as_str().map(str::to_lowercase))
95 .map(|result| {
96 if result.contains("invalid api key") || result.contains("invalid_api_key") {
97 VerifierCredentialProbe::InvalidApiKey
98 } else if result.contains("max rate limit reached") {
99 VerifierCredentialProbe::Inconclusive
100 } else if result.contains("contract source code not verified")
101 || result.contains("contract not found")
102 || result.contains("contract was not found")
103 {
104 VerifierCredentialProbe::Accepted
105 } else {
106 VerifierCredentialProbe::Inconclusive
107 }
108 })
109 .unwrap_or(VerifierCredentialProbe::Inconclusive),
110 Err(_) => VerifierCredentialProbe::Inconclusive,
111 }
112}
113
114pub(crate) fn parse_http_verifier_url(url: &str, label: &str) -> Result<Url> {
115 let url = Url::parse(url).wrap_err_with(|| format!("invalid {label} URL"))?;
116 if !matches!(url.scheme(), "http" | "https") {
117 eyre::bail!("invalid {label} URL: URL scheme must be http or https");
118 }
119 Ok(url)
120}
121
122async fn probe_verifier_credentials(
123 url: Url,
124 api_key: Option<&str>,
125) -> Result<VerifierCredentialProbe, reqwest::Error> {
126 let resp = Client::new()
127 .get(url)
128 .query(&verifier_credential_probe_query(api_key))
129 .timeout(Duration::from_secs(10))
130 .send()
131 .await?;
132 let status = resp.status();
133 let body = resp.text().await?;
134 Ok(classify_verifier_credential_response(status, &body))
135}
136
137#[derive(Copy, Clone, Debug, Eq, PartialEq, ValueEnum)]
141pub enum ContractLanguage {
142 Solidity,
144 Vyper,
146}
147
148#[derive(Clone, Debug, Default, Parser)]
150pub struct VerifierArgs {
151 #[arg(long, help_heading = "Verifier options", value_enum)]
153 pub verifier: Option<VerificationProviderType>,
154
155 #[arg(long, help_heading = "Verifier options", env = "VERIFIER_API_KEY")]
157 pub verifier_api_key: Option<String>,
158
159 #[arg(long, help_heading = "Verifier options", env = "VERIFIER_URL")]
161 pub verifier_url: Option<String>,
162}
163
164impl VerifierArgs {
165 pub fn effective_type(&self) -> VerificationProviderType {
170 self.verifier.unwrap_or_default()
171 }
172
173 pub const fn is_explicitly_set(&self) -> bool {
175 self.verifier.is_some()
176 }
177
178 pub fn resolve_api_key<'a>(&'a self, etherscan_key: Option<&'a str>) -> Option<&'a str> {
181 self.verifier_api_key.as_deref().or(etherscan_key)
182 }
183
184 pub async fn check_credentials(
189 &self,
190 api_key: Option<&str>,
191 chain: Chain,
192 config: &Config,
193 ) -> eyre::Result<()> {
194 let resolved = self.resolve(api_key, Some(chain));
195 match resolved {
196 VerificationProviderType::Etherscan
197 | VerificationProviderType::Blockscout
198 | VerificationProviderType::Oklink => {
199 let etherscan_opts =
200 EtherscanOpts { key: api_key.map(str::to_owned), chain: Some(chain) };
201 let client = EtherscanVerificationProvider::default().client(
202 ðerscan_opts,
203 self,
204 config,
205 )?;
206 match tokio::time::timeout(
207 Duration::from_secs(10),
208 probe_verifier_credentials(
209 client.etherscan_api_url().clone(),
210 client.api_key(),
211 ),
212 )
213 .await
214 {
215 Err(_) => {
216 sh_warn!("verifier credential check timed out, proceeding anyway")?;
217 }
218 Ok(Ok(VerifierCredentialProbe::Accepted)) => {}
219 Ok(Ok(VerifierCredentialProbe::InvalidApiKey)) => {
220 eyre::bail!("verifier credential check failed: invalid API key");
221 }
222 Ok(Ok(VerifierCredentialProbe::Inconclusive) | Err(_)) => {
223 sh_warn!("verifier credential check inconclusive, proceeding anyway")?;
224 }
225 }
226 }
227 VerificationProviderType::Custom => {
228 if let Some(url) = &self.verifier_url {
231 let url = parse_http_verifier_url(url, "verifier")?;
232 match probe_verifier_credentials(url, api_key).await {
233 Err(_) => {
234 sh_warn!("verifier credential check failed, proceeding anyway")?;
235 }
236 Ok(
237 VerifierCredentialProbe::Accepted
238 | VerifierCredentialProbe::Inconclusive,
239 ) => {}
240 Ok(VerifierCredentialProbe::InvalidApiKey) => {
241 eyre::bail!("verifier credential check failed: invalid API key");
242 }
243 }
244 }
245 }
246 VerificationProviderType::Sourcify => {
247 if let Some(url) = &self.verifier_url {
249 let url = parse_http_verifier_url(url, "Sourcify")?;
250 match Client::new()
251 .get(url.clone())
252 .timeout(Duration::from_secs(10))
253 .send()
254 .await
255 {
256 Err(_) => {
257 sh_warn!(
258 "Sourcify URL `{url}` could not be reached, proceeding anyway"
259 )?;
260 }
261 Ok(resp) => {
262 let status = resp.status();
263 if !status.is_success() && status != StatusCode::NOT_FOUND {
264 sh_warn!(
265 "Sourcify URL `{url}` returned HTTP {status}, proceeding anyway"
266 )?;
267 }
268 }
269 }
270 }
271 }
272 }
273 Ok(())
274 }
275
276 pub fn resolve(
286 &self,
287 etherscan_key: Option<&str>,
288 chain: Option<Chain>,
289 ) -> VerificationProviderType {
290 if let Some(v) = self.verifier {
291 return v;
292 }
293 let has_key = etherscan_key.is_some_and(|k| !k.is_empty());
294 if has_key && !chain.is_some_and(|c| c.is_custom_sourcify()) {
298 let chain_has_etherscan_url = chain.is_none_or(|c| c.etherscan_urls().is_some());
299 if chain_has_etherscan_url || self.verifier_url.is_some() {
300 return VerificationProviderType::Etherscan;
301 }
302 }
303 VerificationProviderType::Sourcify
304 }
305}
306
307#[derive(Clone, Debug, Parser)]
309pub struct VerifyArgs {
310 pub address: Address,
312
313 pub contract: Option<ContractInfo>,
315
316 #[arg(
318 long,
319 conflicts_with = "constructor_args_path",
320 value_name = "ARGS",
321 visible_alias = "encoded-constructor-args"
322 )]
323 pub constructor_args: Option<String>,
324
325 #[arg(long, value_hint = ValueHint::FilePath, value_name = "PATH")]
327 pub constructor_args_path: Option<PathBuf>,
328
329 #[arg(long)]
331 pub guess_constructor_args: bool,
332
333 #[arg(long)]
335 pub creation_transaction_hash: Option<TxHash>,
336
337 #[arg(long, value_name = "VERSION")]
339 pub compiler_version: Option<String>,
340
341 #[arg(long, value_name = "PROFILE_NAME")]
343 pub compilation_profile: Option<String>,
344
345 #[arg(long, visible_alias = "optimizer-runs", value_name = "NUM")]
347 pub num_of_optimizations: Option<usize>,
348
349 #[arg(long)]
351 pub flatten: bool,
352
353 #[arg(short, long)]
355 pub force: bool,
356
357 #[arg(long)]
359 pub skip_is_verified_check: bool,
360
361 #[arg(long)]
363 pub watch: bool,
364
365 #[arg(skip)]
368 pub print_submission_result_to_stdout: bool,
369
370 #[arg(long, help_heading = "Linker options", env = "DAPP_LIBRARIES")]
372 pub libraries: Vec<String>,
373
374 #[arg(long, value_hint = ValueHint::DirPath, value_name = "PATH")]
379 pub root: Option<PathBuf>,
380
381 #[arg(long, conflicts_with = "flatten")]
386 pub show_standard_json_input: bool,
387
388 #[arg(long)]
390 pub via_ir: bool,
391
392 #[arg(
400 long,
401 value_name = "LICENSE",
402 help_heading = "Verifier options",
403 value_parser = parse_etherscan_license_type,
404 )]
405 pub license_type: Option<String>,
406
407 #[arg(long)]
411 pub evm_version: Option<EvmVersion>,
412
413 #[arg(long, help_heading = "Compiler options")]
415 pub no_auto_detect: bool,
416
417 #[arg(long = "use", help_heading = "Compiler options", value_name = "SOLC_VERSION")]
421 pub use_solc: Option<String>,
422
423 #[command(flatten)]
424 pub etherscan: EtherscanOpts,
425
426 #[command(flatten)]
427 pub rpc: RpcOpts,
428
429 #[command(flatten)]
430 pub retry: RetryArgs,
431
432 #[command(flatten)]
433 pub verifier: VerifierArgs,
434
435 #[arg(long, value_enum)]
439 pub language: Option<ContractLanguage>,
440}
441
442pub fn parse_etherscan_license_type(value: &str) -> Result<String, String> {
443 let value = value.trim();
444 if value.is_empty() {
445 return Err("license type cannot be empty".into());
446 }
447
448 if let Ok(code) = value.parse::<u32>() {
449 return Ok(code.to_string());
450 }
451
452 let normalized = normalize_license_type(value);
453 let code = match normalized.as_str() {
454 "none" | "no-license" | "unlicensed" => 1,
455 "unlicense" | "the-unlicense" => 2,
456 "mit" | "mit-license" => 3,
457 "gpl-2.0" | "gpl-2.0+" | "gpl-2.0-only" | "gpl-2.0-or-later" | "gplv2" | "gnu-gplv2" => 4,
458 "gpl-3.0" | "gpl-3.0+" | "gpl-3.0-only" | "gpl-3.0-or-later" | "gplv3" | "gnu-gplv3" => 5,
459 "lgpl-2.1" | "lgpl-2.1+" | "lgpl-2.1-only" | "lgpl-2.1-or-later" | "lgplv2.1"
460 | "gnu-lgplv2.1" => 6,
461 "lgpl-3.0" | "lgpl-3.0+" | "lgpl-3.0-only" | "lgpl-3.0-or-later" | "lgplv3"
462 | "gnu-lgplv3" => 7,
463 "bsd-2-clause" => 8,
464 "bsd-3-clause" => 9,
465 "mpl-2.0" => 10,
466 "osl-3.0" => 11,
467 "apache-2.0" | "apache-license-2.0" => 12,
468 "agpl-3.0" | "agpl-3.0+" | "agpl-3.0-only" | "agpl-3.0-or-later" | "agplv3"
469 | "gnu-agplv3" => 13,
470 "bsl-1.1" | "busl-1.1" | "business-source-license-1.1" => 14,
471 _ => {
472 return Err(format!(
473 "unsupported Etherscan license type `{value}`; expected a numeric code or a \
474 supported SPDX identifier such as MIT, Apache-2.0, GPL-3.0-or-later, or \
475 AGPL-3.0-or-later"
476 ));
477 }
478 };
479
480 Ok(code.to_string())
481}
482
483fn normalize_license_type(value: &str) -> String {
484 let mut normalized = String::with_capacity(value.len());
485 let mut last_was_dash = false;
486
487 for ch in value.trim().chars() {
488 let ch = match ch {
489 '_' | ' ' | '\t' | '\n' | '\r' => '-',
490 _ => ch.to_ascii_lowercase(),
491 };
492
493 if ch == '-' {
494 if !last_was_dash {
495 normalized.push(ch);
496 }
497 last_was_dash = true;
498 } else {
499 normalized.push(ch);
500 last_was_dash = false;
501 }
502 }
503
504 normalized.trim_matches('-').to_string()
505}
506
507impl_figment_convert!(VerifyArgs);
508
509impl FigmentProvider for VerifyArgs {
510 fn metadata(&self) -> Metadata {
511 Metadata::named("Verify Provider")
512 }
513
514 fn data(&self) -> Result<Map<Profile, Dict>, Error> {
515 let mut dict = self.etherscan.dict();
516 dict.extend(self.rpc.dict());
517
518 if let Some(root) = self.root.as_ref() {
519 dict.insert("root".to_string(), Value::serialize(root)?);
520 }
521 if let Some(optimizer_runs) = self.num_of_optimizations {
522 dict.insert("optimizer".to_string(), Value::serialize(true)?);
523 dict.insert("optimizer_runs".to_string(), Value::serialize(optimizer_runs)?);
524 }
525 if let Some(evm_version) = self.evm_version {
526 dict.insert("evm_version".to_string(), Value::serialize(evm_version)?);
527 }
528 if self.via_ir {
529 dict.insert("via_ir".to_string(), Value::serialize(self.via_ir)?);
530 }
531
532 if self.no_auto_detect {
533 dict.insert("auto_detect_solc".to_string(), Value::serialize(false)?);
534 }
535
536 if let Some(ref solc) = self.use_solc {
537 let solc = solc.trim_start_matches("solc:");
538 dict.insert("solc".to_string(), Value::serialize(solc)?);
539 }
540
541 if let Some(api_key) = &self.verifier.verifier_api_key {
542 dict.insert("etherscan_api_key".into(), api_key.as_str().into());
543 }
544
545 Ok(Map::from([(Config::selected_profile(), dict)]))
546 }
547}
548
549struct ProviderRun {
550 label: VerificationProviderType,
551 args: VerifyArgs,
552 provider: Box<dyn VerificationProvider>,
553 required: bool,
555}
556
557#[derive(Clone)]
558enum RunContext {
559 Local(Box<VerificationContext>),
560 External(Box<ExternalVerificationContext>),
561}
562
563impl VerifyArgs {
564 pub async fn run(self) -> Result<()> {
566 let context = self.resolve_context().await?;
567 let config = context.config.clone();
568 self.run_with_resolved_context(RunContext::Local(Box::new(context)), config).await
569 }
570
571 pub async fn run_with_external_context(
573 self,
574 context: ExternalVerificationContext,
575 ) -> Result<()> {
576 self.validate_external_args()?;
577 eyre::ensure!(
578 !context.target.is_empty()
579 && context.target.len() <= 8193
580 && context.target.is_ascii()
581 && !context.target.bytes().any(|byte| byte.is_ascii_control()),
582 "external contract identifier must contain bounded printable ASCII"
583 );
584 let config = context.config.clone();
585 self.run_with_resolved_context(RunContext::External(Box::new(context)), config).await
586 }
587
588 fn validate_external_args(&self) -> Result<()> {
589 eyre::ensure!(!self.flatten, "flattening is unsupported for external verification");
590 eyre::ensure!(
591 !matches!(self.language, Some(ContractLanguage::Vyper)),
592 "Vyper is unsupported for external verification"
593 );
594 eyre::ensure!(
595 !self.guess_constructor_args,
596 "constructor argument guessing is unsupported for external verification"
597 );
598 eyre::ensure!(
599 self.constructor_args_path.is_none(),
600 "constructor args paths are unsupported for external verification; provide encoded constructor args"
601 );
602 Ok(())
603 }
604
605 async fn run_with_resolved_context(
606 mut self,
607 context: RunContext,
608 config: Config,
609 ) -> Result<()> {
610 if self.guess_constructor_args && config.get_rpc_url().is_none() {
611 eyre::bail!(
612 "You have to provide a valid RPC URL to use --guess-constructor-args feature"
613 );
614 }
615
616 let chain = match config.get_rpc_url() {
617 Some(_) => {
618 let provider = utils::get_provider(&config)?;
619 utils::get_chain(config.chain, provider).await?
620 }
621 None => config.chain.unwrap_or_default(),
622 };
623
624 self.etherscan.chain = Some(chain);
626 let config_key = match config.get_etherscan_config_with_chain(Some(chain)) {
631 Ok(config) => config.map(|config| config.key),
632 Err(_)
635 if self.verifier.verifier_url.is_some()
636 || self.verifier.verifier.is_some_and(|provider| provider.is_sourcify()) =>
637 {
638 None
639 }
640 Err(err) => return Err(err.into()),
641 }
642 .or_else(|| config.etherscan_api_key.clone());
643 self.etherscan.key =
644 self.verifier.resolve_api_key(config_key.as_deref()).map(str::to_owned);
645
646 let had_user_verifier_url = self.verifier.verifier_url.is_some();
650
651 let etherscan_key = self.etherscan.key();
655 let resolved = self.verifier.resolve(etherscan_key.as_deref(), self.etherscan.chain);
656
657 if resolved.is_sourcify()
660 && !had_user_verifier_url
661 && let Some(url) = sourcify_api_url(chain)
662 {
663 self.verifier.verifier_url = Some(url);
664 }
665
666 if self.show_standard_json_input {
667 match &context {
668 RunContext::Local(context) => {
669 let args = EtherscanVerificationProvider::default()
670 .create_verify_request(&self, context)
671 .await?;
672 sh_println!("{}", args.source)?;
673 }
674 RunContext::External(context) => {
675 sh_println!("{}", serde_json::to_string(&context.standard_json_input)?)?;
676 }
677 }
678 return Ok(());
679 }
680
681 let verifier_url = self.verifier.verifier_url.clone();
682 sh_status!("Start verifying contract `{}` deployed on {chain}", self.address)?;
683 if let Some(version) = &self.evm_version {
684 sh_status!("EVM version: {version}")?;
685 }
686 if let Some(version) = &self.compiler_version {
687 sh_status!("Compiler version: {version}")?;
688 }
689 if let Some(optimizations) = &self.num_of_optimizations {
690 sh_status!("Optimizations: {optimizations}")?
691 }
692 if let Some(args) = &self.constructor_args
693 && !args.is_empty()
694 {
695 sh_status!("Constructor args: {args}")?
696 }
697 let using_etherscan = resolved.is_etherscan();
698 let runs =
699 self.collect_runs(chain, etherscan_key.as_deref(), resolved, had_user_verifier_url)?;
700
701 let mut required_err = None;
703 let mut pending_check = None;
704 for ProviderRun { label, args, mut provider, required } in runs {
705 sh_status!("\nVerifying on {label}...")?;
706 let watch = args.watch;
707 let submission = match context.clone() {
708 RunContext::Local(context) => provider.submit(args, *context).await,
709 RunContext::External(context) => provider.submit_external(args, *context).await,
710 };
711 match submission {
712 Ok(check_args) => {
713 if required
714 && watch
715 && let Some(check_args) = check_args
716 {
717 pending_check = Some((label, provider, check_args));
718 }
719 }
720 Err(err) => {
721 if required {
722 required_err = Some(wrap_verifier_url_error(
723 err,
724 verifier_url.as_deref(),
725 using_etherscan,
726 ));
727 } else {
728 sh_warn!("{label} verification failed: {err}")?;
729 }
730 }
731 }
732 }
733
734 if required_err.is_none()
736 && let Some((label, provider, check_args)) = pending_check
737 {
738 sh_status!("\nWaiting for {label} verification result...")?;
739 if let Err(err) = provider.check(check_args).await {
740 required_err =
741 Some(wrap_verifier_url_error(err, verifier_url.as_deref(), using_etherscan));
742 }
743 }
744
745 required_err.map_or(Ok(()), Err)
746 }
747
748 fn collect_runs(
753 &self,
754 chain: Chain,
755 etherscan_key: Option<&str>,
756 resolved: VerificationProviderType,
757 had_user_verifier_url: bool,
758 ) -> Result<Vec<ProviderRun>> {
759 let mut runs = Vec::new();
760
761 let primary_provider = resolved.client(
762 etherscan_key,
763 self.etherscan.chain,
764 had_user_verifier_url,
765 self.verifier.is_explicitly_set(),
766 )?;
767 let primary_is_sourcify = resolved.is_sourcify();
768 runs.push(ProviderRun {
769 label: resolved,
770 args: self.clone(),
771 provider: primary_provider,
772 required: true,
773 });
774
775 let is_private_setup = had_user_verifier_url || resolved.is_custom() || is_dev_chain(chain);
779 if !primary_is_sourcify && !is_private_setup {
780 let mut args = self.clone();
781 args.verifier.verifier = Some(VerificationProviderType::Sourcify);
782 args.verifier.verifier_api_key = None;
783 args.verifier.verifier_url = sourcify_api_url(chain);
786 args.watch = false;
787 runs.push(ProviderRun {
788 label: VerificationProviderType::Sourcify,
789 args,
790 provider: Box::<SourcifyVerificationProvider>::default(),
791 required: false,
792 });
793 }
794
795 Ok(runs)
796 }
797
798 pub fn verification_provider(&self) -> Result<Box<dyn VerificationProvider>> {
800 self.verifier.effective_type().client(
801 self.etherscan.key().as_deref(),
802 self.etherscan.chain,
803 self.verifier.verifier_url.is_some(),
804 self.verifier.is_explicitly_set(),
805 )
806 }
807
808 pub async fn resolve_context(&self) -> Result<VerificationContext> {
811 let mut config = self.load_config_with_dependencies()?;
812 config.libraries.extend(self.libraries.clone());
813
814 let project = config.project()?;
815
816 if let Some(ref contract) = self.contract {
817 let contract_path = if let Some(ref path) = contract.path {
818 project.root().join(PathBuf::from(path))
819 } else {
820 project.find_contract_path(&contract.name)?
821 };
822
823 let cache = project.read_cache_file().ok();
824
825 let mut version = if let Some(ref version) = self.compiler_version {
826 version.trim_start_matches('v').parse()?
827 } else if let Some(ref solc) = config.solc {
828 solc.try_version()?
829 } else if let Some(entry) =
830 cache.as_ref().and_then(|cache| cache.files.get(&contract_path).cloned())
831 {
832 let unique_versions = entry
833 .artifacts
834 .get(&contract.name)
835 .map(|artifacts| artifacts.keys().collect::<HashSet<_>>())
836 .unwrap_or_default();
837
838 if unique_versions.is_empty() {
839 eyre::bail!(
840 "No matching artifact found for {}. This could be due to:\n\
841 - Compiler version mismatch - the contract was compiled with a different Solidity version than what's being used for verification",
842 contract.name
843 );
844 } else if unique_versions.len() > 1 {
845 warn!(
846 "Ambiguous compiler versions found in cache: {}",
847 unique_versions.iter().join(", ")
848 );
849 eyre::bail!(
850 "Compiler version has to be set in `foundry.toml`. If the project was not deployed with foundry, specify the version through `--compiler-version` flag."
851 );
852 }
853
854 unique_versions.into_iter().next().unwrap().to_owned()
855 } else {
856 eyre::bail!(
857 "If cache is disabled, compiler version must be either provided with `--compiler-version` option or set in foundry.toml"
858 );
859 };
860
861 let settings = if let Some(profile) = &self.compilation_profile {
862 if profile == "default" {
863 &project.settings
864 } else if let Some(settings) = project.additional_settings.get(profile.as_str()) {
865 settings
866 } else {
867 eyre::bail!("Unknown compilation profile: {}", profile);
868 }
869 } else if let Some((cache, entry)) = cache
870 .as_ref()
871 .and_then(|cache| Some((cache, cache.files.get(&contract_path)?.clone())))
872 {
873 let profiles = entry
874 .artifacts
875 .get(&contract.name)
876 .and_then(|artifacts| {
877 let mut cached_artifacts = artifacts.get(&version);
878 if cached_artifacts.is_none() && version.build != BuildMetadata::EMPTY {
886 let mut stripped = version.clone();
887 stripped.build = BuildMetadata::EMPTY;
888 cached_artifacts = artifacts.get(&stripped);
889 if !version.build.as_str().contains("solar") {
892 version = stripped;
893 }
894 }
895 cached_artifacts
896 })
897 .map(|artifacts| artifacts.keys().collect::<HashSet<_>>())
898 .unwrap_or_default();
899
900 if profiles.is_empty() {
901 eyre::bail!(
902 "No matching artifact found for {} with compiler version {}. This could be due to:\n\
903 - Compiler version mismatch - the contract was compiled with a different Solidity version",
904 contract.name,
905 version
906 );
907 } else if profiles.len() > 1 {
908 eyre::bail!(
909 "Ambiguous compilation profiles found in cache: {}, please specify the profile through `--compilation-profile` flag",
910 profiles.iter().join(", ")
911 );
912 }
913
914 let profile = profiles.into_iter().next().unwrap().to_owned();
915 cache.profiles.get(&profile).expect("must be present")
916 } else if project.additional_settings.is_empty() {
917 &project.settings
918 } else {
919 eyre::bail!(
920 "If cache is disabled, compilation profile must be provided with `--compilation-profile` option or set in foundry.toml"
921 );
922 };
923
924 VerificationContext::new(
925 contract_path,
926 contract.name.clone(),
927 version,
928 config,
929 settings.clone(),
930 )
931 } else {
932 if config.get_rpc_url().is_none() {
933 eyre::bail!("You have to provide a contract name or a valid RPC URL");
934 }
935 let provider = utils::get_provider(&config)?;
936 let code = provider.get_code_at(self.address).await?;
937
938 let output = ProjectCompiler::new().quiet(true).compile(&project)?;
939 let contracts = ContractsByArtifact::new(
940 output.artifact_ids().map(|(id, artifact)| (id, artifact.clone().into())),
941 );
942
943 let Some((artifact_id, _)) =
944 contracts.find_by_deployed_code_exact_unambiguous(&code)?
945 else {
946 eyre::bail!(format!(
947 "Bytecode at {} does not match any local contracts",
948 self.address
949 ));
950 };
951
952 let settings = project
953 .settings_profiles()
954 .find_map(|(name, settings)| {
955 (name == artifact_id.profile.as_str()).then_some(settings)
956 })
957 .expect("must be present");
958
959 VerificationContext::new(
960 artifact_id.source.clone(),
961 artifact_id.name.split('.').next().unwrap().to_owned(),
962 artifact_id.version.clone(),
963 config,
964 settings.clone(),
965 )
966 }
967 }
968
969 pub fn detect_language(&self, ctx: &VerificationContext) -> ContractLanguage {
971 self.language.unwrap_or_else(|| {
972 match ctx.target_path.extension().and_then(|e| e.to_str()) {
973 Some("vy") => ContractLanguage::Vyper,
974 _ => ContractLanguage::Solidity,
975 }
976 })
977 }
978}
979
980#[derive(Clone, Debug, Parser)]
982pub struct VerifyCheckArgs {
983 pub id: String,
989
990 #[command(flatten)]
991 pub retry: RetryArgs,
992
993 #[command(flatten)]
994 pub etherscan: EtherscanOpts,
995
996 #[command(flatten)]
997 pub verifier: VerifierArgs,
998}
999
1000impl_figment_convert_cast!(VerifyCheckArgs);
1001
1002impl VerifyCheckArgs {
1003 pub async fn run(mut self) -> Result<()> {
1005 self.etherscan.key =
1006 self.verifier.resolve_api_key(self.etherscan.key.as_deref()).map(str::to_owned);
1007 sh_status!("Checking verification status on {}", self.etherscan.chain.unwrap_or_default())?;
1008 self.verifier
1009 .effective_type()
1010 .client(
1011 self.etherscan.key().as_deref(),
1012 self.etherscan.chain,
1013 self.verifier.verifier_url.is_some(),
1014 self.verifier.is_explicitly_set(),
1015 )?
1016 .check(self)
1017 .await
1018 }
1019}
1020
1021impl FigmentProvider for VerifyCheckArgs {
1022 fn metadata(&self) -> Metadata {
1023 Metadata::named("Verify Check Provider")
1024 }
1025
1026 fn data(&self) -> Result<Map<Profile, Dict>, Error> {
1027 let mut dict = self.etherscan.dict();
1028 if let Some(api_key) = &self.etherscan.key {
1029 dict.insert("etherscan_api_key".into(), api_key.as_str().into());
1030 }
1031
1032 Ok(Map::from([(Config::selected_profile(), dict)]))
1033 }
1034}
1035
1036pub fn sourcify_api_url(chain: Chain) -> Option<String> {
1041 if chain.is_custom_sourcify() {
1042 chain.etherscan_urls().map(|(api_url, _)| {
1043 let api_url = api_url.trim_end_matches('/');
1044 format!("{api_url}/")
1045 })
1046 } else {
1047 None
1048 }
1049}
1050
1051const fn is_dev_chain(chain: Chain) -> bool {
1053 matches!(chain.named(), Some(NamedChain::Dev | NamedChain::AnvilHardhat | NamedChain::Cannon))
1054}
1055
1056#[cfg(test)]
1057mod tests {
1058 use super::*;
1059
1060 #[test]
1061 fn verifier_api_key_takes_precedence_over_etherscan_key() {
1062 let args = VerifierArgs { verifier_api_key: Some("explicit".into()), ..Default::default() };
1063 assert_eq!(args.resolve_api_key(Some("config")), Some("explicit"));
1064 assert_eq!(VerifierArgs::default().resolve_api_key(Some("config")), Some("config"));
1065 }
1066
1067 #[tokio::test]
1068 async fn external_context_rejects_control_characters_in_fqn() {
1069 let args =
1070 VerifyArgs::parse_from(["foundry-cli", "0xd8509bee9c9bf012282ad33aba0d87241baf5064"]);
1071 let context = ExternalVerificationContext {
1072 config: Config::default(),
1073 compiler_version: semver::Version::new(0, 8, 30),
1074 standard_json_input: std::sync::Arc::new(serde_json::json!({})),
1075 target: "A.sol:A\nforged".into(),
1076 };
1077 assert!(
1078 args.run_with_external_context(context)
1079 .await
1080 .unwrap_err()
1081 .to_string()
1082 .contains("printable")
1083 );
1084 }
1085
1086 #[test]
1087 fn external_run_context_clone_shares_standard_json() {
1088 let input = std::sync::Arc::new(serde_json::json!({"large": [1, 2, 3]}));
1089 let context = RunContext::External(Box::new(ExternalVerificationContext {
1090 config: Config::default(),
1091 compiler_version: semver::Version::new(0, 8, 30),
1092 standard_json_input: input.clone(),
1093 target: "A.sol:A".into(),
1094 }));
1095 let RunContext::External(context) = &context else { unreachable!() };
1096 let cloned = context.clone();
1097 assert!(std::sync::Arc::ptr_eq(&input, &cloned.standard_json_input));
1098 }
1099
1100 #[test]
1101 fn can_parse_verify_contract() {
1102 let args: VerifyArgs = VerifyArgs::parse_from([
1103 "foundry-cli",
1104 "0x0000000000000000000000000000000000000000",
1105 "src/Domains.sol:Domains",
1106 "--via-ir",
1107 "--license-type",
1108 "13",
1109 ]);
1110 assert!(args.via_ir);
1111 assert_eq!(args.license_type.as_deref(), Some("13"));
1112 }
1113
1114 #[test]
1115 fn can_parse_verify_contract_license_type_spdx() {
1116 let args: VerifyArgs = VerifyArgs::parse_from([
1117 "foundry-cli",
1118 "0x0000000000000000000000000000000000000000",
1119 "src/Domains.sol:Domains",
1120 "--license-type",
1121 "AGPL-3.0-or-later",
1122 ]);
1123 assert_eq!(args.license_type.as_deref(), Some("13"));
1124
1125 let args: VerifyArgs = VerifyArgs::parse_from([
1126 "foundry-cli",
1127 "0x0000000000000000000000000000000000000000",
1128 "src/Domains.sol:Domains",
1129 "--license-type",
1130 "MIT",
1131 ]);
1132 assert_eq!(args.license_type.as_deref(), Some("3"));
1133
1134 let args: VerifyArgs = VerifyArgs::parse_from([
1135 "foundry-cli",
1136 "0x0000000000000000000000000000000000000000",
1137 "src/Domains.sol:Domains",
1138 "--license-type",
1139 "apache 2.0",
1140 ]);
1141 assert_eq!(args.license_type.as_deref(), Some("12"));
1142 }
1143
1144 #[test]
1145 fn verify_contract_license_type_is_case_insensitive() {
1146 for variant in ["mit", "MIT", "Mit", "mIt"] {
1147 let args: VerifyArgs = VerifyArgs::parse_from([
1148 "foundry-cli",
1149 "0x0000000000000000000000000000000000000000",
1150 "src/Domains.sol:Domains",
1151 "--license-type",
1152 variant,
1153 ]);
1154 assert_eq!(args.license_type.as_deref(), Some("3"), "input: {variant}");
1155 }
1156 }
1157
1158 #[test]
1159 fn verify_contract_license_type_accepts_numeric_codes() {
1160 for (code, expected) in [("1", "1"), ("14", "14"), ("15", "15")] {
1161 let args: VerifyArgs = VerifyArgs::parse_from([
1162 "foundry-cli",
1163 "0x0000000000000000000000000000000000000000",
1164 "src/Domains.sol:Domains",
1165 "--license-type",
1166 code,
1167 ]);
1168 assert_eq!(args.license_type.as_deref(), Some(expected));
1169 }
1170 }
1171
1172 #[test]
1173 fn errors_on_invalid_verify_contract_license_type() {
1174 let err = VerifyArgs::try_parse_from([
1175 "foundry-cli",
1176 "0x0000000000000000000000000000000000000000",
1177 "src/Domains.sol:Domains",
1178 "--license-type",
1179 "Unknown-License",
1180 ])
1181 .unwrap_err();
1182 assert!(err.to_string().contains("unsupported Etherscan license type"));
1183 }
1184
1185 #[test]
1186 fn can_parse_new_compiler_flags() {
1187 let args: VerifyArgs = VerifyArgs::parse_from([
1188 "foundry-cli",
1189 "0x0000000000000000000000000000000000000000",
1190 "src/Domains.sol:Domains",
1191 "--no-auto-detect",
1192 "--use",
1193 "0.8.23",
1194 ]);
1195 assert!(args.no_auto_detect);
1196 assert_eq!(args.use_solc.as_deref(), Some("0.8.23"));
1197 }
1198
1199 #[test]
1200 fn classify_verifier_probe_accepts_not_verified_response() {
1201 let body =
1202 r#"{"status":"0","message":"NOTOK","result":"Contract source code not verified"}"#;
1203 assert_eq!(
1204 classify_verifier_credential_response(StatusCode::OK, body),
1205 VerifierCredentialProbe::Accepted,
1206 );
1207 }
1208
1209 #[test]
1210 fn classify_verifier_probe_rejects_invalid_api_key() {
1211 let body = r#"{"status":"0","message":"NOTOK","result":"Invalid API Key"}"#;
1212 assert_eq!(
1213 classify_verifier_credential_response(StatusCode::OK, body),
1214 VerifierCredentialProbe::InvalidApiKey,
1215 );
1216 assert_eq!(
1217 classify_verifier_credential_response(StatusCode::UNAUTHORIZED, ""),
1218 VerifierCredentialProbe::InvalidApiKey,
1219 );
1220 }
1221
1222 #[test]
1223 fn classify_verifier_probe_treats_transient_errors_as_inconclusive() {
1224 let body = r#"{"status":"0","message":"NOTOK","result":"Max rate limit reached"}"#;
1225 assert_eq!(
1226 classify_verifier_credential_response(StatusCode::OK, body),
1227 VerifierCredentialProbe::Inconclusive,
1228 );
1229 assert_eq!(
1230 classify_verifier_credential_response(
1231 StatusCode::OK,
1232 "Checking if the site connection is secure",
1233 ),
1234 VerifierCredentialProbe::Inconclusive,
1235 );
1236 assert_eq!(
1237 classify_verifier_credential_response(
1238 StatusCode::FORBIDDEN,
1239 "Sorry, you have been blocked",
1240 ),
1241 VerifierCredentialProbe::Inconclusive,
1242 );
1243 assert_eq!(
1244 classify_verifier_credential_response(StatusCode::FORBIDDEN, ""),
1245 VerifierCredentialProbe::Inconclusive,
1246 );
1247 }
1248
1249 #[test]
1250 fn parse_http_verifier_url_rejects_unsupported_schemes() {
1251 assert!(parse_http_verifier_url("https://example.com/api", "verifier").is_ok());
1252 assert!(parse_http_verifier_url("http://example.com/api", "verifier").is_ok());
1253
1254 let err = parse_http_verifier_url("gopher://example.com/api", "verifier").unwrap_err();
1255 assert_eq!(err.to_string(), "invalid verifier URL: URL scheme must be http or https");
1256 }
1257
1258 #[test]
1259 fn resolve_explicit_sourcify_overrides_api_key() {
1260 let args = VerifierArgs {
1261 verifier: Some(VerificationProviderType::Sourcify),
1262 verifier_api_key: None,
1263 verifier_url: None,
1264 };
1265 assert_eq!(
1266 args.resolve(Some("mykey"), Some(Chain::mainnet())),
1267 VerificationProviderType::Sourcify,
1268 );
1269 }
1270
1271 #[test]
1272 fn resolve_explicit_etherscan_is_etherscan() {
1273 let args = VerifierArgs {
1274 verifier: Some(VerificationProviderType::Etherscan),
1275 verifier_api_key: None,
1276 verifier_url: None,
1277 };
1278 assert_eq!(
1279 args.resolve(Some("mykey"), Some(Chain::mainnet())),
1280 VerificationProviderType::Etherscan,
1281 );
1282 }
1283
1284 #[test]
1285 fn resolve_implicit_with_key_and_known_chain_uses_etherscan() {
1286 let args = VerifierArgs { verifier: None, verifier_api_key: None, verifier_url: None };
1287 assert_eq!(
1288 args.resolve(Some("mykey"), Some(Chain::mainnet())),
1289 VerificationProviderType::Etherscan,
1290 );
1291 }
1292
1293 #[test]
1294 fn resolve_implicit_with_key_and_unknown_chain_falls_back_to_sourcify() {
1295 let args = VerifierArgs { verifier: None, verifier_api_key: None, verifier_url: None };
1296 assert_eq!(
1297 args.resolve(Some("mykey"), Some(Chain::from(3658348u64))),
1298 VerificationProviderType::Sourcify,
1299 );
1300 }
1301
1302 #[test]
1303 fn resolve_implicit_with_key_and_unknown_chain_but_url_uses_etherscan() {
1304 let args = VerifierArgs {
1305 verifier: None,
1306 verifier_api_key: None,
1307 verifier_url: Some("https://example.com/api".to_string()),
1308 };
1309 assert_eq!(
1310 args.resolve(Some("mykey"), Some(Chain::from(3658348u64))),
1311 VerificationProviderType::Etherscan,
1312 );
1313 }
1314
1315 #[test]
1316 fn resolve_implicit_no_key_falls_back_to_sourcify() {
1317 let args = VerifierArgs { verifier: None, verifier_api_key: None, verifier_url: None };
1318 assert_eq!(args.resolve(None, Some(Chain::mainnet())), VerificationProviderType::Sourcify,);
1319 }
1320
1321 #[test]
1324 fn resolve_implicit_with_key_and_custom_sourcify_chain_falls_back_to_sourcify() {
1325 let tempo = Chain::from(4217u64); assert!(tempo.is_custom_sourcify(), "sanity: Tempo should be is_custom_sourcify");
1327 let args = VerifierArgs { verifier: None, verifier_api_key: None, verifier_url: None };
1328 assert_eq!(args.resolve(Some("mykey"), Some(tempo)), VerificationProviderType::Sourcify,);
1329 }
1330
1331 #[test]
1335 fn resolve_custom_sourcify_chain_with_url_and_key_stays_sourcify() {
1336 let tempo = Chain::from(4217u64);
1337 let args = VerifierArgs {
1338 verifier: None,
1339 verifier_api_key: None,
1340 verifier_url: Some("https://contracts.tempo.xyz/".to_string()),
1341 };
1342 assert_eq!(args.resolve(Some("mykey"), Some(tempo)), VerificationProviderType::Sourcify,);
1343 }
1344
1345 #[test]
1346 fn collect_runs_adds_sourcify_provider() {
1347 let args: VerifyArgs = VerifyArgs::parse_from([
1348 "foundry-cli",
1349 "0x0000000000000000000000000000000000000000",
1350 "src/Counter.sol:Counter",
1351 "--etherscan-api-key",
1352 "k",
1353 ]);
1354 let resolved = args.verifier.resolve(Some("k"), Some(Chain::mainnet()));
1355 let runs = args.collect_runs(Chain::mainnet(), Some("k"), resolved, false).unwrap();
1356 assert_eq!(runs.len(), 2);
1357 assert_eq!(runs[0].label, VerificationProviderType::Etherscan);
1358 assert!(runs[0].required);
1359 assert_eq!(runs[1].label, VerificationProviderType::Sourcify);
1360 assert!(!runs[1].required);
1361 assert!(runs[1].args.verifier.verifier_api_key.is_none());
1362 }
1363
1364 #[test]
1365 fn collect_runs_skips_secondary_when_primary_is_sourcify() {
1366 let args: VerifyArgs = VerifyArgs::parse_from([
1367 "foundry-cli",
1368 "0x0000000000000000000000000000000000000000",
1369 "src/Counter.sol:Counter",
1370 "--verifier",
1371 "sourcify",
1372 ]);
1373 let resolved = args.verifier.resolve(None, Some(Chain::mainnet()));
1374 let runs = args.collect_runs(Chain::mainnet(), None, resolved, false).unwrap();
1375 assert_eq!(runs.len(), 1);
1376 assert_eq!(runs[0].label, VerificationProviderType::Sourcify);
1377 assert!(runs[0].required);
1378 }
1379
1380 #[test]
1381 fn collect_runs_skips_secondary_for_custom_verifier() {
1382 let args: VerifyArgs = VerifyArgs::parse_from([
1383 "foundry-cli",
1384 "0x0000000000000000000000000000000000000000",
1385 "src/Counter.sol:Counter",
1386 "--verifier",
1387 "custom",
1388 "--verifier-url",
1389 "https://internal.example.com/api",
1390 ]);
1391 let runs = args
1392 .collect_runs(Chain::mainnet(), None, VerificationProviderType::Custom, true)
1393 .unwrap();
1394 assert_eq!(runs.len(), 1);
1395 assert_eq!(runs[0].label, VerificationProviderType::Custom);
1396 }
1397
1398 #[test]
1399 fn collect_runs_skips_secondary_on_dev_chain() {
1400 let args: VerifyArgs = VerifyArgs::parse_from([
1401 "foundry-cli",
1402 "0x0000000000000000000000000000000000000000",
1403 "src/Counter.sol:Counter",
1404 "--etherscan-api-key",
1405 "k",
1406 ]);
1407 let anvil = Chain::from(31337u64);
1408 let resolved = args.verifier.resolve(Some("k"), Some(anvil));
1409 let runs = args.collect_runs(anvil, Some("k"), resolved, false).unwrap();
1410 assert_eq!(runs.len(), 1);
1411 assert!(runs[0].required);
1412 }
1413
1414 #[test]
1415 fn collect_runs_skips_secondary_with_user_verifier_url() {
1416 let args: VerifyArgs = VerifyArgs::parse_from([
1417 "foundry-cli",
1418 "0x0000000000000000000000000000000000000000",
1419 "src/Counter.sol:Counter",
1420 "--verifier",
1421 "blockscout",
1422 "--verifier-url",
1423 "https://internal-blockscout.example.com/api",
1424 ]);
1425 let resolved = args.verifier.resolve(None, Some(Chain::mainnet()));
1426 let runs = args.collect_runs(Chain::mainnet(), None, resolved, true).unwrap();
1427 assert_eq!(runs.len(), 1);
1428 assert_eq!(runs[0].label, VerificationProviderType::Blockscout);
1429 }
1430}