Skip to content

Commit

Permalink
refactor: check key name validation for key import and export (gnolan…
Browse files Browse the repository at this point in the history
…g#1492)

## Summary

- Addition of a new validation of key name for `key import`
- Addition of a new validation of key name or address for `key export`

It's odd if we forget to set the key name(address), it will display an empty key name.
Also, This is unfriendly to `key delete` subcommand

![image](https://github.com/gnolang/gno/assets/25278203/1ed71f59-026a-4279-9139-7f0177fe1435)
  • Loading branch information
Halimao authored and gfanton committed Jan 18, 2024
1 parent ba5f754 commit 5d4aab0
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 1 deletion.
5 changes: 5 additions & 0 deletions tm2/pkg/crypto/keys/client/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package client

import (
"context"
"errors"
"flag"
"fmt"
"os"
Expand Down Expand Up @@ -60,6 +61,10 @@ func (c *exportCfg) RegisterFlags(fs *flag.FlagSet) {
}

func execExport(cfg *exportCfg, io commands.IO) error {
// check keyname
if cfg.nameOrBech32 == "" {
return errors.New("key to be exported shouldn't be empty")
}
// Create a new instance of the key-base
kb, err := keys.NewKeyBaseFromDir(cfg.rootCfg.Home)
if err != nil {
Expand Down
16 changes: 16 additions & 0 deletions tm2/pkg/crypto/keys/client/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,3 +202,19 @@ func TestExport_ExportKey(t *testing.T) {
})
}
}

func TestExport_ExportKeyWithEmptyName(t *testing.T) {
// Generate a temporary key-base directory
_, kbHome := newTestKeybase(t)
err := exportKey(
testExportKeyOpts{
testCmdKeyOptsBase: testCmdKeyOptsBase{
kbHome: kbHome,
keyName: "",
},
},
nil,
)
assert.Error(t, err)
assert.EqualError(t, err, "key to be exported shouldn't be empty")
}
7 changes: 6 additions & 1 deletion tm2/pkg/crypto/keys/client/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package client

import (
"context"
"errors"
"flag"
"fmt"
"os"
Expand Down Expand Up @@ -60,6 +61,10 @@ func (c *importCfg) RegisterFlags(fs *flag.FlagSet) {
}

func execImport(cfg *importCfg, io commands.IO) error {
// check keyname
if cfg.keyName == "" {
return errors.New("name shouldn't be empty")
}
// Create a new instance of the key-base
kb, err := keys.NewKeyBaseFromDir(cfg.rootCfg.Home)
if err != nil {
Expand Down Expand Up @@ -88,7 +93,7 @@ func execImport(cfg *importCfg, io commands.IO) error {
if !cfg.unsafe {
// Get the armor decrypt password
decryptPassword, err = io.GetPassword(
"Enter a passphrase to decrypt your private key armor:",
"Enter the passphrase to decrypt your private key armor:",
cfg.rootCfg.InsecurePasswordStdin,
)
if err != nil {
Expand Down
16 changes: 16 additions & 0 deletions tm2/pkg/crypto/keys/client/import_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,3 +152,19 @@ func TestImport_ImportKey(t *testing.T) {
})
}
}

func TestImport_ImportKeyWithEmptyName(t *testing.T) {
// Generate a temporary key-base directory
_, kbHome := newTestKeybase(t)
err := importKey(
testImportKeyOpts{
testCmdKeyOptsBase: testCmdKeyOptsBase{
kbHome: kbHome,
keyName: "",
},
},
nil,
)
assert.Error(t, err)
assert.EqualError(t, err, "name shouldn't be empty")
}

0 comments on commit 5d4aab0

Please sign in to comment.