status-go/services/web3provider/api_test.go

200 lines
6.2 KiB
Go
Raw Normal View History

2021-12-21 15:44:37 +00:00
package web3provider
import (
"database/sql"
"encoding/json"
"testing"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/require"
"github.com/status-im/status-go/account"
"github.com/status-im/status-go/appdatabase"
"github.com/status-im/status-go/eth-node/types"
"github.com/status-im/status-go/multiaccounts/accounts"
Sync Settings (#2478) * Sync Settings * Added valueHandlers and Database singleton Some issues remain, need a way to comparing incoming sql.DB to check if the connection is to a different file or not. Maybe make singleton instance per filename * Added functionality to check the sqlite filename * Refactor of Database.SaveSyncSettings to be used as a handler * Implemented inteface for setting sync protobuf factories * Refactored and completed adhoc send setting sync * Tidying up * Immutability refactor * Refactor settings into dedicated package * Breakout structs * Tidy up * Refactor of bulk settings sync * Bug fixes * Addressing feedback * Fix code dropped during rebase * Fix for db closed * Fix for node config related crashes * Provisional fix for type assertion - issue 2 * Adding robust type assertion checks * Partial fix for null literal db storage and json encoding * Fix for passively handling nil sql.DB, and checking if elem has len and if len is 0 * Added test for preferred name behaviour * Adding saved sync settings to MessengerResponse * Completed granular initial sync and clock from network on save * add Settings to isEmpty * Refactor of protobufs, partially done * Added syncSetting receiver handling, some bug fixes * Fix for sticker packs * Implement inactive flag on sync protobuf factory * Refactor of types and structs * Added SettingField.CanSync functionality * Addressing rebase artifact * Refactor of Setting SELECT queries * Refactor of string return queries * VERSION bump and migration index bump * Deactiveate Sync Settings * Deactiveated preferred_name and send_status_updates Co-authored-by: Andrea Maria Piana <andrea.maria.piana@gmail.com>
2022-03-23 18:47:00 +00:00
"github.com/status-im/status-go/multiaccounts/settings"
2021-12-21 15:44:37 +00:00
"github.com/status-im/status-go/params"
"github.com/status-im/status-go/services/permissions"
"github.com/status-im/status-go/t/helpers"
2021-12-21 15:44:37 +00:00
"github.com/status-im/status-go/t/utils"
"github.com/status-im/status-go/transactions/fake"
gethrpc "github.com/ethereum/go-ethereum/rpc"
statusRPC "github.com/status-im/status-go/rpc"
)
func createDB(t *testing.T) (*sql.DB, func()) {
db, cleanup, err := helpers.SetupTestSQLDB(appdatabase.DbInitializer{}, "provider-tests-")
2021-12-21 15:44:37 +00:00
require.NoError(t, err)
return db, func() { require.NoError(t, cleanup()) }
2021-12-21 15:44:37 +00:00
}
func setupTestAPI(t *testing.T) (*API, func()) {
db, cancel := createDB(t)
keyStoreDir := t.TempDir()
2021-12-21 15:44:37 +00:00
// Creating a dummy status node to simulate what it's done in get_status_node.go
upstreamConfig := params.UpstreamRPCConfig{
URL: "https://mainnet.infura.io/v3/800c641949d64d768a5070a1b0511938",
Enabled: true,
}
txServiceMockCtrl := gomock.NewController(t)
server, _ := fake.NewTestServer(txServiceMockCtrl)
client := gethrpc.DialInProc(server)
rpcClient, err := statusRPC.NewClient(client, 1, upstreamConfig, nil, db)
require.NoError(t, err)
// import account keys
utils.Init()
require.NoError(t, utils.ImportTestAccount(keyStoreDir, utils.GetAccount1PKFile()))
accManager := account.NewGethManager()
nodeConfig := &params.NodeConfig{
KeyStoreDir: keyStoreDir,
NetworkID: 1,
}
Sync Settings (#2478) * Sync Settings * Added valueHandlers and Database singleton Some issues remain, need a way to comparing incoming sql.DB to check if the connection is to a different file or not. Maybe make singleton instance per filename * Added functionality to check the sqlite filename * Refactor of Database.SaveSyncSettings to be used as a handler * Implemented inteface for setting sync protobuf factories * Refactored and completed adhoc send setting sync * Tidying up * Immutability refactor * Refactor settings into dedicated package * Breakout structs * Tidy up * Refactor of bulk settings sync * Bug fixes * Addressing feedback * Fix code dropped during rebase * Fix for db closed * Fix for node config related crashes * Provisional fix for type assertion - issue 2 * Adding robust type assertion checks * Partial fix for null literal db storage and json encoding * Fix for passively handling nil sql.DB, and checking if elem has len and if len is 0 * Added test for preferred name behaviour * Adding saved sync settings to MessengerResponse * Completed granular initial sync and clock from network on save * add Settings to isEmpty * Refactor of protobufs, partially done * Added syncSetting receiver handling, some bug fixes * Fix for sticker packs * Implement inactive flag on sync protobuf factory * Refactor of types and structs * Added SettingField.CanSync functionality * Addressing rebase artifact * Refactor of Setting SELECT queries * Refactor of string return queries * VERSION bump and migration index bump * Deactiveate Sync Settings * Deactiveated preferred_name and send_status_updates Co-authored-by: Andrea Maria Piana <andrea.maria.piana@gmail.com>
2022-03-23 18:47:00 +00:00
accDB, err := accounts.NewDB(db)
require.NoError(t, err)
service := NewService(db, accDB, rpcClient, nodeConfig, accManager, nil, nil)
2021-12-21 15:44:37 +00:00
networks := json.RawMessage("{}")
Sync Settings (#2478) * Sync Settings * Added valueHandlers and Database singleton Some issues remain, need a way to comparing incoming sql.DB to check if the connection is to a different file or not. Maybe make singleton instance per filename * Added functionality to check the sqlite filename * Refactor of Database.SaveSyncSettings to be used as a handler * Implemented inteface for setting sync protobuf factories * Refactored and completed adhoc send setting sync * Tidying up * Immutability refactor * Refactor settings into dedicated package * Breakout structs * Tidy up * Refactor of bulk settings sync * Bug fixes * Addressing feedback * Fix code dropped during rebase * Fix for db closed * Fix for node config related crashes * Provisional fix for type assertion - issue 2 * Adding robust type assertion checks * Partial fix for null literal db storage and json encoding * Fix for passively handling nil sql.DB, and checking if elem has len and if len is 0 * Added test for preferred name behaviour * Adding saved sync settings to MessengerResponse * Completed granular initial sync and clock from network on save * add Settings to isEmpty * Refactor of protobufs, partially done * Added syncSetting receiver handling, some bug fixes * Fix for sticker packs * Implement inactive flag on sync protobuf factory * Refactor of types and structs * Added SettingField.CanSync functionality * Addressing rebase artifact * Refactor of Setting SELECT queries * Refactor of string return queries * VERSION bump and migration index bump * Deactiveate Sync Settings * Deactiveated preferred_name and send_status_updates Co-authored-by: Andrea Maria Piana <andrea.maria.piana@gmail.com>
2022-03-23 18:47:00 +00:00
settings := settings.Settings{
2021-12-21 15:44:37 +00:00
DappsAddress: types.HexToAddress(utils.TestConfig.Account1.WalletAddress),
Networks: &networks,
}
2022-05-18 10:42:51 +00:00
accounts := []*accounts.Account{
2021-12-21 15:44:37 +00:00
{Address: types.HexToAddress(utils.TestConfig.Account1.WalletAddress), Chat: true, Wallet: true},
}
require.NoError(t, service.accountsDB.SaveOrUpdateAccounts(accounts, false))
2021-12-21 15:44:37 +00:00
require.NoError(t, service.accountsDB.CreateSettings(settings, *nodeConfig))
return &API{
s: service,
}, cancel
}
func TestRequestPermission(t *testing.T) {
api, cancel := setupTestAPI(t)
defer cancel()
request := APIRequest{
Hostname: "www.status.im",
}
_, err := api.ProcessAPIRequest(request)
require.Error(t, err)
request.Permission = PermissionWeb3
response, err := api.ProcessAPIRequest(request)
require.NoError(t, err)
require.False(t, response.IsAllowed)
require.Equal(t, ResponseAPI, response.ProviderResponse.ResponseType)
_ = api.s.permissionsDB.AddPermissions(permissions.DappPermissions{Name: "www.status.im", Permissions: []string{PermissionWeb3, PermissionContactCode, "RandomPermission"}})
response, err = api.ProcessAPIRequest(request)
require.NoError(t, err)
require.True(t, response.IsAllowed)
d := make([]interface{}, 1)
d[0] = types.HexToAddress(utils.TestConfig.Account1.WalletAddress)
var data interface{} = d
require.Equal(t, data, response.Data)
request.Permission = PermissionContactCode
response, err = api.ProcessAPIRequest(request)
require.NoError(t, err)
require.True(t, response.IsAllowed)
pubKey, _ := api.s.accountsDB.GetPublicKey()
data = pubKey
require.Equal(t, data, response.Data)
request.Permission = "RandomPermission"
_, err = api.ProcessAPIRequest(request)
require.Error(t, err)
}
2024-01-16 15:21:02 +00:00
/*
2021-12-21 15:44:37 +00:00
func TestWeb3Call(t *testing.T) {
api, cancel := setupTestAPI(t)
defer cancel()
request := Web3SendAsyncReadOnlyRequest{
Hostname: "www.status.im",
MessageID: 1,
Payload: ETHPayload{
ID: 1,
JSONRPC: "2.0",
From: types.HexToAddress(utils.TestConfig.Account1.WalletAddress).String(),
Method: "net_version",
Params: []interface{}{},
},
}
response, err := api.ProcessWeb3ReadOnlyRequest(request)
require.NoError(t, err)
require.Equal(t, `{"jsonrpc":"2.0","id":1,"result":"1"}`, string(response.Result.(json.RawMessage)))
request.Payload.Method = "eth_accounts"
response, err = api.ProcessWeb3ReadOnlyRequest(request)
require.NoError(t, err)
require.Equal(t, uint(4100), response.Error.(Web3SendAsyncReadOnlyError).Code)
_ = api.s.permissionsDB.AddPermissions(permissions.DappPermissions{Name: "www.status.im", Permissions: []string{PermissionWeb3}})
response, err = api.ProcessWeb3ReadOnlyRequest(request)
require.NoError(t, err)
d := make([]types.Address, 1)
d[0] = types.HexToAddress(utils.TestConfig.Account1.WalletAddress)
var data interface{} = d // eth_account is an array of addresses
require.Equal(t, data, response.Result.(JSONRPCResponse).Result)
request.Payload.Method = "eth_coinbase"
data = d[0] // eth_coinbase is an address
response, err = api.ProcessWeb3ReadOnlyRequest(request)
require.NoError(t, err)
require.Equal(t, data, response.Result.(JSONRPCResponse).Result)
}
2024-01-16 15:21:02 +00:00
*/
2021-12-21 15:44:37 +00:00
func TestWeb3Signature(t *testing.T) {
api, cancel := setupTestAPI(t)
defer cancel()
_ = api.s.permissionsDB.AddPermissions(permissions.DappPermissions{Name: "www.status.im", Permissions: []string{PermissionWeb3}})
request := Web3SendAsyncReadOnlyRequest{
Hostname: "www.status.im",
MessageID: 1,
Payload: ETHPayload{
ID: 1,
JSONRPC: "2.0",
From: types.HexToAddress(utils.TestConfig.Account1.WalletAddress).String(),
Method: "personal_sign",
Params: []interface{}{types.HexBytes{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}},
Password: "wrong-password",
},
}
response, err := api.ProcessWeb3ReadOnlyRequest(request)
require.NoError(t, err)
require.Equal(t, uint(4100), response.Error.(Web3SendAsyncReadOnlyError).Code)
require.Equal(t, "could not decrypt key with given password", response.Error.(Web3SendAsyncReadOnlyError).Message)
request.Payload.Password = utils.TestConfig.Account1.Password
response, err = api.ProcessWeb3ReadOnlyRequest(request)
require.NoError(t, err)
require.Equal(t, types.HexBytes(types.Hex2Bytes("0xc113a94f201334da86b8237c676951932d2b0ee2b539d941736da5b736f0f224448be6435846a9df9ea0085d92b107b6e49b1786e90d6604d3ef7d6f6ec19d531c")), response.Result.(JSONRPCResponse).Result.(types.HexBytes))
}