Skip to content

Commit

Permalink
feat(client/v2): add support for pubkey (cosmos#19039)
Browse files Browse the repository at this point in the history
  • Loading branch information
atheeshp authored and relyt29 committed Jan 22, 2024
1 parent 4b184e0 commit 7eeea5c
Show file tree
Hide file tree
Showing 8 changed files with 234 additions and 155 deletions.
155 changes: 78 additions & 77 deletions api/cosmos/staking/v1beta1/tx.pulsar.go

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions client/v2/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
### Features

* [#18461](https://github.com/cosmos/cosmos-sdk/pull/18461) Support governance proposals.
* [#19039](https://github.com/cosmos/cosmos-sdk/pull/19039) add support for pubkey in autocli.

### API Breaking Changes

Expand Down
2 changes: 2 additions & 0 deletions client/v2/autocli/flag/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const (
AddressStringScalarType = "cosmos.AddressString"
ValidatorAddressStringScalarType = "cosmos.ValidatorAddressString"
ConsensusAddressStringScalarType = "cosmos.ConsensusAddressString"
PubkeyScalarType = "cosmos.Pubkey"
)

// Builder manages options for building pflag flags for protobuf messages.
Expand Down Expand Up @@ -71,6 +72,7 @@ func (b *Builder) init() {
b.scalarFlagTypes[AddressStringScalarType] = addressStringType{}
b.scalarFlagTypes[ValidatorAddressStringScalarType] = validatorAddressStringType{}
b.scalarFlagTypes[ConsensusAddressStringScalarType] = consensusAddressStringType{}
b.scalarFlagTypes[PubkeyScalarType] = pubkeyType{}
}
}

Expand Down
60 changes: 60 additions & 0 deletions client/v2/autocli/flag/pubkey.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package flag

import (
"context"
"fmt"

"google.golang.org/protobuf/reflect/protoreflect"

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

type pubkeyType struct{}

func (a pubkeyType) NewValue(_ context.Context, _ *Builder) Value {
return &pubkeyValue{}
}

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

type pubkeyValue struct {
value *types.Any
}

func (a pubkeyValue) Get(protoreflect.Value) (protoreflect.Value, error) {
return protoreflect.ValueOf(a.value), nil
}

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

func (a *pubkeyValue) Set(s string) error {
registry := types.NewInterfaceRegistry()
cryptocodec.RegisterInterfaces(registry)
cdc := codec.NewProtoCodec(registry)

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

any, err := types.NewAnyWithValue(pk)
if err != nil {
return fmt.Errorf("error converting to any type")
}

a.value = any

return nil
}

func (a pubkeyValue) Type() string {
return "pubkey"
}
6 changes: 6 additions & 0 deletions docs/build/building-modules/05-protobuf-annotations.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ Example of validator address string scalar:
https://github.com/cosmos/cosmos-sdk/blob/e8f28bf5db18b8d6b7e0d94b542ce4cf48fed9d6/proto/cosmos/distribution/v1beta1/query.proto#L87
```

Example of pubkey scalar:

```proto reference
https://github.com/cosmos/cosmos-sdk/blob/11068bfbcd44a7db8af63b6a8aa079b1718f6040/proto/cosmos/staking/v1beta1/tx.proto#L94
```

Example of Decimals scalar:

```proto reference
Expand Down
3 changes: 2 additions & 1 deletion proto/cosmos/staking/v1beta1/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,8 @@ message MsgRotateConsPubKey {
option (gogoproto.equal) = false;

string validator_address = 1 [(cosmos_proto.scalar) = "cosmos.ValidatorAddressString"];
google.protobuf.Any new_pubkey = 2 [(cosmos_proto.accepts_interface) = "cosmos.crypto.PubKey"];
google.protobuf.Any new_pubkey = 2
[(cosmos_proto.accepts_interface) = "cosmos.crypto.PubKey", (cosmos_proto.scalar) = "cosmos.PubKey"];
}

// MsgRotateConsPubKeyResponse defines the response structure for executing a
Expand Down
7 changes: 7 additions & 0 deletions x/staking/autocli.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,13 @@ func (am AppModule) AutoCLIOptions() *autocliv1.ModuleOptions {
Example: fmt.Sprintf(`%s tx staking cancel-unbond cosmosvaloper... 100stake 2 --from mykey`, version.AppName),
PositionalArgs: []*autocliv1.PositionalArgDescriptor{{ProtoField: "validator_address"}, {ProtoField: "amount"}, {ProtoField: "creation_height"}},
},
{
RpcMethod: "RotateConsPubKey",
Use: "rotate-cons-pubkey [validator-address] [new-pubkey]",
Short: fmt.Sprintf("rotate validator consensus pub key. Note: you have to replace the `~/.%sd/config/priv_validator_key.json` with new key and restart the node after rotating the key", version.AppName),
Example: fmt.Sprintf(`%s tx staking rotate-cons-pubkey myvalidator {"@type":"/cosmos.crypto.ed25519.PubKey","key":"oWg2ISpLF405Jcm2vXV+2v4fnjodh6aafuIdeoW+rUw="}`, version.AppName),
PositionalArgs: []*autocliv1.PositionalArgDescriptor{{ProtoField: "validator_address"}, {ProtoField: "new_pubkey"}},
},
{
RpcMethod: "UpdateParams",
Use: "update-params-proposal [params]",
Expand Down
155 changes: 78 additions & 77 deletions x/staking/types/tx.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 7eeea5c

Please sign in to comment.