2 releases
new 0.15.4-beta.1 | Feb 10, 2025 |
---|---|
0.15.4-beta.0 | Feb 7, 2025 |
#392 in Magic Beans
173 downloads per month
4MB
76K
SLoC
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
compute_class_hash
- The main entry point for computing class hashes for both Cairo and Sierra contractsComputedClassHash
- An enum representing the computed hash for either contract typePreparedCairoContractDefinition
- A prepared Cairo contract definition ready for hashingRawCairoContractDefinition
- An unprepared Cairo contract definition
Implementation Details
Cairo Class Hash
The Cairo class hash computation follows these steps:
- The contract definition is prepared by removing debug info and handling special cases for Cairo 0.8+ attributes
- The prepared definition is serialized to JSON with Python-compatible formatting
- A truncated Keccak hash is computed from the serialized JSON
- Entry points, builtins, and bytecode are processed through hash chains
- The final class hash is computed by combining all components
Sierra Class Hash
The Sierra class hash computation is simpler:
- The contract version is validated
- Entry points are processed in order
- The ABI string is hashed
- The Sierra program is hashed
- 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.
Dependencies
~9–16MB
~194K SLoC