From deaee531111fb4aad3ccabbfa6771f9a67ae42ce Mon Sep 17 00:00:00 2001 From: Gianguido Sora Date: Thu, 18 Mar 2021 09:27:03 +0100 Subject: [PATCH] CLI/keys: define default supported keyring algorithms (#8825) * define package-level supported Keyring algorithms This commit lets developer define what algorithms Keyring supports instead of relying on just keyring.New options. This change is needed since it allows us to support those algorithms in CLI commands too without subverting their current execution flow. * read dry run cli flag as persistent command flag * add dry run field to CLI command handling context * use CLI context keyring instead of creating a new one * keyring supported algorithms are used as if they were defaults, name them as such * --dry-run value is now associated to client.context.Simulate Since Simulate is now used in both tx and query commands, move its reading routine into ReadPersistentCommandFlags. Removed DryRun from Context, since it's not needed anymore. * rename supported algorithms variable Co-authored-by: Alessio Treglia * remove keyring algorithms global, let user set them via client.Context methods * remove old keyring globals test Co-authored-by: Alessio Treglia Co-authored-by: Jonathan Gimeno --- client/cmd.go | 10 +++++----- client/context.go | 13 ++++++++++--- client/keys/add.go | 16 +--------------- 3 files changed, 16 insertions(+), 23 deletions(-) diff --git a/client/cmd.go b/client/cmd.go index 437022695d..8c71c40cf4 100644 --- a/client/cmd.go +++ b/client/cmd.go @@ -99,6 +99,11 @@ func ReadPersistentCommandFlags(clientCtx Context, flagSet *pflag.FlagSet) (Cont clientCtx = clientCtx.WithHomeDir(homeDir) } + if !clientCtx.Simulate || flagSet.Changed(flags.FlagDryRun) { + dryRun, _ := flagSet.GetBool(flags.FlagDryRun) + clientCtx = clientCtx.WithSimulation(dryRun) + } + if clientCtx.KeyringDir == "" || flagSet.Changed(flags.FlagKeyringDir) { keyringDir, _ := flagSet.GetString(flags.FlagKeyringDir) @@ -191,11 +196,6 @@ func readTxCommandFlags(clientCtx Context, flagSet *pflag.FlagSet) (Context, err clientCtx = clientCtx.WithGenerateOnly(genOnly) } - if !clientCtx.Simulate || flagSet.Changed(flags.FlagDryRun) { - dryRun, _ := flagSet.GetBool(flags.FlagDryRun) - clientCtx = clientCtx.WithSimulation(dryRun) - } - if !clientCtx.Offline || flagSet.Changed(flags.FlagOffline) { offline, _ := flagSet.GetBool(flags.FlagOffline) clientCtx = clientCtx.WithOffline(offline) diff --git a/client/context.go b/client/context.go index f7f555339c..292f914943 100644 --- a/client/context.go +++ b/client/context.go @@ -27,6 +27,7 @@ type Context struct { InterfaceRegistry codectypes.InterfaceRegistry Input io.Reader Keyring keyring.Keyring + KeyringOptions []keyring.Option Output io.Writer OutputFormat string Height int64 @@ -56,6 +57,12 @@ func (ctx Context) WithKeyring(k keyring.Keyring) Context { return ctx } +// WithKeyringOptions returns a copy of the context with an updated keyring. +func (ctx Context) WithKeyringOptions(opts ...keyring.Option) Context { + ctx.KeyringOptions = opts + return ctx +} + // WithInput returns a copy of the context with an updated input. func (ctx Context) WithInput(r io.Reader) Context { ctx.Input = r @@ -324,9 +331,9 @@ func GetFromFields(kr keyring.Keyring, from string, genOnly bool) (sdk.AccAddres } func newKeyringFromFlags(ctx Context, backend string) (keyring.Keyring, error) { - if ctx.GenerateOnly { - return keyring.New(sdk.KeyringServiceName(), keyring.BackendMemory, ctx.KeyringDir, ctx.Input) + if ctx.GenerateOnly || ctx.Simulate { + return keyring.New(sdk.KeyringServiceName(), keyring.BackendMemory, ctx.KeyringDir, ctx.Input, ctx.KeyringOptions...) } - return keyring.New(sdk.KeyringServiceName(), backend, ctx.KeyringDir, ctx.Input) + return keyring.New(sdk.KeyringServiceName(), backend, ctx.KeyringDir, ctx.Input, ctx.KeyringOptions...) } diff --git a/client/keys/add.go b/client/keys/add.go index ae937b5a4b..e0344decdf 100644 --- a/client/keys/add.go +++ b/client/keys/add.go @@ -90,21 +90,7 @@ func runAddCmd(cmd *cobra.Command, args []string) error { return err } - var kr keyring.Keyring - - dryRun, _ := cmd.Flags().GetBool(flags.FlagDryRun) - if dryRun { - kr, err = keyring.New(sdk.KeyringServiceName(), keyring.BackendMemory, clientCtx.KeyringDir, buf) - } else { - backend, _ := cmd.Flags().GetString(flags.FlagKeyringBackend) - kr, err = keyring.New(sdk.KeyringServiceName(), backend, clientCtx.KeyringDir, buf) - } - - if err != nil { - return err - } - - return RunAddCmd(cmd, args, kr, buf) + return RunAddCmd(cmd, args, clientCtx.Keyring, buf) } /*