Skip to content

Commit

Permalink
feature default validator keystore path (#5592)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
farazdagi committed Apr 23, 2020
1 parent 2ebd684 commit a33bd94
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 40 deletions.
57 changes: 47 additions & 10 deletions validator/accounts/account.go
Expand Up @@ -6,6 +6,9 @@ import (
"fmt"
"io"
"os"
"os/user"
"path/filepath"
"runtime"
"strings"

"github.com/pkg/errors"
Expand Down Expand Up @@ -132,28 +135,62 @@ 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 {
return "", "", errors.Wrapf(err, "could not initialize validator account")
}
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 ""
}
31 changes: 1 addition & 30 deletions validator/keymanager/direct_keystore.go
Expand Up @@ -3,9 +3,6 @@ package keymanager
import (
"encoding/json"
"os"
"os/user"
"path/filepath"
"runtime"
"strings"

"github.com/prysmaticlabs/prysm/shared/bls"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 ""
}

0 comments on commit a33bd94

Please sign in to comment.