Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
cherry-yl-sh committed Mar 13, 2024
1 parent 38ea028 commit 23f1117
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 5 deletions.
33 changes: 33 additions & 0 deletions common/model/user_operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down
3 changes: 2 additions & 1 deletion common/utils/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
6 changes: 4 additions & 2 deletions docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,12 +191,14 @@ const docTemplate = `{
"enum": [
"ethereum",
"sepolia",
"arbitrum"
"arbitrum",
"arb-sepolia"
],
"x-enum-varnames": [
"Ethereum",
"Sepolia",
"Arbitrum"
"Arbitrum",
"ArbTest"
]
}
},
Expand Down
6 changes: 4 additions & 2 deletions docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -180,12 +180,14 @@
"enum": [
"ethereum",
"sepolia",
"arbitrum"
"arbitrum",
"arb-sepolia"
],
"x-enum-varnames": [
"Ethereum",
"Sepolia",
"Arbitrum"
"Arbitrum",
"ArbTest"
]
}
},
Expand Down
2 changes: 2 additions & 0 deletions docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@ definitions:
- ethereum
- sepolia
- arbitrum
- arb-sepolia
type: string
x-enum-varnames:
- Ethereum
- Sepolia
- Arbitrum
- ArbTest
info:
contact:
name: AAStar Support
Expand Down

0 comments on commit 23f1117

Please sign in to comment.