Skip to content

Commit

Permalink
Address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
rianhughes committed Oct 5, 2023
1 parent 94de9d4 commit da283b0
Show file tree
Hide file tree
Showing 9 changed files with 1,222 additions and 34 deletions.
6 changes: 0 additions & 6 deletions artifacts/artifacts.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,6 @@ import (
"github.com/NethermindEth/starknet.go/types"
)

//go:embed hello_starknet_compiled.casm.json
var ExampleWorldCasm []byte

//go:embed hello_starknet_compiled.sierra.json
var ExampleWorldSierra []byte

//go:embed account.json
var AccountCompiled []byte

Expand Down
8 changes: 1 addition & 7 deletions contracts/contracts.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package contracts

import (
"encoding/json"
"io/ioutil"
"os"

"github.com/NethermindEth/juno/core/felt"
Expand All @@ -29,13 +28,8 @@ type CasmClassEntryPoint struct {
}

func UnmarshalCasmClass(filePath string) (*CasmClass, error) {
file, err := os.Open(filePath)
if err != nil {
return nil, err
}
defer file.Close()

content, err := ioutil.ReadAll(file)
content, err := os.ReadFile(filePath)
if err != nil {
return nil, err
}
Expand Down
25 changes: 14 additions & 11 deletions contracts/contracts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,32 @@ package contracts_test

import (
"encoding/json"
"os"
"testing"

"github.com/NethermindEth/starknet.go/artifacts"
"github.com/NethermindEth/starknet.go/contracts"
"github.com/NethermindEth/starknet.go/rpc"
"github.com/test-go/testify/assert"
"github.com/test-go/testify/require"
)

func TestUnmarshalContractClass(t *testing.T) {
compiledClass := artifacts.ExampleWorldSierra
content, err := os.ReadFile("./tests/hello_starknet_compiled.sierra.json")
require.NoError(t, err)

var class rpc.ContractClass
err := json.Unmarshal(compiledClass, &class)
err = json.Unmarshal(content, &class)
require.NoError(t, err)
require.Equal(t, class.SierraProgram[0].String(), "0x1")
require.Equal(t, class.SierraProgram[1].String(), "0x3")
assert.Equal(t, class.SierraProgram[0].String(), "0x1")
assert.Equal(t, class.SierraProgram[1].String(), "0x3")
}

func TestUnmarshalCasmClass(t *testing.T) {
casmClass, err := contracts.UnmarshalCasmClass("../artifacts/hello_starknet_compiled.casm.json")
casmClass, err := contracts.UnmarshalCasmClass("./tests/hello_starknet_compiled.casm.json")
require.NoError(t, err)
require.Equal(t, casmClass.Prime, "0x800000000000011000000000000000000000000000000000000000000000001")
require.Equal(t, casmClass.Version, "2.1.0")
require.Equal(t, casmClass.EntryPointByType.External[0].Selector.String(), "0x362398bec32bc0ebb411203221a35a0301193a96f317ebe5e40be9f60d15320")
require.Equal(t, casmClass.EntryPointByType.External[1].Offset, 130)
require.Equal(t, casmClass.EntryPointByType.External[1].Builtins[0], "range_check")
assert.Equal(t, casmClass.Prime, "0x800000000000011000000000000000000000000000000000000000000000001")
assert.Equal(t, casmClass.Version, "2.1.0")
assert.Equal(t, casmClass.EntryPointByType.External[0].Selector.String(), "0x362398bec32bc0ebb411203221a35a0301193a96f317ebe5e40be9f60d15320")
assert.Equal(t, casmClass.EntryPointByType.External[1].Offset, 130)
assert.Equal(t, casmClass.EntryPointByType.External[1].Builtins[0], "range_check")
}
File renamed without changes.
6 changes: 3 additions & 3 deletions hash/hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/NethermindEth/starknet.go/utils"
)

// computeHashOnElementsFelt hashes the array of felts provided as input
// ComputeHashOnElementsFelt hashes the array of felts provided as input
func ComputeHashOnElementsFelt(feltArr []*felt.Felt) (*felt.Felt, error) {
bigIntArr := utils.FeltArrToBigIntArr(feltArr)

Expand All @@ -19,7 +19,7 @@ func ComputeHashOnElementsFelt(feltArr []*felt.Felt) (*felt.Felt, error) {
return utils.BigIntToFelt(hash), nil
}

// calculateTransactionHashCommon [specification] calculates the transaction hash in the StarkNet network - a unique identifier of the transaction.
// CalculateTransactionHashCommon [specification] calculates the transaction hash in the StarkNet network - a unique identifier of the transaction.
// [specification]: https://github.com/starkware-libs/cairo-lang/blob/master/src/starkware/starknet/core/os/transaction_hash/transaction_hash.py#L27C5-L27C38
func CalculateTransactionHashCommon(
txHashPrefix *felt.Felt,
Expand Down Expand Up @@ -63,7 +63,7 @@ func ClassHash(contract rpc.ContractClass) (*felt.Felt, error) {
}

func hashEntryPointByType(entryPoint []rpc.SierraEntryPoint) *felt.Felt {
flattened := []*felt.Felt{}
flattened := make([]*felt.Felt, 0, len(entryPoint))
for _, elt := range entryPoint {
flattened = append(flattened, elt.Selector, new(felt.Felt).SetUint64(uint64(elt.FunctionIdx)))
}
Expand Down
20 changes: 13 additions & 7 deletions hash/hash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,33 @@ package hash_test

import (
"encoding/json"
"os"
"testing"

"github.com/NethermindEth/starknet.go/artifacts"
"github.com/NethermindEth/starknet.go/contracts"
"github.com/NethermindEth/starknet.go/hash"
"github.com/NethermindEth/starknet.go/rpc"
"github.com/test-go/testify/require"
)

func TestUnmarshalCasmClassHash(t *testing.T) {
compiledClass := artifacts.ExampleWorldCasm
content, err := os.ReadFile("./tests/hello_starknet_compiled.casm.json")
require.NoError(t, err)

var class contracts.CasmClass
err := json.Unmarshal(compiledClass, &class)
err = json.Unmarshal(content, &class)
require.NoError(t, err)
}

func TestClassHash(t *testing.T) {
//https://github.com/software-mansion/starknet.py/blob/development/starknet_py/hash/class_hash_test.py
expectedClasshash := "0x4ec2ecf58014bc2ffd7c84843c3525e5ecb0a2cac33c47e9c347f39fc0c0944"

compiledClass := artifacts.ExampleWorldSierra
content, err := os.ReadFile("./tests/hello_starknet_compiled.sierra.json")
require.NoError(t, err)

var class rpc.ContractClass
err := json.Unmarshal(compiledClass, &class)
err = json.Unmarshal(content, &class)
require.NoError(t, err)
compClassHash, err := hash.ClassHash(class)
require.NoError(t, err)
Expand All @@ -35,9 +39,11 @@ func TestCompiledClassHash(t *testing.T) {
//https://github.com/software-mansion/starknet.py/blob/development/starknet_py/hash/casm_class_hash_test.py
expectedHash := "0x785fa5f2bacf0bfe3bc413be5820a61e1ea63f2ec27ef00331ee9f46ad07603"

compiledClass := artifacts.ExampleWorldCasm
content, err := os.ReadFile("./tests/hello_starknet_compiled.casm.json")
require.NoError(t, err)

var casmClass contracts.CasmClass
err := json.Unmarshal(compiledClass, &casmClass)
err = json.Unmarshal(content, &casmClass)
require.NoError(t, err)

hash := hash.CompiledClassHash(casmClass)
Expand Down
Loading

0 comments on commit da283b0

Please sign in to comment.