2020-07-20 21:12:46 +00:00
|
|
|
package v2
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-07-22 04:49:04 +00:00
|
|
|
"crypto/rand"
|
|
|
|
"fmt"
|
|
|
|
"math/big"
|
2020-07-20 21:12:46 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil"
|
2020-07-22 04:49:04 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil/require"
|
|
|
|
v2keymanager "github.com/prysmaticlabs/prysm/validator/keymanager/v2"
|
|
|
|
"github.com/prysmaticlabs/prysm/validator/keymanager/v2/direct"
|
2020-07-20 21:12:46 +00:00
|
|
|
)
|
|
|
|
|
2020-07-22 04:49:04 +00:00
|
|
|
func TestZipAndUnzip(t *testing.T) {
|
|
|
|
walletDir, passwordsDir := setupWalletAndPasswordsDir(t)
|
|
|
|
randPath, err := rand.Int(rand.Reader, big.NewInt(1000000))
|
|
|
|
require.NoError(t, err, "Could not generate random file path")
|
2020-07-23 03:10:23 +00:00
|
|
|
exportDir := filepath.Join(testutil.TempDir(), fmt.Sprintf("/%d", randPath), "export")
|
|
|
|
importDir := filepath.Join(testutil.TempDir(), fmt.Sprintf("/%d", randPath), "import")
|
2020-07-22 04:49:04 +00:00
|
|
|
t.Cleanup(func() {
|
|
|
|
require.NoError(t, os.RemoveAll(exportDir), "Failed to remove directory")
|
|
|
|
require.NoError(t, os.RemoveAll(importDir), "Failed to remove directory")
|
|
|
|
})
|
|
|
|
cliCtx := setupWalletCtx(t, &testWalletConfig{
|
|
|
|
walletDir: walletDir,
|
|
|
|
passwordsDir: passwordsDir,
|
|
|
|
exportDir: exportDir,
|
|
|
|
keymanagerKind: v2keymanager.Direct,
|
|
|
|
})
|
|
|
|
wallet, err := NewWallet(cliCtx)
|
2020-07-20 21:12:46 +00:00
|
|
|
require.NoError(t, err)
|
2020-07-22 04:49:04 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
keymanager, err := direct.NewKeymanager(
|
|
|
|
ctx,
|
|
|
|
wallet,
|
|
|
|
direct.DefaultConfig(),
|
|
|
|
)
|
2020-07-20 21:12:46 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
_, err = keymanager.CreateAccount(ctx, password)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-07-23 02:12:51 +00:00
|
|
|
accounts, err := keymanager.ValidatingAccountNames()
|
2020-07-20 21:12:46 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
if len(accounts) == 0 {
|
|
|
|
t.Fatal("Expected more accounts, received 0")
|
|
|
|
}
|
|
|
|
err = wallet.zipAccounts(accounts, exportDir)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
if _, err := os.Stat(filepath.Join(exportDir, archiveFilename)); os.IsNotExist(err) {
|
|
|
|
t.Fatal("Expected file to exist")
|
|
|
|
}
|
|
|
|
|
|
|
|
importedAccounts, err := unzipArchiveToTarget(exportDir, importDir)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
allAccountsStr := strings.Join(accounts, " ")
|
|
|
|
for _, importedAccount := range importedAccounts {
|
|
|
|
if !strings.Contains(allAccountsStr, importedAccount) {
|
|
|
|
t.Fatalf("Expected %s to be in %s", importedAccount, allAccountsStr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestExport_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")
|
2020-07-23 03:10:23 +00:00
|
|
|
exportDir := filepath.Join(testutil.TempDir(), fmt.Sprintf("/%d", randPath), "export")
|
2020-07-22 04:49:04 +00:00
|
|
|
t.Cleanup(func() {
|
|
|
|
require.NoError(t, os.RemoveAll(exportDir), "Failed to remove directory")
|
|
|
|
})
|
2020-07-20 21:12:46 +00:00
|
|
|
accounts := "all"
|
2020-07-22 04:49:04 +00:00
|
|
|
cliCtx := setupWalletCtx(t, &testWalletConfig{
|
|
|
|
walletDir: walletDir,
|
|
|
|
passwordsDir: passwordsDir,
|
|
|
|
exportDir: exportDir,
|
|
|
|
accountsToExport: accounts,
|
|
|
|
keymanagerKind: v2keymanager.Direct,
|
|
|
|
})
|
|
|
|
wallet, err := NewWallet(cliCtx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
ctx := context.Background()
|
2020-07-23 02:12:51 +00:00
|
|
|
keymanagerCfg := direct.DefaultConfig()
|
|
|
|
encodedCfg, err := direct.MarshalConfigFile(ctx, keymanagerCfg)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NoError(t, wallet.WriteKeymanagerConfigToDisk(ctx, encodedCfg))
|
2020-07-22 04:49:04 +00:00
|
|
|
keymanager, err := direct.NewKeymanager(
|
|
|
|
ctx,
|
|
|
|
wallet,
|
2020-07-23 02:12:51 +00:00
|
|
|
keymanagerCfg,
|
2020-07-22 04:49:04 +00:00
|
|
|
)
|
|
|
|
require.NoError(t, err)
|
|
|
|
_, err = keymanager.CreateAccount(ctx, password)
|
|
|
|
require.NoError(t, err)
|
2020-07-20 21:12:46 +00:00
|
|
|
require.NoError(t, ExportAccount(cliCtx))
|
|
|
|
if _, err := os.Stat(filepath.Join(exportDir, archiveFilename)); os.IsNotExist(err) {
|
|
|
|
t.Fatal("Expected file to exist")
|
|
|
|
}
|
|
|
|
}
|