Crate pathfinder_class_hash

Crate pathfinder_class_hash 

Source
Expand description

Class hash computation for Cairo and Sierra contracts.

This crate provides functionality to compute class hashes for both Cairo 0.x and Sierra (Cairo 1.x+) contracts in the Starknet ecosystem. The class hash is a unique identifier for a contract’s code that is used throughout the Starknet protocol.

§Class Hash Types

There are two main types of class hashes:

  • Cairo 0.x class hashes - Computed for legacy Cairo contracts
  • Sierra class hashes - Computed for newer Cairo 1.x+ contracts using the Sierra intermediate representation

§Main Components

§Implementation Details

§Cairo Class Hash

The Cairo class hash computation follows these steps:

  1. The contract definition is prepared by removing debug info and handling special cases for Cairo 0.8+ attributes
  2. The prepared definition is serialized to JSON with Python-compatible formatting
  3. A truncated Keccak hash is computed from the serialized JSON
  4. Entry points, builtins, and bytecode are processed through hash chains
  5. The final class hash is computed by combining all components

§Sierra Class Hash

The Sierra class hash computation is simpler:

  1. The contract version is validated
  2. Entry points are processed in order
  3. The ABI string is hashed
  4. The Sierra program is hashed
  5. All components are combined into the final hash

§Compatibility

This crate maintains compatibility with the official Starknet implementation and includes extensive test vectors to ensure hash computation matches the network’s expectations.

See the official Starknet documentation for more details on class hash computation.

Modules§

from_parts
Helpers to compute class hashes from the parts that compose a Cairo or Sierra contract
json
Helpers to parse and serialize the parts that compose a Cairo or Sierra contract

Structs§

PreparedCairoContractDefinition
A prepared Cairo contract definition ready for class hash computation.
RawCairoContractDefinition
An unprepared Cairo contract definition.

Enums§

ComputedClassHash
Computed class hash

Functions§

compute_cairo_class_hash
Computes the class hash for given Cairo class definition.
compute_cairo_hinted_class_hash
Compute class hash for a Cairo contract definition
compute_class_hash
Computes the starknet class hash for given class definition JSON blob.
compute_sierra_class_hash
Computes the class hash for a Sierra class definition.
parse_contract_definition
Parse either a Sierra or a Cairo contract definition.
prepare_json_contract_definition
Prepares a Cairo contract definition for class hash computation by applying necessary transformations.
truncated_keccak
Computes a truncated Keccak hash compatible with Starknet’s field element representation.