Skip to content

Commit

Permalink
chore(cli): move key related cli to key namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
0xHansLee committed Feb 28, 2025
1 parent f2ffbd6 commit e89f93e
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 97 deletions.
99 changes: 99 additions & 0 deletions client/cmd/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,14 @@ import (
"encoding/hex"
"strings"

k1 "github.com/cometbft/cometbft/crypto/secp256k1"
cmtjson "github.com/cometbft/cometbft/libs/json"
"github.com/cometbft/cometbft/libs/tempfile"
"github.com/cometbft/cometbft/privval"
"github.com/pkg/errors"
"github.com/spf13/cobra"

"github.com/piplabs/story/client/app"
)

type keyConfig struct {
Expand All @@ -27,6 +33,8 @@ func newKeyCmds() *cobra.Command {

cmd.AddCommand(
newKeyConvertCmd(),
newKeyGenPrivKeyJSONCmd(),
newKeyEncryptPrivKeyCmd(),
)

return cmd
Expand All @@ -50,6 +58,52 @@ func newKeyConvertCmd() *cobra.Command {
return cmd
}

func newKeyGenPrivKeyJSONCmd() *cobra.Command {
var cfg genPrivKeyJSONConfig

cmd := &cobra.Command{
Use: "gen-priv-key-json",
Short: "Generate a priv_validator_key.json file from EVM private key",
Args: cobra.NoArgs,
PreRunE: func(_ *cobra.Command, _ []string) error {
return initializeBaseConfig(&cfg.baseConfig)
},
RunE: runValidatorCommand(
func(_ *cobra.Command) error {
return validateGenPrivKeyJSONFlags(&cfg)
},
func(ctx context.Context) error { return genValidatorPrivKeyJSON(ctx, cfg) },
),
}

bindValidatorGenPrivKeyJSONFlags(cmd, &cfg)

return cmd
}

func newKeyEncryptPrivKeyCmd() *cobra.Command {
var cfg baseConfig

cmd := &cobra.Command{
Use: "encrypt",
Short: "Encrypt the private key stored in .env",
Args: cobra.NoArgs,
PreRunE: func(_ *cobra.Command, _ []string) error {
return nil
},
RunE: runValidatorCommand(
func(_ *cobra.Command) error {
return validateEncryptPrivKeyFlags(&cfg)
},
func(_ context.Context) error { return encryptPrivKey(cfg) },
),
}

bindValidatorBaseFlags(cmd, &cfg)

return cmd
}

func convertKey(_ context.Context, cfg keyConfig) error {
var compressedPubKeyBytes []byte
var err error
Expand Down Expand Up @@ -92,3 +146,48 @@ func convertKey(_ context.Context, cfg keyConfig) error {

return printKeyFormats(compressedPubKeyBytes)
}

func genValidatorPrivKeyJSON(_ context.Context, cfg genPrivKeyJSONConfig) error {
privKeyBytes, err := hex.DecodeString(cfg.PrivateKey)
if err != nil {
return errors.Wrap(err, "failed to decode private key")
}

privKey := k1.PrivKey(privKeyBytes)
newPV := &privval.FilePVKey{
Address: privKey.PubKey().Address(),
PubKey: privKey.PubKey(),
PrivKey: privKey,
}

jsonBytes, err := cmtjson.MarshalIndent(newPV, "", " ")
if err != nil {
return errors.Wrap(err, "failed to marshal pv data")
}

if err := tempfile.WriteFileAtomic(cfg.ValidatorKeyFile, jsonBytes, 0600); err != nil {
return errors.Wrap(err, "failed to write file")
}

return nil
}

func encryptPrivKey(cfg baseConfig) error {
privKeyBytes, err := hex.DecodeString(cfg.PrivateKey)
if err != nil {
return errors.Wrap(err, "failed to decode private key")
}

pk := k1.PrivKey(privKeyBytes)
pv := privval.FilePVKey{
PrivKey: pk,
PubKey: pk.PubKey(),
Address: pk.PubKey().Address(),
}

if err := app.EncryptAndStoreKey(pv, cfg.EncPrivKeyFile()); err != nil {
return errors.Wrap(err, "failed to encrypt and store the key")
}

return nil
}
97 changes: 0 additions & 97 deletions client/cmd/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,7 @@ import (
"path/filepath"
"strings"

k1 "github.com/cometbft/cometbft/crypto/secp256k1"
cmtjson "github.com/cometbft/cometbft/libs/json"
cmtos "github.com/cometbft/cometbft/libs/os"
"github.com/cometbft/cometbft/libs/tempfile"
"github.com/cometbft/cometbft/privval"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
Expand Down Expand Up @@ -177,8 +173,6 @@ func newValidatorCmds() *cobra.Command {
cmd.AddCommand(
newValidatorCreateCmd(),
newValidatorKeyExportCmd(),
newValidatorGenPrivKeyJSONCmd(),
newValidatorEncryptPrivKeyCmd(),
newValidatorStakeCmd(),
newValidatorStakeOnBehalfCmd(),
newValidatorUnstakeCmd(),
Expand Down Expand Up @@ -469,52 +463,6 @@ func newValidatorKeyExportCmd() *cobra.Command {
return cmd
}

func newValidatorGenPrivKeyJSONCmd() *cobra.Command {
var cfg genPrivKeyJSONConfig

cmd := &cobra.Command{
Use: "gen-priv-key-json",
Short: "Generate a priv_validator_key.json file from EVM private key",
Args: cobra.NoArgs,
PreRunE: func(_ *cobra.Command, _ []string) error {
return initializeBaseConfig(&cfg.baseConfig)
},
RunE: runValidatorCommand(
func(_ *cobra.Command) error {
return validateGenPrivKeyJSONFlags(&cfg)
},
func(ctx context.Context) error { return genValidatorPrivKeyJSON(ctx, cfg) },
),
}

bindValidatorGenPrivKeyJSONFlags(cmd, &cfg)

return cmd
}

func newValidatorEncryptPrivKeyCmd() *cobra.Command {
var cfg baseConfig

cmd := &cobra.Command{
Use: "encrypt-key",
Short: "Encrypt the private key in .env",
Args: cobra.NoArgs,
PreRunE: func(_ *cobra.Command, _ []string) error {
return nil
},
RunE: runValidatorCommand(
func(_ *cobra.Command) error {
return validateEncryptPrivKeyFlags(&cfg)
},
func(_ context.Context) error { return encryptPrivKey(cfg) },
),
}

bindValidatorBaseFlags(cmd, &cfg)

return cmd
}

func newValidatorUnjailCmd() *cobra.Command {
var cfg unjailConfig

Expand Down Expand Up @@ -636,51 +584,6 @@ func exportKey(_ context.Context, cfg exportKeyConfig) error {
return nil
}

func genValidatorPrivKeyJSON(_ context.Context, cfg genPrivKeyJSONConfig) error {
privKeyBytes, err := hex.DecodeString(cfg.PrivateKey)
if err != nil {
return errors.Wrap(err, "failed to decode private key")
}

privKey := k1.PrivKey(privKeyBytes)
newPV := &privval.FilePVKey{
Address: privKey.PubKey().Address(),
PubKey: privKey.PubKey(),
PrivKey: privKey,
}

jsonBytes, err := cmtjson.MarshalIndent(newPV, "", " ")
if err != nil {
return errors.Wrap(err, "failed to marshal pv data")
}

if err := tempfile.WriteFileAtomic(cfg.ValidatorKeyFile, jsonBytes, 0600); err != nil {
return errors.Wrap(err, "failed to write file")
}

return nil
}

func encryptPrivKey(cfg baseConfig) error {
privKeyBytes, err := hex.DecodeString(cfg.PrivateKey)
if err != nil {
return errors.Wrap(err, "failed to decode private key")
}

pk := k1.PrivKey(privKeyBytes)
pv := privval.FilePVKey{
PrivKey: pk,
PubKey: pk.PubKey(),
Address: pk.PubKey().Address(),
}

if err := app.EncryptAndStoreKey(pv, cfg.EncPrivKeyFile()); err != nil {
return errors.Wrap(err, "failed to encrypt and store the key")
}

return nil
}

func createValidator(ctx context.Context, cfg createValidatorConfig) error {
compressedPubKeyBytes, err := validatorKeyFileToCmpPubKey(cfg.ValidatorKeyFile)
if err != nil {
Expand Down

0 comments on commit e89f93e

Please sign in to comment.