Starknet verification
Intro
Starknet aims to achieve secure, low-cost transactions and high performance by using the STARK cryptographic proof system. Starknet contracts and the Starknet OS are written in Cairo, a custom-built and specialized programming language.
Cairo Language
The Cairo language is a smart contract language for Starknet, a programming language designed for virtual CPUs. Because the Cairo language is built for cryptographic constraints, not physical ones, it can efficiently prove the execution of any program.
While Cairo 0 was compiled directly into CASM (Cairo Assembly), Cairo 1 is a high-level language that is first compiled into Sierra. Sierra (Safe Intermediate Representation) is an intermediate representation between Cairo and CASM that is later compiled into a safe subset of CASM. The resulting compiled CASM code is eventually executed by the Cairo VM on the Starknet OS and generates a STARK proof, which is sent to L1.
You can read more about Cairo, Sierra, and CASM in these documents.
Cairo Code
Example code for the Cairo language looks like this:
#[starknet::interface]
trait ISimpleStorage<TContractState> {
fn set(ref self: TContractState, x: u128);
fn get(self: @TContractState) -> u128;
}
#[starknet::contract]
mod simple_storage {
use starknet::get_caller_address;
use starknet::ContractAddress;
#[storage]
struct Storage {
stored_data: u128
}
#[abi(embed_v0)]
impl SimpleStorage of super::ISimpleStorage<ContractState> {
fn set(ref self: ContractState, x: u128) {
self.stored_data.write(x);
}
fn get(self: @ContractState) -> u128 {
self.stored_data.read()
}
}
}
Cairo Verification
The above simple_storage.cairo
can be verified using WELLDONE Studio's VeriWell. To deploy a smart contract written in Cairo, you can refer to the Starknet documentation for deployment. Once deployed, you can check that it was deployed correctly with Starkscan or Nethermind Voyager Explorer.
To verify a Cairo smart contract using the Multi-chain Verification Tool, a total of seven pieces of information are required:
Contract Address: Deployed smart contract address
DECLARE
Transaction Hash: Declare transaction hash is required because there is a need to verify not onlyclass_hash
but alsocompiled_class_hash
. In the future, we will remove theDECLARE
transaction hash.Scarb Version: Scarb is a Cairo package manager that compiles Cairo projects. It depends on the compiler version to compile, so it is important to specify it.
Chain ID: To differentiate between Mainnet (
0x534e5f4d41494e
) and Sepolia (0x534e5f5345504f4c4941
)Cairo project source code
In order to validate Cairo, you must add the following config to
Scarb.toml
:
[[target.starknet-contract]]
casm = true
sierra = true
- The Cairo project structure is as follows (assuming a DECLARE and DEPLOY to the network):
├── Scarb.lock
├── Scarb.toml
├── src
│ ├── lib.cairo
│ └── simple_storage.cairo
└── target
├── CACHEDIR.TAG
└── dev
├── simple_storage.starknet_artifacts.json
├── simple_storage_simple_storage.compiled_contract_class.json
└── simple_storage_simple_storage.contract_class.json
Result
Code
For verified contracts, users can review the overall structure, see where each file is located, and examine how the code in each file is written. Even users who are not familiar with reading code can understand what the code does using AI code assistance.
Interact
Users can directly interact with the contract simply by connecting their wallet.
Before initiating a transaction, users can review the code and use AI assistance to understand the functions and features of the contract. Keep in mind that AI code assistance is still a developing tool and may have limitations. While it can provide useful insights, it should be used as a reference rather than a definitive guide.