Skip to content

Commit

Permalink
feat: check that the compiled class hash matches the supplied class
Browse files Browse the repository at this point in the history
  • Loading branch information
ArniStarkware committed Jun 16, 2024
1 parent af43060 commit 3ac61e8
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ axum = "0.6.12"
blockifier = { git = "https://github.com/starkware-libs/blockifier.git", rev = "6babc28a", features = [
"testing",
] }
cairo-felt = "0.9.1"
cairo-lang-sierra = "2.6.0"
cairo-lang-starknet-classes = "2.6.0"
cairo-lang-utils = "2.6.0"
Expand Down
1 change: 1 addition & 0 deletions crates/gateway/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ testing = []
[dependencies]
axum.workspace = true
blockifier.workspace = true
cairo-felt.workspace = true
cairo-lang-starknet-classes.workspace = true
cairo-lang-utils.workspace = true
hyper.workspace = true
Expand Down
6 changes: 6 additions & 0 deletions crates/gateway/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use blockifier::blockifier::stateful_validator::StatefulValidatorError;
use blockifier::state::errors::StateError;
use blockifier::transaction::errors::TransactionExecutionError;
use starknet_api::block::BlockNumber;
use starknet_api::core::CompiledClassHash;
use starknet_api::transaction::{Resource, ResourceBounds};
use starknet_api::StarknetApiError;
use thiserror::Error;
Expand All @@ -22,6 +23,11 @@ pub enum GatewayError {
StatefulTransactionValidatorError(#[from] StatefulTransactionValidatorError),
#[error(transparent)]
StatelessTransactionValidatorError(#[from] StatelessTransactionValidatorError),
#[error(
"The supplied compiled class hash {supplied:?} does not match the hash of the Casm class \
compiled from the supplied Sierra {hash_result:?}."
)]
CompiledClassHashMismatch { supplied: CompiledClassHash, hash_result: CompiledClassHash },
}

impl IntoResponse for GatewayError {
Expand Down
18 changes: 18 additions & 0 deletions crates/gateway/src/gateway.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ use axum::extract::State;
use axum::routing::{get, post};
use axum::{Json, Router};
use blockifier::execution::contract_class::{ClassInfo, ContractClassV1};
use cairo_felt::Felt252;
use cairo_lang_starknet_classes::contract_class::{
ContractClass as CairoLangContractClass, ContractEntryPoint as CairoLangContractEntryPoint,
ContractEntryPoints as CairoLangContractEntryPoints,
};
use cairo_lang_utils::bigint::BigUintAsHex;
use num_bigint::BigUint;
use starknet_api::core::CompiledClassHash;
use starknet_api::external_transaction::{
ContractClass as StarknetApiContractClass, EntryPointByType as StarknetApiEntryPointByType,
ExternalDeclareTransaction, ExternalTransaction,
Expand Down Expand Up @@ -169,6 +171,16 @@ fn get_optional_class_info(tx: &ExternalTransaction) -> GatewayResult<Option<Cla
}
};

// TODO: Handle unwrap.
let hash_result =
CompiledClassHash(felt_to_stark_felt(&casm_contract_class.compiled_class_hash()));
if hash_result != tx.compiled_class_hash {
return Err(GatewayError::CompiledClassHashMismatch {
supplied: tx.compiled_class_hash,
hash_result,
});
}

// Convert Casm contract class to Starknet contract class directly.
let raw_contract_class = serde_json::to_string(&casm_contract_class).unwrap();
let contact_class_v1: ContractClassV1 =
Expand Down Expand Up @@ -238,3 +250,9 @@ fn stark_felt_to_big_uint(stark_felt: StarkFelt) -> BigUint {
let radix = 256;
BigUint::from_radix_be(stark_felt.bytes(), radix).expect("Unexpected None value.")
}

// TODO(Arni): Move to starknet_api. This function already exists in the blockifier repo.
pub fn felt_to_stark_felt(felt: &Felt252) -> StarkFelt {
let biguint = format!("{:#x}", felt.to_biguint());
StarkFelt::try_from(biguint.as_str()).expect("Felt252 must be in StarkFelt's range.")
}

0 comments on commit 3ac61e8

Please sign in to comment.