Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(client/v2): add support for pubkey #19039

Merged
merged 12 commits into from
Jan 12, 2024
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"
julienrbrt marked this conversation as resolved.
Show resolved Hide resolved
)

// 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
64 changes: 64 additions & 0 deletions client/v2/autocli/flag/pubkey.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package flag

import (
"context"
"fmt"

"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"
"google.golang.org/protobuf/reflect/protoreflect"
)

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 {
// fallback to pubkey parsing
atheeshp marked this conversation as resolved.
Show resolved Hide resolved
registry := types.NewInterfaceRegistry()
cryptocodec.RegisterInterfaces(registry)
cdc := codec.NewProtoCodec(registry)

var pk cryptotypes.PubKey
err2 := cdc.UnmarshalInterfaceJSON([]byte(s), &pk)
atheeshp marked this conversation as resolved.
Show resolved Hide resolved
if err2 != nil {
return fmt.Errorf("input isn't a pubkey, or is an invalid account address: %w", err2)
atheeshp marked this conversation as resolved.
Show resolved Hide resolved
}

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

// a.value, err = a.addressCodec.BytesToString(pk.Address())
atheeshp marked this conversation as resolved.
Show resolved Hide resolved
// if err != nil {
// return fmt.Errorf("invalid pubkey address: %w", err)
// }

return nil
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The method Set in pubkeyValue struct is responsible for parsing the public key from a string and setting it to the value field. The error message in line 45 has been updated to include both cases of invalid public key and invalid account address, which is a good practice for error handling. However, the commented-out code from lines 54-57 should be removed if it's no longer needed to avoid confusion.


func (a pubkeyValue) Type() string {
return "pubkey"
}
2 changes: 1 addition & 1 deletion proto/cosmos/staking/v1beta1/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ 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.scalar) = "cosmos.PubKey"];
atheeshp marked this conversation as resolved.
Show resolved Hide resolved
}

// 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: "rotate validator consensus pub key. Note: you have to replace the `~/.simapp/config/priv_validator_key.json` with new key and restart the node after rotating the key ",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's use version.AppName+"d" instead of simapp

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
Loading