Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Displaying sensitive information during key creation with using less so is not saved in terminal history #50

Merged
merged 11 commits into from
Feb 20, 2024
113 changes: 94 additions & 19 deletions pkg/operator/keys/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ import (
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"

"github.com/Layr-Labs/eigenlayer-cli/pkg/utils"
"github.com/Layr-Labs/eigensdk-go/crypto/bls"
Expand Down Expand Up @@ -130,17 +132,15 @@ func saveBlsKey(keyName string, p utils.Prompter, keyPair *bls.KeyPair, insecure
return err
}

err = keyPair.SaveToFile(fileLoc, password)
if err != nil {
return err
}
// TODO: display it using `less` of `vi` so that it is not saved in terminal history
fmt.Println("BLS Private Key: " + keyPair.PrivKey.String())
fmt.Println("Please backup the above private key in safe place.")
fmt.Println()
fmt.Println("BLS Pub key: " + keyPair.PubKey.String())
fmt.Println("Key location: " + fileLoc)
return nil
err = keyPair.SaveToFile(fileLoc, password)
if err != nil {
return err
}

privateKeyHex := keyPair.PrivKey.String()
publicKeyHex := keyPair.PubKey.String()

return displayWithLess(fileLoc, privateKeyHex, fileLoc, publicKeyHex, "", KeyTypeBLS)
}

func saveEcdsaKey(keyName string, p utils.Prompter, privateKey *ecdsa.PrivateKey, insecure bool) error {
Expand Down Expand Up @@ -185,20 +185,95 @@ func saveEcdsaKey(keyName string, p utils.Prompter, privateKey *ecdsa.PrivateKey
}

privateKeyHex := hex.EncodeToString(privateKey.D.Bytes())
// TODO: display it using `less` of `vi` so that it is not saved in terminal history
fmt.Println("ECDSA Private Key (Hex): ", privateKeyHex)
fmt.Println("\033[1;32m馃攼 Please backup the above private key hex in a safe place 馃敀\033[0m")
fmt.Println("Key location: " + fileLoc)
publicKey := privateKey.Public()
publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey)
if !ok {
return err
return errors.New("error casting public key to ECDSA public key")
}
publicKeyBytes := crypto.FromECDSAPub(publicKeyECDSA)
fmt.Println("Public Key hex: ", hexutil.Encode(publicKeyBytes)[4:])
publicKeyHex := hexutil.Encode(publicKeyBytes)[4:]
address := crypto.PubkeyToAddress(*publicKeyECDSA).Hex()
fmt.Println("Ethereum Address", address)
return nil

return displayWithLess(fileLoc, privateKeyHex, fileLoc, publicKeyHex, address, KeyTypeECDSA)
}

func displayWithLess(filePath, privateKeyHex, fileLoc, publicKeyHex, address, keyType string) error {
var message, border, keyLine string
tabSpace := " "

keyContent := tabSpace + privateKeyHex + tabSpace
borderLength := len(keyContent) + 4
border = strings.Repeat("/", borderLength)
paddingLine := "//" + strings.Repeat(" ", borderLength-4) + "//"

keyLine = fmt.Sprintf("//%s//", keyContent)

if keyType == KeyTypeECDSA {
message = fmt.Sprintf(`
ECDSA Private Key (Hex):
shrimalmadhur marked this conversation as resolved.
Show resolved Hide resolved

%s
%s
%s
%s
%s

馃攼 Please backup the above private key hex in a safe place 馃敀

Key location: %s

Public Key hex: %s

Ethereum Address: %s

`, border, paddingLine, keyLine, paddingLine, border, fileLoc, publicKeyHex, address)
} else if keyType == KeyTypeBLS {
message = fmt.Sprintf(`
BLS Private Key Information:

%s
%s
%s
%s
%s

馃攼 Please backup the above private key in a safe place 馃敀

Public Key: %s

Key location: %s

`, border, paddingLine, keyLine, paddingLine, border, publicKeyHex, fileLoc)
} else {
return fmt.Errorf("unsupported key type: %s", keyType)
}

cmd := exec.Command("less", "-R")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr

stdin, err := cmd.StdinPipe()
if err != nil {
return fmt.Errorf("error creating stdin pipe: %w", err)
}

if err := cmd.Start(); err != nil {
return fmt.Errorf("error starting less command: %w", err)
}

if _, err := stdin.Write([]byte(message)); err != nil {
return fmt.Errorf("error writing message to less command: %w", err)
}

if err := stdin.Close(); err != nil {
return fmt.Errorf("error closing stdin pipe: %w", err)
}

if err := cmd.Wait(); err != nil {
return fmt.Errorf("error waiting for less command: %w", err)
}

return nil
}

func checkIfKeyExists(fileLoc string) bool {
Expand Down
Loading