Mikhail Rogachev 4c6ca00520
Feat: implement connector service for browser plugin (#5433)
* feat(connector)_: impl `eth_requestAccounts` for browser plugin

* feat(connector)_: add impl for `wallet_switchEthereumChain` and `eth_chainId`

* feat(connector)_: add impl for `eth_sendTransaction`

* feat(connector)_: add a signal and an endpoint for wallet ui side

* chore_: refactor connector tests

* feat(connector)_: impl `eth_requestAccounts` with signal

* chore(connector)_: Add test, covering full transaction flow

And polish impl & test for connector endpoints

* fix(connector)_: temporary allow all origins for ws connection

* chore_: review fixes

* fix(connector)_: make user select chain id for dApp

* fix(connector)_: add requestID and fine tune endpoints

* chore(connector)_: naming fixes and tests improvments
2024-07-18 17:30:10 +02:00

76 lines
1.9 KiB
Go

package connector
import (
"github.com/status-im/status-go/services/connector/commands"
persistence "github.com/status-im/status-go/services/connector/database"
)
type API struct {
s *Service
r *CommandRegistry
c *commands.ClientSideHandler
}
func NewAPI(s *Service) *API {
r := NewCommandRegistry()
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,
})
return &API{
s: s,
r: r,
c: c,
}
}
func (api *API) CallRPC(inputJSON string) (string, error) {
request, err := commands.RPCRequestFromJSON(inputJSON)
if err != nil {
return "", err
}
if command, exists := api.r.GetCommand(request.Method); exists {
return command.Execute(request)
}
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)
}