-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cli): add key conversion subcommand (#174)
* feat(cli): add key conversion subcommand * adds uncompressed pubkey (hex) as input
- Loading branch information
Showing
5 changed files
with
307 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package cmd | ||
|
||
import ( | ||
"context" | ||
"encoding/base64" | ||
"encoding/hex" | ||
"strings" | ||
|
||
"github.com/pkg/errors" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
type keyConfig struct { | ||
ValidatorKeyFile string | ||
PrivateKeyFile string | ||
PubKeyHex string | ||
PubKeyBase64 string | ||
PubKeyHexUncompressed string | ||
} | ||
|
||
func newKeyCmds() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "key", | ||
Short: "Commands for key management", | ||
Args: cobra.NoArgs, | ||
} | ||
|
||
cmd.AddCommand( | ||
newKeyConvertCmd(), | ||
) | ||
|
||
return cmd | ||
} | ||
|
||
func newKeyConvertCmd() *cobra.Command { | ||
var cfg keyConfig | ||
|
||
cmd := &cobra.Command{ | ||
Use: "convert", | ||
Short: "Convert between various key formats", | ||
Args: cobra.NoArgs, | ||
RunE: runValidatorCommand( | ||
func() error { return validateKeyConvertFlags(cfg) }, | ||
func(ctx context.Context) error { return convertKey(ctx, cfg) }, | ||
), | ||
} | ||
|
||
bindKeyConvertFlags(cmd, &cfg) | ||
|
||
return cmd | ||
} | ||
|
||
func convertKey(_ context.Context, cfg keyConfig) error { | ||
var compressedPubKeyBytes []byte | ||
var err error | ||
|
||
switch { | ||
case cfg.ValidatorKeyFile != "": | ||
compressedPubKeyBytes, err = validatorKeyFileToCmpPubKey(cfg.ValidatorKeyFile) | ||
if err != nil { | ||
return errors.Wrap(err, "failed to load validator private key") | ||
} | ||
case cfg.PrivateKeyFile != "": | ||
compressedPubKeyBytes, err = privKeyFileToCmpPubKey(cfg.PrivateKeyFile) | ||
if err != nil { | ||
return errors.Wrap(err, "failed to load private key file") | ||
} | ||
case cfg.PubKeyHex != "": | ||
pubKeyHex := strings.TrimPrefix(cfg.PubKeyHex, "0x") | ||
compressedPubKeyBytes, err = hex.DecodeString(pubKeyHex) | ||
if err != nil { | ||
return errors.Wrap(err, "failed to decode hex public key") | ||
} | ||
case cfg.PubKeyBase64 != "": | ||
compressedPubKeyBytes, err = base64.StdEncoding.DecodeString(cfg.PubKeyBase64) | ||
if err != nil { | ||
return errors.Wrap(err, "failed to decode base64 public key") | ||
} | ||
case cfg.PubKeyHexUncompressed != "": | ||
pubKeyHex := strings.TrimPrefix(cfg.PubKeyHexUncompressed, "0x") | ||
uncompressedPubKeyBytes, err := hex.DecodeString(pubKeyHex) | ||
if err != nil { | ||
return errors.Wrap(err, "failed to decode hex public key") | ||
} | ||
compressedPubKeyBytes, err = uncmpPubKeyToCmpPubKey(uncompressedPubKeyBytes) | ||
if err != nil { | ||
return errors.Wrap(err, "failed to convert uncompressed pub key") | ||
} | ||
default: | ||
return errors.New("no valid key input provided") | ||
} | ||
|
||
return printKeyFormats(compressedPubKeyBytes) | ||
} |
Oops, something went wrong.