Skip to content
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: introduce --keyring-dir=... flag #7436

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,9 @@ be used to retrieve the actual proposal `Content`. Also the `NewMsgSubmitProposa
* (store) [\#6390](https://github.com/cosmos/cosmos-sdk/pull/6390) `RootMulti` store query proofs now return `CommitmentOp` which wraps `CommitmentProofs`
* `store.Query` now only returns chained `ics23.CommitmentProof` wrapped in `merkle.Proof`
* `ProofRuntime` only decodes and verifies `ics23.CommitmentProof`
* (x/auth) [\6350](https://github.com/cosmos/cosmos-sdk/pull/6350) New sign-batch command to sign StdTx batch files.
* (x/auth) [\#6350](https://github.com/cosmos/cosmos-sdk/pull/6350) New sign-batch command to sign StdTx batch files.
* (x/staking) [\#7436](https://github.com/cosmos/cosmos-sdk/pull/7436) New
`--keyring-dir=...` flag to enable keystore and server config in separate directories.

### Bug Fixes

Expand Down
8 changes: 6 additions & 2 deletions client/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,15 @@ func ReadPersistentCommandFlags(clientCtx Context, flagSet *pflag.FlagSet) (Cont
clientCtx = clientCtx.WithChainID(chainID)
}

if clientCtx.Keyring == nil || flagSet.Changed(flags.FlagKeyringBackend) {
if clientCtx.Keyring == nil || flagSet.Changed(flags.FlagKeyringBackend) || flagSet.Changed(flags.FlagKeyringDir) {
keyringDir, _ := flagSet.GetString(flags.FlagKeyringDir)
if keyringDir == "" {
keyringDir = clientCtx.HomeDir
}
keyringBackend, _ := flagSet.GetString(flags.FlagKeyringBackend)

if keyringBackend != "" {
kr, err := newKeyringFromFlags(clientCtx, keyringBackend)
kr, err := newKeyringFromFlags(clientCtx, keyringBackend, keyringDir)
if err != nil {
return clientCtx, err
}
Expand Down
6 changes: 3 additions & 3 deletions client/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,10 +300,10 @@ func GetFromFields(kr keyring.Keyring, from string, genOnly bool) (sdk.AccAddres
return info.GetAddress(), info.GetName(), nil
}

func newKeyringFromFlags(ctx Context, backend string) (keyring.Keyring, error) {
func newKeyringFromFlags(ctx Context, backend, keyringDir string) (keyring.Keyring, error) {
if ctx.GenerateOnly {
return keyring.New(sdk.KeyringServiceName(), keyring.BackendMemory, ctx.HomeDir, ctx.Input)
return keyring.New(sdk.KeyringServiceName(), keyring.BackendMemory, keyringDir, ctx.Input)
}

return keyring.New(sdk.KeyringServiceName(), backend, ctx.HomeDir, ctx.Input)
return keyring.New(sdk.KeyringServiceName(), backend, keyringDir, ctx.Input)
}
2 changes: 2 additions & 0 deletions client/flags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ const (
FlagSkipConfirmation = "yes"
FlagProve = "prove"
FlagKeyringBackend = "keyring-backend"
FlagKeyringDir = "keyring-dir"
FlagPage = "page"
FlagLimit = "limit"
FlagSignMode = "sign-mode"
Expand Down Expand Up @@ -99,6 +100,7 @@ func AddTxFlagsToCmd(cmd *cobra.Command) {
cmd.Flags().Bool(FlagOffline, false, "Offline mode (does not allow any online functionality")
cmd.Flags().BoolP(FlagSkipConfirmation, "y", false, "Skip tx broadcasting prompt confirmation")
cmd.Flags().String(FlagKeyringBackend, DefaultKeyringBackend, "Select keyring's backend (os|file|kwallet|pass|test)")
cmd.Flags().String(FlagKeyringDir, "", "Keyring's key storage directory")
cmd.Flags().String(FlagSignMode, "", "Choose sign mode (direct|amino-json), this is an advanced feature")
cmd.Flags().Uint64(FlagTimeoutHeight, 0, "Set a block timeout height to prevent the tx from being committed past a certain height")

Expand Down
11 changes: 7 additions & 4 deletions client/keys/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,19 +85,22 @@ the flag --nosort is set.
func runAddCmd(cmd *cobra.Command, args []string) error {
buf := bufio.NewReader(cmd.InOrStdin())

homeDir, _ := cmd.Flags().GetString(flags.FlagHome)
dryRun, _ := cmd.Flags().GetBool(flags.FlagHome)
keyringDir, _ := cmd.Flags().GetString(flags.FlagKeyringDir)
if keyringDir == "" {
keyringDir, _ = cmd.Flags().GetString(flags.FlagHome)
}
dryRun, _ := cmd.Flags().GetBool(flags.FlagDryRun)

var (
kr keyring.Keyring
err error
)

if dryRun {
kr, err = keyring.New(sdk.KeyringServiceName(), keyring.BackendMemory, homeDir, buf)
kr, err = keyring.New(sdk.KeyringServiceName(), keyring.BackendMemory, keyringDir, buf)
} else {
backend, _ := cmd.Flags().GetString(flags.FlagKeyringBackend)
kr, err = keyring.New(sdk.KeyringServiceName(), backend, homeDir, buf)
kr, err = keyring.New(sdk.KeyringServiceName(), backend, keyringDir, buf)
}

if err != nil {
Expand Down
7 changes: 5 additions & 2 deletions client/keys/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,12 @@ private keys stored in a ledger device cannot be deleted with the CLI.
RunE: func(cmd *cobra.Command, args []string) error {
buf := bufio.NewReader(cmd.InOrStdin())

keyringDir, _ := cmd.Flags().GetString(flags.FlagKeyringDir)
if keyringDir == "" {
keyringDir, _ = cmd.Flags().GetString(flags.FlagHome)
}
backend, _ := cmd.Flags().GetString(flags.FlagKeyringBackend)
homeDir, _ := cmd.Flags().GetString(flags.FlagHome)
kb, err := keyring.New(sdk.KeyringServiceName(), backend, homeDir, buf)
kb, err := keyring.New(sdk.KeyringServiceName(), backend, keyringDir, buf)
if err != nil {
return err
}
Expand Down
7 changes: 5 additions & 2 deletions client/keys/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,12 @@ func ExportKeyCommand() *cobra.Command {
RunE: func(cmd *cobra.Command, args []string) error {
buf := bufio.NewReader(cmd.InOrStdin())

keyringDir, _ := cmd.Flags().GetString(flags.FlagKeyringDir)
if keyringDir == "" {
keyringDir, _ = cmd.Flags().GetString(flags.FlagHome)
}
backend, _ := cmd.Flags().GetString(flags.FlagKeyringBackend)
homeDir, _ := cmd.Flags().GetString(flags.FlagHome)
kb, err := keyring.New(sdk.KeyringServiceName(), backend, homeDir, buf)
kb, err := keyring.New(sdk.KeyringServiceName(), backend, keyringDir, buf)
if err != nil {
return err
}
Expand Down
8 changes: 6 additions & 2 deletions client/keys/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,13 @@ func ImportKeyCommand() *cobra.Command {
RunE: func(cmd *cobra.Command, args []string) error {
buf := bufio.NewReader(cmd.InOrStdin())

keyringDir, _ := cmd.Flags().GetString(flags.FlagKeyringDir)
if keyringDir == "" {
keyringDir, _ = cmd.Flags().GetString(flags.FlagHome)
}
backend, _ := cmd.Flags().GetString(flags.FlagKeyringBackend)
homeDir, _ := cmd.Flags().GetString(flags.FlagHome)
kb, err := keyring.New(sdk.KeyringServiceName(), backend, homeDir, buf)

kb, err := keyring.New(sdk.KeyringServiceName(), backend, keyringDir, buf)
if err != nil {
return err
}
Expand Down
8 changes: 6 additions & 2 deletions client/keys/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,13 @@ along with their associated name and address.`,
}

func runListCmd(cmd *cobra.Command, _ []string) error {
keyringDir, _ := cmd.Flags().GetString(flags.FlagKeyringDir)
if keyringDir == "" {
keyringDir, _ = cmd.Flags().GetString(flags.FlagHome)
}
Comment on lines +29 to +32
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need to do these calls in each command. Can we not use the Context?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need to do these calls in each command. Can we not use the Context?

The keys * commands don't appear to have a Context. What do you have in mind?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or at least they don't use the clientCtx at all and I was just following how --keyring-backend was implemented.

backend, _ := cmd.Flags().GetString(flags.FlagKeyringBackend)
homeDir, _ := cmd.Flags().GetString(flags.FlagHome)
kb, err := keyring.New(sdk.KeyringServiceName(), backend, homeDir, cmd.InOrStdin())

kb, err := keyring.New(sdk.KeyringServiceName(), backend, keyringDir, cmd.InOrStdin())
if err != nil {
return err
}
Expand Down
11 changes: 7 additions & 4 deletions client/keys/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,14 @@ It is recommended to run in 'dry-run' mode first to verify all key migration mat
}

func runMigrateCmd(cmd *cobra.Command, args []string) error {
rootDir, _ := cmd.Flags().GetString(flags.FlagHome)
keyringDir, _ := cmd.Flags().GetString(flags.FlagKeyringDir)
if keyringDir == "" {
keyringDir, _ = cmd.Flags().GetString(flags.FlagHome)
}

// instantiate legacy keybase
var legacyKb keyring.LegacyKeybase
legacyKb, err := NewLegacyKeyBaseFromDir(rootDir)
legacyKb, err := NewLegacyKeyBaseFromDir(keyringDir)
if err != nil {
return err
}
Expand Down Expand Up @@ -76,13 +79,13 @@ func runMigrateCmd(cmd *cobra.Command, args []string) error {
migrator, err = keyring.NewInfoImporter(keyringServiceName, "test", tmpDir, buf)
} else {
backend, _ := cmd.Flags().GetString(flags.FlagKeyringBackend)
migrator, err = keyring.NewInfoImporter(keyringServiceName, backend, rootDir, buf)
migrator, err = keyring.NewInfoImporter(keyringServiceName, backend, keyringDir, buf)
}

if err != nil {
return errors.Wrap(err, fmt.Sprintf(
"failed to initialize keybase for service %s at directory %s",
keyringServiceName, rootDir,
keyringServiceName, keyringDir,
))
}

Expand Down
1 change: 1 addition & 0 deletions client/keys/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ The pass backend requires GnuPG: https://gnupg.org/

cmd.PersistentFlags().String(flags.FlagHome, defaultNodeHome, "The application home directory")
cmd.PersistentFlags().String(flags.FlagKeyringBackend, flags.DefaultKeyringBackend, "Select keyring's backend (os|file|test)")
cmd.PersistentFlags().String(flags.FlagKeyringDir, "", "Keyring's key storage directory")
cmd.PersistentFlags().String(cli.OutputFlag, "text", "Output format (text|json)")

return cmd
Expand Down
7 changes: 5 additions & 2 deletions client/keys/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,12 @@ consisting of all the keys provided by name and multisig threshold.`,
func runShowCmd(cmd *cobra.Command, args []string) (err error) {
var info keyring.Info

keyringDir, _ := cmd.Flags().GetString(flags.FlagKeyringDir)
if keyringDir == "" {
keyringDir, _ = cmd.Flags().GetString(flags.FlagHome)
}
backend, _ := cmd.Flags().GetString(flags.FlagKeyringBackend)
homeDir, _ := cmd.Flags().GetString(flags.FlagHome)
kb, err := keyring.New(sdk.KeyringServiceName(), backend, homeDir, cmd.InOrStdin())
kb, err := keyring.New(sdk.KeyringServiceName(), backend, keyringDir, cmd.InOrStdin())
if err != nil {
return err
}
Expand Down
1 change: 1 addition & 0 deletions client/rpc/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ func ValidatorCommand() *cobra.Command {

cmd.Flags().StringP(flags.FlagNode, "n", "tcp://localhost:26657", "Node to connect to")
cmd.Flags().String(flags.FlagKeyringBackend, flags.DefaultKeyringBackend, "Select keyring's backend (os|file|kwallet|pass|test)")
cmd.Flags().String(flags.FlagKeyringDir, "", "Keyring's key storage directory")
cmd.Flags().Int(flags.FlagPage, rest.DefaultPage, "Query a specific page of paginated results")
cmd.Flags().Int(flags.FlagLimit, 100, "Query number of results returned per page")

Expand Down
2 changes: 1 addition & 1 deletion docs/interfaces/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ The root command (called `rootCmd`) is what the user first types into the comman
* **Status** command from the SDK rpc client tools, which prints information about the status of the connected [`Node`](../core/node.md). The Status of a node includes `NodeInfo`,`SyncInfo` and `ValidatorInfo`.
* **Config** [command](https://github.com/cosmos/cosmos-sdk/blob/master/client/config.go) from the SDK client tools, which allows the user to edit a `config.toml` file that sets values for [flags](#flags) such as `--chain-id` and which `--node` they wish to connect to.
The `config` command can be invoked by typing `appcli config` with optional arguments `<key> [value]` and a `--get` flag to query configurations or `--home` flag to create a new configuration.
* **Keys** [commands](https://github.com/cosmos/cosmos-sdk/blob/master/client/keys) from the SDK client tools, which includes a collection of subcommands for using the key functions in the SDK crypto tools, including adding a new key and saving it to disk, listing all public keys stored in the key manager, and deleting a key. For example, users can type `appcli keys add <name>` to add a new key and save an encrypted copy to disk, using the flag `--recover` to recover a private key from a seed phrase or the flag `--multisig` to group multiple keys together to create a multisig key. For full details on the `add` key command, see the code [here](https://github.com/cosmos/cosmos-sdk/blob/master/client/keys/add.go). For more details about usage of `--keyring-backend` for storage of key credentials look at the [keyring docs](/keyring.md).
* **Keys** [commands](https://github.com/cosmos/cosmos-sdk/blob/master/client/keys) from the SDK client tools, which includes a collection of subcommands for using the key functions in the SDK crypto tools, including adding a new key and saving it to disk, listing all public keys stored in the key manager, and deleting a key. For example, users can type `appcli keys add <name>` to add a new key and save an encrypted copy to disk, using the flag `--recover` to recover a private key from a seed phrase or the flag `--multisig` to group multiple keys together to create a multisig key. For full details on the `add` key command, see the code [here](https://github.com/cosmos/cosmos-sdk/blob/master/client/keys/add.go). For more details about usage of `--keyring-backend` for storage of key credentials look at the [keyring docs](/keyring.md). Note that the `--keyring-dir` flag specifies where on disk the keyring is stored (by default in `--home`).
* [**Transaction**](#transaction-commands) commands.
* [**Query**](#query-commands) commands.

Expand Down
8 changes: 7 additions & 1 deletion simapp/simd/cmd/genaccounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,15 @@ contain valid denominations. Accounts may optionally be supplied with vesting pa
addr, err := sdk.AccAddressFromBech32(args[0])
if err != nil {
inBuf := bufio.NewReader(cmd.InOrStdin())

keyringDir, _ := cmd.Flags().GetString(flags.FlagKeyringDir)
if keyringDir == "" {
keyringDir = clientCtx.HomeDir
}
keyringBackend, _ := cmd.Flags().GetString(flags.FlagKeyringBackend)

// attempt to lookup address from Keybase if no address was provided
kb, err := keyring.New(sdk.KeyringServiceName(), keyringBackend, clientCtx.HomeDir, inBuf)
kb, err := keyring.New(sdk.KeyringServiceName(), keyringBackend, keyringDir, inBuf)
if err != nil {
return err
}
Expand Down Expand Up @@ -171,6 +176,7 @@ contain valid denominations. Accounts may optionally be supplied with vesting pa

cmd.Flags().String(flags.FlagHome, defaultNodeHome, "The application home directory")
cmd.Flags().String(flags.FlagKeyringBackend, flags.DefaultKeyringBackend, "Select keyring's backend (os|file|kwallet|pass|test)")
cmd.Flags().String(flags.FlagKeyringDir, "", "Keyring's key storage directory")
cmd.Flags().String(flagVestingAmt, "", "amount of coins for vesting accounts")
cmd.Flags().Int64(flagVestingStart, 0, "schedule start time (unix epoch) for vesting accounts")
cmd.Flags().Int64(flagVestingEnd, 0, "schedule end time (unix epoch) for vesting accounts")
Expand Down
1 change: 1 addition & 0 deletions simapp/simd/cmd/testnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ Example:
cmd.Flags().String(flags.FlagChainID, "", "genesis file chain-id, if left blank will be randomly created")
cmd.Flags().String(server.FlagMinGasPrices, fmt.Sprintf("0.000006%s", sdk.DefaultBondDenom), "Minimum gas prices to accept for transactions; All fees in a tx must meet this minimum (e.g. 0.01photino,0.001stake)")
cmd.Flags().String(flags.FlagKeyringBackend, flags.DefaultKeyringBackend, "Select keyring's backend (os|file|test)")
cmd.Flags().String(flags.FlagKeyringDir, "", "Keyring's key storage directory")
cmd.Flags().String(flags.FlagKeyAlgorithm, string(hd.Secp256k1Type), "Key signing algorithm to generate keys for")

return cmd
Expand Down
1 change: 1 addition & 0 deletions x/auth/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ $ %s query txs --%s 'message.sender=cosmos1...&message.action=withdraw_delegator

cmd.Flags().StringP(flags.FlagNode, "n", "tcp://localhost:26657", "Node to connect to")
cmd.Flags().String(flags.FlagKeyringBackend, flags.DefaultKeyringBackend, "Select keyring's backend (os|file|kwallet|pass|test)")
cmd.Flags().String(flags.FlagKeyringDir, "", "Keyring's key storage directory")
cmd.Flags().Int(flags.FlagPage, rest.DefaultPage, "Query a specific page of paginated results")
cmd.Flags().Int(flags.FlagLimit, rest.DefaultLimit, "Query number of transactions results per page returned")
cmd.Flags().String(flagEvents, "", fmt.Sprintf("list of transaction events in the form of %s", eventFormat))
Expand Down
6 changes: 5 additions & 1 deletion x/auth/client/cli/tx_multisign.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,14 @@ func makeMultiSignCmd() func(cmd *cobra.Command, args []string) error {
return err
}

keyringDir, _ := cmd.Flags().GetString(flags.FlagKeyringDir)
if keyringDir == "" {
keyringDir = clientCtx.HomeDir
}
backend, _ := cmd.Flags().GetString(flags.FlagKeyringBackend)

inBuf := bufio.NewReader(cmd.InOrStdin())
kb, err := keyring.New(sdk.KeyringServiceName(), backend, clientCtx.HomeDir, inBuf)
kb, err := keyring.New(sdk.KeyringServiceName(), backend, keyringDir, inBuf)
if err != nil {
return
}
Expand Down