Use public keys consistently for validators and all addresses are for accounts

This commit is contained in:
Zaki Manian 2018-05-27 14:21:15 +02:00
parent f33f49a840
commit a163a3558f
3 changed files with 33 additions and 18 deletions

View File

@ -40,20 +40,20 @@ func ShowValidatorCmd(ctx *Context) *cobra.Command {
cfg := ctx.Config
privValidator := pvm.LoadOrGenFilePV(cfg.PrivValidatorFile())
valAddr := sdk.Address(privValidator.PubKey.Address())
valPubKey := privValidator.PubKey
if viper.GetBool(flagJSON) {
cdc := wire.NewCodec()
wire.RegisterCrypto(cdc)
pubKeyJSONBytes, err := cdc.MarshalJSON(valAddr)
pubKeyJSONBytes, err := cdc.MarshalJSON(valPubKey)
if err != nil {
return err
}
fmt.Println(string(pubKeyJSONBytes))
return nil
}
addr, err := sdk.Bech32CosmosifyVal(valAddr)
addr, err := sdk.Bech32CosmosifyValPub(valPubKey)
if err != nil {
return err
}

View File

@ -63,7 +63,7 @@ func GetAccAddressBech32Cosmos(address string) (addr Address, err error) {
return Address(bz), nil
}
// create an Address from a string
// create an Address from a hex string
func GetValAddressHex(address string) (addr Address, err error) {
if len(address) == 0 {
return addr, errors.New("must use provide address")
@ -75,7 +75,7 @@ func GetValAddressHex(address string) (addr Address, err error) {
return Address(bz), nil
}
// create an Address from a string
// create an Address from a bech32cosmos string
func GetValAddressBech32Cosmos(address string) (addr Address, err error) {
if len(address) == 0 {
return addr, errors.New("must use provide address")
@ -92,3 +92,26 @@ func GetValAddressBech32Cosmos(address string) (addr Address, err error) {
}
return Address(bz), nil
}
//Decode a validator publickey into a public key
func GetValPubKeyBech32Cosmos(pubkey string) (pk crypto.PubKey, err error) {
if len(pubkey) == 0 {
return pk, errors.New("must use provide pubkey")
}
hrp, bz, err := bech32cosmos.DecodeAndConvert(pubkey)
if hrp != "cosmosvalpub" {
return pk, fmt.Errorf("Invalid Validator Pubkey Prefix. Expected cosmosvalpub, Got %s", hrp)
}
if err != nil {
return nil, err
}
pk, err = crypto.PubKeyFromBytes(bz)
if err != nil {
return nil, err
}
return pk, nil
}

View File

@ -1,14 +1,11 @@
package cli
import (
"encoding/hex"
"fmt"
"github.com/spf13/cobra"
"github.com/spf13/viper"
crypto "github.com/tendermint/go-crypto"
"github.com/cosmos/cosmos-sdk/client/context"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/wire"
@ -28,7 +25,7 @@ func GetCmdDeclareCandidacy(cdc *wire.Codec) *cobra.Command {
if err != nil {
return err
}
validatorAddr, err := sdk.GetValAddressBech32Cosmos(viper.GetString(FlagAddressValidator))
validatorAddr, err := sdk.GetAccAddressBech32Cosmos(viper.GetString(FlagAddressValidator))
if err != nil {
return err
}
@ -37,15 +34,10 @@ func GetCmdDeclareCandidacy(cdc *wire.Codec) *cobra.Command {
if len(pkStr) == 0 {
return fmt.Errorf("must use --pubkey flag")
}
pkBytes, err := hex.DecodeString(pkStr)
pk, err := sdk.GetValPubKeyBech32Cosmos(pkStr)
if err != nil {
return err
}
pk, err := crypto.PubKeyFromBytes(pkBytes)
if err != nil {
return err
}
if viper.GetString(FlagMoniker) == "" {
return fmt.Errorf("please enter a moniker for the validator using --moniker")
}
@ -82,7 +74,7 @@ func GetCmdEditCandidacy(cdc *wire.Codec) *cobra.Command {
Short: "edit and existing validator account",
RunE: func(cmd *cobra.Command, args []string) error {
validatorAddr, err := sdk.GetValAddressBech32Cosmos(viper.GetString(FlagAddressValidator))
validatorAddr, err := sdk.GetAccAddressBech32Cosmos(viper.GetString(FlagAddressValidator))
if err != nil {
return err
}
@ -124,7 +116,7 @@ func GetCmdDelegate(cdc *wire.Codec) *cobra.Command {
}
delegatorAddr, err := sdk.GetAccAddressBech32Cosmos(viper.GetString(FlagAddressDelegator))
validatorAddr, err := sdk.GetValAddressBech32Cosmos(viper.GetString(FlagAddressValidator))
validatorAddr, err := sdk.GetAccAddressBech32Cosmos(viper.GetString(FlagAddressValidator))
if err != nil {
return err
}
@ -172,7 +164,7 @@ func GetCmdUnbond(cdc *wire.Codec) *cobra.Command {
}
delegatorAddr, err := sdk.GetAccAddressBech32Cosmos(viper.GetString(FlagAddressDelegator))
validatorAddr, err := sdk.GetValAddressBech32Cosmos(viper.GetString(FlagAddressValidator))
validatorAddr, err := sdk.GetAccAddressBech32Cosmos(viper.GetString(FlagAddressValidator))
if err != nil {
return err
}