Skip to content

Commit

Permalink
check conversions (#1936)
Browse files Browse the repository at this point in the history
  • Loading branch information
tudor-malene authored May 24, 2024
1 parent b97d0b9 commit 5780140
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 56 deletions.
74 changes: 20 additions & 54 deletions go/common/gethencoding/geth_encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,54 +77,6 @@ func NewGethEncodingService(storage storage.Storage, logger gethlog.Logger) Enco
}
}

// ExtractEthCallMapString extracts the eth_call gethapi.TransactionArgs from an interface{}
// it ensures that :
// - All types are string
// - All keys are lowercase
// - There is only one key per value
// - From field is set by default
func ExtractEthCallMapString(paramBytes interface{}) (map[string]string, error) {
// geth lowercase the field name and uses the last seen value
var valString string
var ok bool
callMsg := map[string]string{
// From field is set by default
"from": gethcommon.HexToAddress("0x0").Hex(),
}
for field, val := range paramBytes.(map[string]interface{}) {
if val == nil {
continue
}
valString, ok = val.(string)
if !ok {
return nil, fmt.Errorf("unexpected type supplied in `%s` field", field)
}
if len(strings.TrimSpace(valString)) == 0 {
continue
}
switch strings.ToLower(field) {
case callFieldTo:
callMsg[callFieldTo] = valString
case CallFieldFrom:
callMsg[CallFieldFrom] = valString
case callFieldData, callFieldInput:
callMsg[callFieldInput] = valString
case callFieldValue:
callMsg[callFieldValue] = valString
case callFieldGas:
callMsg[callFieldGas] = valString
case callFieldMaxFeePerGas:
callMsg[callFieldMaxFeePerGas] = valString
case callFieldMaxPriorityFeePerGas:
callMsg[callFieldMaxPriorityFeePerGas] = valString
default:
callMsg[field] = valString
}
}

return callMsg, nil
}

// ExtractAddress returns a gethcommon.Address given an interface{}, errors if unexpected values are used
func ExtractAddress(param interface{}) (*gethcommon.Address, error) {
if param == nil {
Expand All @@ -140,7 +92,7 @@ func ExtractAddress(param interface{}) (*gethcommon.Address, error) {
return nil, fmt.Errorf("no address specified")
}

addr := gethcommon.HexToAddress(param.(string))
addr := gethcommon.HexToAddress(paramStr)
return &addr, nil
}

Expand Down Expand Up @@ -183,10 +135,13 @@ func ExtractBlockNumber(param interface{}) (*gethrpc.BlockNumberOrHash, error) {

blockAndHash, ok := param.(map[string]any)
if !ok {
return nil, fmt.Errorf("invalid block or hash parameter %s", param.(string))
return nil, fmt.Errorf("invalid block or hash parameter")
}
if blockAndHash["blockNumber"] != nil {
b := blockAndHash["blockNumber"].(string)
b, ok := blockAndHash["blockNumber"].(string)
if !ok {
return nil, fmt.Errorf("invalid blockNumber parameter")
}
blockNumber := gethrpc.BlockNumber(0)
err := blockNumber.UnmarshalJSON([]byte(b))
if err != nil {
Expand All @@ -195,11 +150,17 @@ func ExtractBlockNumber(param interface{}) (*gethrpc.BlockNumberOrHash, error) {
blockNo = &blockNumber
}
if blockAndHash["blockHash"] != nil {
bh := blockAndHash["blockHash"].(gethcommon.Hash)
bh, ok := blockAndHash["blockHash"].(gethcommon.Hash)
if !ok {
return nil, fmt.Errorf("invalid blockhash parameter")
}
blockHa = &bh
}
if blockAndHash["RequireCanonical"] != nil {
reqCanon = blockAndHash["RequireCanonical"].(bool)
reqCanon, ok = blockAndHash["RequireCanonical"].(bool)
if !ok {
return nil, fmt.Errorf("invalid RequireCanonical parameter")
}
}

return &gethrpc.BlockNumberOrHash{
Expand All @@ -222,7 +183,12 @@ func ExtractEthCall(param interface{}) (*gethapi.TransactionArgs, error) {
// if gas is not set it should be null
gas := (*hexutil.Uint64)(nil)

for field, val := range param.(map[string]interface{}) {
ethCallMap, ok := param.(map[string]interface{})
if !ok {
return nil, fmt.Errorf("invalid eth call parameter")
}

for field, val := range ethCallMap {
if val == nil {
continue
}
Expand Down
7 changes: 6 additions & 1 deletion go/enclave/rpc/SubmitTx.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package rpc

import (
"errors"
"fmt"

gethcommon "github.com/ethereum/go-ethereum/common"
Expand All @@ -9,7 +10,11 @@ import (
)

func SubmitTxValidate(reqParams []any, builder *CallBuilder[common.L2Tx, gethcommon.Hash], _ *EncryptionManager) error {
l2Tx, err := ExtractTx(reqParams[0].(string))
txStr, ok := reqParams[0].(string)
if !ok {
return errors.New("unsupported format")
}
l2Tx, err := ExtractTx(txStr)
if err != nil {
builder.Err = fmt.Errorf("could not extract transaction. Cause: %w", err)
return nil
Expand Down
5 changes: 4 additions & 1 deletion go/enclave/storage/enclavedb/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,10 @@ func stringToHash(ns sql.NullString) gethcommon.Hash {
if err != nil {
return [32]byte{}
}
s := value.(string)
s, ok := value.(string)
if !ok {
return [32]byte{}
}
result := gethcommon.Hash{}
result.SetBytes([]byte(s))
return result
Expand Down

0 comments on commit 5780140

Please sign in to comment.