Skip to content

Commit

Permalink
fix(BrowserConnect)_: Send client event when the dApp permissions hav…
Browse files Browse the repository at this point in the history
…e been revoked
  • Loading branch information
alexjba committed Nov 7, 2024
1 parent 0894b5e commit ba2baec
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 6 deletions.
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)
}
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

0 comments on commit ba2baec

Please sign in to comment.