2024-07-18 15:30:10 +00:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
2024-07-26 20:19:03 +00:00
|
|
|
"database/sql"
|
|
|
|
"strconv"
|
2024-07-18 15:30:10 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
|
|
|
"github.com/status-im/status-go/eth-node/types"
|
2024-07-26 20:19:03 +00:00
|
|
|
"github.com/status-im/status-go/params"
|
2024-07-18 15:30:10 +00:00
|
|
|
walletCommon "github.com/status-im/status-go/services/wallet/common"
|
|
|
|
)
|
|
|
|
|
2024-07-26 20:19:03 +00:00
|
|
|
func setupNetworks(db *sql.DB) *ChainIDCommand {
|
|
|
|
nm := NetworkManagerMock{}
|
|
|
|
nm.SetNetworks([]*params.Network{
|
|
|
|
{
|
|
|
|
ChainID: walletCommon.EthereumMainnet,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ChainID: walletCommon.EthereumGoerli,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
cmd := &ChainIDCommand{
|
|
|
|
Db: db,
|
|
|
|
NetworkManager: &nm,
|
|
|
|
}
|
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2024-07-18 15:30:10 +00:00
|
|
|
func TestFailToGetChainIdWithMissingDAppFields(t *testing.T) {
|
|
|
|
db, close := SetupTestDB(t)
|
|
|
|
defer close()
|
|
|
|
|
2024-07-26 20:19:03 +00:00
|
|
|
cmd := setupNetworks(db)
|
2024-07-18 15:30:10 +00:00
|
|
|
|
|
|
|
// Missing DApp fields
|
|
|
|
request, err := ConstructRPCRequest("eth_chainId", []interface{}{}, nil)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
result, err := cmd.Execute(request)
|
|
|
|
assert.Equal(t, ErrRequestMissingDAppData, err)
|
|
|
|
assert.Empty(t, result)
|
|
|
|
}
|
|
|
|
|
2024-07-26 20:19:03 +00:00
|
|
|
func TestGetDefaultChainIdForUnpermittedDApp(t *testing.T) {
|
2024-07-18 15:30:10 +00:00
|
|
|
db, close := SetupTestDB(t)
|
|
|
|
defer close()
|
|
|
|
|
2024-07-26 20:19:03 +00:00
|
|
|
cmd := setupNetworks(db)
|
2024-07-18 15:30:10 +00:00
|
|
|
|
|
|
|
request, err := ConstructRPCRequest("eth_chainId", []interface{}{}, &testDAppData)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
result, err := cmd.Execute(request)
|
2024-07-26 20:19:03 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, result, strconv.FormatUint(walletCommon.EthereumMainnet, 16))
|
2024-07-18 15:30:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetChainIdForPermittedDApp(t *testing.T) {
|
|
|
|
db, close := SetupTestDB(t)
|
|
|
|
defer close()
|
|
|
|
|
2024-07-26 20:19:03 +00:00
|
|
|
cmd := setupNetworks(db)
|
2024-07-18 15:30:10 +00:00
|
|
|
|
|
|
|
sharedAccount := types.HexToAddress("0x6d0aa2a774b74bb1d36f97700315adf962c69fcg")
|
|
|
|
chainID := uint64(0x123)
|
|
|
|
|
|
|
|
err := PersistDAppData(db, testDAppData, sharedAccount, chainID)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
request, err := ConstructRPCRequest("eth_chainId", []interface{}{}, &testDAppData)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
response, err := cmd.Execute(request)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, walletCommon.ChainID(chainID).String(), response)
|
|
|
|
}
|