2024-06-24 14:29:40 +00:00
|
|
|
package connector
|
|
|
|
|
2024-07-12 18:12:14 +00:00
|
|
|
import (
|
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-07-18 15:30:10 +00:00
|
|
|
c := commands.NewClientSideHandler()
|
|
|
|
|
|
|
|
r.Register("eth_sendTransaction", &commands.SendTransactionCommand{
|
|
|
|
Db: s.db,
|
|
|
|
ClientHandler: c,
|
|
|
|
})
|
|
|
|
|
|
|
|
// Accounts query and dapp permissions
|
|
|
|
r.Register("eth_accounts", &commands.AccountsCommand{Db: s.db})
|
|
|
|
r.Register("eth_requestAccounts", &commands.RequestAccountsCommand{
|
|
|
|
ClientHandler: c,
|
|
|
|
AccountsCommand: commands.AccountsCommand{Db: s.db},
|
|
|
|
})
|
|
|
|
|
|
|
|
// 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-07-25 16:02:26 +00:00
|
|
|
// Request permissions
|
|
|
|
r.Register("wallet_requestPermissions", &commands.RequestPermissionsCommand{})
|
|
|
|
|
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-08-02 07:43:31 +00:00
|
|
|
func (api *API) forwardRPC(URL string, inputJSON string) (interface{}, error) {
|
|
|
|
dApp, err := persistence.SelectDAppByUrl(api.s.db, URL)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if dApp == nil {
|
|
|
|
return "", commands.ErrDAppIsNotPermittedByUser
|
|
|
|
}
|
|
|
|
|
|
|
|
var response map[string]interface{}
|
|
|
|
rawResponse := api.s.rpc.CallRaw(inputJSON)
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (api *API) CallRPC(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-07-18 15:30:10 +00:00
|
|
|
return command.Execute(request)
|
2024-07-12 18:12:14 +00:00
|
|
|
}
|
|
|
|
|
2024-08-02 07:43:31 +00:00
|
|
|
return api.forwardRPC(request.URL, inputJSON)
|
2024-07-18 15:30:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (api *API) RecallDAppPermission(origin string) error {
|
|
|
|
return persistence.DeleteDApp(api.s.db, origin)
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|