From 2c90a0fa22fbcd56241eea15bc3c742517c7e84b Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Fri, 23 Sep 2022 14:26:27 -0500 Subject: [PATCH] perf: reduce user's password prompts when calling keyring List function (backport #13207) (#13368) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * perf: reduce user's password prompts when calling keyring List function (#13207) * Reduce user password prompts by taking advantage of the already existing MigrateAll function * Print message when no records were found on the keyring * Update changelog * Fix migration test * Add keys sort (cherry picked from commit 4882f933b1a108d578343573595fb06d7fd319c2) # Conflicts: # CHANGELOG.md * fix conflicts * suggestions Co-authored-by: Aleksandr Bezobchuk Co-authored-by: Ezequiel Raynaudo Co-authored-by: Federico Kunze Küllmer <31522760+fedekunze@users.noreply.github.com> Co-authored-by: Julien Robert Co-authored-by: Aleksandr Bezobchuk --- CHANGELOG.md | 1 + client/keys/list.go | 2 +- crypto/keyring/keyring.go | 53 +++------------------------------------ 3 files changed, 5 insertions(+), 51 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4fe2bb20e1e1..36ef5ff3f75b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -44,6 +44,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ ### Features +* (cli) [#13207](https://github.com/cosmos/cosmos-sdk/pull/13207) Reduce user's password prompts when calling keyring `List()` function. * (cli) [#13353](https://github.com/cosmos/cosmos-sdk/pull/13353) Add `tx group draft-proposal` command for generating group proposal JSONs (skeleton). * (cli) [#13304](https://github.com/cosmos/cosmos-sdk/pull/13304) Add `tx gov draft-proposal` command for generating proposal JSONs (skeleton). * (x/authz) [#13047](https://github.com/cosmos/cosmos-sdk/pull/13047) Add a GetAuthorization function to the keeper. diff --git a/client/keys/list.go b/client/keys/list.go index f7ec267df233..2da7daba95b5 100644 --- a/client/keys/list.go +++ b/client/keys/list.go @@ -34,7 +34,7 @@ func runListCmd(cmd *cobra.Command, _ []string) error { return err } - if len(records) == 0 && clientCtx.OutputFormat == flags.OutputFormatText { + if len(records) == 0 { cmd.Println("No records were found in keyring") return nil } diff --git a/crypto/keyring/keyring.go b/crypto/keyring/keyring.go index c9c858ac0896..a0de3c1a4f4f 100644 --- a/crypto/keyring/keyring.go +++ b/crypto/keyring/keyring.go @@ -535,56 +535,8 @@ func wrapKeyNotFound(err error, msg string) error { return err } -func (ks keystore) List() ([]Info, error) { - res := []Info{} - - keys, err := ks.db.Keys() - if err != nil { - return nil, err - } - - if len(keys) == 0 { - return res, nil - } - - sort.Strings(keys) - for _, key := range keys { - if strings.HasSuffix(key, infoSuffix) { - rawInfo, err := ks.db.Get(key) - if err != nil { - fmt.Printf("err for key %s: %q\n", key, err) - - // add the name of the key in case the user wants to retrieve it - // afterwards - info := newOfflineInfo(key, nil, hd.PubKeyType("")) - res = append(res, info) - continue - } - - if len(rawInfo.Data) == 0 { - fmt.Println(sdkerrors.Wrap(sdkerrors.ErrKeyNotFound, key)) - - // add the name of the key in case the user wants to retrieve it - // afterwards - info := newOfflineInfo(key, nil, hd.PubKeyType("")) - res = append(res, info) - continue - } - - info, err := unmarshalInfo(rawInfo.Data) - if err != nil { - fmt.Printf("err for key %s: %q\n", key, err) - - // add the name of the key in case the user wants to retrieve it - // afterwards - info = newOfflineInfo(key, nil, hd.PubKeyType("")) - } - - res = append(res, info) - } - } - - return res, nil +func (ks keystore) List() ([]*Record, error) { + return ks.MigrateAll() } func (ks keystore) NewMnemonic(uid string, language Language, hdPath, bip39Passphrase string, algo SignatureAlgo) (*Record, string, error) { @@ -959,6 +911,7 @@ func (ks keystore) MigrateAll() ([]*Record, error) { } sort.Strings(keys) + var recs []*Record for _, key := range keys { // The keyring items only with `.info` consists the key info.