2024-07-18 15:30:10 +00:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
|
|
|
"github.com/status-im/status-go/eth-node/types"
|
|
|
|
persistence "github.com/status-im/status-go/services/connector/database"
|
|
|
|
walletCommon "github.com/status-im/status-go/services/wallet/common"
|
|
|
|
"github.com/status-im/status-go/signal"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestFailToRequestAccountsWithMissingDAppFields(t *testing.T) {
|
2024-09-19 18:24:28 +00:00
|
|
|
state, close := setupCommand(t, Method_EthRequestAccounts)
|
|
|
|
t.Cleanup(close)
|
2024-07-18 15:30:10 +00:00
|
|
|
|
|
|
|
// Missing DApp fields
|
|
|
|
request, err := ConstructRPCRequest("eth_requestAccounts", []interface{}{}, nil)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2024-09-19 18:24:28 +00:00
|
|
|
result, err := state.cmd.Execute(state.ctx, request)
|
2024-07-18 15:30:10 +00:00
|
|
|
assert.Equal(t, ErrRequestMissingDAppData, err)
|
|
|
|
assert.Empty(t, result)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRequestAccountsWithSignalTimeout(t *testing.T) {
|
2024-09-19 18:24:28 +00:00
|
|
|
state, close := setupCommand(t, Method_EthRequestAccounts)
|
|
|
|
t.Cleanup(close)
|
2024-07-18 15:30:10 +00:00
|
|
|
|
|
|
|
request, err := prepareSendTransactionRequest(testDAppData, types.Address{0x01})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
backupWalletResponseMaxInterval := WalletResponseMaxInterval
|
|
|
|
WalletResponseMaxInterval = 1 * time.Millisecond
|
|
|
|
|
2024-09-19 18:24:28 +00:00
|
|
|
_, err = state.cmd.Execute(state.ctx, request)
|
2024-07-18 15:30:10 +00:00
|
|
|
assert.Equal(t, ErrWalletResponseTimeout, err)
|
|
|
|
WalletResponseMaxInterval = backupWalletResponseMaxInterval
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRequestAccountsAcceptedAndRequestAgain(t *testing.T) {
|
2024-09-19 18:24:28 +00:00
|
|
|
state, close := setupCommand(t, Method_EthRequestAccounts)
|
|
|
|
t.Cleanup(close)
|
2024-07-18 15:30:10 +00:00
|
|
|
|
|
|
|
request, err := ConstructRPCRequest("eth_requestAccounts", []interface{}{}, &testDAppData)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
accountAddress := types.Address{0x03}
|
2024-08-06 19:28:55 +00:00
|
|
|
dAppPermissionGranted := false
|
2024-07-18 15:30:10 +00:00
|
|
|
|
|
|
|
signal.SetMobileSignalHandler(signal.MobileSignalHandler(func(s []byte) {
|
|
|
|
var evt EventType
|
|
|
|
err := json.Unmarshal(s, &evt)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
switch evt.Type {
|
|
|
|
case signal.EventConnectorSendRequestAccounts:
|
|
|
|
var ev signal.ConnectorSendRequestAccountsSignal
|
|
|
|
err := json.Unmarshal(evt.Event, &ev)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2024-09-19 18:24:28 +00:00
|
|
|
err = state.handler.RequestAccountsAccepted(RequestAccountsAcceptedArgs{
|
2024-07-18 15:30:10 +00:00
|
|
|
RequestID: ev.RequestID,
|
|
|
|
Account: accountAddress,
|
|
|
|
ChainID: walletCommon.EthereumMainnet,
|
|
|
|
})
|
|
|
|
assert.NoError(t, err)
|
2024-08-06 19:28:55 +00:00
|
|
|
case signal.EventConnectorDAppPermissionGranted:
|
|
|
|
dAppPermissionGranted = true
|
2024-07-18 15:30:10 +00:00
|
|
|
}
|
|
|
|
}))
|
2024-09-03 08:41:56 +00:00
|
|
|
t.Cleanup(signal.ResetMobileSignalHandler)
|
2024-07-18 15:30:10 +00:00
|
|
|
|
2024-08-02 07:43:31 +00:00
|
|
|
expectedResponse := FormatAccountAddressToResponse(accountAddress)
|
2024-09-19 18:24:28 +00:00
|
|
|
response, err := state.cmd.Execute(state.ctx, request)
|
2024-07-18 15:30:10 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NoError(t, err)
|
2024-08-02 07:43:31 +00:00
|
|
|
assert.Equal(t, expectedResponse, response)
|
2024-07-18 15:30:10 +00:00
|
|
|
|
|
|
|
// Check dApp in the database
|
2024-09-19 18:24:28 +00:00
|
|
|
dApp, err := persistence.SelectDAppByUrl(state.walletDb, request.URL)
|
2024-07-18 15:30:10 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, request.Name, dApp.Name)
|
|
|
|
assert.Equal(t, request.IconURL, dApp.IconURL)
|
|
|
|
assert.Equal(t, accountAddress, dApp.SharedAccount)
|
|
|
|
assert.Equal(t, walletCommon.EthereumMainnet, dApp.ChainID)
|
|
|
|
|
|
|
|
// This should not invoke UI side
|
2024-09-19 18:24:28 +00:00
|
|
|
response, err = state.cmd.Execute(state.ctx, request)
|
2024-07-18 15:30:10 +00:00
|
|
|
assert.NoError(t, err)
|
2024-08-02 07:43:31 +00:00
|
|
|
assert.Equal(t, expectedResponse, response)
|
2024-08-06 19:28:55 +00:00
|
|
|
|
|
|
|
assert.True(t, dAppPermissionGranted)
|
2024-07-18 15:30:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRequestAccountsRejected(t *testing.T) {
|
2024-09-19 18:24:28 +00:00
|
|
|
state, close := setupCommand(t, Method_EthRequestAccounts)
|
|
|
|
t.Cleanup(close)
|
2024-07-18 15:30:10 +00:00
|
|
|
|
|
|
|
request, err := ConstructRPCRequest("eth_requestAccounts", []interface{}{}, &testDAppData)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
signal.SetMobileSignalHandler(signal.MobileSignalHandler(func(s []byte) {
|
|
|
|
var evt EventType
|
|
|
|
err := json.Unmarshal(s, &evt)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
switch evt.Type {
|
|
|
|
case signal.EventConnectorSendRequestAccounts:
|
|
|
|
var ev signal.ConnectorSendRequestAccountsSignal
|
|
|
|
err := json.Unmarshal(evt.Event, &ev)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2024-09-19 18:24:28 +00:00
|
|
|
err = state.handler.RequestAccountsRejected(RejectedArgs{
|
2024-07-18 15:30:10 +00:00
|
|
|
RequestID: ev.RequestID,
|
|
|
|
})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
}))
|
2024-09-03 08:41:56 +00:00
|
|
|
t.Cleanup(signal.ResetMobileSignalHandler)
|
2024-07-18 15:30:10 +00:00
|
|
|
|
2024-09-19 18:24:28 +00:00
|
|
|
_, err = state.cmd.Execute(state.ctx, request)
|
2024-07-18 15:30:10 +00:00
|
|
|
assert.Equal(t, ErrRequestAccountsRejectedByUser, err)
|
|
|
|
}
|