From 5d4aab08338b0d03038c0be4a2176fd4c1fa00f9 Mon Sep 17 00:00:00 2001 From: Halimao <1065621723@qq.com> Date: Mon, 15 Jan 2024 22:31:43 +0800 Subject: [PATCH] refactor: check key name validation for key import and export (#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) --- tm2/pkg/crypto/keys/client/export.go | 5 +++++ tm2/pkg/crypto/keys/client/export_test.go | 16 ++++++++++++++++ tm2/pkg/crypto/keys/client/import.go | 7 ++++++- tm2/pkg/crypto/keys/client/import_test.go | 16 ++++++++++++++++ 4 files changed, 43 insertions(+), 1 deletion(-) diff --git a/tm2/pkg/crypto/keys/client/export.go b/tm2/pkg/crypto/keys/client/export.go index c3921c31fd7..71955cff5d2 100644 --- a/tm2/pkg/crypto/keys/client/export.go +++ b/tm2/pkg/crypto/keys/client/export.go @@ -2,6 +2,7 @@ package client import ( "context" + "errors" "flag" "fmt" "os" @@ -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 { diff --git a/tm2/pkg/crypto/keys/client/export_test.go b/tm2/pkg/crypto/keys/client/export_test.go index 0f4c5311dfa..2ff5fe9eb37 100644 --- a/tm2/pkg/crypto/keys/client/export_test.go +++ b/tm2/pkg/crypto/keys/client/export_test.go @@ -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") +} diff --git a/tm2/pkg/crypto/keys/client/import.go b/tm2/pkg/crypto/keys/client/import.go index e4f20ff6402..9a3f0377782 100644 --- a/tm2/pkg/crypto/keys/client/import.go +++ b/tm2/pkg/crypto/keys/client/import.go @@ -2,6 +2,7 @@ package client import ( "context" + "errors" "flag" "fmt" "os" @@ -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 { @@ -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 { diff --git a/tm2/pkg/crypto/keys/client/import_test.go b/tm2/pkg/crypto/keys/client/import_test.go index 7bc00e06ec9..9324817d9ac 100644 --- a/tm2/pkg/crypto/keys/client/import_test.go +++ b/tm2/pkg/crypto/keys/client/import_test.go @@ -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") +}