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")
|
2024-11-19 12:12:08 +00:00
|
|
|
ErrInvalidMethod = errors.New("invalid method")
|
2024-08-13 09:11:24 +00:00
|
|
|
)
|
|
|
|
|
2024-11-19 12:12:08 +00:00
|
|
|
type SignCommand struct {
|
2024-08-13 09:11:24 +00:00
|
|
|
Db *sql.DB
|
|
|
|
ClientHandler ClientSideHandlerInterface
|
|
|
|
}
|
|
|
|
|
2024-11-19 12:12:08 +00:00
|
|
|
type SignParams struct {
|
2024-08-13 09:11:24 +00:00
|
|
|
Challenge string `json:"challenge"`
|
|
|
|
Address string `json:"address"`
|
2024-11-19 12:12:08 +00:00
|
|
|
Method string `json:"method"`
|
2024-08-13 09:11:24 +00:00
|
|
|
}
|
|
|
|
|
2024-11-19 12:12:08 +00:00
|
|
|
func (r *RPCRequest) getSignParams() (*SignParams, error) {
|
|
|
|
if r.Method != Method_PersonalSign && r.Method != Method_SignTypedDataV4 {
|
|
|
|
return nil, ErrInvalidMethod
|
|
|
|
}
|
|
|
|
|
2024-08-13 09:11:24 +00:00
|
|
|
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-19 12:12:08 +00:00
|
|
|
challengeIndex := 0
|
|
|
|
addressIndex := 1
|
|
|
|
|
|
|
|
if r.Method == Method_SignTypedDataV4 {
|
|
|
|
challengeIndex = 1
|
|
|
|
addressIndex = 0
|
|
|
|
}
|
|
|
|
|
2024-11-11 14:09:01 +00:00
|
|
|
// Extract the Challenge and Address fields from paramsArray
|
2024-11-19 12:12:08 +00:00
|
|
|
challenge, ok := r.Params[challengeIndex].(string)
|
2024-08-13 09:11:24 +00:00
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("missing or invalid 'challenge' field")
|
|
|
|
}
|
|
|
|
|
2024-11-19 12:12:08 +00:00
|
|
|
address, ok := r.Params[addressIndex].(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
|
2024-11-19 12:12:08 +00:00
|
|
|
return &SignParams{
|
2024-08-13 09:11:24 +00:00
|
|
|
Challenge: challenge,
|
|
|
|
Address: address,
|
2024-11-19 12:12:08 +00:00
|
|
|
Method: r.Method,
|
2024-08-13 09:11:24 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2024-11-19 12:12:08 +00:00
|
|
|
func (c *SignCommand) Execute(ctx context.Context, request RPCRequest) (interface{}, error) {
|
2024-08-13 09:11:24 +00:00
|
|
|
err := request.Validate()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2024-11-19 12:12:08 +00:00
|
|
|
params, err := request.getSignParams()
|
2024-08-13 09:11:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
dApp, err := persistence.SelectDAppByUrl(c.Db, request.URL)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if dApp == nil {
|
|
|
|
return "", ErrDAppIsNotPermittedByUser
|
|
|
|
}
|
|
|
|
|
2024-11-19 12:12:08 +00:00
|
|
|
return c.ClientHandler.RequestSign(signal.ConnectorDApp{
|
2024-08-13 09:11:24 +00:00
|
|
|
URL: request.URL,
|
|
|
|
Name: request.Name,
|
|
|
|
IconURL: request.IconURL,
|
2024-11-19 12:12:08 +00:00
|
|
|
}, params.Challenge, params.Address, params.Method)
|
2024-08-13 09:11:24 +00:00
|
|
|
}
|