-
Notifications
You must be signed in to change notification settings - Fork 3.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat!: key rename cli command #9601
Merged
Merged
Changes from all commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
238dbe2
added key renaming
technicallyty 46762a6
add cli cmds
technicallyty 950752a
changelog
technicallyty 4368e14
gofmt
technicallyty 8283b67
move changelog
technicallyty 9acfb00
fix root_test
technicallyty 16b6632
Apply suggestions from code review
technicallyty ec01a52
Merge branch 'master' into ty/9407-rename_keys_cli
technicallyty 26ec3a5
add equality function
technicallyty 9a2ba38
Update crypto/keyring/keyring.go
technicallyty 2c7395b
move passphrase to const defs
technicallyty 588c082
refactor test
technicallyty ed51363
allow duplicate keys as long as names are different
technicallyty 8d464aa
Merge branch 'master' into ty/9407-rename_keys_cli
technicallyty fc19867
lint
technicallyty c6aed98
Update CHANGELOG.md
technicallyty 3c1e9b9
Update crypto/keyring/keyring.go
technicallyty 4044320
Merge branch 'master' into ty/9407-rename_keys_cli
technicallyty e4d2a3f
Merge branch 'master' into ty/9407-rename_keys_cli
mergify[bot] 2d563b2
Merge branch 'master' into ty/9407-rename_keys_cli
mergify[bot] 1a9347c
Merge branch 'master' into ty/9407-rename_keys_cli
mergify[bot] 102169b
Merge branch 'master' into ty/9407-rename_keys_cli
mergify[bot] e3cca96
Merge branch 'master' into ty/9407-rename_keys_cli
mergify[bot] 3865a9e
Merge branch 'master' into ty/9407-rename_keys_cli
mergify[bot] aae59f9
Merge branch 'master' into ty/9407-rename_keys_cli
mergify[bot] e6f8508
Merge branch 'master' into ty/9407-rename_keys_cli
mergify[bot] e5e35f6
Merge branch 'master' into ty/9407-rename_keys_cli
mergify[bot] 4edd229
Merge branch 'master' into ty/9407-rename_keys_cli
technicallyty c21bd5f
Merge branch 'master' into ty/9407-rename_keys_cli
amaury1093 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package keys | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
|
||
"github.com/cosmos/cosmos-sdk/client" | ||
"github.com/cosmos/cosmos-sdk/client/input" | ||
"github.com/cosmos/cosmos-sdk/crypto/keyring" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// RenameKeyCommand renames a key from the key store. | ||
func RenameKeyCommand() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "rename <old_name> <new_name>", | ||
Short: "Rename an existing key", | ||
Long: `Rename a key from the Keybase backend. | ||
|
||
Note that renaming offline or ledger keys will rename | ||
only the public key references stored locally, i.e. | ||
private keys stored in a ledger device cannot be renamed with the CLI. | ||
`, | ||
Args: cobra.ExactArgs(2), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
buf := bufio.NewReader(cmd.InOrStdin()) | ||
clientCtx, err := client.GetClientQueryContext(cmd) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
oldName, newName := args[0], args[1] | ||
|
||
info, err := clientCtx.Keyring.Key(oldName) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// confirm rename, unless -y is passed | ||
if skip, _ := cmd.Flags().GetBool(flagYes); !skip { | ||
prompt := fmt.Sprintf("Key reference will be renamed from %s to %s. Continue?", args[0], args[1]) | ||
if yes, err := input.GetConfirmation(prompt, buf, cmd.ErrOrStderr()); err != nil { | ||
return err | ||
} else if !yes { | ||
return nil | ||
} | ||
} | ||
|
||
if err := clientCtx.Keyring.Rename(oldName, newName); err != nil { | ||
return err | ||
} | ||
|
||
if info.GetType() == keyring.TypeLedger || info.GetType() == keyring.TypeOffline { | ||
cmd.PrintErrln("Public key reference renamed") | ||
return nil | ||
} | ||
|
||
cmd.PrintErrln(fmt.Sprintf("Key was successfully renamed from %s to %s", oldName, newName)) | ||
|
||
return nil | ||
}, | ||
} | ||
|
||
cmd.Flags().BoolP(flagYes, "y", false, "Skip confirmation prompt when renaming offline or ledger key references") | ||
|
||
return cmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package keys | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/cosmos/cosmos-sdk/client" | ||
"github.com/cosmos/cosmos-sdk/client/flags" | ||
"github.com/cosmos/cosmos-sdk/crypto/hd" | ||
"github.com/cosmos/cosmos-sdk/crypto/keyring" | ||
"github.com/cosmos/cosmos-sdk/testutil" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
) | ||
|
||
func Test_runRenameCmd(t *testing.T) { | ||
// temp keybase | ||
kbHome := t.TempDir() | ||
cmd := RenameKeyCommand() | ||
cmd.Flags().AddFlagSet(Commands(kbHome).PersistentFlags()) | ||
mockIn := testutil.ApplyMockIODiscardOutErr(cmd) | ||
|
||
yesF, _ := cmd.Flags().GetBool(flagYes) | ||
require.False(t, yesF) | ||
|
||
fakeKeyName1 := "runRenameCmd_Key1" | ||
fakeKeyName2 := "runRenameCmd_Key2" | ||
|
||
path := sdk.GetConfig().GetFullBIP44Path() | ||
|
||
kb, err := keyring.New(sdk.KeyringServiceName(), keyring.BackendTest, kbHome, mockIn) | ||
require.NoError(t, err) | ||
|
||
// put fakeKeyName1 in keyring | ||
_, err = kb.NewAccount(fakeKeyName1, testutil.TestMnemonic, "", path, hd.Secp256k1) | ||
require.NoError(t, err) | ||
|
||
clientCtx := client.Context{}. | ||
WithKeyringDir(kbHome). | ||
WithKeyring(kb) | ||
|
||
ctx := context.WithValue(context.Background(), client.ClientContextKey, &clientCtx) | ||
|
||
// rename a key 'blah' which doesnt exist | ||
cmd.SetArgs([]string{"blah", "blaah", fmt.Sprintf("--%s=%s", flags.FlagHome, kbHome)}) | ||
err = cmd.ExecuteContext(ctx) | ||
require.Error(t, err) | ||
require.EqualError(t, err, "blah.info: key not found") | ||
|
||
// User confirmation missing | ||
cmd.SetArgs([]string{ | ||
fakeKeyName1, | ||
"nokey", | ||
fmt.Sprintf("--%s=%s", flags.FlagHome, kbHome), | ||
fmt.Sprintf("--%s=%s", flags.FlagKeyringBackend, keyring.BackendTest), | ||
}) | ||
err = cmd.Execute() | ||
require.Error(t, err) | ||
require.Equal(t, "EOF", err.Error()) | ||
|
||
oldKey, err := kb.Key(fakeKeyName1) | ||
require.NoError(t, err) | ||
|
||
// add a confirmation | ||
cmd.SetArgs([]string{ | ||
fakeKeyName1, | ||
fakeKeyName2, | ||
fmt.Sprintf("--%s=%s", flags.FlagHome, kbHome), | ||
fmt.Sprintf("--%s=true", flagYes), | ||
fmt.Sprintf("--%s=%s", flags.FlagKeyringBackend, keyring.BackendTest), | ||
}) | ||
require.NoError(t, cmd.Execute()) | ||
|
||
// key1 is gone | ||
_, err = kb.Key(fakeKeyName1) | ||
require.Error(t, err) | ||
|
||
// key2 exists now | ||
renamedKey, err := kb.Key(fakeKeyName2) | ||
require.NoError(t, err) | ||
|
||
require.Equal(t, oldKey.GetPubKey(), renamedKey.GetPubKey()) | ||
require.Equal(t, oldKey.GetType(), renamedKey.GetType()) | ||
require.Equal(t, oldKey.GetAddress(), renamedKey.GetAddress()) | ||
require.Equal(t, oldKey.GetAlgo(), renamedKey.GetAlgo()) | ||
|
||
// try to rename key1 but it doesnt exist anymore so error | ||
cmd.SetArgs([]string{ | ||
fakeKeyName1, | ||
fakeKeyName2, | ||
fmt.Sprintf("--%s=%s", flags.FlagHome, kbHome), | ||
fmt.Sprintf("--%s=true", flagYes), | ||
fmt.Sprintf("--%s=%s", flags.FlagKeyringBackend, keyring.BackendTest), | ||
}) | ||
require.Error(t, cmd.Execute()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For each test, should we check more than just an error? Should we also check whether each key does or does not exist within the keyring after renaming?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is already checked on L1175 and L1182-L1183
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Within this test, rename is called three times at the end without checking the specific error thrown or what keys exist after each failed attempt. I was thinking the extra checks would be added to the failed attempts. At the least, I think we should check to make sure we are receiving the expected error and not just any error.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
refactored the tests. should be a bit more concise. let me know if its missing anything please!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice! I like the direction with adding test cases. I'm not sure if it's necessary given that the only parameter for each test case is the
run
function and there is no overlap with the key names you are using so creating a new keyring for each test case might not be necessary but I also think it's ok to leave it as is.