Skip to content

Commit

Permalink
feat(client/v2): support consensus address decoding (#16927)
Browse files Browse the repository at this point in the history
  • Loading branch information
julienrbrt authored Jul 14, 2023
1 parent d0661d8 commit 90b3907
Show file tree
Hide file tree
Showing 16 changed files with 214 additions and 57 deletions.
2 changes: 2 additions & 0 deletions client/v2/autocli/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type AppOptions struct {
// AddressCodec is the address codec to use for the app.
AddressCodec address.Codec
ValidatorAddressCodec stakingtypes.ValidatorAddressCodec
ConsensusAddressCodec stakingtypes.ConsensusAddressCodec
}

// EnhanceRootCommand enhances the provided root command with autocli AppOptions,
Expand All @@ -64,6 +65,7 @@ func (appOptions AppOptions) EnhanceRootCommand(rootCmd *cobra.Command) error {
FileResolver: proto.HybridResolver,
AddressCodec: appOptions.AddressCodec,
ValidatorAddressCodec: appOptions.ValidatorAddressCodec,
ConsensusAddressCodec: appOptions.ConsensusAddressCodec,
},
GetClientConn: func(cmd *cobra.Command) (grpc.ClientConnInterface, error) {
return client.GetClientQueryContext(cmd)
Expand Down
4 changes: 4 additions & 0 deletions client/v2/autocli/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ func (b *Builder) Validate() error {
return errors.New("validator address codec is required in builder")
}

if b.ConsensusAddressCodec == nil {
return errors.New("consensus address codec is required in builder")
}

if b.TypeResolver == nil {
return errors.New("type resolver is required in builder")
}
Expand Down
1 change: 1 addition & 0 deletions client/v2/autocli/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ func initFixture(t *testing.T) *fixture {
FileResolver: protoregistry.GlobalFiles,
AddressCodec: addresscodec.NewBech32Codec("cosmos"),
ValidatorAddressCodec: addresscodec.NewBech32Codec("cosmosvaloper"),
ConsensusAddressCodec: addresscodec.NewBech32Codec("cosmosvalcons"),
},
GetClientConn: func(*cobra.Command) (grpc.ClientConnInterface, error) {
return conn, nil
Expand Down
50 changes: 50 additions & 0 deletions client/v2/autocli/flag/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ import (
"google.golang.org/protobuf/reflect/protoreflect"

"cosmossdk.io/core/address"

"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/codec/types"
cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
sdk "github.com/cosmos/cosmos-sdk/types"
)

type addressStringType struct{}
Expand Down Expand Up @@ -57,3 +63,47 @@ func (a *addressValue) Set(s string) error {
func (a addressValue) Type() string {
return "bech32 account address key name"
}

type consensusAddressStringType struct{}

func (a consensusAddressStringType) NewValue(ctx context.Context, b *Builder) Value {
return &consensusAddressValue{addressValue: addressValue{addressCodec: b.ConsensusAddressCodec}}
}

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

type consensusAddressValue struct {
addressValue
}

func (a consensusAddressValue) Get(protoreflect.Value) (protoreflect.Value, error) {
return protoreflect.ValueOfString(a.value), nil
}

func (a consensusAddressValue) String() string {
return a.value
}

func (a *consensusAddressValue) Set(s string) error {
_, err := a.addressCodec.StringToBytes(s)
if err == nil {
a.value = s
return nil
}

// fallback to pubkey parsing
registry := types.NewInterfaceRegistry()
cryptocodec.RegisterInterfaces(registry)
cdc := codec.NewProtoCodec(registry)

var pk cryptotypes.PubKey
err2 := cdc.UnmarshalInterfaceJSON([]byte(s), &pk)
if err2 != nil {
return fmt.Errorf("input isn't a pubkey %w or is invalid bech32 account address: %w", err, err2)
}

a.value = sdk.ConsAddress(pk.Address()).String()
return nil
}
2 changes: 2 additions & 0 deletions client/v2/autocli/flag/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type Builder struct {
// AddressCodec is the address codec used for the address flag
AddressCodec address.Codec
ValidatorAddressCodec address.Codec
ConsensusAddressCodec address.Codec
}

func (b *Builder) init() {
Expand All @@ -41,6 +42,7 @@ func (b *Builder) init() {
b.scalarFlagTypes = map[string]Type{}
b.scalarFlagTypes["cosmos.AddressString"] = addressStringType{}
b.scalarFlagTypes["cosmos.ValidatorAddressString"] = validatorAddressStringType{}
b.scalarFlagTypes["cosmos.ConsensusAddressString"] = consensusAddressStringType{}
}
}

Expand Down
15 changes: 15 additions & 0 deletions client/v2/autocli/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,8 @@ func TestEverything(t *testing.T) {
"--timestamp", "2019-01-02T00:01:02Z",
"--a-coin", "100000foo",
"--an-address", "cosmos1y74p8wyy4enfhfn342njve6cjmj5c8dtl6emdk",
"--a-validator-address", "cosmosvaloper1tnh2q55v8wyygtt9srz5safamzdengsn9dsd7z",
"--a-consensus-address", "cosmosvalcons16vm0nx49eam4q0xasdnwdzsdl6ymgyjt757sgr",
"--bz", "c2RncXdlZndkZ3NkZw==",
"--page-count-total",
"--page-key", "MTIzNTQ4N3NnaGRhcw==",
Expand Down Expand Up @@ -358,6 +360,19 @@ func TestEverything(t *testing.T) {
assert.DeepEqual(t, fixture.conn.lastRequest, fixture.conn.lastResponse.(*testpb.EchoResponse).Request, protocmp.Transform())
}

func TestPubKeyParsingConsensusAddress(t *testing.T) {
fixture := initFixture(t)

_, err := runCmd(fixture.conn, fixture.b, buildModuleQueryCommand,
"echo",
"1", "abc", "1foo",
"--a-consensus-address", "{\"@type\":\"/cosmos.crypto.ed25519.PubKey\",\"key\":\"j8qdbR+AlH/V6aBTCSWXRvX3JUESF2bV+SEzndBhF0o=\"}",
"-u", "27", // shorthand
)
assert.NilError(t, err)
assert.DeepEqual(t, fixture.conn.lastRequest, fixture.conn.lastResponse.(*testpb.EchoResponse).Request, protocmp.Transform())
}

func TestJSONParsing(t *testing.T) {
fixture := initFixture(t)

Expand Down
1 change: 1 addition & 0 deletions client/v2/autocli/testdata/help-deprecated.golden
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Usage:
Flags:
--a-bool
--a-coin cosmos.base.v1beta1.Coin
--a-consensus-address bech32 account address key name
--a-message testpb.AMessage (json)
--a-validator-address bech32 account address key name
--an-address bech32 account address key name
Expand Down
1 change: 1 addition & 0 deletions client/v2/autocli/testdata/help-echo.golden
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ echo 1 abc {}
Flags:
--a-bool
--a-coin cosmos.base.v1beta1.Coin some random coin
--a-consensus-address bech32 account address key name
--a-message testpb.AMessage (json)
--a-validator-address bech32 account address key name
--an-address bech32 account address key name
Expand Down
2 changes: 1 addition & 1 deletion client/v2/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require (
cosmossdk.io/depinject v1.0.0-alpha.3
github.com/cockroachdb/errors v1.10.0
github.com/cosmos/cosmos-proto v1.0.0-beta.3
github.com/cosmos/cosmos-sdk v0.46.0-beta2.0.20230712155822-3b269baa9c90
github.com/cosmos/cosmos-sdk v0.46.0-beta2.0.20230714071224-3dd1fabfc2b1
github.com/cosmos/gogoproto v1.4.10
github.com/spf13/cobra v1.7.0
github.com/spf13/pflag v1.0.5
Expand Down
4 changes: 2 additions & 2 deletions client/v2/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,8 @@ github.com/cosmos/cosmos-db v1.0.0 h1:EVcQZ+qYag7W6uorBKFPvX6gRjw6Uq2hIh4hCWjuQ0
github.com/cosmos/cosmos-db v1.0.0/go.mod h1:iBvi1TtqaedwLdcrZVYRSSCb6eSy61NLj4UNmdIgs0U=
github.com/cosmos/cosmos-proto v1.0.0-beta.3 h1:VitvZ1lPORTVxkmF2fAp3IiA61xVwArQYKXTdEcpW6o=
github.com/cosmos/cosmos-proto v1.0.0-beta.3/go.mod h1:t8IASdLaAq+bbHbjq4p960BvcTqtwuAxid3b/2rOD6I=
github.com/cosmos/cosmos-sdk v0.46.0-beta2.0.20230712155822-3b269baa9c90 h1:DBxe7gSu1/UPsSqsimostzeis6P732bBTbxG511s/WA=
github.com/cosmos/cosmos-sdk v0.46.0-beta2.0.20230712155822-3b269baa9c90/go.mod h1:L/QMvN62eJFdeaf5AlQPI3g4dSwwsdPA5YCnMbxCvjc=
github.com/cosmos/cosmos-sdk v0.46.0-beta2.0.20230714071224-3dd1fabfc2b1 h1:f5esYd6KIl9WytiRitKEAxG2hpf6UzhGl6RtAVzeAMM=
github.com/cosmos/cosmos-sdk v0.46.0-beta2.0.20230714071224-3dd1fabfc2b1/go.mod h1:LME6v5XztqVK7/1uTQj/G6ZJdosJEz24rKaPYk+WbqI=
github.com/cosmos/go-bip39 v1.0.0 h1:pcomnQdrdH22njcAatO0yWojsUnCO3y2tNoV1cb6hHY=
github.com/cosmos/go-bip39 v1.0.0/go.mod h1:RNJv0H/pOIVgxw6KS7QeX2a0Uo0aKUlfhZ4xuwvCdJw=
github.com/cosmos/gogogateway v1.2.0 h1:Ae/OivNhp8DqBi/sh2A8a1D0y638GpL3tkmLQAiKxTE=
Expand Down
1 change: 1 addition & 0 deletions client/v2/internal/testpb/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ message EchoRequest {
map<string, uint32> map_string_uint32 = 34;
map<string, cosmos.base.v1beta1.Coin> map_string_coin = 35;
string a_validator_address = 36 [(cosmos_proto.scalar) = "cosmos.ValidatorAddressString"];
string a_consensus_address = 37 [(cosmos_proto.scalar) = "cosmos.ConsensusAddressString"];
}

enum Enum {
Expand Down
Loading

0 comments on commit 90b3907

Please sign in to comment.