feature default validator keystore path (#5592)

* feature default validator path
* Merge refs/heads/master into fix-validation-path-issue
* Merge refs/heads/master into fix-validation-path-issue
* Merge refs/heads/master into fix-validation-path-issue
* Merge refs/heads/master into fix-validation-path-issue
* Merge refs/heads/master into fix-validation-path-issue
* Merge refs/heads/master into fix-validation-path-issue
This commit is contained in:
Victor Farazdagi
2020-04-23 23:02:15 +03:00
committed by GitHub
parent 2ebd684924
commit a33bd94ffb
2 changed files with 48 additions and 40 deletions

View File

@@ -6,6 +6,9 @@ import (
"fmt"
"io"
"os"
"os/user"
"path/filepath"
"runtime"
"strings"
"github.com/pkg/errors"
@@ -132,24 +135,30 @@ func Exists(keystorePath string) (bool, error) {
// CreateValidatorAccount creates a validator account from the given cli context.
func CreateValidatorAccount(path string, passphrase string) (string, string, error) {
if passphrase == "" {
reader := bufio.NewReader(os.Stdin)
log.Info("Create a new validator account for eth2")
log.Info("Enter a password:")
bytePassword, err := terminal.ReadPassword(int(os.Stdin.Fd()))
if err != nil {
log.Fatalf("Could not read account password: %v", err)
return path, passphrase, err
}
text := string(bytePassword)
passphrase = strings.Replace(text, "\n", "", -1)
log.Infof("Keystore path to save your private keys (leave blank for default %s):", path)
text, err = reader.ReadString('\n')
if err != nil {
log.Fatal(err)
}
text = strings.Replace(text, "\n", "", -1)
if text != "" {
path = text
}
}
if path == "" {
path = DefaultValidatorDir()
}
log.Infof("Keystore path to save your private keys (default: %q):", path)
reader := bufio.NewReader(os.Stdin)
text, err := reader.ReadString('\n')
if err != nil {
log.Fatal(err)
return path, passphrase, err
}
if text = strings.Replace(text, "\n", "", -1); text != "" {
path = text
}
if err := NewValidatorAccount(path, passphrase); err != nil {
@@ -157,3 +166,31 @@ func CreateValidatorAccount(path string, passphrase string) (string, string, err
}
return path, passphrase, nil
}
// DefaultValidatorDir returns OS-specific default keystore directory.
func DefaultValidatorDir() string {
// Try to place the data folder in the user's home dir
home := homeDir()
if home != "" {
if runtime.GOOS == "darwin" {
return filepath.Join(home, "Library", "Eth2Validators")
} else if runtime.GOOS == "windows" {
return filepath.Join(home, "AppData", "Roaming", "Eth2Validators")
} else {
return filepath.Join(home, ".eth2validators")
}
}
// As we cannot guess a stable location, return empty and handle later
return ""
}
// homeDir returns home directory path.
func homeDir() string {
if home := os.Getenv("HOME"); home != "" {
return home
}
if usr, err := user.Current(); err == nil {
return usr.HomeDir
}
return ""
}

View File

@@ -3,9 +3,6 @@ package keymanager
import (
"encoding/json"
"os"
"os/user"
"path/filepath"
"runtime"
"strings"
"github.com/prysmaticlabs/prysm/shared/bls"
@@ -46,7 +43,7 @@ func NewKeystore(input string) (KeyManager, string, error) {
}
if opts.Path == "" {
opts.Path = defaultValidatorDir()
opts.Path = accounts.DefaultValidatorDir()
}
exists, err := accounts.Exists(opts.Path)
@@ -93,29 +90,3 @@ func NewKeystore(input string) (KeyManager, string, error) {
}
return km, "", nil
}
func homeDir() string {
if home := os.Getenv("HOME"); home != "" {
return home
}
if usr, err := user.Current(); err == nil {
return usr.HomeDir
}
return ""
}
func defaultValidatorDir() string {
// Try to place the data folder in the user's home dir
home := homeDir()
if home != "" {
if runtime.GOOS == "darwin" {
return filepath.Join(home, "Library", "Eth2Validators")
} else if runtime.GOOS == "windows" {
return filepath.Join(home, "AppData", "Roaming", "Eth2Validators")
} else {
return filepath.Join(home, ".eth2validators")
}
}
// As we cannot guess a stable location, return empty and handle later
return ""
}