2020-07-20 21:12:46 +00:00
|
|
|
package v2
|
|
|
|
|
|
|
|
import (
|
2020-07-22 04:49:04 +00:00
|
|
|
"context"
|
|
|
|
"crypto/rand"
|
|
|
|
"fmt"
|
2020-07-20 21:12:46 +00:00
|
|
|
"io/ioutil"
|
2020-07-22 04:49:04 +00:00
|
|
|
"math/big"
|
2020-07-20 21:12:46 +00:00
|
|
|
"os"
|
2020-07-22 04:49:04 +00:00
|
|
|
"path"
|
2020-07-20 21:12:46 +00:00
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil"
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil/assert"
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil/require"
|
2020-07-22 04:49:04 +00:00
|
|
|
v2keymanager "github.com/prysmaticlabs/prysm/validator/keymanager/v2"
|
|
|
|
"github.com/prysmaticlabs/prysm/validator/keymanager/v2/direct"
|
2020-07-20 21:12:46 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestImport_Noninteractive(t *testing.T) {
|
2020-07-22 04:49:04 +00:00
|
|
|
walletDir, passwordsDir := setupWalletAndPasswordsDir(t)
|
|
|
|
randPath, err := rand.Int(rand.Reader, big.NewInt(1000000))
|
|
|
|
require.NoError(t, err, "Could not generate random file path")
|
|
|
|
exportDir := path.Join(testutil.TempDir(), fmt.Sprintf("/%d", randPath), "export")
|
|
|
|
importDir := path.Join(testutil.TempDir(), fmt.Sprintf("/%d", randPath), "import")
|
|
|
|
importPasswordDir := path.Join(testutil.TempDir(), fmt.Sprintf("/%d", randPath), "importpassword")
|
|
|
|
t.Cleanup(func() {
|
|
|
|
require.NoError(t, os.RemoveAll(exportDir), "Failed to remove directory")
|
|
|
|
require.NoError(t, os.RemoveAll(importDir), "Failed to remove directory")
|
|
|
|
require.NoError(t, os.RemoveAll(importPasswordDir), "Failed to remove directory")
|
|
|
|
})
|
|
|
|
require.NoError(t, os.MkdirAll(importPasswordDir, os.ModePerm))
|
|
|
|
passwordFilePath := filepath.Join(importPasswordDir, passwordFileName)
|
2020-07-20 21:12:46 +00:00
|
|
|
require.NoError(t, ioutil.WriteFile(passwordFilePath, []byte(password), os.ModePerm))
|
|
|
|
|
2020-07-22 04:49:04 +00:00
|
|
|
cliCtx := setupWalletCtx(t, &testWalletConfig{
|
|
|
|
walletDir: walletDir,
|
|
|
|
passwordsDir: passwordsDir,
|
|
|
|
exportDir: exportDir,
|
|
|
|
keymanagerKind: v2keymanager.Direct,
|
|
|
|
passwordFile: passwordFilePath,
|
|
|
|
})
|
|
|
|
wallet, err := NewWallet(cliCtx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
keymanager, err := direct.NewKeymanager(
|
|
|
|
ctx,
|
|
|
|
wallet,
|
|
|
|
direct.DefaultConfig(),
|
|
|
|
true, /* skip mnemonic */
|
|
|
|
)
|
|
|
|
require.NoError(t, err)
|
|
|
|
_, err = keymanager.CreateAccount(ctx, password)
|
|
|
|
require.NoError(t, err)
|
2020-07-20 21:12:46 +00:00
|
|
|
|
|
|
|
accounts, err := wallet.AccountNames()
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, len(accounts), 1)
|
|
|
|
|
|
|
|
require.NoError(t, wallet.zipAccounts(accounts, exportDir))
|
|
|
|
if _, err := os.Stat(filepath.Join(exportDir, archiveFilename)); os.IsNotExist(err) {
|
|
|
|
t.Fatal("Expected file to exist")
|
|
|
|
}
|
|
|
|
require.NoError(t, ImportAccount(cliCtx))
|
|
|
|
}
|