2024-06-24 14:29:40 +00:00
|
|
|
package connector
|
|
|
|
|
2024-07-12 18:12:14 +00:00
|
|
|
import (
|
2024-09-19 18:24:06 +00:00
|
|
|
"context"
|
2024-08-02 07:43:31 +00:00
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
2024-07-18 15:30:10 +00:00
|
|
|
"github.com/status-im/status-go/services/connector/commands"
|
|
|
|
persistence "github.com/status-im/status-go/services/connector/database"
|
2024-07-12 18:12:14 +00:00
|
|
|
)
|
|
|
|
|
2024-08-02 07:43:31 +00:00
|
|
|
var (
|
|
|
|
ErrInvalidResponseFromForwardedRpc = errors.New("invalid response from forwarded RPC")
|
|
|
|
)
|
|
|
|
|
2024-07-12 18:12:14 +00:00
|
|
|
type API struct {
|
|
|
|
s *Service
|
|
|
|
r *CommandRegistry
|
2024-07-18 15:30:10 +00:00
|
|
|
c *commands.ClientSideHandler
|
2024-07-12 18:12:14 +00:00
|
|
|
}
|
|
|
|
|
2024-06-24 14:29:40 +00:00
|
|
|
func NewAPI(s *Service) *API {
|
2024-07-12 18:12:14 +00:00
|
|
|
r := NewCommandRegistry()
|
2024-11-05 14:00:28 +00:00
|
|
|
c := commands.NewClientSideHandler(s.db)
|
2024-07-18 15:30:10 +00:00
|
|
|
|
2024-08-13 09:11:24 +00:00
|
|
|
// Transactions and signing
|
2024-07-18 15:30:10 +00:00
|
|
|
r.Register("eth_sendTransaction", &commands.SendTransactionCommand{
|
2024-09-19 18:24:06 +00:00
|
|
|
RpcClient: s.rpc,
|
2024-07-18 15:30:10 +00:00
|
|
|
Db: s.db,
|
|
|
|
ClientHandler: c,
|
|
|
|
})
|
2024-11-19 12:12:08 +00:00
|
|
|
r.Register("personal_sign", &commands.SignCommand{
|
|
|
|
Db: s.db,
|
|
|
|
ClientHandler: c,
|
|
|
|
})
|
|
|
|
r.Register("eth_signTypedData_v4", &commands.SignCommand{
|
2024-08-13 09:11:24 +00:00
|
|
|
Db: s.db,
|
|
|
|
ClientHandler: c,
|
|
|
|
})
|
2024-07-18 15:30:10 +00:00
|
|
|
|
|
|
|
// Accounts query and dapp permissions
|
2024-08-06 19:28:55 +00:00
|
|
|
// NOTE: Some dApps expect same behavior for both eth_accounts and eth_requestAccounts
|
|
|
|
accountsCommand := &commands.RequestAccountsCommand{
|
2024-08-15 11:49:29 +00:00
|
|
|
ClientHandler: c,
|
|
|
|
Db: s.db,
|
2024-08-06 19:28:55 +00:00
|
|
|
}
|
|
|
|
r.Register("eth_accounts", accountsCommand)
|
|
|
|
r.Register("eth_requestAccounts", accountsCommand)
|
2024-07-18 15:30:10 +00:00
|
|
|
|
|
|
|
// Active chain per dapp management
|
2024-07-26 16:18:01 +00:00
|
|
|
r.Register("eth_chainId", &commands.ChainIDCommand{
|
|
|
|
Db: s.db,
|
|
|
|
NetworkManager: s.nm,
|
|
|
|
})
|
2024-07-18 15:30:10 +00:00
|
|
|
r.Register("wallet_switchEthereumChain", &commands.SwitchEthereumChainCommand{
|
|
|
|
Db: s.db,
|
|
|
|
NetworkManager: s.nm,
|
|
|
|
})
|
2024-07-12 18:12:14 +00:00
|
|
|
|
2024-08-06 19:28:55 +00:00
|
|
|
// Permissions
|
2024-07-25 16:02:26 +00:00
|
|
|
r.Register("wallet_requestPermissions", &commands.RequestPermissionsCommand{})
|
2024-08-06 19:28:55 +00:00
|
|
|
r.Register("wallet_revokePermissions", &commands.RevokePermissionsCommand{
|
|
|
|
Db: s.db,
|
|
|
|
})
|
2024-07-25 16:02:26 +00:00
|
|
|
|
2024-06-24 14:29:40 +00:00
|
|
|
return &API{
|
|
|
|
s: s,
|
2024-07-12 18:12:14 +00:00
|
|
|
r: r,
|
2024-07-18 15:30:10 +00:00
|
|
|
c: c,
|
2024-06-24 14:29:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-19 18:24:06 +00:00
|
|
|
func (api *API) forwardRPC(URL string, request commands.RPCRequest) (interface{}, error) {
|
2024-08-02 07:43:31 +00:00
|
|
|
dApp, err := persistence.SelectDAppByUrl(api.s.db, URL)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if dApp == nil {
|
|
|
|
return "", commands.ErrDAppIsNotPermittedByUser
|
|
|
|
}
|
|
|
|
|
2024-09-19 18:24:06 +00:00
|
|
|
if request.ChainID != dApp.ChainID {
|
|
|
|
request.ChainID = dApp.ChainID
|
|
|
|
}
|
|
|
|
|
2024-08-02 07:43:31 +00:00
|
|
|
var response map[string]interface{}
|
2024-09-19 18:24:06 +00:00
|
|
|
byteRequest, err := json.Marshal(request)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
rawResponse := api.s.rpc.CallRaw(string(byteRequest))
|
2024-08-02 07:43:31 +00:00
|
|
|
if err := json.Unmarshal([]byte(rawResponse), &response); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if errorField, ok := response["error"]; ok {
|
|
|
|
errorMap, _ := errorField.(map[string]interface{})
|
|
|
|
errorCode, _ := errorMap["code"].(float64)
|
|
|
|
errorMessage, _ := errorMap["message"].(string)
|
|
|
|
return nil, fmt.Errorf("error code %v: %s", errorCode, errorMessage)
|
|
|
|
}
|
|
|
|
|
|
|
|
if result, ok := response["result"]; ok {
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, ErrInvalidResponseFromForwardedRpc
|
|
|
|
}
|
|
|
|
|
2024-09-19 18:24:06 +00:00
|
|
|
func (api *API) CallRPC(ctx context.Context, inputJSON string) (interface{}, error) {
|
2024-07-18 15:30:10 +00:00
|
|
|
request, err := commands.RPCRequestFromJSON(inputJSON)
|
2024-07-12 18:12:14 +00:00
|
|
|
if err != nil {
|
2024-07-18 15:30:10 +00:00
|
|
|
return "", err
|
2024-07-12 18:12:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if command, exists := api.r.GetCommand(request.Method); exists {
|
2024-09-19 18:24:06 +00:00
|
|
|
return command.Execute(ctx, request)
|
2024-07-12 18:12:14 +00:00
|
|
|
}
|
|
|
|
|
2024-09-19 18:24:06 +00:00
|
|
|
return api.forwardRPC(request.URL, request)
|
2024-07-18 15:30:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (api *API) RecallDAppPermission(origin string) error {
|
2024-08-15 11:49:29 +00:00
|
|
|
// TODO: close the websocket connection
|
2024-11-05 14:00:28 +00:00
|
|
|
return api.c.RecallDAppPermissions(commands.RecallDAppPermissionsArgs{URL: origin})
|
2024-07-18 15:30:10 +00:00
|
|
|
}
|
|
|
|
|
2024-08-06 19:28:55 +00:00
|
|
|
func (api *API) GetPermittedDAppsList() ([]persistence.DApp, error) {
|
|
|
|
return persistence.SelectAllDApps(api.s.db)
|
|
|
|
}
|
|
|
|
|
2024-07-18 15:30:10 +00:00
|
|
|
func (api *API) RequestAccountsAccepted(args commands.RequestAccountsAcceptedArgs) error {
|
|
|
|
return api.c.RequestAccountsAccepted(args)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (api *API) RequestAccountsRejected(args commands.RejectedArgs) error {
|
|
|
|
return api.c.RequestAccountsRejected(args)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (api *API) SendTransactionAccepted(args commands.SendTransactionAcceptedArgs) error {
|
|
|
|
return api.c.SendTransactionAccepted(args)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (api *API) SendTransactionRejected(args commands.RejectedArgs) error {
|
|
|
|
return api.c.SendTransactionRejected(args)
|
2024-06-24 14:29:40 +00:00
|
|
|
}
|
2024-08-13 09:11:24 +00:00
|
|
|
|
2024-11-19 12:12:08 +00:00
|
|
|
func (api *API) SignAccepted(args commands.SignAcceptedArgs) error {
|
|
|
|
return api.c.SignAccepted(args)
|
2024-08-13 09:11:24 +00:00
|
|
|
}
|
|
|
|
|
2024-11-19 12:12:08 +00:00
|
|
|
func (api *API) SignRejected(args commands.RejectedArgs) error {
|
|
|
|
return api.c.SignRejected(args)
|
2024-08-13 09:11:24 +00:00
|
|
|
}
|