Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(BC)_: Notify client on dapp connection changes #6051

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions services/connector/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type API struct {

func NewAPI(s *Service) *API {
r := NewCommandRegistry()
c := commands.NewClientSideHandler()
c := commands.NewClientSideHandler(s.db)

// Transactions and signing
r.Register("eth_sendTransaction", &commands.SendTransactionCommand{
Expand Down Expand Up @@ -121,7 +121,7 @@ func (api *API) CallRPC(ctx context.Context, inputJSON string) (interface{}, err

func (api *API) RecallDAppPermission(origin string) error {
// TODO: close the websocket connection
return persistence.DeleteDApp(api.s.db, origin)
return api.c.RecallDAppPermissions(commands.RecallDAppPermissionsArgs{URL: origin})
}

func (api *API) GetPermittedDAppsList() ([]persistence.DApp, error) {
Expand Down
35 changes: 34 additions & 1 deletion services/connector/commands/client_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ package commands

import (
"crypto/sha256"
"database/sql"
"encoding/hex"
"encoding/json"
"fmt"
"sync/atomic"
"time"

"github.com/status-im/status-go/eth-node/types"
persistence "github.com/status-im/status-go/services/connector/database"
"github.com/status-im/status-go/signal"
"github.com/status-im/status-go/transactions"
)
Expand All @@ -23,6 +25,8 @@ var (
ErrPersonalSignRejectedByUser = fmt.Errorf("personal sign was rejected by user")
ErrEmptyRequestID = fmt.Errorf("empty requestID")
ErrAnotherConnectorOperationIsAwaitingFor = fmt.Errorf("another connector operation is awaiting for user input")
ErrEmptyUrl = fmt.Errorf("empty URL")
ErrDAppDoesNotHavePermissions = fmt.Errorf("dApp does not have permissions")
)

type MessageType int
Expand All @@ -40,12 +44,14 @@ type Message struct {
}

type ClientSideHandler struct {
Db *sql.DB
responseChannel chan Message
isRequestRunning int32
}

func NewClientSideHandler() *ClientSideHandler {
func NewClientSideHandler(db *sql.DB) *ClientSideHandler {
return &ClientSideHandler{
Db: db,
responseChannel: make(chan Message, 1), // Buffer of 1 to avoid blocking
isRequestRunning: 0,
}
Expand Down Expand Up @@ -115,6 +121,33 @@ func (c *ClientSideHandler) RequestAccountsRejected(args RejectedArgs) error {
return nil
}

func (c *ClientSideHandler) RecallDAppPermissions(args RecallDAppPermissionsArgs) error {
if args.URL == "" {
return ErrEmptyUrl
}

dApp, err := persistence.SelectDAppByUrl(c.Db, args.URL)
if err != nil {
return err
}

if dApp == nil {
return ErrDAppDoesNotHavePermissions
}

err = persistence.DeleteDApp(c.Db, dApp.URL)
if err != nil {
return err
}

signal.SendConnectorDAppPermissionRevoked(signal.ConnectorDApp{
URL: dApp.URL,
Name: dApp.Name,
IconURL: dApp.IconURL,
})
return nil
}

func (c *ClientSideHandler) RequestSendTransaction(dApp signal.ConnectorDApp, chainID uint64, txArgs *transactions.SendTxArgs) (types.Hash, error) {
if !c.setRequestRunning() {
return types.Hash{}, ErrAnotherConnectorOperationIsAwaitingFor
Expand Down
41 changes: 39 additions & 2 deletions services/connector/commands/client_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,16 @@ import (
"testing"
"time"

"github.com/status-im/status-go/eth-node/types"
persistence "github.com/status-im/status-go/services/connector/database"
"github.com/stretchr/testify/assert"
)

func TestClientHandlerTimeout(t *testing.T) {
clientHandler := NewClientSideHandler()
db, cleanup := createWalletDB(t)
t.Cleanup(cleanup)

clientHandler := NewClientSideHandler(db)

backupWalletResponseMaxInterval := WalletResponseMaxInterval
WalletResponseMaxInterval = 1 * time.Millisecond
Expand All @@ -19,10 +24,42 @@ func TestClientHandlerTimeout(t *testing.T) {
}

func TestRequestRejectedWhileWaiting(t *testing.T) {
clientHandler := NewClientSideHandler()
db, cleanup := createWalletDB(t)
t.Cleanup(cleanup)

clientHandler := NewClientSideHandler(db)

clientHandler.setRequestRunning()

_, _, err := clientHandler.RequestShareAccountForDApp(testDAppData)
assert.Equal(t, ErrAnotherConnectorOperationIsAwaitingFor, err)
}

func TestRecallDAppPermission(t *testing.T) {
db, cleanup := createWalletDB(t)
t.Cleanup(cleanup)

dapp := persistence.DApp{
Name: "Test DApp",
URL: "http://testDAppURL",
IconURL: "http://testDAppIconUrl",
SharedAccount: types.HexToAddress("0x1234567890"),
ChainID: 0x1,
}

err := persistence.UpsertDApp(db, &dapp)
assert.NoError(t, err)

persistedDapp, err := persistence.SelectDAppByUrl(db, dapp.URL)
assert.Equal(t, persistedDapp, &dapp)
assert.NoError(t, err)

clientHandler := NewClientSideHandler(db)
err = clientHandler.RecallDAppPermissions(RecallDAppPermissionsArgs{URL: dapp.URL})
assert.NoError(t, err)

recalledDapp, err := persistence.SelectDAppByUrl(db, dapp.URL)

assert.Equal(t, recalledDapp, (*persistence.DApp)(nil))
assert.NoError(t, err)
}
2 changes: 1 addition & 1 deletion services/connector/commands/request_accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func (c *RequestAccountsCommand) Execute(ctx context.Context, request RPCRequest
if err != nil {
return "", err
}
signal.SendConnectorDAppPermissionGranted(connectorDApp)
signal.SendConnectorDAppPermissionGranted(connectorDApp, account, []uint64{chainID})
}

return FormatAccountAddressToResponse(dApp.SharedAccount), nil
Expand Down
5 changes: 5 additions & 0 deletions services/connector/commands/rpc_traits.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,15 @@ type RejectedArgs struct {
RequestID string `json:"requestId"`
}

type RecallDAppPermissionsArgs struct {
URL string `json:"url"`
}

type ClientSideHandlerInterface interface {
RequestShareAccountForDApp(dApp signal.ConnectorDApp) (types.Address, uint64, error)
RequestAccountsAccepted(args RequestAccountsAcceptedArgs) error
RequestAccountsRejected(args RejectedArgs) error
RecallDAppPermissions(args RecallDAppPermissionsArgs) error

RequestSendTransaction(dApp signal.ConnectorDApp, chainID uint64, txArgs *transactions.SendTxArgs) (types.Hash, error)
SendTransactionAccepted(args SendTransactionAcceptedArgs) error
Expand Down
2 changes: 1 addition & 1 deletion services/connector/commands/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func setupCommand(t *testing.T, method string) (state testState, close func()) {
})
require.NoError(t, err)

state.handler = NewClientSideHandler()
state.handler = NewClientSideHandler(state.db)

state.mockCtrl = gomock.NewController(t)
state.rpcClient = mock_rpcclient.NewMockClientInterface(state.mockCtrl)
Expand Down
18 changes: 16 additions & 2 deletions signal/events_connector.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package signal

import (
"github.com/status-im/status-go/eth-node/types"
)

const (
EventConnectorSendRequestAccounts = "connector.sendRequestAccounts"
EventConnectorSendTransaction = "connector.sendTransaction"
Expand Down Expand Up @@ -29,6 +33,12 @@ type ConnectorSendTransactionSignal struct {
TxArgs string `json:"txArgs"`
}

type ConnectorSendDappPermissionGrantedSignal struct {
ConnectorDApp
Chains []uint64 `json:"chains"`
SharedAccount types.Address `json:"sharedAccount"`
}

type ConnectorPersonalSignSignal struct {
ConnectorDApp
RequestID string `json:"requestId"`
Expand Down Expand Up @@ -66,8 +76,12 @@ func SendConnectorPersonalSign(dApp ConnectorDApp, requestID, challenge, address
})
}

func SendConnectorDAppPermissionGranted(dApp ConnectorDApp) {
send(EventConnectorDAppPermissionGranted, dApp)
func SendConnectorDAppPermissionGranted(dApp ConnectorDApp, account types.Address, chains []uint64) {
send(EventConnectorDAppPermissionGranted, ConnectorSendDappPermissionGrantedSignal{
ConnectorDApp: dApp,
Chains: chains,
SharedAccount: account,
})
}

func SendConnectorDAppPermissionRevoked(dApp ConnectorDApp) {
Expand Down