Skip to content

Commit

Permalink
chore_: use validator
Browse files Browse the repository at this point in the history
  • Loading branch information
qfrank committed Sep 24, 2024
1 parent 6136704 commit dd9dded
Show file tree
Hide file tree
Showing 9 changed files with 126 additions and 145 deletions.
2 changes: 1 addition & 1 deletion mobile/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ func MigrateKeyStoreDirV2(requestJSON string) string {
}

func migrateKeyStoreDirV2(requestJSON string) string {
var request requests.MigrateKeyStoreDir
var request requests.MigrateKeystoreDir
err := json.Unmarshal([]byte(requestJSON), &request)
if err != nil {
return makeJSONResponse(err)
Expand Down
25 changes: 5 additions & 20 deletions protocol/requests/delete_imported_key.go
Original file line number Diff line number Diff line change
@@ -1,37 +1,22 @@
package requests

import (
"errors"
"gopkg.in/go-playground/validator.v9"
)

// DeleteImportedKey represents a request to delete an imported key.
type DeleteImportedKey struct {
// Address is the address of the imported key to delete.
Address string `json:"address"`
Address string `json:"address" validate:"required"`

// Password is the password used to decrypt the key.
Password string `json:"password"`
Password string `json:"password" validate:"required"`

// KeyStoreDir is the directory where the key is stored.
KeyStoreDir string `json:"keyStoreDir"`
KeyStoreDir string `json:"keyStoreDir" validate:"required"`
}

// Validate checks the validity of the DeleteImportedKey request.
var (
ErrDeleteImportedKeyEmptyAddress = errors.New("delete-imported-key: Address cannot be empty")
ErrDeleteImportedKeyEmptyPassword = errors.New("delete-imported-key: Password cannot be empty")
ErrDeleteImportedKeyEmptyKeyStoreDir = errors.New("delete-imported-key: KeyStoreDir cannot be empty")
)

func (r *DeleteImportedKey) Validate() error {
if r.Address == "" {
return ErrDeleteImportedKeyEmptyAddress
}
if r.Password == "" {
return ErrDeleteImportedKeyEmptyPassword
}
if r.KeyStoreDir == "" {
return ErrDeleteImportedKeyEmptyKeyStoreDir
}
return nil
return validator.New().Struct(r)
}
14 changes: 7 additions & 7 deletions protocol/requests/delete_imported_key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ func TestDeleteImportedKey_Validate(t *testing.T) {
testCases := []struct {
name string
req DeleteImportedKey
expectedErr error
expectedErr string
}{
{
name: "valid request",
Expand All @@ -19,39 +19,39 @@ func TestDeleteImportedKey_Validate(t *testing.T) {
Password: "password",
KeyStoreDir: "/keystore/dir",
},
expectedErr: nil,
},
{
name: "empty address",
req: DeleteImportedKey{
Password: "password",
KeyStoreDir: "/keystore/dir",
},
expectedErr: ErrDeleteImportedKeyEmptyAddress,
expectedErr: "Address",
},
{
name: "empty password",
req: DeleteImportedKey{
Address: "0x1234567890123456789012345678901234567890",
KeyStoreDir: "/keystore/dir",
},
expectedErr: ErrDeleteImportedKeyEmptyPassword,
expectedErr: "Password",
},
{
name: "empty keystore dir",
req: DeleteImportedKey{
Address: "0x1234567890123456789012345678901234567890",
Password: "password",
},
expectedErr: ErrDeleteImportedKeyEmptyKeyStoreDir,
expectedErr: "KeyStoreDir",
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
err := tc.req.Validate()
if tc.expectedErr != nil {
require.Equal(t, tc.expectedErr, err)
if tc.expectedErr != "" {
t.Log("err", err.Error())
require.Contains(t, err.Error(), tc.expectedErr)
} else {
require.NoError(t, err)
}
Expand Down
46 changes: 13 additions & 33 deletions protocol/requests/migrate_keystore_dir.go
Original file line number Diff line number Diff line change
@@ -1,46 +1,26 @@
package requests

import (
"errors"

"github.com/status-im/status-go/multiaccounts"
"gopkg.in/go-playground/validator.v9"
)

// MigrateKeyStoreDir represents a request to migrate key files to a new directory.
type MigrateKeyStoreDir struct {
// Account represents the account associated with the key files.
// MigrateKeystoreDir represents a request to migrate keystore directory.
type MigrateKeystoreDir struct {
// Account is the account associated with the keystore.
Account multiaccounts.Account `json:"account"`

// Password is the password used to decrypt the key files.
Password string `json:"password"`
// Password is the password for the keystore.
Password string `json:"password" validate:"required"`

// OldDir is the old directory path where the key files are currently located.
OldDir string `json:"oldDir"`
// OldDir is the old keystore directory.
OldDir string `json:"oldDir" validate:"required"`

// NewDir is the new directory path where the key files will be migrated to.
NewDir string `json:"newDir"`
// NewDir is the new keystore directory.
NewDir string `json:"newDir" validate:"required"`
}

// Validate checks the validity of the MigrateKeyStoreDir request.
var (
ErrMigrateKeyStoreDirEmptyAccount = errors.New("migrate-keystore-dir: Account cannot be empty")
ErrMigrateKeyStoreDirEmptyPassword = errors.New("migrate-keystore-dir: Password cannot be empty")
ErrMigrateKeyStoreDirEmptyOldDir = errors.New("migrate-keystore-dir: OldDir cannot be empty")
ErrMigrateKeyStoreDirEmptyNewDir = errors.New("migrate-keystore-dir: NewDir cannot be empty")
)

func (r *MigrateKeyStoreDir) Validate() error {
if r.Account.KeyUID == "" {
return ErrMigrateKeyStoreDirEmptyAccount
}
if r.Password == "" {
return ErrMigrateKeyStoreDirEmptyPassword
}
if r.OldDir == "" {
return ErrMigrateKeyStoreDirEmptyOldDir
}
if r.NewDir == "" {
return ErrMigrateKeyStoreDirEmptyNewDir
}
return nil
// Validate checks the validity of the MigrateKeystoreDir request.
func (r *MigrateKeystoreDir) Validate() error {
return validator.New().Struct(r)
}
64 changes: 31 additions & 33 deletions protocol/requests/migrate_keystore_dir_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,70 +3,68 @@ package requests
import (
"testing"

"github.com/stretchr/testify/require"

"github.com/status-im/status-go/multiaccounts"
"github.com/stretchr/testify/require"
)

func TestMigrateKeyStoreDir_Validate(t *testing.T) {
func TestMigrateKeystoreDir_Validate(t *testing.T) {
testCases := []struct {
name string
req MigrateKeyStoreDir
expectedErr error
req MigrateKeystoreDir
expectedErr string
}{
{
name: "valid request",
req: MigrateKeyStoreDir{
Account: multiaccounts.Account{KeyUID: "0x1234"},
Password: "password",
OldDir: "/old/dir",
NewDir: "/new/dir",
req: MigrateKeystoreDir{
Account: multiaccounts.Account{Name: "test-account"},
Password: "test-password",
OldDir: "/old/keystore/dir",
NewDir: "/new/keystore/dir",
},
expectedErr: nil,
},
{
name: "empty account",
req: MigrateKeyStoreDir{
Password: "password",
OldDir: "/old/dir",
NewDir: "/new/dir",
req: MigrateKeystoreDir{
Password: "test-password",
OldDir: "/old/keystore/dir",
NewDir: "/new/keystore/dir",
},
expectedErr: ErrMigrateKeyStoreDirEmptyAccount,
},
{
name: "empty password",
req: MigrateKeyStoreDir{
Account: multiaccounts.Account{KeyUID: "0x1234"},
OldDir: "/old/dir",
NewDir: "/new/dir",
req: MigrateKeystoreDir{
Account: multiaccounts.Account{Name: "test-account"},
OldDir: "/old/keystore/dir",
NewDir: "/new/keystore/dir",
},
expectedErr: ErrMigrateKeyStoreDirEmptyPassword,
expectedErr: "Password",
},
{
name: "empty old dir",
req: MigrateKeyStoreDir{
Account: multiaccounts.Account{KeyUID: "0x1234"},
Password: "password",
NewDir: "/new/dir",
req: MigrateKeystoreDir{
Account: multiaccounts.Account{Name: "test-account"},
Password: "test-password",
NewDir: "/new/keystore/dir",
},
expectedErr: ErrMigrateKeyStoreDirEmptyOldDir,
expectedErr: "OldDir",
},
{
name: "empty new dir",
req: MigrateKeyStoreDir{
Account: multiaccounts.Account{KeyUID: "0x1234"},
Password: "password",
OldDir: "/old/dir",
req: MigrateKeystoreDir{
Account: multiaccounts.Account{Name: "test-account"},
Password: "test-password",
OldDir: "/old/keystore/dir",
},
expectedErr: ErrMigrateKeyStoreDirEmptyNewDir,
expectedErr: "NewDir",
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
err := tc.req.Validate()
if tc.expectedErr != nil {
require.Equal(t, tc.expectedErr, err)
if tc.expectedErr != "" {
require.Error(t, err)
require.Contains(t, err.Error(), tc.expectedErr)
} else {
require.NoError(t, err)
}
Expand Down
29 changes: 12 additions & 17 deletions protocol/requests/sign_typed_data.go
Original file line number Diff line number Diff line change
@@ -1,39 +1,34 @@
package requests

import (
"errors"
"gopkg.in/go-playground/validator.v9"

"github.com/status-im/status-go/services/typeddata"
)

// SignTypedData represents a request to sign typed data.
type SignTypedData struct {
// TypedData is the typed data to sign.
TypedData typeddata.TypedData `json:"typedData"`
TypedData typeddata.TypedData `json:"typedData" validate:"required"`

// Address is the address of the account to sign with.
Address string `json:"address"`
Address string `json:"address" validate:"required"`

// Password is the password of the account to sign with.
Password string `json:"password"`
Password string `json:"password" validate:"required"`
}

// Validate checks the validity of the SignTypedData request.
var (
ErrSignTypedDataEmptyTypedData = errors.New("sign-typed-data: TypedData cannot be empty")
ErrSignTypedDataEmptyAddress = errors.New("sign-typed-data: Address cannot be empty")
ErrSignTypedDataEmptyPassword = errors.New("sign-typed-data: Password cannot be empty")
)

func (r *SignTypedData) Validate() error {
if err := r.TypedData.Validate(); err != nil {
return ErrSignTypedDataEmptyTypedData
}
if r.Address == "" {
return ErrSignTypedDataEmptyAddress
// Use the validator package to validate the struct fields
if err := validator.New().Struct(r); err != nil {
return err
}
if r.Password == "" {
return ErrSignTypedDataEmptyPassword

// Additional validation logic from the old signTypedData function
if err := r.TypedData.Validate(); err != nil {
return err
}

return nil
}
Loading

0 comments on commit dd9dded

Please sign in to comment.