|
| 1 | +package keys |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/hex" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "os" |
| 8 | + "path/filepath" |
| 9 | + |
| 10 | + "github.com/Layr-Labs/eigenlayer-cli/pkg/utils" |
| 11 | + "github.com/Layr-Labs/eigensdk-go/crypto/bls" |
| 12 | + "github.com/Layr-Labs/eigensdk-go/crypto/ecdsa" |
| 13 | + "github.com/urfave/cli/v2" |
| 14 | +) |
| 15 | + |
| 16 | +func ExportCmd(p utils.Prompter) *cli.Command { |
| 17 | + exportCmd := &cli.Command{ |
| 18 | + Name: "export", |
| 19 | + Usage: "Used to export existing keys from local keystore", |
| 20 | + UsageText: "export --key-type <key-type> [flags] [keyname]", |
| 21 | + Description: `Used to export ecdsa and bls key from local keystore |
| 22 | +
|
| 23 | +keyname - This will be the name of the key to be imported. If the path of keys is |
| 24 | +different from default path created by "create"/"import" command, then provide the |
| 25 | +full path using --key-path flag. |
| 26 | +
|
| 27 | +If both keyname is provided and --key-path flag is provided, then keyname will be used. |
| 28 | +
|
| 29 | +use --key-type ecdsa/bls to export ecdsa/bls key. |
| 30 | +- ecdsa - exported key should be plaintext hex encoded private key |
| 31 | +- bls - exported key should be plaintext bls private key |
| 32 | +
|
| 33 | +It will prompt for password to encrypt the key. |
| 34 | +
|
| 35 | +This command will import keys from $HOME/.eigenlayer/operator_keys/ location |
| 36 | +
|
| 37 | +But if you want it to export from a different location, use --key-path flag`, |
| 38 | + |
| 39 | + Flags: []cli.Flag{ |
| 40 | + &KeyTypeFlag, |
| 41 | + &KeyPathFlag, |
| 42 | + }, |
| 43 | + Action: func(c *cli.Context) error { |
| 44 | + keyType := c.String(KeyTypeFlag.Name) |
| 45 | + |
| 46 | + keyName := c.Args().Get(0) |
| 47 | + if err := validateKeyName(keyName); err != nil { |
| 48 | + return err |
| 49 | + } |
| 50 | + |
| 51 | + keyPath := c.String(KeyPathFlag.Name) |
| 52 | + if len(keyPath) == 0 && len(keyName) == 0 { |
| 53 | + return errors.New("one of keyname or --key-path is required") |
| 54 | + } |
| 55 | + |
| 56 | + if len(keyPath) > 0 && len(keyName) > 0 { |
| 57 | + return errors.New("keyname and --key-path both are provided. Please provide only one") |
| 58 | + } |
| 59 | + |
| 60 | + filePath, err := getKeyPath(keyPath, keyName, keyType) |
| 61 | + if err != nil { |
| 62 | + return err |
| 63 | + } |
| 64 | + |
| 65 | + confirm, err := p.Confirm("This will show your private key. Are you sure you want to export?") |
| 66 | + if err != nil { |
| 67 | + return err |
| 68 | + } |
| 69 | + if !confirm { |
| 70 | + return nil |
| 71 | + } |
| 72 | + |
| 73 | + password, err := p.InputHiddenString("Enter password to decrypt the key", "", func(s string) error { |
| 74 | + return nil |
| 75 | + }) |
| 76 | + if err != nil { |
| 77 | + return err |
| 78 | + } |
| 79 | + fmt.Println("exporting key from: ", filePath) |
| 80 | + |
| 81 | + privateKey, err := getPrivateKey(keyType, filePath, password) |
| 82 | + if err != nil { |
| 83 | + return err |
| 84 | + } |
| 85 | + fmt.Println("Private key: ", privateKey) |
| 86 | + return nil |
| 87 | + }, |
| 88 | + } |
| 89 | + |
| 90 | + return exportCmd |
| 91 | +} |
| 92 | + |
| 93 | +func getPrivateKey(keyType string, filePath string, password string) (string, error) { |
| 94 | + switch keyType { |
| 95 | + case KeyTypeECDSA: |
| 96 | + key, err := ecdsa.ReadKey(filePath, password) |
| 97 | + if err != nil { |
| 98 | + return "", err |
| 99 | + } |
| 100 | + return hex.EncodeToString(key.D.Bytes()), nil |
| 101 | + case KeyTypeBLS: |
| 102 | + key, err := bls.ReadPrivateKeyFromFile(filePath, password) |
| 103 | + if err != nil { |
| 104 | + return "", err |
| 105 | + } |
| 106 | + return key.PrivKey.String(), nil |
| 107 | + default: |
| 108 | + return "", ErrInvalidKeyType |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +func getKeyPath(keyPath string, keyName string, keyType string) (string, error) { |
| 113 | + homePath, err := os.UserHomeDir() |
| 114 | + if err != nil { |
| 115 | + return "", err |
| 116 | + } |
| 117 | + |
| 118 | + var filePath string |
| 119 | + if len(keyName) > 0 { |
| 120 | + switch keyType { |
| 121 | + case KeyTypeECDSA: |
| 122 | + filePath = filepath.Join(homePath, OperatorKeystoreSubFolder, keyName+".ecdsa.key.json") |
| 123 | + case KeyTypeBLS: |
| 124 | + filePath = filepath.Join(homePath, OperatorKeystoreSubFolder, keyName+".bls.key.json") |
| 125 | + default: |
| 126 | + return "", ErrInvalidKeyType |
| 127 | + } |
| 128 | + |
| 129 | + } else { |
| 130 | + filePath = filepath.Clean(keyPath) |
| 131 | + } |
| 132 | + |
| 133 | + return filePath, nil |
| 134 | +} |
0 commit comments