status-go/services/connector/commands/request_accounts.go
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

66 lines
1.5 KiB
Go

package commands
import (
"errors"
"github.com/status-im/status-go/multiaccounts/accounts"
persistence "github.com/status-im/status-go/services/connector/database"
"github.com/status-im/status-go/signal"
)
// errors
var (
ErrAccountsRequestDeniedByUser = errors.New("accounts request denied by user")
ErrNoAccountsAvailable = errors.New("no accounts available")
)
type RequestAccountsCommand struct {
ClientHandler ClientSideHandlerInterface
AccountsCommand
}
type RawAccountsResponse struct {
JSONRPC string `json:"jsonrpc"`
ID int `json:"id"`
Result []accounts.Account `json:"result"`
}
func (c *RequestAccountsCommand) Execute(request RPCRequest) (string, error) {
err := request.Validate()
if err != nil {
return "", err
}
dApp, err := persistence.SelectDAppByUrl(c.Db, request.URL)
if err != nil {
return "", err
}
// FIXME: this may have a security issue in case some malicious software tries to fake the origin
if dApp == nil {
account, chainID, err := c.ClientHandler.RequestShareAccountForDApp(signal.ConnectorDApp{
URL: request.URL,
Name: request.Name,
IconURL: request.IconURL,
})
if err != nil {
return "", err
}
dApp = &persistence.DApp{
URL: request.URL,
Name: request.Name,
IconURL: request.IconURL,
SharedAccount: account,
ChainID: chainID,
}
err = persistence.UpsertDApp(c.Db, dApp)
if err != nil {
return "", err
}
}
return c.dAppToAccountsResponse(dApp)
}