feat(client/v2): add key lookup interface (#17308)

This commit is contained in:
Julien Robert 2023-08-08 11:51:22 +02:00 committed by GitHub
parent bdb7387fa9
commit f14f421a03
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 50 additions and 5 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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