2018-04-20 15:39:53 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2019-12-05 08:00:57 +00:00
|
|
|
"crypto/sha256"
|
2019-01-24 15:44:46 +00:00
|
|
|
"encoding/hex"
|
2019-03-28 14:56:21 +00:00
|
|
|
"encoding/json"
|
2018-04-20 15:39:53 +00:00
|
|
|
"fmt"
|
2019-08-29 08:06:22 +00:00
|
|
|
"io/ioutil"
|
2018-04-20 15:39:53 +00:00
|
|
|
"math/rand"
|
2019-08-29 08:06:22 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2018-04-20 15:39:53 +00:00
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
|
2019-03-28 14:56:21 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2019-01-24 15:44:46 +00:00
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
|
|
|
"github.com/status-im/status-go/account"
|
2019-11-23 17:57:05 +00:00
|
|
|
"github.com/status-im/status-go/eth-node/types"
|
2019-08-29 08:06:22 +00:00
|
|
|
"github.com/status-im/status-go/multiaccounts"
|
|
|
|
"github.com/status-im/status-go/multiaccounts/accounts"
|
2018-06-08 11:29:50 +00:00
|
|
|
"github.com/status-im/status-go/node"
|
|
|
|
"github.com/status-im/status-go/params"
|
2018-08-02 07:07:55 +00:00
|
|
|
"github.com/status-im/status-go/rpc"
|
2019-03-28 14:56:21 +00:00
|
|
|
"github.com/status-im/status-go/services/typeddata"
|
2018-09-13 16:31:29 +00:00
|
|
|
"github.com/status-im/status-go/t/utils"
|
2019-08-29 08:06:22 +00:00
|
|
|
"github.com/status-im/status-go/transactions"
|
2018-04-20 15:39:53 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestBackendStartNodeConcurrently(t *testing.T) {
|
2019-10-04 15:21:24 +00:00
|
|
|
utils.Init()
|
|
|
|
|
2019-11-23 17:57:05 +00:00
|
|
|
backend := NewGethStatusBackend()
|
2018-09-13 16:31:29 +00:00
|
|
|
config, err := utils.MakeTestNodeConfig(params.StatusChainNetworkID)
|
|
|
|
require.NoError(t, err)
|
2019-08-20 15:38:40 +00:00
|
|
|
require.NoError(t, backend.AccountManager().InitKeystore(config.KeyStoreDir))
|
2018-04-20 15:39:53 +00:00
|
|
|
count := 2
|
|
|
|
resultCh := make(chan error)
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(count)
|
|
|
|
|
|
|
|
for i := 0; i < count; i++ {
|
|
|
|
go func() {
|
2018-09-13 16:31:29 +00:00
|
|
|
resultCh <- backend.StartNode(config)
|
2018-04-20 15:39:53 +00:00
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
// close channel as otherwise for loop never finishes
|
|
|
|
go func() { wg.Wait(); close(resultCh) }()
|
|
|
|
|
|
|
|
var results []error
|
|
|
|
for err := range resultCh {
|
|
|
|
results = append(results, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
require.Contains(t, results, nil)
|
|
|
|
require.Contains(t, results, node.ErrNodeRunning)
|
|
|
|
|
2018-09-13 16:31:29 +00:00
|
|
|
err = backend.StopNode()
|
2018-04-20 15:39:53 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBackendRestartNodeConcurrently(t *testing.T) {
|
2019-10-04 15:21:24 +00:00
|
|
|
utils.Init()
|
|
|
|
|
2019-11-23 17:57:05 +00:00
|
|
|
backend := NewGethStatusBackend()
|
2018-09-13 16:31:29 +00:00
|
|
|
config, err := utils.MakeTestNodeConfig(params.StatusChainNetworkID)
|
|
|
|
require.NoError(t, err)
|
2018-04-20 15:39:53 +00:00
|
|
|
count := 3
|
2019-08-20 15:38:40 +00:00
|
|
|
require.NoError(t, backend.AccountManager().InitKeystore(config.KeyStoreDir))
|
|
|
|
require.NoError(t, backend.StartNode(config))
|
2018-04-20 15:39:53 +00:00
|
|
|
defer func() {
|
|
|
|
require.NoError(t, backend.StopNode())
|
|
|
|
}()
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(count)
|
|
|
|
|
|
|
|
for i := 0; i < count; i++ {
|
|
|
|
go func(idx int) {
|
|
|
|
assert.NoError(t, backend.RestartNode())
|
|
|
|
wg.Done()
|
|
|
|
}(i)
|
|
|
|
}
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(adam): add concurrent tests for ResetChainData()
|
|
|
|
|
|
|
|
func TestBackendGettersConcurrently(t *testing.T) {
|
2019-10-04 15:21:24 +00:00
|
|
|
utils.Init()
|
|
|
|
|
2019-11-23 17:57:05 +00:00
|
|
|
backend := NewGethStatusBackend()
|
2018-09-13 16:31:29 +00:00
|
|
|
config, err := utils.MakeTestNodeConfig(params.StatusChainNetworkID)
|
|
|
|
require.NoError(t, err)
|
2019-08-20 15:38:40 +00:00
|
|
|
require.NoError(t, backend.AccountManager().InitKeystore(config.KeyStoreDir))
|
2018-09-13 16:31:29 +00:00
|
|
|
err = backend.StartNode(config)
|
2018-04-20 15:39:53 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
defer func() {
|
|
|
|
require.NoError(t, backend.StopNode())
|
|
|
|
}()
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
assert.NotNil(t, backend.StatusNode())
|
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
assert.NotNil(t, backend.AccountManager())
|
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
2018-06-27 08:11:45 +00:00
|
|
|
assert.NotNil(t, backend.personalAPI)
|
2018-04-20 15:39:53 +00:00
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
assert.NotNil(t, backend.Transactor())
|
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
assert.True(t, backend.IsNodeRunning())
|
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
assert.True(t, backend.IsNodeRunning())
|
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBackendAccountsConcurrently(t *testing.T) {
|
2019-10-04 15:21:24 +00:00
|
|
|
utils.Init()
|
|
|
|
|
2019-11-23 17:57:05 +00:00
|
|
|
backend := NewGethStatusBackend()
|
2018-09-13 16:31:29 +00:00
|
|
|
config, err := utils.MakeTestNodeConfig(params.StatusChainNetworkID)
|
|
|
|
require.NoError(t, err)
|
2019-08-20 15:38:40 +00:00
|
|
|
require.NoError(t, backend.AccountManager().InitKeystore(config.KeyStoreDir))
|
2018-09-13 16:31:29 +00:00
|
|
|
err = backend.StartNode(config)
|
2018-04-20 15:39:53 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
defer func() {
|
|
|
|
require.NoError(t, backend.StopNode())
|
|
|
|
}()
|
|
|
|
|
|
|
|
var wgCreateAccounts sync.WaitGroup
|
|
|
|
count := 3
|
2019-01-18 09:01:14 +00:00
|
|
|
addressCh := make(chan [3]string, count) // use buffered channel to avoid blocking
|
2018-04-20 15:39:53 +00:00
|
|
|
|
|
|
|
// create new accounts concurrently
|
|
|
|
for i := 0; i < count; i++ {
|
|
|
|
wgCreateAccounts.Add(1)
|
|
|
|
go func(pass string) {
|
2019-01-18 09:01:14 +00:00
|
|
|
accountInfo, _, err := backend.AccountManager().CreateAccount(pass)
|
2018-04-20 15:39:53 +00:00
|
|
|
assert.NoError(t, err)
|
2019-01-18 09:01:14 +00:00
|
|
|
addressCh <- [...]string{accountInfo.WalletAddress, accountInfo.ChatAddress, pass}
|
2018-04-20 15:39:53 +00:00
|
|
|
wgCreateAccounts.Done()
|
|
|
|
}("password-00" + string(i))
|
|
|
|
}
|
|
|
|
|
|
|
|
// close addressCh as otherwise for loop never finishes
|
|
|
|
go func() { wgCreateAccounts.Wait(); close(addressCh) }()
|
|
|
|
|
|
|
|
// select, reselect or logout concurrently
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
|
|
|
for tuple := range addressCh {
|
|
|
|
wg.Add(1)
|
2019-01-18 09:01:14 +00:00
|
|
|
go func(tuple [3]string) {
|
2019-07-26 14:45:10 +00:00
|
|
|
loginParams := account.LoginParams{
|
|
|
|
MainAccount: common.HexToAddress(tuple[0]),
|
|
|
|
ChatAddress: common.HexToAddress(tuple[1]),
|
|
|
|
Password: tuple[2],
|
|
|
|
}
|
|
|
|
assert.NoError(t, backend.SelectAccount(loginParams))
|
2018-04-20 15:39:53 +00:00
|
|
|
wg.Done()
|
|
|
|
}(tuple)
|
|
|
|
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
2018-06-27 08:11:45 +00:00
|
|
|
assert.NoError(t, backend.reSelectAccount())
|
2018-04-20 15:39:53 +00:00
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
assert.NoError(t, backend.Logout())
|
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
}
|
|
|
|
|
2019-01-24 15:44:46 +00:00
|
|
|
func TestBackendInjectChatAccount(t *testing.T) {
|
2019-10-04 15:21:24 +00:00
|
|
|
utils.Init()
|
|
|
|
|
2019-11-23 17:57:05 +00:00
|
|
|
backend := NewGethStatusBackend()
|
2019-01-24 15:44:46 +00:00
|
|
|
config, err := utils.MakeTestNodeConfig(params.StatusChainNetworkID)
|
|
|
|
require.NoError(t, err)
|
2019-08-20 15:38:40 +00:00
|
|
|
require.NoError(t, backend.AccountManager().InitKeystore(config.KeyStoreDir))
|
2019-01-24 15:44:46 +00:00
|
|
|
err = backend.StartNode(config)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer func() {
|
|
|
|
require.NoError(t, backend.StopNode())
|
|
|
|
}()
|
|
|
|
|
|
|
|
chatPrivKey, err := crypto.GenerateKey()
|
|
|
|
require.NoError(t, err)
|
|
|
|
encryptionPrivKey, err := crypto.GenerateKey()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
chatPrivKeyHex := hex.EncodeToString(crypto.FromECDSA(chatPrivKey))
|
2019-11-23 17:57:05 +00:00
|
|
|
chatPubKeyHex := types.EncodeHex(crypto.FromECDSAPub(&chatPrivKey.PublicKey))
|
2019-01-24 15:44:46 +00:00
|
|
|
encryptionPrivKeyHex := hex.EncodeToString(crypto.FromECDSA(encryptionPrivKey))
|
|
|
|
|
|
|
|
whisperService, err := backend.StatusNode().WhisperService()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// public key should not be already in whisper
|
|
|
|
require.False(t, whisperService.HasKeyPair(chatPubKeyHex), "identity already present in whisper")
|
|
|
|
|
|
|
|
// call InjectChatAccount
|
|
|
|
require.NoError(t, backend.InjectChatAccount(chatPrivKeyHex, encryptionPrivKeyHex))
|
|
|
|
|
|
|
|
// public key should now be in whisper
|
|
|
|
require.True(t, whisperService.HasKeyPair(chatPubKeyHex), "identity not injected into whisper")
|
|
|
|
|
|
|
|
// wallet account should not be selected
|
2019-07-26 14:45:10 +00:00
|
|
|
mainAccountAddress, err := backend.AccountManager().MainAccountAddress()
|
|
|
|
require.Equal(t, common.Address{}, mainAccountAddress)
|
2019-01-24 15:44:46 +00:00
|
|
|
require.Equal(t, account.ErrNoAccountSelected, err)
|
|
|
|
|
|
|
|
// selected chat account should have the key injected previously
|
|
|
|
chatAcc, err := backend.AccountManager().SelectedChatAccount()
|
|
|
|
require.Nil(t, err)
|
|
|
|
require.Equal(t, chatPrivKey, chatAcc.AccountKey.PrivateKey)
|
|
|
|
}
|
|
|
|
|
2018-04-20 15:39:53 +00:00
|
|
|
func TestBackendConnectionChangesConcurrently(t *testing.T) {
|
2018-06-27 08:11:45 +00:00
|
|
|
connections := [...]string{wifi, cellular, unknown}
|
2019-11-23 17:57:05 +00:00
|
|
|
backend := NewGethStatusBackend()
|
2018-04-20 15:39:53 +00:00
|
|
|
count := 3
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
|
|
|
for i := 0; i < count; i++ {
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
connIdx := rand.Intn(len(connections))
|
2018-04-23 13:34:49 +00:00
|
|
|
backend.ConnectionChange(connections[connIdx], false)
|
2018-04-20 15:39:53 +00:00
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
}
|
|
|
|
|
2018-06-27 08:11:45 +00:00
|
|
|
func TestBackendConnectionChangesToOffline(t *testing.T) {
|
2019-11-23 17:57:05 +00:00
|
|
|
b := NewGethStatusBackend()
|
2018-06-27 08:11:45 +00:00
|
|
|
b.ConnectionChange(none, false)
|
|
|
|
assert.True(t, b.connectionState.Offline)
|
|
|
|
|
|
|
|
b.ConnectionChange(wifi, false)
|
|
|
|
assert.False(t, b.connectionState.Offline)
|
|
|
|
|
|
|
|
b.ConnectionChange("unknown-state", false)
|
|
|
|
assert.False(t, b.connectionState.Offline)
|
|
|
|
}
|
|
|
|
|
2018-04-20 15:39:53 +00:00
|
|
|
func TestBackendCallRPCConcurrently(t *testing.T) {
|
2019-10-04 15:21:24 +00:00
|
|
|
utils.Init()
|
|
|
|
|
2019-11-23 17:57:05 +00:00
|
|
|
backend := NewGethStatusBackend()
|
2018-09-13 16:31:29 +00:00
|
|
|
config, err := utils.MakeTestNodeConfig(params.StatusChainNetworkID)
|
|
|
|
require.NoError(t, err)
|
2019-08-20 15:38:40 +00:00
|
|
|
require.NoError(t, backend.AccountManager().InitKeystore(config.KeyStoreDir))
|
2018-04-20 15:39:53 +00:00
|
|
|
count := 3
|
|
|
|
|
2018-09-13 16:31:29 +00:00
|
|
|
err = backend.StartNode(config)
|
2018-04-20 15:39:53 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
defer func() {
|
|
|
|
require.NoError(t, backend.StopNode())
|
|
|
|
}()
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
|
|
|
for i := 0; i < count; i++ {
|
|
|
|
wg.Add(1)
|
|
|
|
go func(idx int) {
|
2018-12-20 08:31:17 +00:00
|
|
|
result, err := backend.CallRPC(fmt.Sprintf(
|
2018-04-20 15:39:53 +00:00
|
|
|
`{"jsonrpc":"2.0","method":"web3_clientVersion","params":[],"id":%d}`,
|
|
|
|
idx+1,
|
|
|
|
))
|
2018-12-20 08:31:17 +00:00
|
|
|
assert.NoError(t, err)
|
2018-04-20 15:39:53 +00:00
|
|
|
assert.NotContains(t, result, "error")
|
|
|
|
wg.Done()
|
|
|
|
}(i)
|
|
|
|
|
|
|
|
wg.Add(1)
|
|
|
|
go func(idx int) {
|
2018-12-20 08:31:17 +00:00
|
|
|
result, err := backend.CallPrivateRPC(fmt.Sprintf(
|
2018-04-20 15:39:53 +00:00
|
|
|
`{"jsonrpc":"2.0","method":"web3_clientVersion","params":[],"id":%d}`,
|
|
|
|
idx+1,
|
|
|
|
))
|
2018-12-20 08:31:17 +00:00
|
|
|
assert.NoError(t, err)
|
2018-04-20 15:39:53 +00:00
|
|
|
assert.NotContains(t, result, "error")
|
|
|
|
wg.Done()
|
|
|
|
}(i)
|
|
|
|
}
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
}
|
|
|
|
|
2018-06-27 08:11:45 +00:00
|
|
|
func TestAppStateChange(t *testing.T) {
|
2019-11-23 17:57:05 +00:00
|
|
|
backend := NewGethStatusBackend()
|
2018-06-27 08:11:45 +00:00
|
|
|
|
|
|
|
var testCases = []struct {
|
|
|
|
name string
|
|
|
|
fromState appState
|
|
|
|
toState appState
|
|
|
|
expectedState appState
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "success",
|
|
|
|
fromState: appStateInactive,
|
|
|
|
toState: appStateBackground,
|
|
|
|
expectedState: appStateBackground,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "invalid state",
|
|
|
|
fromState: appStateInactive,
|
|
|
|
toState: "unexisting",
|
|
|
|
expectedState: appStateInactive,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
backend.appState = tc.fromState
|
|
|
|
backend.AppStateChange(tc.toState.String())
|
|
|
|
assert.Equal(t, tc.expectedState.String(), backend.appState.String())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-02 07:07:55 +00:00
|
|
|
func TestBlockedRPCMethods(t *testing.T) {
|
2019-10-04 15:21:24 +00:00
|
|
|
utils.Init()
|
|
|
|
|
2019-11-23 17:57:05 +00:00
|
|
|
backend := NewGethStatusBackend()
|
2018-09-13 16:31:29 +00:00
|
|
|
config, err := utils.MakeTestNodeConfig(params.StatusChainNetworkID)
|
|
|
|
require.NoError(t, err)
|
2019-08-20 15:38:40 +00:00
|
|
|
require.NoError(t, backend.AccountManager().InitKeystore(config.KeyStoreDir))
|
2018-09-13 16:31:29 +00:00
|
|
|
err = backend.StartNode(config)
|
2018-08-02 07:07:55 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
defer func() { require.NoError(t, backend.StopNode()) }()
|
|
|
|
|
|
|
|
for idx, m := range rpc.BlockedMethods() {
|
2018-12-20 08:31:17 +00:00
|
|
|
result, err := backend.CallRPC(fmt.Sprintf(
|
2018-08-02 07:07:55 +00:00
|
|
|
`{"jsonrpc":"2.0","method":"%s","params":[],"id":%d}`,
|
|
|
|
m,
|
|
|
|
idx+1,
|
|
|
|
))
|
2018-12-20 08:31:17 +00:00
|
|
|
assert.NoError(t, err)
|
2018-08-02 07:07:55 +00:00
|
|
|
assert.Contains(t, result, fmt.Sprintf(`{"code":-32700,"message":"%s"}`, rpc.ErrMethodNotFound))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-20 08:31:17 +00:00
|
|
|
func TestCallRPCWithStoppedNode(t *testing.T) {
|
2019-11-23 17:57:05 +00:00
|
|
|
backend := NewGethStatusBackend()
|
2018-12-20 08:31:17 +00:00
|
|
|
|
|
|
|
resp, err := backend.CallRPC(
|
|
|
|
`{"jsonrpc":"2.0","method":"web3_clientVersion","params":[],"id":1}`,
|
|
|
|
)
|
|
|
|
assert.Equal(t, ErrRPCClientUnavailable, err)
|
|
|
|
assert.Equal(t, "", resp)
|
|
|
|
|
|
|
|
resp, err = backend.CallPrivateRPC(
|
|
|
|
`{"jsonrpc":"2.0","method":"web3_clientVersion","params":[],"id":1}`,
|
|
|
|
)
|
|
|
|
assert.Equal(t, ErrRPCClientUnavailable, err)
|
|
|
|
assert.Equal(t, "", resp)
|
|
|
|
}
|
|
|
|
|
2018-08-16 11:37:53 +00:00
|
|
|
// TODO(adam): add concurrent tests for: SendTransaction
|
2019-01-17 11:02:45 +00:00
|
|
|
|
|
|
|
func TestStartStopMultipleTimes(t *testing.T) {
|
2019-10-04 15:21:24 +00:00
|
|
|
utils.Init()
|
|
|
|
|
2019-11-23 17:57:05 +00:00
|
|
|
backend := NewGethStatusBackend()
|
2019-01-17 11:02:45 +00:00
|
|
|
config, err := utils.MakeTestNodeConfig(params.StatusChainNetworkID)
|
|
|
|
require.NoError(t, err)
|
2019-08-20 15:38:40 +00:00
|
|
|
require.NoError(t, backend.AccountManager().InitKeystore(config.KeyStoreDir))
|
2019-01-17 11:02:45 +00:00
|
|
|
config.NoDiscovery = false
|
|
|
|
// doesn't have to be running. just any valid enode to bypass validation.
|
|
|
|
config.ClusterConfig.BootNodes = []string{
|
|
|
|
"enode://e8a7c03b58911e98bbd66accb2a55d57683f35b23bf9dfca89e5e244eb5cc3f25018b4112db507faca34fb69ffb44b362f79eda97a669a8df29c72e654416784@0.0.0.0:30404",
|
|
|
|
}
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NoError(t, backend.StartNode(config))
|
|
|
|
require.NoError(t, backend.StopNode())
|
|
|
|
require.NoError(t, backend.StartNode(config))
|
|
|
|
require.NoError(t, backend.StopNode())
|
|
|
|
}
|
2019-03-28 14:56:21 +00:00
|
|
|
|
2019-04-18 13:52:08 +00:00
|
|
|
func TestSignHash(t *testing.T) {
|
2019-10-04 15:21:24 +00:00
|
|
|
utils.Init()
|
|
|
|
|
2019-11-23 17:57:05 +00:00
|
|
|
backend := NewGethStatusBackend()
|
2019-04-18 13:52:08 +00:00
|
|
|
config, err := utils.MakeTestNodeConfig(params.StatusChainNetworkID)
|
|
|
|
require.NoError(t, err)
|
2019-08-20 15:38:40 +00:00
|
|
|
require.NoError(t, backend.AccountManager().InitKeystore(config.KeyStoreDir))
|
2019-04-18 13:52:08 +00:00
|
|
|
|
|
|
|
require.NoError(t, backend.StartNode(config))
|
|
|
|
defer func() {
|
|
|
|
require.NoError(t, backend.StopNode())
|
|
|
|
}()
|
|
|
|
|
|
|
|
var testCases = []struct {
|
|
|
|
name string
|
|
|
|
chatPrivKeyHex string
|
|
|
|
hashHex string
|
|
|
|
expectedSignatureHex string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "tc1",
|
|
|
|
chatPrivKeyHex: "facadefacadefacadefacadefacadefacadefacadefacadefacadefacadefaca",
|
|
|
|
hashHex: "0xa4735de5193362fe856416000105cdfa6ce56265607311cebae93b26e5adf438",
|
|
|
|
expectedSignatureHex: "0x176c971bae188c663614fc535ac9dbf62871dfeaadb38645809a510d28b3c4b0245415d5547c1b27f7cfea3341564f9c6981421144d3606b455346be69bd078c01",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
const dummyEncKey = "facadefacadefacadefacadefacadefacadefacadefacadefacadefacadefaca"
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
require.NoError(t, backend.InjectChatAccount(tc.chatPrivKeyHex, dummyEncKey))
|
|
|
|
|
|
|
|
signature, err := backend.SignHash(tc.hashHex)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, signature, tc.expectedSignatureHex)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-28 14:56:21 +00:00
|
|
|
func TestHashTypedData(t *testing.T) {
|
2019-10-04 15:21:24 +00:00
|
|
|
utils.Init()
|
|
|
|
|
2019-11-23 17:57:05 +00:00
|
|
|
backend := NewGethStatusBackend()
|
2019-03-28 14:56:21 +00:00
|
|
|
config, err := utils.MakeTestNodeConfig(params.StatusChainNetworkID)
|
|
|
|
require.NoError(t, err)
|
2019-08-20 15:38:40 +00:00
|
|
|
require.NoError(t, backend.AccountManager().InitKeystore(config.KeyStoreDir))
|
2019-03-28 14:56:21 +00:00
|
|
|
err = backend.StartNode(config)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer func() {
|
|
|
|
require.NoError(t, backend.StopNode())
|
|
|
|
}()
|
|
|
|
|
|
|
|
eip712Domain := "EIP712Domain"
|
|
|
|
mytypes := typeddata.Types{
|
|
|
|
eip712Domain: []typeddata.Field{
|
|
|
|
{Name: "name", Type: "string"},
|
|
|
|
{Name: "version", Type: "string"},
|
|
|
|
{Name: "chainId", Type: "uint256"},
|
|
|
|
{Name: "verifyingContract", Type: "address"},
|
|
|
|
},
|
|
|
|
"Text": []typeddata.Field{
|
|
|
|
{Name: "body", Type: "string"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
domain := map[string]json.RawMessage{
|
|
|
|
"name": json.RawMessage(`"Ether Text"`),
|
|
|
|
"version": json.RawMessage(`"1"`),
|
|
|
|
"chainId": json.RawMessage(fmt.Sprintf("%d", params.StatusChainNetworkID)),
|
|
|
|
"verifyingContract": json.RawMessage(`"0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC"`),
|
|
|
|
}
|
|
|
|
msg := map[string]json.RawMessage{
|
|
|
|
"body": json.RawMessage(`"Hello, Bob!"`),
|
|
|
|
}
|
|
|
|
|
|
|
|
typed := typeddata.TypedData{
|
|
|
|
Types: mytypes,
|
|
|
|
PrimaryType: "Text",
|
|
|
|
Domain: domain,
|
|
|
|
Message: msg,
|
|
|
|
}
|
|
|
|
|
|
|
|
hash, err := backend.HashTypedData(typed)
|
|
|
|
require.NoError(t, err)
|
2019-11-23 17:57:05 +00:00
|
|
|
assert.NotEqual(t, types.Hash{}, hash)
|
2019-03-28 14:56:21 +00:00
|
|
|
}
|
2019-08-29 08:06:22 +00:00
|
|
|
|
|
|
|
func TestBackendGetVerifiedAccount(t *testing.T) {
|
2019-10-04 15:21:24 +00:00
|
|
|
utils.Init()
|
|
|
|
|
2019-08-29 08:06:22 +00:00
|
|
|
password := "test"
|
|
|
|
tmpdir, err := ioutil.TempDir("", "verified-account-test-")
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer os.Remove(tmpdir)
|
2019-11-23 17:57:05 +00:00
|
|
|
backend := NewGethStatusBackend()
|
2019-08-29 08:06:22 +00:00
|
|
|
backend.UpdateRootDataDir(tmpdir)
|
2019-11-23 17:57:05 +00:00
|
|
|
require.NoError(t, backend.AccountManager().InitKeystore(filepath.Join(tmpdir, "keystore")))
|
2019-12-05 08:00:57 +00:00
|
|
|
require.NoError(t, backend.ensureAppDBOpened(multiaccounts.Account{KeyUID: "0x1"}, password))
|
2019-08-29 08:06:22 +00:00
|
|
|
config, err := params.NewNodeConfig(tmpdir, 178733)
|
|
|
|
require.NoError(t, err)
|
|
|
|
// this is for StatusNode().Config() call inside of the getVerifiedWalletAccount
|
|
|
|
require.NoError(t, backend.StartNode(config))
|
|
|
|
defer func() {
|
|
|
|
require.NoError(t, backend.StopNode())
|
|
|
|
}()
|
|
|
|
|
|
|
|
t.Run("AccountDoesntExist", func(t *testing.T) {
|
|
|
|
pkey, err := crypto.GenerateKey()
|
|
|
|
require.NoError(t, err)
|
|
|
|
address := crypto.PubkeyToAddress(pkey.PublicKey)
|
|
|
|
key, err := backend.getVerifiedWalletAccount(address.String(), password)
|
|
|
|
require.EqualError(t, err, transactions.ErrAccountDoesntExist.Error())
|
|
|
|
require.Nil(t, key)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("PasswordDoesntMatch", func(t *testing.T) {
|
|
|
|
pkey, err := crypto.GenerateKey()
|
|
|
|
require.NoError(t, err)
|
|
|
|
address := crypto.PubkeyToAddress(pkey.PublicKey)
|
|
|
|
db := accounts.NewDB(backend.appDB)
|
2019-11-23 17:57:05 +00:00
|
|
|
_, err = backend.AccountManager().ImportAccount(pkey, password)
|
2019-08-29 08:06:22 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NoError(t, db.SaveAccounts([]accounts.Account{{Address: address}}))
|
|
|
|
key, err := backend.getVerifiedWalletAccount(address.String(), "wrong-password")
|
2019-10-04 15:21:24 +00:00
|
|
|
require.EqualError(t, err, "could not decrypt key with given password")
|
2019-08-29 08:06:22 +00:00
|
|
|
require.Nil(t, key)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Success", func(t *testing.T) {
|
|
|
|
pkey, err := crypto.GenerateKey()
|
|
|
|
require.NoError(t, err)
|
|
|
|
address := crypto.PubkeyToAddress(pkey.PublicKey)
|
|
|
|
db := accounts.NewDB(backend.appDB)
|
2019-11-23 17:57:05 +00:00
|
|
|
_, err = backend.AccountManager().ImportAccount(pkey, password)
|
2019-08-29 08:06:22 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NoError(t, db.SaveAccounts([]accounts.Account{{Address: address}}))
|
|
|
|
key, err := backend.getVerifiedWalletAccount(address.String(), password)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, address, key.Address)
|
|
|
|
})
|
|
|
|
}
|
2019-09-02 19:03:15 +00:00
|
|
|
|
|
|
|
func TestLoginWithKey(t *testing.T) {
|
2019-10-04 15:21:24 +00:00
|
|
|
utils.Init()
|
|
|
|
|
2019-11-23 17:57:05 +00:00
|
|
|
b := NewGethStatusBackend()
|
2019-09-02 19:03:15 +00:00
|
|
|
pkey, err := crypto.GenerateKey()
|
|
|
|
require.NoError(t, err)
|
2019-12-05 08:00:57 +00:00
|
|
|
keyUIDHex := sha256.Sum256(crypto.FromECDSAPub(&pkey.PublicKey))
|
|
|
|
keyUID := types.EncodeHex(keyUIDHex[:])
|
2019-09-02 19:03:15 +00:00
|
|
|
main := multiaccounts.Account{
|
2019-12-05 08:00:57 +00:00
|
|
|
KeyUID: keyUID,
|
2019-09-02 19:03:15 +00:00
|
|
|
}
|
|
|
|
tmpdir, err := ioutil.TempDir("", "login-with-key-test-")
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer os.Remove(tmpdir)
|
|
|
|
conf, err := params.NewNodeConfig(tmpdir, 1777)
|
|
|
|
require.NoError(t, err)
|
|
|
|
keyhex := hex.EncodeToString(crypto.FromECDSA(pkey))
|
|
|
|
|
2019-11-23 17:57:05 +00:00
|
|
|
require.NoError(t, b.AccountManager().InitKeystore(conf.KeyStoreDir))
|
2019-09-02 19:03:15 +00:00
|
|
|
b.UpdateRootDataDir(conf.DataDir)
|
|
|
|
require.NoError(t, b.OpenAccounts())
|
|
|
|
|
|
|
|
require.NoError(t, b.SaveAccountAndStartNodeWithKey(main, conf, "test-pass", keyhex))
|
|
|
|
require.NoError(t, b.Logout())
|
|
|
|
require.NoError(t, b.StopNode())
|
|
|
|
|
|
|
|
require.NoError(t, b.StartNodeWithKey(main, "test-pass", keyhex))
|
|
|
|
defer func() {
|
|
|
|
assert.NoError(t, b.Logout())
|
|
|
|
assert.NoError(t, b.StopNode())
|
|
|
|
}()
|
|
|
|
extkey, err := b.accountManager.SelectedChatAccount()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, crypto.PubkeyToAddress(pkey.PublicKey), extkey.Address)
|
|
|
|
}
|