2024-08-13 09:11:24 +00:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
2024-09-19 18:24:06 +00:00
|
|
|
"context"
|
2024-08-13 09:11:24 +00:00
|
|
|
"database/sql"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
persistence "github.com/status-im/status-go/services/connector/database"
|
|
|
|
"github.com/status-im/status-go/signal"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
ErrInvalidParamsStructure = errors.New("invalid params structure")
|
|
|
|
)
|
|
|
|
|
|
|
|
type PersonalSignCommand struct {
|
|
|
|
Db *sql.DB
|
|
|
|
ClientHandler ClientSideHandlerInterface
|
|
|
|
}
|
|
|
|
|
|
|
|
type PersonalSignParams struct {
|
|
|
|
Challenge string `json:"challenge"`
|
|
|
|
Address string `json:"address"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RPCRequest) getPersonalSignParams() (*PersonalSignParams, error) {
|
|
|
|
if r.Params == nil || len(r.Params) == 0 {
|
|
|
|
return nil, ErrEmptyRPCParams
|
|
|
|
}
|
|
|
|
|
2024-11-11 14:09:01 +00:00
|
|
|
if len(r.Params) < 2 {
|
2024-08-13 09:11:24 +00:00
|
|
|
return nil, ErrInvalidParamsStructure
|
|
|
|
}
|
|
|
|
|
2024-11-11 14:09:01 +00:00
|
|
|
// Extract the Challenge and Address fields from paramsArray
|
|
|
|
challenge, ok := r.Params[0].(string)
|
2024-08-13 09:11:24 +00:00
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("missing or invalid 'challenge' field")
|
|
|
|
}
|
|
|
|
|
2024-11-11 14:09:01 +00:00
|
|
|
address, ok := r.Params[1].(string)
|
2024-08-13 09:11:24 +00:00
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("missing or invalid 'address' field")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create and return the PersonalSignParams
|
|
|
|
return &PersonalSignParams{
|
|
|
|
Challenge: challenge,
|
|
|
|
Address: address,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2024-09-19 18:24:06 +00:00
|
|
|
func (c *PersonalSignCommand) Execute(ctx context.Context, request RPCRequest) (interface{}, error) {
|
2024-08-13 09:11:24 +00:00
|
|
|
err := request.Validate()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
params, err := request.getPersonalSignParams()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
dApp, err := persistence.SelectDAppByUrl(c.Db, request.URL)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if dApp == nil {
|
|
|
|
return "", ErrDAppIsNotPermittedByUser
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.ClientHandler.RequestPersonalSign(signal.ConnectorDApp{
|
|
|
|
URL: request.URL,
|
|
|
|
Name: request.Name,
|
|
|
|
IconURL: request.IconURL,
|
|
|
|
}, params.Challenge, params.Address)
|
|
|
|
}
|