feat: validate mnemonic (#18)

This commit is contained in:
Igor Sirotin 2025-02-06 19:54:11 +00:00 committed by GitHub
parent 9569b66ab8
commit 49e71ae4bb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 39 additions and 14 deletions

1
go.mod
View File

@ -10,6 +10,7 @@ require (
github.com/gorilla/websocket v1.4.2
github.com/pkg/errors v0.9.1
github.com/status-im/keycard-go v0.3.3
github.com/tyler-smith/go-bip39 v1.0.1-0.20181017060643-dbb3b84ba2ef
go.uber.org/zap v1.27.0
golang.org/x/crypto v0.32.0
golang.org/x/text v0.21.0

2
go.sum
View File

@ -38,6 +38,8 @@ github.com/status-im/keycard-go v0.3.3 h1:qk/JHSkT9sMka+lVXrTOIVSgHIY7lDm46wrUqT
github.com/status-im/keycard-go v0.3.3/go.mod h1:wlp8ZLbsmrF6g6WjugPAx+IzoLrkdf9+mHxBEeo3Hbg=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/tyler-smith/go-bip39 v1.0.1-0.20181017060643-dbb3b84ba2ef h1:wHSqTBrZW24CsNJDfeh9Ex6Pm0Rcpc7qrgKBiL44vF4=
github.com/tyler-smith/go-bip39 v1.0.1-0.20181017060643-dbb3b84ba2ef/go.mod h1:sJ5fKU0s6JVwZjjcUEX2zFOnvq0ASQ2K9Zr6cf67kNs=
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
go.uber.org/multierr v1.10.0 h1:S0h4aNzvfcFsC3dRF1jLoaov7oRaKqRGC/pUEJ2yvPQ=

View File

@ -1,9 +1,6 @@
package session
import (
goerrors "errors"
"github.com/go-playground/validator/v10"
"github.com/pkg/errors"
"github.com/status-im/status-keycard-go/internal"
@ -12,18 +9,8 @@ import (
var (
errKeycardServiceNotStarted = errors.New("keycard service not started")
validate = validator.New()
)
func validateRequest(v interface{}) error {
err := validate.Struct(v)
if err != nil {
errs := err.(validator.ValidationErrors)
return goerrors.Join(errs)
}
return nil
}
type KeycardService struct {
keycardContext *internal.KeycardContextV2
simulateError error
@ -191,7 +178,7 @@ func (s *KeycardService) GenerateMnemonic(args *GenerateMnemonicRequest, reply *
}
type LoadMnemonicRequest struct {
Mnemonic string `json:"mnemonic" validate:"required"`
Mnemonic string `json:"mnemonic" validate:"required,mnemonic"`
Passphrase string `json:"passphrase"`
}

35
pkg/session/validate.go Normal file
View File

@ -0,0 +1,35 @@
package session
import (
goerrors "errors"
"github.com/go-playground/validator/v10"
"github.com/tyler-smith/go-bip39"
)
var (
validate = validator.New()
)
func init() {
// Register the custom validation function
err := validate.RegisterValidation("mnemonic", isMnemonic)
if err != nil {
panic(err)
}
}
func validateRequest(v interface{}) error {
err := validate.Struct(v)
if err != nil {
errs := err.(validator.ValidationErrors)
return goerrors.Join(errs)
}
return nil
}
// Custom validation function to check if a string is a list of space-separated words
func isMnemonic(fl validator.FieldLevel) bool {
mnemonic := fl.Field().String()
return bip39.IsMnemonicValid(mnemonic)
}