2020-07-01 21:30:01 +00:00
|
|
|
package v2
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/rand"
|
2020-07-22 04:49:04 +00:00
|
|
|
"flag"
|
2020-07-01 21:30:01 +00:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"math/big"
|
|
|
|
"os"
|
2020-07-23 03:10:23 +00:00
|
|
|
"path/filepath"
|
2020-07-01 21:30:01 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil"
|
2020-07-22 04:49:04 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil/assert"
|
2020-07-20 10:56:45 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil/require"
|
2020-07-22 04:49:04 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/validator/flags"
|
2020-07-01 21:30:01 +00:00
|
|
|
v2keymanager "github.com/prysmaticlabs/prysm/validator/keymanager/v2"
|
|
|
|
"github.com/sirupsen/logrus"
|
2020-07-22 04:49:04 +00:00
|
|
|
"github.com/urfave/cli/v2"
|
2020-07-01 21:30:01 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
logrus.SetLevel(logrus.DebugLevel)
|
|
|
|
logrus.SetOutput(ioutil.Discard)
|
|
|
|
}
|
|
|
|
|
2020-07-22 04:49:04 +00:00
|
|
|
type testWalletConfig struct {
|
|
|
|
walletDir string
|
|
|
|
passwordsDir string
|
|
|
|
exportDir string
|
|
|
|
accountsToExport string
|
|
|
|
passwordFile string
|
|
|
|
keymanagerKind v2keymanager.Kind
|
|
|
|
}
|
|
|
|
|
|
|
|
func setupWalletCtx(
|
|
|
|
tb testing.TB,
|
|
|
|
cfg *testWalletConfig,
|
|
|
|
) *cli.Context {
|
|
|
|
app := cli.App{}
|
|
|
|
set := flag.NewFlagSet("test", 0)
|
|
|
|
set.String(flags.WalletDirFlag.Name, cfg.walletDir, "")
|
|
|
|
set.String(flags.WalletPasswordsDirFlag.Name, cfg.passwordsDir, "")
|
|
|
|
set.String(flags.KeymanagerKindFlag.Name, cfg.keymanagerKind.String(), "")
|
2020-07-23 03:10:23 +00:00
|
|
|
set.String(flags.BackupDirFlag.Name, cfg.exportDir, "")
|
2020-07-22 04:49:04 +00:00
|
|
|
set.String(flags.AccountsFlag.Name, cfg.accountsToExport, "")
|
|
|
|
set.String(flags.PasswordFileFlag.Name, cfg.passwordFile, "")
|
|
|
|
assert.NoError(tb, set.Set(flags.WalletDirFlag.Name, cfg.walletDir))
|
|
|
|
assert.NoError(tb, set.Set(flags.WalletPasswordsDirFlag.Name, cfg.passwordsDir))
|
|
|
|
assert.NoError(tb, set.Set(flags.KeymanagerKindFlag.Name, cfg.keymanagerKind.String()))
|
2020-07-23 03:10:23 +00:00
|
|
|
assert.NoError(tb, set.Set(flags.BackupDirFlag.Name, cfg.exportDir))
|
2020-07-22 04:49:04 +00:00
|
|
|
assert.NoError(tb, set.Set(flags.AccountsFlag.Name, cfg.accountsToExport))
|
|
|
|
assert.NoError(tb, set.Set(flags.PasswordFileFlag.Name, cfg.passwordFile))
|
|
|
|
return cli.NewContext(&app, set, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func setupWalletAndPasswordsDir(t testing.TB) (string, string) {
|
2020-07-01 21:30:01 +00:00
|
|
|
randPath, err := rand.Int(rand.Reader, big.NewInt(1000000))
|
2020-07-20 10:56:45 +00:00
|
|
|
require.NoError(t, err, "Could not generate random file path")
|
2020-07-23 03:10:23 +00:00
|
|
|
walletDir := filepath.Join(testutil.TempDir(), fmt.Sprintf("/%d", randPath), "wallet")
|
2020-07-20 10:56:45 +00:00
|
|
|
require.NoError(t, os.RemoveAll(walletDir), "Failed to remove directory")
|
2020-07-23 03:10:23 +00:00
|
|
|
passwordsDir := filepath.Join(testutil.TempDir(), fmt.Sprintf("/%d", randPath), "passwords")
|
2020-07-20 10:56:45 +00:00
|
|
|
require.NoError(t, os.RemoveAll(passwordsDir), "Failed to remove directory")
|
2020-07-01 21:30:01 +00:00
|
|
|
t.Cleanup(func() {
|
2020-07-20 10:56:45 +00:00
|
|
|
require.NoError(t, os.RemoveAll(walletDir), "Failed to remove directory")
|
|
|
|
require.NoError(t, os.RemoveAll(passwordsDir), "Failed to remove directory")
|
2020-07-01 21:30:01 +00:00
|
|
|
})
|
|
|
|
return walletDir, passwordsDir
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCreateAndReadWallet(t *testing.T) {
|
2020-07-22 04:49:04 +00:00
|
|
|
walletDir, passwordsDir := setupWalletAndPasswordsDir(t)
|
|
|
|
cliCtx := setupWalletCtx(t, &testWalletConfig{
|
|
|
|
walletDir: walletDir,
|
|
|
|
passwordsDir: passwordsDir,
|
|
|
|
keymanagerKind: v2keymanager.Direct,
|
2020-07-01 21:30:01 +00:00
|
|
|
})
|
2020-07-24 00:43:01 +00:00
|
|
|
wallet, err := NewWallet(cliCtx, v2keymanager.Direct)
|
|
|
|
require.NoError(t, wallet.SaveWallet())
|
2020-07-20 10:56:45 +00:00
|
|
|
require.NoError(t, err)
|
2020-07-01 21:30:01 +00:00
|
|
|
// We should be able to now read the wallet as well.
|
2020-07-22 04:49:04 +00:00
|
|
|
_, err = OpenWallet(cliCtx)
|
2020-07-20 10:56:45 +00:00
|
|
|
require.NoError(t, err)
|
2020-07-01 21:30:01 +00:00
|
|
|
}
|