Skip to content

Commit

Permalink
fix empty password via pipe (#77)
Browse files Browse the repository at this point in the history
  • Loading branch information
shrimalmadhur committed Mar 12, 2024
1 parent 96308f8 commit 288455a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
26 changes: 17 additions & 9 deletions pkg/operator/keys/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ This command will create keys in $HOME/.eigenlayer/operator_keys/ location
}

// Check if input is available in the pipe and read the password from it
stdInPassword := getStdInPassword()
stdInPassword, readFromPipe := getStdInPassword()

keyType := ctx.String(KeyTypeFlag.Name)
insecure := ctx.Bool(InsecureFlag.Name)
Expand All @@ -78,13 +78,13 @@ This command will create keys in $HOME/.eigenlayer/operator_keys/ location
if err != nil {
return err
}
return saveEcdsaKey(keyName, p, privateKey, insecure, stdInPassword)
return saveEcdsaKey(keyName, p, privateKey, insecure, stdInPassword, readFromPipe)
case KeyTypeBLS:
blsKeyPair, err := bls.GenRandomBlsKeys()
if err != nil {
return err
}
return saveBlsKey(keyName, p, blsKeyPair, insecure, stdInPassword)
return saveBlsKey(keyName, p, blsKeyPair, insecure, stdInPassword, readFromPipe)
default:
return ErrInvalidKeyType
}
Expand All @@ -105,7 +105,14 @@ func validateKeyName(keyName string) error {
return nil
}

func saveBlsKey(keyName string, p utils.Prompter, keyPair *bls.KeyPair, insecure bool, stdInPassword string) error {
func saveBlsKey(
keyName string,
p utils.Prompter,
keyPair *bls.KeyPair,
insecure bool,
stdInPassword string,
readFromPipe bool,
) error {
homePath, err := os.UserHomeDir()
if err != nil {
return err
Expand All @@ -117,7 +124,7 @@ func saveBlsKey(keyName string, p utils.Prompter, keyPair *bls.KeyPair, insecure
}

var password string
if len(stdInPassword) == 0 {
if !readFromPipe {
password, err = getPasswordFromPrompt(p, insecure, "Enter password to encrypt the bls private key:")
if err != nil {
return err
Expand Down Expand Up @@ -150,6 +157,7 @@ func saveEcdsaKey(
privateKey *ecdsa.PrivateKey,
insecure bool,
stdInPassword string,
readFromPipe bool,
) error {
homePath, err := os.UserHomeDir()
if err != nil {
Expand All @@ -162,7 +170,7 @@ func saveEcdsaKey(
}

var password string
if len(stdInPassword) == 0 {
if !readFromPipe {
password, err = getPasswordFromPrompt(p, insecure, "Enter password to encrypt the ecdsa private key:")
if err != nil {
return err
Expand Down Expand Up @@ -264,16 +272,16 @@ BLS Private Key (Hex):
return nil
}

func getStdInPassword() string {
func getStdInPassword() (string, bool) {
stat, _ := os.Stdin.Stat()
if (stat.Mode() & os.ModeCharDevice) == 0 {
// Input is available in the pipe, read from it
scanner := bufio.NewScanner(os.Stdin)
if scanner.Scan() {
return scanner.Text()
return scanner.Text(), true
}
}
return ""
return "", false
}

func getPasswordFromPrompt(p utils.Prompter, insecure bool, prompt string) (string, error) {
Expand Down
6 changes: 3 additions & 3 deletions pkg/operator/keys/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ This command will import keys in $HOME/.eigenlayer/operator_keys/ location
}

// Check if input is available in the pipe and read the password from it
stdInPassword := getStdInPassword()
stdInPassword, readFromPipe := getStdInPassword()

keyType := ctx.String(KeyTypeFlag.Name)
insecure := ctx.Bool(InsecureFlag.Name)
Expand All @@ -68,7 +68,7 @@ This command will import keys in $HOME/.eigenlayer/operator_keys/ location
if err != nil {
return err
}
return saveEcdsaKey(keyName, p, privateKeyPair, insecure, stdInPassword)
return saveEcdsaKey(keyName, p, privateKeyPair, insecure, stdInPassword, readFromPipe)
case KeyTypeBLS:
privateKeyBigInt := new(big.Int)
_, ok := privateKeyBigInt.SetString(privateKey, 10)
Expand All @@ -94,7 +94,7 @@ This command will import keys in $HOME/.eigenlayer/operator_keys/ location
return err
}
}
return saveBlsKey(keyName, p, blsKeyPair, insecure, stdInPassword)
return saveBlsKey(keyName, p, blsKeyPair, insecure, stdInPassword, readFromPipe)
default:
return ErrInvalidKeyType
}
Expand Down

0 comments on commit 288455a

Please sign in to comment.