UnmarshalCasmClass
Loads and unmarshals a CASM (Cairo Assembly) class from a JSON file.
Function Signature
func UnmarshalCasmClass(filePath string) (*CasmClass, error)Parameters
filePath(string): Path to the .casm.json file
Returns
*CasmClass: Pointer to the unmarshaled CASM classerror: Error if file reading or unmarshaling fails
Description
UnmarshalCasmClass reads a compiled Cairo contract (CASM format) from a JSON file and unmarshals it into a CasmClass struct. CASM is the Cairo Assembly format - the compiled bytecode that runs on the Starknet VM.
CasmClass Structure
type CasmClass struct {
EntryPointsByType CasmEntryPointsByType
ByteCode []*felt.Felt
Prime NumAsHex
CompilerVersion string
Hints []Hints
BytecodeSegmentLengths *NestedUints
}Usage Example
package main
import (
"fmt"
"log"
"github.com/NethermindEth/starknet.go/contracts"
)
func main() {
// Load CASM class from file
casmClass, err := contracts.UnmarshalCasmClass("contract.casm.json")
if err != nil {
log.Fatal("Failed to load CASM class:", err)
}
fmt.Printf("Compiler Version: %s\n", casmClass.CompilerVersion)
fmt.Printf("Bytecode Length: %d instructions\n", len(casmClass.ByteCode))
fmt.Printf("External Entry Points: %d\n", len(casmClass.EntryPointsByType.External))
fmt.Printf("Constructor Entry Points: %d\n", len(casmClass.EntryPointsByType.Constructor))
}Use Cases
1. Contract Deployment
// Load CASM for deployment
casmClass, err := contracts.UnmarshalCasmClass("my_contract.casm.json")
if err != nil {
return err
}
// Calculate compiled class hash
compiledHash, _ := hash.CompiledClassHash(casmClass)2. Contract Analysis
// Analyze contract structure
casmClass, _ := contracts.UnmarshalCasmClass("contract.casm.json")
fmt.Printf("Contract has %d external functions\n",
len(casmClass.EntryPointsByType.External))
for _, ep := range casmClass.EntryPointsByType.External {
fmt.Printf("Function: selector=%s, offset=%d\n",
ep.Selector.String(), ep.Offset)
}3. Declare Transaction
// Prepare declare V2 transaction
sierraClass, _ := loadSierraClass("contract.json")
casmClass, _ := contracts.UnmarshalCasmClass("contract.casm.json")
// Both classes needed for declare V2/V3
compiledClassHash, _ := hash.CompiledClassHash(casmClass)Notes
- CASM files are generated by the Cairo compiler (starknet-compile)
- File must be valid JSON matching the CASM format specification
- The ByteCode field contains the executable instructions
- Entry points map function selectors to bytecode offsets
- Hints provide execution guidance for the VM
Related Functions
- hash.CompiledClassHash - Calculate CASM class hash
- hash.ClassHash - Calculate Sierra class hash

