Skip to content

Commit

Permalink
feat(client/v2): support consensus address decoding (backport #16927) (
Browse files Browse the repository at this point in the history
…#16997)

Co-authored-by: Julien Robert <[email protected]>
  • Loading branch information
mergify[bot] and julienrbrt authored Jul 14, 2023
1 parent d2b61d0 commit 1018f53
Show file tree
Hide file tree
Showing 20 changed files with 561 additions and 70 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.50.0-alpha.1.0.20230713153355-bc7221f5c5f4
github.com/cosmos/cosmos-sdk v0.50.0-alpha.1.0.20230713222737-d2b61d0bf557
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.50.0-alpha.1.0.20230713153355-bc7221f5c5f4 h1:7W1dwR+b81odxtPX3LrRHjsBun+9EbMzrSi7rFeEdNY=
github.com/cosmos/cosmos-sdk v0.50.0-alpha.1.0.20230713153355-bc7221f5c5f4/go.mod h1:6RlLvQl5prjf/lX8lugfXT/YYSQS8fB5Xj/5mdKjCYA=
github.com/cosmos/cosmos-sdk v0.50.0-alpha.1.0.20230713222737-d2b61d0bf557 h1:+n9s0LLVKHaff9WPC7NZuzsG8lcFsZfpmaCIbnyqGIU=
github.com/cosmos/cosmos-sdk v0.50.0-alpha.1.0.20230713222737-d2b61d0bf557/go.mod h1:dZ6guNQwL28MFTnKS8sCLM04cHDQ3iivrGRRtKj0Q2c=
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 1018f53

Please sign in to comment.