sp1_cli/commands/
vkey.rs

1use std::{fs::File, io::Read};
2
3use anyhow::Result;
4use clap::{Args, Parser};
5use sp1_build::{generate_elf_paths, BuildArgs};
6use sp1_sdk::{HashableKey, ProverClient};
7
8#[derive(Parser)]
9#[command(name = "vkey", about = "View the verification key hash for a program.")]
10pub struct VkeyCmd {
11    /// Path to the ELF.
12    #[command(flatten)]
13    elf: Elf,
14}
15
16#[derive(Debug, Clone, Args)]
17#[group(required = true, multiple = false)]
18pub struct Elf {
19    /// The path to the ELF file
20    #[arg(long = "elf")]
21    path: Option<String>,
22    /// The crate used to generate the ELF file
23    #[arg(long)]
24    program: Option<String>,
25}
26
27impl VkeyCmd {
28    pub fn run(&self) -> Result<()> {
29        let elf_paths = if let Some(path) = &self.elf.path {
30            vec![(None, path.clone())]
31        } else if let Some(program) = &self.elf.program {
32            let metadata_cmd = cargo_metadata::MetadataCommand::new();
33            let metadata = metadata_cmd.exec()?;
34            let build_args = BuildArgs { packages: vec![program.clone()], ..Default::default() };
35
36            generate_elf_paths(&metadata, Some(&build_args))?
37                .into_iter()
38                .map(|(target, path)| (Some(target), path.to_string()))
39                .collect()
40        } else {
41            unreachable!()
42        };
43
44        for (target, elf_path) in elf_paths {
45            // Read the elf file contents
46            let mut file = File::open(elf_path)?;
47            let mut elf = Vec::new();
48            file.read_to_end(&mut elf)?;
49
50            // Get the verification key
51            let prover = ProverClient::from_env();
52            let (_, vk) = prover.setup(&elf);
53
54            // Print the verification key hash
55            if let Some(target) = target {
56                println!("Verification Key Hash for '{target}':\n{}", vk.vk.bytes32());
57            } else {
58                println!("Verification Key Hash:\n{}", vk.vk.bytes32());
59            }
60        }
61
62        Ok(())
63    }
64}