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

89 lines
2.0 KiB
Go

package commands
import (
"database/sql"
"encoding/json"
"testing"
"github.com/stretchr/testify/require"
"github.com/status-im/status-go/eth-node/types"
"github.com/status-im/status-go/params"
persistence "github.com/status-im/status-go/services/connector/database"
"github.com/status-im/status-go/signal"
"github.com/status-im/status-go/t/helpers"
"github.com/status-im/status-go/walletdatabase"
)
var testDAppData = signal.ConnectorDApp{
URL: "http://testDAppURL",
Name: "testDAppName",
IconURL: "http://testDAppIconUrl",
}
type RPCClientMock struct {
response string
}
type NetworkManagerMock struct {
networks []*params.Network
}
type EventType struct {
Type string `json:"type"`
Event json.RawMessage `json:"event"`
}
func (c *RPCClientMock) CallRaw(request string) string {
return c.response
}
func (c *RPCClientMock) SetResponse(response string) {
c.response = response
}
func (nm *NetworkManagerMock) GetActiveNetworks() ([]*params.Network, error) {
return nm.networks, nil
}
func (nm *NetworkManagerMock) SetNetworks(networks []*params.Network) {
nm.networks = networks
}
func SetupTestDB(t *testing.T) (db *sql.DB, close func()) {
db, err := helpers.SetupTestMemorySQLDB(walletdatabase.DbInitializer{})
require.NoError(t, err)
return db, func() {
require.NoError(t, db.Close())
}
}
func PersistDAppData(db *sql.DB, dApp signal.ConnectorDApp, sharedAccount types.Address, chainID uint64) error {
dAppDb := persistence.DApp{
URL: dApp.URL,
Name: dApp.Name,
IconURL: dApp.IconURL,
SharedAccount: sharedAccount,
ChainID: chainID,
}
return persistence.UpsertDApp(db, &dAppDb)
}
func ConstructRPCRequest(method string, params []interface{}, dApp *signal.ConnectorDApp) (RPCRequest, error) {
request := RPCRequest{
JSONRPC: "2.0",
ID: 1,
Method: method,
Params: params,
}
if dApp != nil {
request.URL = dApp.URL
request.Name = dApp.Name
request.IconURL = dApp.IconURL
}
return request, nil
}