Skip to content

Commit

Permalink
feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
julienrbrt committed Apr 24, 2023
1 parent 906c75a commit 50abaac
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 19 deletions.
7 changes: 1 addition & 6 deletions client/v2/autocli/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,9 @@ type AppOptions struct {
// rootCmd := initRootCmd()
// err = autoCliOpts.EnhanceRootCommand(rootCmd)
func (appOptions AppOptions) EnhanceRootCommand(rootCmd *cobra.Command) error {
var addressPrefix string
if appOptions.AddressCodec != nil {
addressPrefix = appOptions.AddressCodec.GetAddressPrefix()
}

builder := &Builder{
Builder: flag.Builder{
AddressPrefix: addressPrefix,
AddressCodec: appOptions.AddressCodec,
GetClientConn: func() (grpc.ClientConnInterface, error) {
return client.GetClientQueryContext(rootCmd)
},
Expand Down
21 changes: 13 additions & 8 deletions client/v2/autocli/flag/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@ package flag

import (
"context"
"fmt"

reflectionv2alpha1 "cosmossdk.io/api/cosmos/base/reflection/v2alpha1"
"cosmossdk.io/core/address"
"google.golang.org/protobuf/reflect/protoreflect"

sdk "github.com/cosmos/cosmos-sdk/types"
addresscodec "github.com/cosmos/cosmos-sdk/codec/address"
)

type addressStringType struct{}

func (a addressStringType) NewValue(ctx context.Context, b *Builder) Value {
if b.AddressPrefix == "" {
if b.AddressCodec == nil {
conn, err := b.GetClientConn()
if err != nil {
panic(err)
Expand All @@ -25,18 +27,20 @@ func (a addressStringType) NewValue(ctx context.Context, b *Builder) Value {
if resp == nil || resp.Config == nil {
panic("bech32 account address prefix is not set")
}
b.AddressPrefix = resp.Config.Bech32AccountAddressPrefix

b.AddressCodec = addresscodec.NewBech32Codec(resp.Config.Bech32AccountAddressPrefix)
}
return &addressValue{addressPrefix: b.AddressPrefix}

return &addressValue{addressCodec: b.AddressCodec}
}

func (a addressStringType) DefaultValue() string {
return ""
}

type addressValue struct {
value string
addressPrefix string
value string
addressCodec address.Codec
}

func (a addressValue) Get(protoreflect.Value) (protoreflect.Value, error) {
Expand All @@ -49,10 +53,11 @@ 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 := sdk.GetFromBech32(s, a.addressPrefix)
_, err := a.addressCodec.StringToBytes(s)
if err != nil {
return err
return fmt.Errorf("invalid bech32 account address: %w", err)
}

a.value = s

return nil
Expand Down
6 changes: 4 additions & 2 deletions client/v2/autocli/flag/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"google.golang.org/protobuf/reflect/protodesc"
"google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/reflect/protoregistry"

"cosmossdk.io/core/address"
)

// Builder manages options for building pflag flags for protobuf messages.
Expand All @@ -23,8 +25,8 @@ type Builder struct {
messageFlagTypes map[protoreflect.FullName]Type
scalarFlagTypes map[string]Type

// AddressPrefix is the prefix for the address flag
AddressPrefix string
// AddressCodec is the address codec used for the address flag
AddressCodec address.Codec

// GetClientConn is the reflection client for the address flag
GetClientConn func() (grpc.ClientConnInterface, error)
Expand Down
2 changes: 1 addition & 1 deletion client/v2/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.20

require (
cosmossdk.io/api v0.4.1
cosmossdk.io/core v0.6.2-0.20230424115605-271a70eff50f
cosmossdk.io/core v0.6.2-0.20230423164157-c63dec0f7aab
cosmossdk.io/depinject v1.0.0-alpha.3
github.com/cockroachdb/errors v1.9.1
github.com/cosmos/cosmos-proto v1.0.0-beta.3
Expand Down
4 changes: 2 additions & 2 deletions client/v2/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ cosmossdk.io/api v0.4.1 h1:0ikaYM6GyxTYYcfBiyR8YnLCfhNnhKpEFnaSepCTmqg=
cosmossdk.io/api v0.4.1/go.mod h1:jR7k5ok90LxW2lFUXvd8Vpo/dr4PpiyVegxdm7b1ZdE=
cosmossdk.io/collections v0.1.0 h1:nzJGeiq32KnZroSrhB6rPifw4I85Cgmzw/YAmr4luv8=
cosmossdk.io/collections v0.1.0/go.mod h1:xbauc0YsbUF8qKMVeBZl0pFCunxBIhKN/WlxpZ3lBuo=
cosmossdk.io/core v0.6.2-0.20230424115605-271a70eff50f h1:RujZ3w96i8+FI9+3JgIj0Yc5tIUs4RM3mAVV0AVC9Xw=
cosmossdk.io/core v0.6.2-0.20230424115605-271a70eff50f/go.mod h1:ozChgjag+fc/pzA13FTT2+IPQ1e76NpQcZSoGJOj400=
cosmossdk.io/core v0.6.2-0.20230423164157-c63dec0f7aab h1:T5M7ZlsIoOi+ystyBuEzk19kh72bXbtfMqq6NEIkub8=
cosmossdk.io/core v0.6.2-0.20230423164157-c63dec0f7aab/go.mod h1:beJu7PuC+A7Os512U3gdsBrc3Qcax9XJnVE7jtiA+Gc=
cosmossdk.io/depinject v1.0.0-alpha.3 h1:6evFIgj//Y3w09bqOUOzEpFj5tsxBqdc5CfkO7z+zfw=
cosmossdk.io/depinject v1.0.0-alpha.3/go.mod h1:eRbcdQ7MRpIPEM5YUJh8k97nxHpYbc3sMUnEtt8HPWU=
cosmossdk.io/errors v1.0.0-beta.7 h1:gypHW76pTQGVnHKo6QBkb4yFOJjC+sUGRc5Al3Odj1w=
Expand Down

0 comments on commit 50abaac

Please sign in to comment.