forked from prysmaticlabs/prysm
-
Notifications
You must be signed in to change notification settings - Fork 2
/
keyutils.go
89 lines (81 loc) · 3 KB
/
keyutils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package node
import (
"bufio"
"os"
"strings"
"syscall"
"github.com/pkg/errors"
"github.com/prysmaticlabs/prysm/shared/keystore"
"github.com/prysmaticlabs/prysm/validator/accounts"
"github.com/prysmaticlabs/prysm/validator/flags"
"github.com/sirupsen/logrus"
"github.com/urfave/cli"
"golang.org/x/crypto/ssh/terminal"
)
func keysParser(ctx *cli.Context) (map[string]*keystore.Key, error) {
// Unsafe start from plain text keys.
if unencryptedKeys := ctx.String(flags.UnencryptedKeysFlag.Name); unencryptedKeys != "" {
return accounts.LoadUnencryptedKeys(unencryptedKeys)
}
// Interop start from generated keys.
if numValidatorKeys := ctx.GlobalUint64(flags.InteropNumValidators.Name); numValidatorKeys > 0 {
return accounts.InteropValidatorKeys(ctx.GlobalUint64(flags.InteropStartIndex.Name), numValidatorKeys)
}
// Normal production key start.
keystoreDirectory := ctx.String(flags.KeystorePathFlag.Name)
keystorePassword := ctx.String(flags.PasswordFlag.Name)
exists, err := accounts.Exists(keystoreDirectory)
if err != nil {
log.Fatal(err)
}
if !exists {
// If an account does not exist, we create a new one and start the node.
keystoreDirectory, keystorePassword, err = CreateValidatorAccount(ctx)
if err != nil {
log.Fatalf("Could not create validator account: %v", err)
}
} else {
if keystorePassword == "" {
log.Info("Enter your validator account password:")
bytePassword, err := terminal.ReadPassword(int(syscall.Stdin))
if err != nil {
log.Fatalf("Could not read account password: %v", err)
}
text := string(bytePassword)
keystorePassword = strings.Replace(text, "\n", "", -1)
}
if err := accounts.VerifyAccountNotExists(keystoreDirectory, keystorePassword); err == nil {
log.Info("No account found, creating new validator account...")
}
}
return accounts.DecryptKeysFromKeystore(keystoreDirectory, keystorePassword)
}
// CreateValidatorAccount creates a validator account from the given cli context.
func CreateValidatorAccount(ctx *cli.Context) (string, string, error) {
keystoreDirectory := ctx.String(flags.KeystorePathFlag.Name)
keystorePassword := ctx.String(flags.PasswordFlag.Name)
if keystorePassword == "" {
reader := bufio.NewReader(os.Stdin)
logrus.Info("Create a new validator account for eth2")
log.Info("Enter a password:")
bytePassword, err := terminal.ReadPassword(int(syscall.Stdin))
if err != nil {
log.Fatalf("Could not read account password: %v", err)
}
text := string(bytePassword)
keystorePassword = strings.Replace(text, "\n", "", -1)
log.Infof("Keystore path to save your private keys (leave blank for default %s):", keystoreDirectory)
text, err = reader.ReadString('\n')
if err != nil {
log.Fatal(err)
}
text = strings.Replace(text, "\n", "", -1)
if text != "" {
keystoreDirectory = text
}
}
if err := accounts.NewValidatorAccount(keystoreDirectory, keystorePassword); err != nil {
return "", "", errors.Wrapf(err, "could not initialize validator account")
}
return keystoreDirectory, keystorePassword, nil
}