Skip to content

Commit 5bbbd28

Browse files
committed
feat(cli): add show encrypted key cli
1 parent 29c1d77 commit 5bbbd28

File tree

4 files changed

+68
-2
lines changed

4 files changed

+68
-2
lines changed

.secrets.baseline

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@
179179
"filename": "client/cmd/flags.go",
180180
"hashed_secret": "6924110cde4fa051bfdc600a60620dc7aa9d3c6a",
181181
"is_verified": false,
182-
"line_number": 511
182+
"line_number": 516
183183
}
184184
],
185185
"client/config/config.go": [
@@ -958,5 +958,5 @@
958958
}
959959
]
960960
},
961-
"generated_at": "2025-02-28T05:12:18Z"
961+
"generated_at": "2025-02-28T08:18:38Z"
962962
}

client/cmd/flags.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,11 @@ func bindKeyGenPrivKeyJSONFlags(cmd *cobra.Command, cfg *genPrivKeyJSONConfig) {
160160
bindValidatorBaseFlags(cmd, &cfg.baseConfig)
161161
}
162162

163+
func bindKeyShowEncryptedFlags(cmd *cobra.Command, cfg *showEncryptedConfig) {
164+
bindValidatorBaseFlags(cmd, &cfg.baseConfig)
165+
cmd.Flags().BoolVar(&cfg.ShowPrivate, "show-private", false, "Show private key")
166+
}
167+
163168
func bindValidatorKeyFlags(cmd *cobra.Command, keyFilePath *string) {
164169
defaultKeyFilePath := filepath.Join(config.DefaultHomeDir(), "config", "priv_validator_key.json")
165170
cmd.Flags().StringVar(keyFilePath, "keyfile", defaultKeyFilePath, "Path to the Tendermint key file")
@@ -513,6 +518,14 @@ func validateEncryptFlags(cfg *baseConfig) error {
513518
return nil
514519
}
515520

521+
func validateShowEncryptedFlags(cfg *showEncryptedConfig) error {
522+
if !cmtos.FileExists(cfg.EncPrivKeyFile()) {
523+
return errors.New("no encrypted private key file")
524+
}
525+
526+
return nil
527+
}
528+
516529
func validateValidatorUnjailFlags(ctx context.Context, cmd *cobra.Command, cfg *unjailConfig) error {
517530
if err := validateFlags(cmd, []string{}); err != nil {
518531
return err

client/cmd/keys.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"encoding/base64"
66
"encoding/hex"
7+
"fmt"
78
"strings"
89

910
k1 "github.com/cometbft/cometbft/crypto/secp256k1"
@@ -35,6 +36,7 @@ func newKeyCmds() *cobra.Command {
3536
newKeyConvertCmd(),
3637
newKeyGenPrivKeyJSONCmd(),
3738
newKeyEncryptCmd(),
39+
newKeyShowEncryptedCmd(),
3840
)
3941

4042
return cmd
@@ -104,6 +106,29 @@ func newKeyEncryptCmd() *cobra.Command {
104106
return cmd
105107
}
106108

109+
func newKeyShowEncryptedCmd() *cobra.Command {
110+
var cfg showEncryptedConfig
111+
112+
cmd := &cobra.Command{
113+
Use: "show-encrypted",
114+
Short: "Show the encrypted private key after decryption",
115+
Args: cobra.NoArgs,
116+
PreRunE: func(_ *cobra.Command, _ []string) error {
117+
return nil
118+
},
119+
RunE: runValidatorCommand(
120+
func(_ *cobra.Command) error {
121+
return validateShowEncryptedFlags(&cfg)
122+
},
123+
func(_ context.Context) error { return showEncryptedKey(cfg) },
124+
),
125+
}
126+
127+
bindKeyShowEncryptedFlags(cmd, &cfg)
128+
129+
return cmd
130+
}
131+
107132
func convertKey(_ context.Context, cfg keyConfig) error {
108133
var compressedPubKeyBytes []byte
109134
var err error
@@ -191,3 +216,26 @@ func encryptPrivKey(cfg baseConfig) error {
191216

192217
return nil
193218
}
219+
220+
func showEncryptedKey(cfg showEncryptedConfig) error {
221+
encPrivKeyFile := cfg.EncPrivKeyFile()
222+
pv, err := app.LoadEncryptedPrivKey(encPrivKeyFile)
223+
if err != nil {
224+
return errors.Wrap(err, "failed to load encrypted private key")
225+
}
226+
227+
cmpPubKeyBytes, err := privKeyToCmpPubKey(pv.PrivKey.Bytes())
228+
if err != nil {
229+
return errors.Wrap(err, "failed to get compressed public key from private key")
230+
}
231+
232+
if err := printKeyFormats(cmpPubKeyBytes); err != nil {
233+
return errors.Wrap(err, "failed to print key formats")
234+
}
235+
236+
if cfg.ShowPrivate {
237+
fmt.Println("Private Key (hex):", hex.EncodeToString(pv.PrivKey.Bytes()))
238+
}
239+
240+
return nil
241+
}

client/cmd/validator.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,11 @@ type genPrivKeyJSONConfig struct {
157157
ValidatorKeyFile string
158158
}
159159

160+
type showEncryptedConfig struct {
161+
baseConfig
162+
ShowPrivate bool
163+
}
164+
160165
func loadEnv() {
161166
if err := godotenv.Load(); err != nil {
162167
fmt.Println("Warning: No .env file found")

0 commit comments

Comments
 (0)