2024-07-18 15:30:10 +00:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
2024-07-26 20:00:12 +00:00
|
|
|
"fmt"
|
2024-07-26 16:18:01 +00:00
|
|
|
"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:00:12 +00:00
|
|
|
"github.com/status-im/status-go/services/connector/chainutils"
|
2024-07-18 15:30:10 +00:00
|
|
|
walletCommon "github.com/status-im/status-go/services/wallet/common"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestFailToGetChainIdWithMissingDAppFields(t *testing.T) {
|
2024-09-19 18:24:28 +00:00
|
|
|
state, close := setupCommand(t, Method_EthChainId)
|
|
|
|
t.Cleanup(close)
|
2024-07-18 15:30:10 +00:00
|
|
|
|
|
|
|
// Missing DApp fields
|
|
|
|
request, err := ConstructRPCRequest("eth_chainId", []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)
|
|
|
|
}
|
|
|
|
|
2024-07-26 16:18:01 +00:00
|
|
|
func TestGetDefaultChainIdForUnpermittedDApp(t *testing.T) {
|
2024-09-19 18:24:28 +00:00
|
|
|
state, close := setupCommand(t, Method_EthChainId)
|
|
|
|
t.Cleanup(close)
|
2024-07-18 15:30:10 +00:00
|
|
|
|
|
|
|
request, err := ConstructRPCRequest("eth_chainId", []interface{}{}, &testDAppData)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2024-09-19 18:24:28 +00:00
|
|
|
result, err := state.cmd.Execute(state.ctx, request)
|
2024-07-26 16:18:01 +00:00
|
|
|
assert.NoError(t, err)
|
2024-07-26 20:00:12 +00:00
|
|
|
chainId, err := chainutils.GetHexChainID(strconv.FormatUint(walletCommon.EthereumMainnet, 16))
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, result, chainId)
|
2024-07-18 15:30:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetChainIdForPermittedDApp(t *testing.T) {
|
2024-09-19 18:24:28 +00:00
|
|
|
state, close := setupCommand(t, Method_EthChainId)
|
|
|
|
t.Cleanup(close)
|
2024-07-18 15:30:10 +00:00
|
|
|
|
|
|
|
sharedAccount := types.HexToAddress("0x6d0aa2a774b74bb1d36f97700315adf962c69fcg")
|
|
|
|
chainID := uint64(0x123)
|
|
|
|
|
2024-09-19 18:24:28 +00:00
|
|
|
err := PersistDAppData(state.walletDb, testDAppData, sharedAccount, chainID)
|
2024-07-18 15:30:10 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
request, err := ConstructRPCRequest("eth_chainId", []interface{}{}, &testDAppData)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
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-07-26 20:00:12 +00:00
|
|
|
chainId := fmt.Sprintf(`0x%s`, strconv.FormatUint(chainID, 16))
|
|
|
|
assert.Equal(t, chainId, response)
|
2024-07-18 15:30:10 +00:00
|
|
|
}
|