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"
|
2022-05-04 14:46:49 +00:00
|
|
|
"path"
|
2019-08-29 08:06:22 +00:00
|
|
|
"path/filepath"
|
2022-11-08 13:30:33 +00:00
|
|
|
"strings"
|
2018-04-20 15:39:53 +00:00
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
|
2020-01-02 09:10:19 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2019-12-11 13:59:37 +00:00
|
|
|
gethcrypto "github.com/ethereum/go-ethereum/crypto"
|
2020-01-02 09:10:19 +00:00
|
|
|
|
2021-05-14 10:55:42 +00:00
|
|
|
"github.com/status-im/status-go/connection"
|
2019-12-11 13:59:37 +00:00
|
|
|
"github.com/status-im/status-go/eth-node/crypto"
|
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"
|
2022-03-23 18:47:00 +00:00
|
|
|
"github.com/status-im/status-go/multiaccounts/settings"
|
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
|
|
|
)
|
|
|
|
|
2019-12-27 09:58:25 +00:00
|
|
|
var (
|
2022-03-23 18:47:00 +00:00
|
|
|
networks = json.RawMessage("{}")
|
|
|
|
testSettings = settings.Settings{
|
2019-12-27 09:58:25 +00:00
|
|
|
Address: types.HexToAddress("0xeC540f3745Ff2964AFC1171a5A0DD726d1F6B472"),
|
|
|
|
CurrentNetwork: "mainnet_rpc",
|
|
|
|
DappsAddress: types.HexToAddress("0xe1300f99fDF7346986CbC766903245087394ecd0"),
|
|
|
|
EIP1581Address: types.HexToAddress("0xe1DDDE9235a541d1344550d969715CF43982de9f"),
|
|
|
|
InstallationID: "d3efcff6-cffa-560e-a547-21d3858cbc51",
|
|
|
|
KeyUID: "0x4e8129f3edfc004875be17bf468a784098a9f69b53c095be1f52deff286935ab",
|
|
|
|
LatestDerivedPath: 0,
|
|
|
|
Name: "Jittery Cornflowerblue Kingbird",
|
|
|
|
Networks: &networks,
|
|
|
|
PhotoPath: "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADIAAAAyCAIAAACRXR/mAAAAjklEQVR4nOzXwQmFMBAAUZXUYh32ZB32ZB02sxYQQSZGsod55/91WFgSS0RM+SyjA56ZRZhFmEWYRRT6h+M6G16zrxv6fdJpmUWYRbxsYr13dKfanpN0WmYRZhGzXz6AWYRZRIfbaX26fT9Jk07LLMIsosPt9I/dTDotswizCG+nhFmEWYRZhFnEHQAA///z1CFkYamgfQAAAABJRU5ErkJggg==",
|
|
|
|
PreviewPrivacy: false,
|
|
|
|
PublicKey: "0x04211fe0f69772ecf7eb0b5bfc7678672508a9fb01f2d699096f0d59ef7fe1a0cb1e648a80190db1c0f5f088872444d846f2956d0bd84069f3f9f69335af852ac0",
|
|
|
|
SigningPhrase: "yurt joey vibe",
|
|
|
|
WalletRootAddress: types.HexToAddress("0xeB591fd819F86D0A6a2EF2Bcb94f77807a7De1a6")}
|
|
|
|
)
|
|
|
|
|
2018-04-20 15:39:53 +00:00
|
|
|
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 TestBackendConnectionChangesConcurrently(t *testing.T) {
|
2021-05-14 10:55:42 +00:00
|
|
|
connections := [...]string{connection.Wifi, connection.Cellular, connection.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() {
|
2020-12-28 09:09:45 +00:00
|
|
|
connIdx := rand.Intn(len(connections)) // nolint: gosec
|
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()
|
2021-05-14 10:55:42 +00:00
|
|
|
b.ConnectionChange(connection.None, false)
|
2018-06-27 08:11:45 +00:00
|
|
|
assert.True(t, b.connectionState.Offline)
|
|
|
|
|
2021-05-14 10:55:42 +00:00
|
|
|
b.ConnectionChange(connection.Wifi, false)
|
2018-06-27 08:11:45 +00:00
|
|
|
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
|
|
|
|
|
|
|
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"
|
2022-07-06 16:12:49 +00:00
|
|
|
backend, defers, err := setupWalletTest(t, password)
|
2019-08-29 08:06:22 +00:00
|
|
|
require.NoError(t, err)
|
2022-07-06 16:12:49 +00:00
|
|
|
defer defers()
|
2019-08-29 08:06:22 +00:00
|
|
|
|
|
|
|
t.Run("AccountDoesntExist", func(t *testing.T) {
|
2019-12-11 13:59:37 +00:00
|
|
|
pkey, err := gethcrypto.GenerateKey()
|
2019-08-29 08:06:22 +00:00
|
|
|
require.NoError(t, err)
|
2019-12-11 13:59:37 +00:00
|
|
|
address := gethcrypto.PubkeyToAddress(pkey.PublicKey)
|
2019-08-29 08:06:22 +00:00
|
|
|
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)
|
2022-03-23 18:47:00 +00:00
|
|
|
db, err := accounts.NewDB(backend.appDB)
|
2022-07-06 16:12:49 +00:00
|
|
|
|
2022-03-23 18:47:00 +00:00
|
|
|
require.NoError(t, err)
|
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)
|
2022-05-18 10:42:51 +00:00
|
|
|
require.NoError(t, db.SaveAccounts([]*accounts.Account{{Address: address}}))
|
2019-08-29 08:06:22 +00:00
|
|
|
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)
|
|
|
|
})
|
|
|
|
|
2022-07-06 16:12:49 +00:00
|
|
|
t.Run("PartialAccount", func(t *testing.T) {
|
|
|
|
// Create a derived wallet account without storing the keys
|
|
|
|
db, err := accounts.NewDB(backend.appDB)
|
|
|
|
require.NoError(t, err)
|
|
|
|
newPath := "m/0"
|
|
|
|
walletRootAddress, err := db.GetWalletRootAddress()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
walletInfo, err := backend.AccountManager().AccountsGenerator().LoadAccount(walletRootAddress.String(), password)
|
|
|
|
require.NoError(t, err)
|
|
|
|
derivedInfos, err := backend.AccountManager().AccountsGenerator().DeriveAddresses(walletInfo.ID, []string{newPath})
|
|
|
|
require.NoError(t, err)
|
|
|
|
derivedInfo := derivedInfos[newPath]
|
|
|
|
|
|
|
|
partialAcc := &accounts.Account{
|
|
|
|
Address: types.HexToAddress(derivedInfo.Address),
|
|
|
|
Type: accounts.AccountTypeGenerated,
|
|
|
|
PublicKey: types.Hex2Bytes(derivedInfo.PublicKey),
|
|
|
|
Path: newPath,
|
|
|
|
Wallet: false,
|
|
|
|
Name: "PartialAccount",
|
|
|
|
}
|
|
|
|
require.NoError(t, db.SaveAccounts([]*accounts.Account{partialAcc}))
|
|
|
|
|
|
|
|
// With partial account we need to dynamically generate private key
|
|
|
|
key, err := backend.getVerifiedWalletAccount(partialAcc.Address.Hex(), password)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, partialAcc.Address, key.Address)
|
|
|
|
})
|
|
|
|
|
2019-08-29 08:06:22 +00:00
|
|
|
t.Run("Success", func(t *testing.T) {
|
|
|
|
pkey, err := crypto.GenerateKey()
|
|
|
|
require.NoError(t, err)
|
|
|
|
address := crypto.PubkeyToAddress(pkey.PublicKey)
|
2022-03-23 18:47:00 +00:00
|
|
|
db, err := accounts.NewDB(backend.appDB)
|
|
|
|
require.NoError(t, err)
|
2022-07-06 16:12:49 +00:00
|
|
|
defer db.Close()
|
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)
|
2022-05-18 10:42:51 +00:00
|
|
|
require.NoError(t, db.SaveAccounts([]*accounts.Account{{Address: address}}))
|
2019-08-29 08:06:22 +00:00
|
|
|
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-12-11 13:59:37 +00:00
|
|
|
chatKey, err := gethcrypto.GenerateKey()
|
2019-09-02 19:03:15 +00:00
|
|
|
require.NoError(t, err)
|
2019-12-11 13:59:37 +00:00
|
|
|
walletKey, err := gethcrypto.GenerateKey()
|
2019-12-18 15:09:04 +00:00
|
|
|
require.NoError(t, err)
|
2019-12-11 13:59:37 +00:00
|
|
|
keyUIDHex := sha256.Sum256(gethcrypto.FromECDSAPub(&chatKey.PublicKey))
|
2019-12-05 08:00:57 +00:00
|
|
|
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)
|
2019-12-11 13:59:37 +00:00
|
|
|
keyhex := hex.EncodeToString(gethcrypto.FromECDSA(chatKey))
|
2019-09-02 19:03:15 +00:00
|
|
|
|
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())
|
|
|
|
|
2019-12-18 15:09:04 +00:00
|
|
|
address := crypto.PubkeyToAddress(walletKey.PublicKey)
|
2022-05-18 10:42:51 +00:00
|
|
|
require.NoError(t, b.SaveAccountAndStartNodeWithKey(main, "test-pass", testSettings, conf, []*accounts.Account{{Address: address, Wallet: true}}, keyhex))
|
2019-09-02 19:03:15 +00:00
|
|
|
require.NoError(t, b.Logout())
|
|
|
|
require.NoError(t, b.StopNode())
|
|
|
|
|
2021-07-12 09:44:26 +00:00
|
|
|
require.NoError(t, b.AccountManager().InitKeystore(conf.KeyStoreDir))
|
|
|
|
b.UpdateRootDataDir(conf.DataDir)
|
|
|
|
require.NoError(t, b.OpenAccounts())
|
|
|
|
|
2019-09-02 19:03:15 +00:00
|
|
|
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)
|
2019-12-18 15:09:04 +00:00
|
|
|
require.Equal(t, crypto.PubkeyToAddress(chatKey.PublicKey), extkey.Address)
|
2019-09-02 19:03:15 +00:00
|
|
|
}
|
2020-07-13 10:45:36 +00:00
|
|
|
|
2021-07-20 11:48:10 +00:00
|
|
|
func TestVerifyDatabasePassword(t *testing.T) {
|
|
|
|
utils.Init()
|
|
|
|
|
|
|
|
b := NewGethStatusBackend()
|
|
|
|
chatKey, err := gethcrypto.GenerateKey()
|
|
|
|
require.NoError(t, err)
|
|
|
|
walletKey, err := gethcrypto.GenerateKey()
|
|
|
|
require.NoError(t, err)
|
|
|
|
keyUIDHex := sha256.Sum256(gethcrypto.FromECDSAPub(&chatKey.PublicKey))
|
|
|
|
keyUID := types.EncodeHex(keyUIDHex[:])
|
|
|
|
main := multiaccounts.Account{
|
|
|
|
KeyUID: keyUID,
|
|
|
|
}
|
|
|
|
tmpdir, err := ioutil.TempDir("", "verify-database-password-")
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer os.Remove(tmpdir)
|
|
|
|
conf, err := params.NewNodeConfig(tmpdir, 1777)
|
|
|
|
require.NoError(t, err)
|
|
|
|
keyhex := hex.EncodeToString(gethcrypto.FromECDSA(chatKey))
|
|
|
|
|
|
|
|
require.NoError(t, b.AccountManager().InitKeystore(conf.KeyStoreDir))
|
|
|
|
b.UpdateRootDataDir(conf.DataDir)
|
|
|
|
require.NoError(t, b.OpenAccounts())
|
|
|
|
|
|
|
|
address := crypto.PubkeyToAddress(walletKey.PublicKey)
|
2022-05-18 10:42:51 +00:00
|
|
|
require.NoError(t, b.SaveAccountAndStartNodeWithKey(main, "test-pass", testSettings, conf, []*accounts.Account{{Address: address, Wallet: true}}, keyhex))
|
2021-07-20 11:48:10 +00:00
|
|
|
require.NoError(t, b.Logout())
|
|
|
|
require.NoError(t, b.StopNode())
|
|
|
|
|
|
|
|
require.Error(t, b.VerifyDatabasePassword(main.KeyUID, "wrong-pass"))
|
|
|
|
require.NoError(t, b.VerifyDatabasePassword(main.KeyUID, "test-pass"))
|
|
|
|
}
|
|
|
|
|
2022-07-06 16:12:49 +00:00
|
|
|
func TestDeleteMultiaccount(t *testing.T) {
|
2020-07-13 10:45:36 +00:00
|
|
|
backend := NewGethStatusBackend()
|
|
|
|
|
|
|
|
rootDataDir, err := ioutil.TempDir("", "test-keystore-dir")
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer os.Remove(rootDataDir)
|
|
|
|
|
|
|
|
keyStoreDir := filepath.Join(rootDataDir, "keystore")
|
|
|
|
|
|
|
|
backend.rootDataDir = rootDataDir
|
|
|
|
|
|
|
|
err = backend.AccountManager().InitKeystore(keyStoreDir)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
backend.AccountManager()
|
|
|
|
accs, err := backend.AccountManager().
|
|
|
|
AccountsGenerator().
|
|
|
|
GenerateAndDeriveAddresses(12, 1, "", []string{"m/44'/60'/0'/0"})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
generateAccount := accs[0]
|
|
|
|
accountInfo, err := backend.AccountManager().
|
|
|
|
AccountsGenerator().
|
|
|
|
StoreAccount(generateAccount.ID, "123123")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
account := multiaccounts.Account{
|
|
|
|
Name: "foo",
|
|
|
|
Timestamp: 1,
|
|
|
|
KeycardPairing: "pairing",
|
|
|
|
KeyUID: generateAccount.KeyUID,
|
|
|
|
}
|
|
|
|
|
|
|
|
err = backend.ensureAppDBOpened(account, "123123")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-03-23 18:47:00 +00:00
|
|
|
s := settings.Settings{
|
2020-07-13 10:45:36 +00:00
|
|
|
Address: types.HexToAddress(accountInfo.Address),
|
|
|
|
CurrentNetwork: "mainnet_rpc",
|
|
|
|
DappsAddress: types.HexToAddress(accountInfo.Address),
|
|
|
|
EIP1581Address: types.HexToAddress(accountInfo.Address),
|
|
|
|
InstallationID: "d3efcff6-cffa-560e-a547-21d3858cbc51",
|
|
|
|
KeyUID: account.KeyUID,
|
|
|
|
LatestDerivedPath: 0,
|
|
|
|
Name: "Jittery Cornflowerblue Kingbird",
|
|
|
|
Networks: &networks,
|
|
|
|
PhotoPath: "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADIAAAAyCAIAAACRXR/mAAAAjklEQVR4nOzXwQmFMBAAUZXUYh32ZB32ZB02sxYQQSZGsod55/91WFgSS0RM+SyjA56ZRZhFmEWYRRT6h+M6G16zrxv6fdJpmUWYRbxsYr13dKfanpN0WmYRZhGzXz6AWYRZRIfbaX26fT9Jk07LLMIsosPt9I/dTDotswizCG+nhFmEWYRZhFnEHQAA///z1CFkYamgfQAAAABJRU5ErkJggg==",
|
|
|
|
PreviewPrivacy: false,
|
|
|
|
PublicKey: accountInfo.PublicKey,
|
|
|
|
SigningPhrase: "yurt joey vibe",
|
|
|
|
WalletRootAddress: types.HexToAddress(accountInfo.Address)}
|
|
|
|
|
|
|
|
err = backend.saveAccountsAndSettings(
|
2022-03-23 18:47:00 +00:00
|
|
|
s,
|
2020-07-13 10:45:36 +00:00
|
|
|
¶ms.NodeConfig{},
|
|
|
|
nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = backend.OpenAccounts()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = backend.SaveAccount(account)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
files, err := ioutil.ReadDir(rootDataDir)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotEqual(t, 3, len(files))
|
|
|
|
|
2022-07-06 16:12:49 +00:00
|
|
|
err = backend.DeleteMultiaccount(account.KeyUID, keyStoreDir)
|
2020-07-13 10:45:36 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
files, err = ioutil.ReadDir(rootDataDir)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 3, len(files))
|
|
|
|
}
|
2021-07-20 11:48:10 +00:00
|
|
|
|
|
|
|
func TestConvertAccount(t *testing.T) {
|
|
|
|
backend := NewGethStatusBackend()
|
|
|
|
password := "123123"
|
|
|
|
rootDataDir, err := ioutil.TempDir("", "test-keystore-dir")
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer os.Remove(rootDataDir)
|
|
|
|
|
|
|
|
keyStoreDir := filepath.Join(rootDataDir, "keystore")
|
|
|
|
|
|
|
|
backend.rootDataDir = rootDataDir
|
|
|
|
|
|
|
|
err = backend.AccountManager().InitKeystore(keyStoreDir)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-11-08 13:30:33 +00:00
|
|
|
const path = "m/44'/60'/0'/0"
|
2021-07-20 11:48:10 +00:00
|
|
|
backend.AccountManager()
|
|
|
|
accs, err := backend.AccountManager().
|
|
|
|
AccountsGenerator().
|
2022-11-08 13:30:33 +00:00
|
|
|
GenerateAndDeriveAddresses(12, 1, "", []string{path})
|
2021-07-20 11:48:10 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
generateAccount := accs[0]
|
|
|
|
accountInfo, err := backend.AccountManager().
|
|
|
|
AccountsGenerator().
|
|
|
|
StoreAccount(generateAccount.ID, password)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
account := multiaccounts.Account{
|
|
|
|
Name: "foo",
|
|
|
|
Timestamp: 1,
|
|
|
|
KeyUID: generateAccount.KeyUID,
|
|
|
|
}
|
|
|
|
|
|
|
|
err = backend.ensureAppDBOpened(account, password)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-03-23 18:47:00 +00:00
|
|
|
s := settings.Settings{
|
2021-07-20 11:48:10 +00:00
|
|
|
Address: types.HexToAddress(accountInfo.Address),
|
|
|
|
CurrentNetwork: "mainnet_rpc",
|
|
|
|
DappsAddress: types.HexToAddress(accountInfo.Address),
|
|
|
|
EIP1581Address: types.HexToAddress(accountInfo.Address),
|
|
|
|
InstallationID: "d3efcff6-cffa-560e-a547-21d3858cbc51",
|
|
|
|
KeyUID: account.KeyUID,
|
|
|
|
LatestDerivedPath: 0,
|
|
|
|
Name: "Jittery Cornflowerblue Kingbird",
|
|
|
|
Networks: &networks,
|
|
|
|
PhotoPath: "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADIAAAAyCAIAAACRXR/mAAAAjklEQVR4nOzXwQmFMBAAUZXUYh32ZB32ZB02sxYQQSZGsod55/91WFgSS0RM+SyjA56ZRZhFmEWYRRT6h+M6G16zrxv6fdJpmUWYRbxsYr13dKfanpN0WmYRZhGzXz6AWYRZRIfbaX26fT9Jk07LLMIsosPt9I/dTDotswizCG+nhFmEWYRZhFnEHQAA///z1CFkYamgfQAAAABJRU5ErkJggg==",
|
|
|
|
PreviewPrivacy: false,
|
|
|
|
PublicKey: accountInfo.PublicKey,
|
|
|
|
SigningPhrase: "yurt joey vibe",
|
|
|
|
WalletRootAddress: types.HexToAddress(accountInfo.Address)}
|
|
|
|
|
2022-11-08 13:30:33 +00:00
|
|
|
acc := accounts.Account{
|
|
|
|
Address: types.HexToAddress(generateAccount.Address),
|
|
|
|
KeyUID: generateAccount.KeyUID,
|
|
|
|
Type: accounts.AccountTypeGenerated,
|
|
|
|
PublicKey: types.Hex2Bytes(generateAccount.PublicKey),
|
|
|
|
Path: path,
|
|
|
|
Wallet: false,
|
|
|
|
Chat: false,
|
|
|
|
Name: "GeneratedAccount",
|
|
|
|
}
|
|
|
|
|
|
|
|
accounts := []*accounts.Account{&acc}
|
2021-07-20 11:48:10 +00:00
|
|
|
err = backend.saveAccountsAndSettings(
|
2022-03-23 18:47:00 +00:00
|
|
|
s,
|
2021-07-20 11:48:10 +00:00
|
|
|
¶ms.NodeConfig{},
|
2022-11-08 13:30:33 +00:00
|
|
|
accounts)
|
2021-07-20 11:48:10 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = backend.OpenAccounts()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = backend.SaveAccount(account)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
files, err := ioutil.ReadDir(rootDataDir)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotEqual(t, 3, len(files))
|
|
|
|
|
|
|
|
keycardPassword := "0xcafecafe"
|
|
|
|
|
|
|
|
keycardAccount := account
|
|
|
|
keycardAccount.KeycardPairing = "pairing"
|
|
|
|
|
2022-03-23 18:47:00 +00:00
|
|
|
keycardSettings := settings.Settings{
|
2021-07-20 11:48:10 +00:00
|
|
|
KeycardInstanceUID: "0xdeadbeef",
|
2022-08-25 15:09:08 +00:00
|
|
|
KeycardPairedOn: 1,
|
2021-07-20 11:48:10 +00:00
|
|
|
KeycardPairing: "pairing",
|
|
|
|
}
|
|
|
|
|
2022-11-08 13:30:33 +00:00
|
|
|
addrWithoutPrefix := strings.ToLower(generateAccount.Address[2:len(generateAccount.Address)])
|
|
|
|
found := false
|
|
|
|
err = filepath.Walk(keyStoreDir, func(path string, fileInfo os.FileInfo, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !fileInfo.IsDir() && strings.Contains(path, addrWithoutPrefix) {
|
|
|
|
found = true
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.True(t, found)
|
2022-09-26 06:50:42 +00:00
|
|
|
|
2021-07-20 11:48:10 +00:00
|
|
|
err = backend.ConvertToKeycardAccount(keyStoreDir, keycardAccount, keycardSettings, password, keycardPassword)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-11-08 13:30:33 +00:00
|
|
|
err = filepath.Walk(keyStoreDir, func(path string, fileInfo os.FileInfo, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !fileInfo.IsDir() && strings.Contains(path, addrWithoutPrefix) {
|
|
|
|
found = false
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.True(t, found)
|
2021-07-20 11:48:10 +00:00
|
|
|
|
|
|
|
err = backend.ensureAppDBOpened(keycardAccount, keycardPassword)
|
|
|
|
require.NoError(t, err)
|
2022-08-31 16:11:42 +00:00
|
|
|
|
|
|
|
b := NewGethStatusBackend()
|
|
|
|
require.NoError(t, b.OpenAccounts())
|
2021-07-20 11:48:10 +00:00
|
|
|
}
|
2022-05-04 14:46:49 +00:00
|
|
|
|
|
|
|
func copyFile(srcFolder string, dstFolder string, fileName string, t *testing.T) {
|
|
|
|
data, err := ioutil.ReadFile(path.Join(srcFolder, fileName))
|
|
|
|
if err != nil {
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
|
|
|
|
err = ioutil.WriteFile(path.Join(dstFolder, fileName), data, 0600)
|
|
|
|
if err != nil {
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func copyDir(srcFolder string, dstFolder string, t *testing.T) {
|
|
|
|
files, err := ioutil.ReadDir(srcFolder)
|
|
|
|
require.NoError(t, err)
|
|
|
|
for _, file := range files {
|
|
|
|
if !file.IsDir() {
|
|
|
|
copyFile(srcFolder, dstFolder, file.Name(), t)
|
|
|
|
} else {
|
|
|
|
childFolder := path.Join(srcFolder, file.Name())
|
|
|
|
newFolder := path.Join(dstFolder, file.Name())
|
|
|
|
err = os.MkdirAll(newFolder, os.ModePerm)
|
|
|
|
require.NoError(t, err)
|
|
|
|
copyDir(childFolder, newFolder, t)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func login(t *testing.T, conf *params.NodeConfig) {
|
|
|
|
// The following passwords and DB used in this test unit are only
|
|
|
|
// used to determine if login process works correctly after a migration
|
|
|
|
|
|
|
|
// Expected account data:
|
|
|
|
keyUID := "0x7c46c8f6f059ab72d524f2a6d356904db30bb0392636172ab3929a6bd2220f84" // #nosec G101
|
|
|
|
username := "TestUser"
|
|
|
|
passwd := "0xC888C9CE9E098D5864D3DED6EBCC140A12142263BACE3A23A36F9905F12BD64A" // #nosec G101
|
|
|
|
|
|
|
|
b := NewGethStatusBackend()
|
|
|
|
|
|
|
|
require.NoError(t, b.AccountManager().InitKeystore(conf.KeyStoreDir))
|
|
|
|
b.UpdateRootDataDir(conf.DataDir)
|
|
|
|
|
|
|
|
require.NoError(t, b.OpenAccounts())
|
|
|
|
|
|
|
|
accounts, err := b.GetAccounts()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.Len(t, accounts, 1)
|
|
|
|
require.Equal(t, username, accounts[0].Name)
|
|
|
|
require.Equal(t, keyUID, accounts[0].KeyUID)
|
|
|
|
|
|
|
|
wg := sync.WaitGroup{}
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
err := b.StartNodeWithAccount(accounts[0], passwd, conf)
|
|
|
|
require.NoError(t, err)
|
|
|
|
}()
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
require.NoError(t, b.Logout())
|
|
|
|
require.NoError(t, b.StopNode())
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLoginAndMigrationsStillWorkWithExistingUsers(t *testing.T) {
|
|
|
|
utils.Init()
|
|
|
|
|
|
|
|
srcFolder := "../static/test-0.97.3-account/"
|
|
|
|
|
|
|
|
tmpdir, err := ioutil.TempDir("", "login-and-migrations-with-existing-users")
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer os.Remove(tmpdir)
|
|
|
|
|
|
|
|
copyDir(srcFolder, tmpdir, t)
|
|
|
|
|
|
|
|
conf, err := params.NewNodeConfig(tmpdir, 1777)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
login(t, conf)
|
|
|
|
login(t, conf) // Login twice to catch weird errors that only appear after logout
|
|
|
|
}
|