2024-06-24 14:29:40 +00:00
|
|
|
package connector
|
|
|
|
|
2024-07-12 18:12:14 +00:00
|
|
|
import (
|
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
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
|
r.Register("eth_chainId", &commands.ChainIDCommand{Db: s.db})
|
|
|
|
r.Register("wallet_switchEthereumChain", &commands.SwitchEthereumChainCommand{
|
|
|
|
Db: s.db,
|
|
|
|
NetworkManager: s.nm,
|
|
|
|
})
|
2024-07-12 18:12:14 +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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (api *API) CallRPC(inputJSON string) (string, 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-07-18 15:30:10 +00:00
|
|
|
return api.s.rpc.CallRaw(inputJSON), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|