2020-01-05 04:32:09 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
|
2020-05-06 23:50:19 +00:00
|
|
|
"github.com/pkg/errors"
|
2021-09-17 21:55:24 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/io/file"
|
2020-01-05 04:32:09 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2020-10-22 05:53:03 +00:00
|
|
|
"github.com/urfave/cli/v2"
|
2020-01-05 04:32:09 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var log = logrus.WithField("prefix", "node")
|
|
|
|
|
|
|
|
// ConfirmAction uses the passed in actionText as the confirmation text displayed in the terminal.
|
|
|
|
// The user must enter Y or N to indicate whether they confirm the action detailed in the warning text.
|
|
|
|
// Returns a boolean representing the user's answer.
|
2020-10-12 15:43:19 +00:00
|
|
|
func ConfirmAction(actionText, deniedText string) (bool, error) {
|
2020-01-05 04:32:09 +00:00
|
|
|
var confirmed bool
|
|
|
|
reader := bufio.NewReader(os.Stdin)
|
|
|
|
log.Warn(actionText)
|
|
|
|
|
|
|
|
for {
|
|
|
|
fmt.Print(">> ")
|
|
|
|
|
|
|
|
line, _, err := reader.ReadLine()
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
trimmedLine := strings.TrimSpace(string(line))
|
|
|
|
lineInput := strings.ToUpper(trimmedLine)
|
|
|
|
if lineInput != "Y" && lineInput != "N" {
|
|
|
|
log.Errorf("Invalid option of %s chosen, please only enter Y/N", line)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if lineInput == "Y" {
|
|
|
|
confirmed = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
log.Warn(deniedText)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
return confirmed, nil
|
|
|
|
}
|
2020-05-05 20:13:18 +00:00
|
|
|
|
|
|
|
// EnterPassword queries the user for their password through the terminal, in order to make sure it is
|
|
|
|
// not passed in a visible way to the terminal.
|
2020-05-12 15:01:02 +00:00
|
|
|
func EnterPassword(confirmPassword bool, pr PasswordReader) (string, error) {
|
2020-05-05 20:13:18 +00:00
|
|
|
var passphrase string
|
|
|
|
log.Info("Enter a password:")
|
2020-05-12 15:01:02 +00:00
|
|
|
bytePassword, err := pr.ReadPassword()
|
2020-05-05 20:13:18 +00:00
|
|
|
if err != nil {
|
2020-05-06 23:50:19 +00:00
|
|
|
return "", errors.Wrap(err, "could not read account password")
|
2020-05-05 20:13:18 +00:00
|
|
|
}
|
2020-05-29 12:42:39 +00:00
|
|
|
text := bytePassword
|
2020-08-10 16:16:45 +00:00
|
|
|
passphrase = strings.ReplaceAll(text, "\n", "")
|
2020-05-06 23:50:19 +00:00
|
|
|
if confirmPassword {
|
|
|
|
log.Info("Please re-enter your password:")
|
2020-05-12 15:01:02 +00:00
|
|
|
bytePassword, err := pr.ReadPassword()
|
2020-05-06 23:50:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", errors.Wrap(err, "could not read account password")
|
|
|
|
}
|
2020-05-29 12:42:39 +00:00
|
|
|
text := bytePassword
|
2020-08-10 16:16:45 +00:00
|
|
|
confirmedPass := strings.ReplaceAll(text, "\n", "")
|
2020-05-06 23:50:19 +00:00
|
|
|
if passphrase != confirmedPass {
|
|
|
|
log.Info("Passwords did not match, please try again")
|
2020-05-12 15:01:02 +00:00
|
|
|
return EnterPassword(true, pr)
|
2020-05-06 23:50:19 +00:00
|
|
|
}
|
|
|
|
}
|
2020-05-05 20:13:18 +00:00
|
|
|
return passphrase, nil
|
|
|
|
}
|
2020-10-22 05:53:03 +00:00
|
|
|
|
2020-12-11 11:15:04 +00:00
|
|
|
// ExpandSingleEndpointIfFile expands the path for --http-web3provider if specified as a file.
|
|
|
|
func ExpandSingleEndpointIfFile(ctx *cli.Context, flag *cli.StringFlag) error {
|
2020-10-23 03:57:19 +00:00
|
|
|
// Return early if no flag value is set.
|
|
|
|
if !ctx.IsSet(flag.Name) {
|
|
|
|
return nil
|
|
|
|
}
|
2020-10-22 05:53:03 +00:00
|
|
|
web3endpoint := ctx.String(flag.Name)
|
2020-10-23 03:57:19 +00:00
|
|
|
switch {
|
|
|
|
case strings.HasPrefix(web3endpoint, "http://"):
|
|
|
|
case strings.HasPrefix(web3endpoint, "https://"):
|
|
|
|
case strings.HasPrefix(web3endpoint, "ws://"):
|
|
|
|
case strings.HasPrefix(web3endpoint, "wss://"):
|
|
|
|
default:
|
2021-09-17 21:55:24 +00:00
|
|
|
web3endpoint, err := file.ExpandPath(ctx.String(flag.Name))
|
2020-10-22 05:53:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "could not expand path for %s", web3endpoint)
|
|
|
|
}
|
|
|
|
if err := ctx.Set(flag.Name, web3endpoint); err != nil {
|
|
|
|
return errors.Wrapf(err, "could not set %s to %s", flag.Name, web3endpoint)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2020-12-11 11:15:04 +00:00
|
|
|
|
|
|
|
// ExpandWeb3EndpointsIfFile expands the path for --fallback-web3provider if specified as a file.
|
|
|
|
func ExpandWeb3EndpointsIfFile(ctx *cli.Context, flags *cli.StringSliceFlag) error {
|
|
|
|
// Return early if no flag value is set.
|
|
|
|
if !ctx.IsSet(flags.Name) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
rawFlags := ctx.StringSlice(flags.Name)
|
|
|
|
for i, rawValue := range rawFlags {
|
|
|
|
switch {
|
|
|
|
case strings.HasPrefix(rawValue, "http://"):
|
|
|
|
case strings.HasPrefix(rawValue, "https://"):
|
|
|
|
case strings.HasPrefix(rawValue, "ws://"):
|
|
|
|
case strings.HasPrefix(rawValue, "wss://"):
|
|
|
|
default:
|
2021-09-17 21:55:24 +00:00
|
|
|
web3endpoint, err := file.ExpandPath(rawValue)
|
2020-12-11 11:15:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "could not expand path for %s", rawValue)
|
|
|
|
}
|
|
|
|
// Given that rawFlags is a pointer this will replace the unexpanded path
|
|
|
|
// with the expanded one. Also there is no easy way to replace the string
|
|
|
|
// slice flag value compared to other flag types. This is why we resort to
|
|
|
|
// replacing it like this.
|
|
|
|
rawFlags[i] = web3endpoint
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|