-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into docs/gh9575-rosetta
- Loading branch information
Showing
70 changed files
with
1,527 additions
and
2,976 deletions.
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
Oops, something went wrong.