From 23f1117f7a4e979b45d093032853b996cf1bf9ef Mon Sep 17 00:00:00 2001 From: dylanyang Date: Wed, 13 Mar 2024 20:04:12 +0800 Subject: [PATCH] update --- common/model/user_operation.go | 33 +++++++++++++++++++++++++++++++++ common/utils/util_test.go | 3 ++- docs/docs.go | 6 ++++-- docs/swagger.json | 6 ++++-- docs/swagger.yaml | 2 ++ 5 files changed, 45 insertions(+), 5 deletions(-) diff --git a/common/model/user_operation.go b/common/model/user_operation.go index 827b7b99..cf79132d 100644 --- a/common/model/user_operation.go +++ b/common/model/user_operation.go @@ -3,10 +3,17 @@ package model import ( "encoding/hex" "github.com/ethereum/go-ethereum/common" + "github.com/go-playground/validator/v10" "github.com/mitchellh/mapstructure" "golang.org/x/xerrors" "math/big" "reflect" + "sync" +) + +var ( + validate = validator.New() + onlyOnce = sync.Once{} ) // UserOperation entrypoint v0.0.6 @@ -58,10 +65,36 @@ func NewUserOp(userOp *map[string]any) (*UserOperation, error) { if err := decoder.Decode(userOp); err != nil { return nil, xerrors.Errorf("data [%w] convert failed: [%w]", userOp, err) } + onlyOnce.Do(func() { + validate.RegisterCustomTypeFunc(validateAddressType, common.Address{}) + validate.RegisterCustomTypeFunc(validateBigIntType, big.Int{}) + }) + err = validate.Struct(result) + if err != nil { + return nil, err + } return &result, nil } +func validateAddressType(field reflect.Value) interface{} { + value, ok := field.Interface().(common.Address) + if !ok || value == common.HexToAddress("0x") { + return nil + } + + return field +} + +func validateBigIntType(field reflect.Value) interface{} { + value, ok := field.Interface().(big.Int) + if !ok || value.Cmp(big.NewInt(0)) == -1 { + return nil + } + + return field +} + func exactFieldMatch(mapKey, fieldName string) bool { return mapKey == fieldName } diff --git a/common/utils/util_test.go b/common/utils/util_test.go index 0beff9be..b65aa0e5 100644 --- a/common/utils/util_test.go +++ b/common/utils/util_test.go @@ -31,10 +31,11 @@ func TestSignUserOp(t *testing.T) { } signByte, err := SignUserOp("1d8a58126e87e53edc7b24d58d1328230641de8c4242c135492bf5560e0ff421", userOp) assert.NoError(t, err) + len := len(signByte) + fmt.Printf("signByte len: %d\n", len) fmt.Printf("signByte: %x\n", signByte) singature := hex.EncodeToString(signByte) fmt.Printf("singature: %s\n", singature) - } func TestNewUserOp(t *testing.T) { userOp, newErr := model.NewUserOp(GenerateMockUserOperation()) diff --git a/docs/docs.go b/docs/docs.go index a5400acb..5128b19b 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -191,12 +191,14 @@ const docTemplate = `{ "enum": [ "ethereum", "sepolia", - "arbitrum" + "arbitrum", + "arb-sepolia" ], "x-enum-varnames": [ "Ethereum", "Sepolia", - "Arbitrum" + "Arbitrum", + "ArbTest" ] } }, diff --git a/docs/swagger.json b/docs/swagger.json index 87f9ac7c..a4c6fd73 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -180,12 +180,14 @@ "enum": [ "ethereum", "sepolia", - "arbitrum" + "arbitrum", + "arb-sepolia" ], "x-enum-varnames": [ "Ethereum", "Sepolia", - "Arbitrum" + "Arbitrum", + "ArbTest" ] } }, diff --git a/docs/swagger.yaml b/docs/swagger.yaml index ed1f05be..830a0db3 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -24,11 +24,13 @@ definitions: - ethereum - sepolia - arbitrum + - arb-sepolia type: string x-enum-varnames: - Ethereum - Sepolia - Arbitrum + - ArbTest info: contact: name: AAStar Support