Skip to content

Commit

Permalink
feat(client/v2): add key lookup interface (backport #17308) (#17317)
Browse files Browse the repository at this point in the history
Co-authored-by: Julien Robert <[email protected]>
  • Loading branch information
mergify[bot] and julienrbrt authored Aug 8, 2023
1 parent d9247a9 commit 6b395f6
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 5 deletions.
5 changes: 5 additions & 0 deletions client/v2/autocli/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
Expand All @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions client/v2/autocli/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
}
24 changes: 19 additions & 5 deletions client/v2/autocli/flag/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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) {
Expand All @@ -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)
}
Expand All @@ -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 {
Expand All @@ -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
Expand Down
4 changes: 4 additions & 0 deletions client/v2/autocli/flag/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -38,6 +39,9 @@ type Builder struct {
AddressCodec address.Codec
ValidatorAddressCodec address.Codec
ConsensusAddressCodec address.Codec

// Keyring
Keyring keyring.Keyring
}

func (b *Builder) init() {
Expand Down
6 changes: 6 additions & 0 deletions client/v2/autocli/keyring/interface.go
Original file line number Diff line number Diff line change
@@ -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)
}
11 changes: 11 additions & 0 deletions client/v2/autocli/keyring/no_keyring.go
Original file line number Diff line number Diff line change
@@ -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")
}

0 comments on commit 6b395f6

Please sign in to comment.