feature(starknet): Adding injected class-hash values into code.#10160
Conversation
|
Warning This pull request is not mergeable via GitHub because a downstack PR is open. Once all requirements are satisfied, merge this PR as a stack on Graphite.
This stack of pull requests is managed by Graphite. Learn more about stacking. |
8404305 to
8294d7a
Compare
4d2e354 to
7ff65e3
Compare
| // to zero (to obtain the class hash), then with that hash injected. Embedding the hash changes | ||
| // the bytecode, so the final class's true hash differs from the injected value (a deliberate, | ||
| // documented approximation). |
There was a problem hiding this comment.
Well that's no good for us
There was a problem hiding this comment.
ignore this - it is not correct - as long as you set the data in the provider - by asking for a calculation it shouldn't matter at all. this is just an example in any case.
|
@orizi also this is not what we want to achieve - we want to embed external contracts, and that implementation self-embeds the class hash which is totally unneeded |
orizi
left a comment
There was a problem hiding this comment.
@orizi made 1 comment.
Reviewable status: 0 of 34 files reviewed, 1 unresolved discussion (waiting on Arcticae).
| // to zero (to obtain the class hash), then with that hash injected. Embedding the hash changes | ||
| // the bytecode, so the final class's true hash differs from the injected value (a deliberate, | ||
| // documented approximation). |
There was a problem hiding this comment.
ignore this - it is not correct - as long as you set the data in the provider - by asking for a calculation it shouldn't matter at all. this is just an example in any case.

Summary
Adds a
ContractClass::class_hash()method that computes the Starknet Sierra class hash using Poseidon over the contract class version marker, entry-point groups, ABI keccak hash, and Sierra program hash. Integrates this intocompile_pathvia a two-pass compilation strategy: a first pass compiles with the class hash stubbed to zero, computes the resulting class hash, then a second pass re-compiles with that hash injected via the reserved__externally_provided_const__extern. The Starknet plugin now generates a hidden__class_hash__module in every contract, exposing aclass_hash()function and a genericForwardingClassHashImplthat delegates to it.Type of change
Please check one:
Why is this change needed?
Contracts previously had no way to statically reference their own class hash at compile time. The
TEST_CLASS_HASHconstant existed only under#[cfg(target: 'test')]. This change makes the class hash available at runtime via a generated accessor backed by a compiler-injected constant, enabling patterns like forwarding contracts that need to statically embed their own class hash.What was the behavior or documentation before?
Contracts could not call their own class hash from within Cairo code outside of test targets. Sierra generation would fail if
__externally_provided_const__was referenced without a registered provider.What is the behavior or documentation after?
Every
#[starknet::contract]module now contains a generated__class_hash__submodule with:class_hash() -> starknet::ClassHash— returns the contract's own class hash as a compile-time constant injected during Sierra generation.ForwardingClassHashImpl<T>— a generic impl ofstarknet::ForwardingClassHash<T>that returns this contract's class hash, usable by forwarding contracts.compile_pathperforms two compilation passes: the first compiles with the hash stubbed to zero to obtain the Sierra output, computes its class hash viaContractClass::class_hash(), then re-compiles with that hash injected. The injected value is an approximation because embedding the hash changes the bytecode, but the result is deterministic across runs.Related issue or discussion (if any)
Additional context
The
ContractClass::class_hash()implementation mirrors the algorithm used by the sequencer (starknet_api): a Poseidon hash overCONTRACT_CLASS_V0.1.0, the three entry-point group hashes, the ABI keccak hash, and the Sierra program hash. The ABI is hashed via its canonical JSON string representation, so the exact serialization affects the result. A test (test_class_hash_is_deterministic_and_sensitive) verifies determinism and sensitivity to entry-point mutations. An end-to-end compilation test (class_hash_injection_compiles) verifies that a contract calling__class_hash__::class_hash()compiles successfully and produces a deterministic Sierra program.