diff --git a/rpc/class.go b/rpc/class.go index c51a942500..fd1bf2c1f9 100644 --- a/rpc/class.go +++ b/rpc/class.go @@ -56,8 +56,14 @@ func adaptDeclaredClass(declaredClass json.RawMessage) (core.Class, error) { } return sn2core.AdaptCairo1Class(feederClass.V1, compiledClass) case feederClass.V0 != nil: + program := feederClass.V0.Program + // strip the quotes - base64Program := string(feederClass.V0.Program[1 : len(feederClass.V0.Program)-1]) + if len(program) < 2 { + return nil, errors.New("invalid program") + } + base64Program := string(program[1 : len(program)-1]) + feederClass.V0.Program, err = utils.Gzip64Decode(base64Program) if err != nil { return nil, err diff --git a/rpc/estimate_fee_test.go b/rpc/estimate_fee_test.go index fdc3699a8e..131a702600 100644 --- a/rpc/estimate_fee_test.go +++ b/rpc/estimate_fee_test.go @@ -14,6 +14,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "go.uber.org/mock/gomock" + "github.com/NethermindEth/juno/jsonrpc" ) func TestEstimateFee(t *testing.T) { @@ -65,6 +66,32 @@ func TestEstimateFee(t *testing.T) { }), err) require.Equal(t, httpHeader.Get(rpc.ExecutionStepsHeader), "0") }) + + t.Run("transaction with invalid contract class", func(t *testing.T) { + toFelt := func(hex string) *felt.Felt { + return utils.HexToFelt(t, hex) + } + invalidTx := rpc.BroadcastedTransaction{ + Transaction: rpc.Transaction{ + Type: rpc.TxnDeclare, + Version: toFelt("0x1"), + Nonce: toFelt("0x0"), + MaxFee: toFelt("0x1"), + SenderAddress: toFelt("0x2"), + Signature: &[]*felt.Felt{ + toFelt("0x123"), + }, + }, + ContractClass: json.RawMessage(`{}`), + } + _, _, err := handler.EstimateFee([]rpc.BroadcastedTransaction{invalidTx}, []rpc.SimulationFlag{}, rpc.BlockID{Latest: true}) + expectedErr := &jsonrpc.Error{ + Code: jsonrpc.InvalidParams, + Message: "Invalid Params", + Data: "invalid program", + } + require.Equal(t, expectedErr, err) + }) } func assertEqualCairo0Class(t *testing.T, cairo0Class *core.Cairo0Class, class *rpc.Class) {