2020-01-04 03:51:53 +00:00
|
|
|
package keymanager
|
|
|
|
|
|
|
|
import (
|
2020-01-24 17:21:31 +00:00
|
|
|
"encoding/json"
|
|
|
|
"os"
|
|
|
|
"os/user"
|
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
2020-01-04 03:51:53 +00:00
|
|
|
"strings"
|
|
|
|
"syscall"
|
|
|
|
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/bls"
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/bytesutil"
|
|
|
|
"github.com/prysmaticlabs/prysm/validator/accounts"
|
|
|
|
"golang.org/x/crypto/ssh/terminal"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Keystore is a key manager that loads keys from a standard keystore.
|
|
|
|
type Keystore struct {
|
|
|
|
*Direct
|
|
|
|
}
|
|
|
|
|
2020-01-24 17:21:31 +00:00
|
|
|
type keystoreOpts struct {
|
|
|
|
Path string `json:"path"`
|
|
|
|
Passphrase string `json:"passphrase"`
|
|
|
|
}
|
|
|
|
|
|
|
|
var keystoreOptsHelp = `The keystore key manager generates keys and stores them in a local encrypted store. The options are:
|
|
|
|
- path This is the filesystem path to where keys will be stored. Defaults to the user's home directory if not supplied
|
|
|
|
- passphrase This is the passphrase used to encrypt keys. Will be asked for if not supplied
|
|
|
|
A sample set of options are:
|
|
|
|
{
|
|
|
|
"path": "/home/me/keys", // Store the keys in '/home/me/keys'
|
|
|
|
"passphrase": "secret" // Use the passphrase 'secret' to encrypt and decrypt keys
|
|
|
|
}`
|
|
|
|
|
2020-01-04 03:51:53 +00:00
|
|
|
// NewKeystore creates a key manager populated with the keys from the keystore at the given path.
|
2020-01-24 17:21:31 +00:00
|
|
|
func NewKeystore(input string) (KeyManager, string, error) {
|
|
|
|
opts := &keystoreOpts{}
|
|
|
|
err := json.Unmarshal([]byte(input), opts)
|
|
|
|
if err != nil {
|
|
|
|
return nil, keystoreOptsHelp, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if opts.Path == "" {
|
|
|
|
opts.Path = defaultValidatorDir()
|
|
|
|
}
|
|
|
|
|
|
|
|
exists, err := accounts.Exists(opts.Path)
|
2020-01-04 03:51:53 +00:00
|
|
|
if err != nil {
|
2020-01-24 17:21:31 +00:00
|
|
|
return nil, keystoreOptsHelp, err
|
2020-01-04 03:51:53 +00:00
|
|
|
}
|
|
|
|
if !exists {
|
|
|
|
// If an account does not exist, we create a new one and start the node.
|
2020-01-24 17:21:31 +00:00
|
|
|
opts.Path, opts.Passphrase, err = accounts.CreateValidatorAccount(opts.Path, opts.Passphrase)
|
2020-01-04 03:51:53 +00:00
|
|
|
if err != nil {
|
2020-01-24 17:21:31 +00:00
|
|
|
return nil, keystoreOptsHelp, err
|
2020-01-04 03:51:53 +00:00
|
|
|
}
|
|
|
|
} else {
|
2020-01-24 17:21:31 +00:00
|
|
|
if opts.Passphrase == "" {
|
2020-01-04 03:51:53 +00:00
|
|
|
log.Info("Enter your validator account password:")
|
2020-01-08 02:36:55 +00:00
|
|
|
bytePassword, err := terminal.ReadPassword(syscall.Stdin)
|
2020-01-04 03:51:53 +00:00
|
|
|
if err != nil {
|
2020-01-24 17:21:31 +00:00
|
|
|
return nil, keystoreOptsHelp, err
|
2020-01-04 03:51:53 +00:00
|
|
|
}
|
|
|
|
text := string(bytePassword)
|
2020-01-24 17:21:31 +00:00
|
|
|
opts.Passphrase = strings.Replace(text, "\n", "", -1)
|
2020-01-04 03:51:53 +00:00
|
|
|
}
|
|
|
|
|
2020-01-24 17:21:31 +00:00
|
|
|
if err := accounts.VerifyAccountNotExists(opts.Path, opts.Passphrase); err == nil {
|
2020-01-04 03:51:53 +00:00
|
|
|
log.Info("No account found, creating new validator account...")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-24 17:21:31 +00:00
|
|
|
keyMap, err := accounts.DecryptKeysFromKeystore(opts.Path, opts.Passphrase)
|
2020-01-04 03:51:53 +00:00
|
|
|
if err != nil {
|
2020-01-24 17:21:31 +00:00
|
|
|
return nil, keystoreOptsHelp, err
|
2020-01-04 03:51:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
km := &Unencrypted{
|
|
|
|
Direct: &Direct{
|
|
|
|
publicKeys: make(map[[48]byte]*bls.PublicKey),
|
|
|
|
secretKeys: make(map[[48]byte]*bls.SecretKey),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, key := range keyMap {
|
|
|
|
pubKey := bytesutil.ToBytes48(key.PublicKey.Marshal())
|
|
|
|
km.publicKeys[pubKey] = key.PublicKey
|
|
|
|
km.secretKeys[pubKey] = key.SecretKey
|
|
|
|
}
|
2020-01-24 17:21:31 +00:00
|
|
|
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 ""
|
2020-01-04 03:51:53 +00:00
|
|
|
}
|