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 <alessio@tendermint.com>

* remove keyring algorithms global, let user set them via client.Context methods

* remove old keyring globals test

Co-authored-by: Alessio Treglia <alessio@tendermint.com>
Co-authored-by: Jonathan Gimeno <jgimeno@gmail.com>
This commit is contained in:
Gianguido Sora 2021-03-18 09:27:03 +01:00 committed by GitHub
parent 1fddce73b3
commit deaee53111
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 23 deletions

View File

@ -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)

View File

@ -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...)
}

View File

@ -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)
}
/*