Skip to main content

soroban_cli/commands/contract/
spec_verify.rs

1use clap::Parser;
2
3use crate::{commands::global, print::Print, wasm};
4
5/// Verify that a contract's spec references only defined types
6///
7/// Reads a contract WASM and checks that all user-defined types (UDTs)
8/// referenced in function signatures, events, and type definitions are
9/// defined within the spec itself.
10#[derive(Parser, Debug, Clone)]
11#[group(skip)]
12pub struct Cmd {
13    #[command(flatten)]
14    wasm: wasm::Args,
15}
16
17#[derive(thiserror::Error, Debug)]
18pub enum Error {
19    #[error(transparent)]
20    Wasm(#[from] wasm::Error),
21    #[error(transparent)]
22    Spec(#[from] soroban_spec_tools::Error),
23    #[error("contract spec has undefined types")]
24    VerifyFailed,
25}
26
27impl Cmd {
28    pub fn run(&self, global_args: &global::Args) -> Result<(), Error> {
29        let print = Print::new(global_args.quiet);
30        let wasm_bytes = self.wasm.read()?;
31        let spec = soroban_spec_tools::Spec::from_wasm(&wasm_bytes)?;
32        let warnings = spec.verify();
33        if warnings.is_empty() {
34            print.checkln("contract spec verification passed: all types are defined");
35        } else {
36            for w in &warnings {
37                print.errorln(w);
38            }
39            return Err(Error::VerifyFailed);
40        }
41        Ok(())
42    }
43}