prysm-pulse/validator/accounts/v2/wallet_test.go
Raul Jordan f12f75224e
Accounts V2: Create Wallet via CLI (#6543)
* begin on the proto definitions

* define remote signer service protos

* basic implementation

* remote keymanager docs

* finalize remote client doc

* amend response

* fix proto defs

* test new and begin test sign

* test sign done

* remote oneof

* rename

* fix build

* viz

* Update validator/keymanager/v2/remote/remote_test.go

Co-authored-by: Ivan Martinez <ivanthegreatdev@gmail.com>

* Update validator/accounts/v2/wallet.go

Co-authored-by: Ivan Martinez <ivanthegreatdev@gmail.com>

* fmt

* move the input check to accounts new

* begin on requesting cli input for remote keymanager config

* move the input check to accounts new

* begin on requesting cli input for remote keymanager config

* define wallet v2

* create wallet most logic done

* init remote wallet

* create wallet moving nicely

* ensure wallet create works

* reenable import export list

* further refactor

* improve handling of input wallet dir

* add all validation to cert path checks

* lint

* list test

* new wallet lint

Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com>
Co-authored-by: Ivan Martinez <ivanthegreatdev@gmail.com>
2020-07-14 23:05:21 -05:00

95 lines
2.6 KiB
Go

package v2
import (
"context"
"crypto/rand"
"fmt"
"io/ioutil"
"math/big"
"os"
"path"
"testing"
"github.com/prysmaticlabs/prysm/shared/testutil"
v2keymanager "github.com/prysmaticlabs/prysm/validator/keymanager/v2"
"github.com/prysmaticlabs/prysm/validator/keymanager/v2/direct"
mock "github.com/prysmaticlabs/prysm/validator/keymanager/v2/testing"
"github.com/sirupsen/logrus"
)
func init() {
logrus.SetLevel(logrus.DebugLevel)
logrus.SetOutput(ioutil.Discard)
}
var _ = direct.Wallet(&Wallet{})
func setupWalletDir(t testing.TB) (string, string) {
randPath, err := rand.Int(rand.Reader, big.NewInt(1000000))
if err != nil {
t.Fatalf("Could not generate random file path: %v", err)
}
walletDir := path.Join(testutil.TempDir(), fmt.Sprintf("/%d", randPath))
if err := os.RemoveAll(walletDir); err != nil {
t.Fatalf("Failed to remove directory: %v", err)
}
passwordsDir := path.Join(testutil.TempDir(), fmt.Sprintf("/%d", randPath))
if err := os.RemoveAll(passwordsDir); err != nil {
t.Fatalf("Failed to remove directory: %v", err)
}
t.Cleanup(func() {
if err := os.RemoveAll(walletDir); err != nil {
t.Fatalf("Failed to remove directory: %v", err)
}
if err := os.RemoveAll(passwordsDir); err != nil {
t.Fatalf("Failed to remove directory: %v", err)
}
})
return walletDir, passwordsDir
}
func TestCreateAndReadWallet(t *testing.T) {
ctx := context.Background()
if _, err := NewWallet(ctx, &WalletConfig{
PasswordsDir: "",
WalletDir: "",
}); err == nil {
t.Error("Expected error when passing in empty directories, received nil")
}
walletDir, passwordsDir := setupWalletDir(t)
keymanagerKind := v2keymanager.Direct
wallet, err := NewWallet(ctx, &WalletConfig{
PasswordsDir: passwordsDir,
WalletDir: walletDir,
KeymanagerKind: keymanagerKind,
})
if err != nil {
t.Fatal(err)
}
keymanager := &mock.MockKeymanager{
ConfigFileContents: []byte("hello-world"),
}
keymanagerConfig, err := keymanager.MarshalConfigFile(ctx)
if err != nil {
t.Fatalf("Could not marshal keymanager config file: %v", err)
}
if err := wallet.WriteKeymanagerConfigToDisk(ctx, keymanagerConfig); err != nil {
t.Fatalf("Could not write keymanager config file to disk: %v", err)
}
walletPath := path.Join(walletDir, keymanagerKind.String())
configFilePath := path.Join(walletPath, keymanagerConfigFileName)
if !fileExists(configFilePath) {
t.Fatalf("Expected config file to have been created at path: %s", configFilePath)
}
// We should be able to now read the wallet as well.
if _, err := NewWallet(ctx, &WalletConfig{
PasswordsDir: passwordsDir,
WalletDir: walletDir,
}); err != nil {
t.Fatal(err)
}
}