Skip to content

Commit

Permalink
feat: declare with already-compiled CASM file
Browse files Browse the repository at this point in the history
Allows users to directly supply a CASM file for the purpose of
determining the CASM hash used in contract declaration. This was
previously already possible with the `--casm-hash` flag, but required
users to first obtain the hash via the `starkli class-hash` command.
  • Loading branch information
xJonathanLEI committed Sep 2, 2023
1 parent 8d2fe49 commit 12c510c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
34 changes: 28 additions & 6 deletions src/casm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ use std::path::PathBuf;
use anyhow::Result;
use clap::Parser;
use colored::Colorize;
use starknet::core::types::{contract::SierraClass, FieldElement};
use starknet::core::types::{
contract::{CompiledClass, SierraClass},
FieldElement,
};

use crate::{
compiler::{BuiltInCompiler, CompilerBinary, CompilerVersion},
Expand All @@ -21,6 +24,12 @@ pub struct CasmArgs {
help = "Path to the starknet-sierra-compile binary"
)]
compiler_path: Option<PathBuf>,
#[clap(
long,
value_parser = ExpandedPathbufParser,
help = "Path to already-compiled CASM file"
)]
casm_file: Option<PathBuf>,
#[clap(long, help = "Override Sierra compilation and use CASM hash directly")]
casm_hash: Option<String>,
}
Expand All @@ -29,6 +38,7 @@ pub struct CasmArgs {
pub enum CasmHashSource {
BuiltInCompiler(BuiltInCompiler),
CompilerBinary(CompilerBinary),
CasmFile(PathBuf),
Hash(FieldElement),
}

Expand All @@ -37,16 +47,22 @@ impl CasmArgs {
where
N: NetworkSource,
{
match (self.compiler_version, self.compiler_path, self.casm_hash) {
(Some(compiler_version), None, None) => {
match (
self.compiler_version,
self.compiler_path,
self.casm_file,
self.casm_hash,
) {
(Some(compiler_version), None, None, None) => {
Ok(CasmHashSource::BuiltInCompiler(compiler_version.into()))
}
(None, Some(compiler_path), None) => {
(None, Some(compiler_path), None, None) => {
Ok(CasmHashSource::CompilerBinary(compiler_path.into()))
}
(None, None, Some(casm_hash)) => Ok(CasmHashSource::Hash(casm_hash.parse()?)),
(None, None, Some(casm_file), None) => Ok(CasmHashSource::CasmFile(casm_file)),
(None, None, None, Some(casm_hash)) => Ok(CasmHashSource::Hash(casm_hash.parse()?)),
// Tries to detect compiler version if nothing provided
(None, None, None) => {
(None, None, None, None) => {
eprintln!(
"Sierra compiler version not specified. \
Attempting to automatically decide version to use..."
Expand Down Expand Up @@ -98,6 +114,12 @@ impl CasmHashSource {
match self {
Self::BuiltInCompiler(compiler) => compiler.compile(sierra_class),
Self::CompilerBinary(compiler) => compiler.compile(sierra_class),
Self::CasmFile(path) => {
let mut casm_file = std::fs::File::open(path)?;
let casm_class = serde_json::from_reader::<_, CompiledClass>(&mut casm_file)?;

Ok(casm_class.class_hash()?)
}
Self::Hash(hash) => Ok(*hash),
}
}
Expand Down
6 changes: 6 additions & 0 deletions src/subcommands/declare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,12 @@ impl Declare {
format!("{}", compiler.path().display()).bright_yellow()
);
}
CasmHashSource::CasmFile(path) => {
eprintln!(
"Using a compiled CASM file directly: {}...",
format!("{}", path.display()).bright_yellow()
);
}
CasmHashSource::Hash(hash) => {
eprintln!(
"Using the provided CASM hash: {}...",
Expand Down

0 comments on commit 12c510c

Please sign in to comment.