diff --git a/client/v2/autocli/app.go b/client/v2/autocli/app.go index 367622a2ce21..e8197fc08cfc 100644 --- a/client/v2/autocli/app.go +++ b/client/v2/autocli/app.go @@ -8,6 +8,7 @@ import ( autocliv1 "cosmossdk.io/api/cosmos/autocli/v1" "cosmossdk.io/client/v2/autocli/flag" + "cosmossdk.io/client/v2/autocli/keyring" "cosmossdk.io/core/address" "cosmossdk.io/core/appmodule" "cosmossdk.io/depinject" @@ -41,6 +42,9 @@ type AppOptions struct { AddressCodec address.Codec ValidatorAddressCodec runtime.ValidatorAddressCodec ConsensusAddressCodec runtime.ConsensusAddressCodec + + // Keyring is the keyring to use for client/v2. + Keyring keyring.Keyring `optional:"true"` } // EnhanceRootCommand enhances the provided root command with autocli AppOptions, @@ -66,6 +70,7 @@ func (appOptions AppOptions) EnhanceRootCommand(rootCmd *cobra.Command) error { AddressCodec: appOptions.AddressCodec, ValidatorAddressCodec: appOptions.ValidatorAddressCodec, ConsensusAddressCodec: appOptions.ConsensusAddressCodec, + Keyring: appOptions.Keyring, }, GetClientConn: func(cmd *cobra.Command) (grpc.ClientConnInterface, error) { return client.GetClientQueryContext(cmd) diff --git a/client/v2/autocli/builder.go b/client/v2/autocli/builder.go index 580a02e3d724..470d8e07e8fb 100644 --- a/client/v2/autocli/builder.go +++ b/client/v2/autocli/builder.go @@ -7,6 +7,7 @@ import ( "google.golang.org/grpc" "cosmossdk.io/client/v2/autocli/flag" + "cosmossdk.io/client/v2/autocli/keyring" ) // Builder manages options for building CLI commands. @@ -44,5 +45,9 @@ func (b *Builder) Validate() error { return errors.New("file resolver is required in builder") } + if b.Keyring == nil { + b.Keyring = keyring.NoKeyring{} + } + return nil } diff --git a/client/v2/autocli/flag/address.go b/client/v2/autocli/flag/address.go index 7e2985ac3c37..70d6a70f1c07 100644 --- a/client/v2/autocli/flag/address.go +++ b/client/v2/autocli/flag/address.go @@ -6,6 +6,7 @@ import ( "google.golang.org/protobuf/reflect/protoreflect" + "cosmossdk.io/client/v2/autocli/keyring" "cosmossdk.io/core/address" "github.com/cosmos/cosmos-sdk/codec" @@ -18,7 +19,7 @@ import ( type addressStringType struct{} func (a addressStringType) NewValue(_ context.Context, b *Builder) Value { - return &addressValue{addressCodec: b.AddressCodec} + return &addressValue{addressCodec: b.AddressCodec, keyring: b.Keyring} } func (a addressStringType) DefaultValue() string { @@ -28,7 +29,7 @@ func (a addressStringType) DefaultValue() string { type validatorAddressStringType struct{} func (a validatorAddressStringType) NewValue(_ context.Context, b *Builder) Value { - return &addressValue{addressCodec: b.ValidatorAddressCodec} + return &addressValue{addressCodec: b.ValidatorAddressCodec, keyring: b.Keyring} } func (a validatorAddressStringType) DefaultValue() string { @@ -38,6 +39,7 @@ func (a validatorAddressStringType) DefaultValue() string { type addressValue struct { value string addressCodec address.Codec + keyring keyring.Keyring } func (a addressValue) Get(protoreflect.Value) (protoreflect.Value, error) { @@ -50,7 +52,13 @@ func (a addressValue) String() string { // Set implements the flag.Value interface for addressValue it only supports bech32 addresses. func (a *addressValue) Set(s string) error { - _, err := a.addressCodec.StringToBytes(s) + _, err := a.keyring.LookupAddressByKeyName(s) + if err == nil { + a.value = s + return nil + } + + _, err = a.addressCodec.StringToBytes(s) if err != nil { return fmt.Errorf("invalid bech32 account address: %w", err) } @@ -67,7 +75,7 @@ func (a addressValue) Type() string { type consensusAddressStringType struct{} func (a consensusAddressStringType) NewValue(ctx context.Context, b *Builder) Value { - return &consensusAddressValue{addressValue: addressValue{addressCodec: b.ConsensusAddressCodec}} + return &consensusAddressValue{addressValue: addressValue{addressCodec: b.ConsensusAddressCodec, keyring: b.Keyring}} } func (a consensusAddressStringType) DefaultValue() string { @@ -87,7 +95,13 @@ func (a consensusAddressValue) String() string { } func (a *consensusAddressValue) Set(s string) error { - _, err := a.addressCodec.StringToBytes(s) + _, err := a.keyring.LookupAddressByKeyName(s) + if err == nil { + a.value = s + return nil + } + + _, err = a.addressCodec.StringToBytes(s) if err == nil { a.value = s return nil diff --git a/client/v2/autocli/flag/builder.go b/client/v2/autocli/flag/builder.go index 6720bb3faa18..7e4b477ad84e 100644 --- a/client/v2/autocli/flag/builder.go +++ b/client/v2/autocli/flag/builder.go @@ -14,6 +14,7 @@ import ( "google.golang.org/protobuf/reflect/protoregistry" autocliv1 "cosmossdk.io/api/cosmos/autocli/v1" + "cosmossdk.io/client/v2/autocli/keyring" "cosmossdk.io/client/v2/internal/util" "cosmossdk.io/core/address" ) @@ -38,6 +39,9 @@ type Builder struct { AddressCodec address.Codec ValidatorAddressCodec address.Codec ConsensusAddressCodec address.Codec + + // Keyring + Keyring keyring.Keyring } func (b *Builder) init() { diff --git a/client/v2/autocli/keyring/interface.go b/client/v2/autocli/keyring/interface.go new file mode 100644 index 000000000000..0ebb9df9cf27 --- /dev/null +++ b/client/v2/autocli/keyring/interface.go @@ -0,0 +1,6 @@ +package keyring + +type Keyring interface { + // LookupAddressByKeyName returns the address of the key with the given name. + LookupAddressByKeyName(name string) (string, error) +} diff --git a/client/v2/autocli/keyring/no_keyring.go b/client/v2/autocli/keyring/no_keyring.go new file mode 100644 index 000000000000..dc5c1b075c27 --- /dev/null +++ b/client/v2/autocli/keyring/no_keyring.go @@ -0,0 +1,11 @@ +package keyring + +import "errors" + +var _ Keyring = NoKeyring{} + +type NoKeyring struct{} + +func (k NoKeyring) LookupAddressByKeyName(name string) (string, error) { + return "", errors.New("no keyring configured") +}