Skip to content

Commit

Permalink
Implement CompiledClasHash
Browse files Browse the repository at this point in the history
  • Loading branch information
rianhughes committed Sep 28, 2023
1 parent 314f547 commit 81d488e
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 31 deletions.
77 changes: 46 additions & 31 deletions hash/hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package hash
import (
"github.com/NethermindEth/juno/core/felt"
starknetgo "github.com/NethermindEth/starknet.go"
newcontract "github.com/NethermindEth/starknet.go/newcontracts"
"github.com/NethermindEth/starknet.go/rpc"
"github.com/NethermindEth/starknet.go/utils"
)
Expand Down Expand Up @@ -79,40 +80,54 @@ func ClassHash(contract rpc.ContractClass) (*felt.Felt, error) {
)
}

func hashEntryPointByType(qwe []rpc.SierraEntryPoint) (*felt.Felt, error) {
func hashEntryPointByType(entryPoint []rpc.SierraEntryPoint) (*felt.Felt, error) {
flattened := []*felt.Felt{}
for _, elt := range qwe {
for _, elt := range entryPoint {
flattened = append(flattened, elt.Selector, new(felt.Felt).SetUint64(uint64(elt.FunctionIdx)))
}
return ComputeHashOnElementsFelt(flattened)
}

// func CompiledClassHash() (*felt.Felt, error) {
// // https://github.com/software-mansion/starknet.py/blob/development/starknet_py/hash/casm_class_hash.py#L10
// panic("CompiledClassHash needs implementing")
// ContractClassVersionHash := new(felt.Felt).SetBytes([]byte(contract.ContractClassVersion))
// ExternalHash, err := hashEntryPointByType(contract.EntryPointsByType.External)
// if err != nil {
// return nil, err
// }
// L1HandleHash, err := hashEntryPointByType(contract.EntryPointsByType.L1Handler)
// if err != nil {
// return nil, err
// }
// ConstructorHash, err := hashEntryPointByType(contract.EntryPointsByType.Constructor)
// if err != nil {
// return nil, err
// }
// ByteCodeHasH, err := hashEntryPointByType(contract.ByteCode)
// if err != nil {
// return nil, err
// }
// return ComputeHashOnElementsFelt(
// []*felt.Felt{
// ContractClassVersionHash,
// ExternalHash,
// L1HandleHash,
// ConstructorHash,
// ByteCodeHasH},
// )
// }
func CompiledClassHash(casmClass newcontract.CasmClass) (*felt.Felt, error) {
ContractClassVersionHash := new(felt.Felt).SetBytes([]byte(casmClass.Version))
ExternalHash, err := hashCasmClassEntryPointByType(casmClass.EntryPointByType.External)
if err != nil {
return nil, err
}
L1HandleHash, err := hashCasmClassEntryPointByType(casmClass.EntryPointByType.L1Handler)
if err != nil {
return nil, err
}
ConstructorHash, err := hashCasmClassEntryPointByType(casmClass.EntryPointByType.Constructor)
if err != nil {
return nil, err
}
ByteCodeHasH, err := ComputeHashOnElementsFelt(casmClass.ByteCode)
if err != nil {
return nil, err
}
// https://github.com/software-mansion/starknet.py/blob/development/starknet_py/hash/casm_class_hash.py#L10
return ComputeHashOnElementsFelt(
[]*felt.Felt{
ContractClassVersionHash,
ExternalHash,
L1HandleHash,
ConstructorHash,
ByteCodeHasH},
)
}
func hashCasmClassEntryPointByType(entryPoint []newcontract.CasmClassEntryPoint) (*felt.Felt, error) {
flattened := []*felt.Felt{}
for _, elt := range entryPoint {
builtInFlat := []*felt.Felt{}
for _, builtIn := range elt.Builtins {
builtInFlat = append(builtInFlat, new(felt.Felt).SetBytes([]byte(builtIn)))
}
builtInHash, err := ComputeHashOnElementsFelt(builtInFlat)
if err != nil {
return nil, err
}
flattened = append(flattened, elt.Selector, new(felt.Felt).SetUint64(uint64(elt.Offset)), builtInHash)
}
return ComputeHashOnElementsFelt(flattened)
}
12 changes: 12 additions & 0 deletions newcontracts/contract_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"

"github.com/NethermindEth/starknet.go/artifacts"
"github.com/NethermindEth/starknet.go/hash"
newcontract "github.com/NethermindEth/starknet.go/newcontracts"
"github.com/NethermindEth/starknet.go/rpc"
"github.com/test-go/testify/require"
Expand All @@ -29,3 +30,14 @@ func TestUnmarshalCasmClass(t *testing.T) {
require.Equal(t, casmClass.EntryPointByType.External[1].Offset, 111)
require.Equal(t, casmClass.EntryPointByType.External[1].Builtins[0], "range_check")
}

func TestCompiledClassHash(t *testing.T) {
expectedHash := "0x_todo_"

casmClass, err := newcontract.UnmarshalCasmClass("../artifacts/starknet_hello_world_Balance.casm.json")
require.NoError(t, err)

hash, err := hash.CompiledClassHash(*casmClass)
require.NoError(t, err)
require.Equal(t, expectedHash, hash.String())
}

0 comments on commit 81d488e

Please sign in to comment.