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: Add debug pubkey-raw cli command #11006

Merged
merged 5 commits into from
Feb 14, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
* [\#10311](https://github.com/cosmos/cosmos-sdk/pull/10311) Adds cli to use tips transactions. It adds an `--aux` flag to all CLI tx commands to generate the aux signer data (with optional tip), and a new `tx aux-to-fee` subcommand to let the fee payer gather aux signer data and broadcast the tx
* [\#10430](https://github.com/cosmos/cosmos-sdk/pull/10430) ADR-040: Add store/v2 `MultiStore` implementation
* [\#10947](https://github.com/cosmos/cosmos-sdk/pull/10947) Add `AllowancesByGranter` query to the feegrant module
* [\#11006](https://github.com/cosmos/cosmos-sdk/pull/11006) Add `debug pubkey-raw` command to allow inspecting of pubkeys in legacy bech32 format

### API Breaking Changes

Expand Down
85 changes: 85 additions & 0 deletions client/debug/legacybech32pubkey/bech32pubkey.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package bech32pubkey
sunnya97 marked this conversation as resolved.
Show resolved Hide resolved

import (
"github.com/cosmos/cosmos-sdk/codec/legacy"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/bech32"

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

// Bech32PubKeyType defines a string type alias for a Bech32 public key type.
type Bech32PubKeyType string

// Bech32 conversion constants
const (
Bech32PubKeyTypeAccPub Bech32PubKeyType = "accpub"
Bech32PubKeyTypeValPub Bech32PubKeyType = "valpub"
Bech32PubKeyTypeConsPub Bech32PubKeyType = "conspub"
)

// Bech32ifyPubKey returns a Bech32 encoded string containing the appropriate
// prefix based on the key type provided for a given PublicKey.
// TODO: Remove Bech32ifyPubKey and all usages (cosmos/cosmos-sdk/issues/#7357)
func Bech32ifyPubKey(pkt Bech32PubKeyType, pubkey cryptotypes.PubKey) (string, error) {
var bech32Prefix string

switch pkt {
case Bech32PubKeyTypeAccPub:
bech32Prefix = sdk.GetConfig().GetBech32AccountPubPrefix()

case Bech32PubKeyTypeValPub:
bech32Prefix = sdk.GetConfig().GetBech32ValidatorPubPrefix()

case Bech32PubKeyTypeConsPub:
bech32Prefix = sdk.GetConfig().GetBech32ConsensusPubPrefix()

}

return bech32.ConvertAndEncode(bech32Prefix, legacy.Cdc.Amino.MustMarshalBinaryBare(pubkey))
}

// MustBech32ifyPubKey calls Bech32ifyPubKey except it panics on error.
func MustBech32ifyPubKey(pkt Bech32PubKeyType, pubkey cryptotypes.PubKey) string {
res, err := Bech32ifyPubKey(pkt, pubkey)
if err != nil {
panic(err)
}

return res
}

// GetPubKeyFromBech32 returns a PublicKey from a bech32-encoded PublicKey with
// a given key type.
func GetPubKeyFromBech32(pkt Bech32PubKeyType, pubkeyStr string) (cryptotypes.PubKey, error) {
var bech32Prefix string

switch pkt {
case Bech32PubKeyTypeAccPub:
bech32Prefix = sdk.GetConfig().GetBech32AccountPubPrefix()

case Bech32PubKeyTypeValPub:
bech32Prefix = sdk.GetConfig().GetBech32ValidatorPubPrefix()

case Bech32PubKeyTypeConsPub:
bech32Prefix = sdk.GetConfig().GetBech32ConsensusPubPrefix()

}

bz, err := sdk.GetFromBech32(pubkeyStr, bech32Prefix)
if err != nil {
return nil, err
}

return legacy.PubKeyFromBytes(bz)
}

// MustGetPubKeyFromBech32 calls GetPubKeyFromBech32 except it panics on error.
func MustGetPubKeyFromBech32(pkt Bech32PubKeyType, pubkeyStr string) cryptotypes.PubKey {
res, err := GetPubKeyFromBech32(pkt, pubkeyStr)
if err != nil {
panic(err)
}

return res
}
130 changes: 130 additions & 0 deletions client/debug/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package debug

import (
"encoding/base64"
"encoding/hex"
"fmt"
"strconv"
Expand All @@ -9,9 +10,18 @@ import (
"github.com/spf13/cobra"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/version"

legacybech32prefix "github.com/cosmos/cosmos-sdk/client/debug/legacybech32pubkey"
)

var (
flagPubkeyType = "type"
)

// Cmd creates a main CLI command
Expand All @@ -23,6 +33,7 @@ func Cmd() *cobra.Command {
}

cmd.AddCommand(PubkeyCmd())
cmd.AddCommand(PubkeyRawCmd())
cmd.AddCommand(AddrCmd())
cmd.AddCommand(RawBytesCmd())

Expand Down Expand Up @@ -59,6 +70,125 @@ $ %s debug pubkey '{"@type":"/cosmos.crypto.secp256k1.PubKey","key":"AurroA7jvfP
}
}

func bytesToPubkey(bz []byte, keytype string) (cryptotypes.PubKey, bool) {
if keytype == "ed25519" {
if len(bz) == ed25519.PubKeySize {
return &ed25519.PubKey{Key: bz}, true
}
}

if len(bz) == secp256k1.PubKeySize {
return &secp256k1.PubKey{Key: bz}, true
}
return nil, false
}

// getPubKeyFromRawString returns a PubKey (PubKeyEd25519 or PubKeySecp256k1) by attempting
// to decode the pubkey string from hex, base64, and finally bech32. If all
// encodings fail, an error is returned.
func getPubKeyFromRawString(pkstr string, keytype string) (cryptotypes.PubKey, error) {
// Try hex decoding
bz, err := hex.DecodeString(pkstr)
if err == nil {
pk, ok := bytesToPubkey(bz, keytype)
if ok {
return pk, nil
}
}

bz, err = base64.StdEncoding.DecodeString(pkstr)
if err == nil {
pk, ok := bytesToPubkey(bz, keytype)
if ok {
return pk, nil
}
}

pk, err := legacybech32prefix.GetPubKeyFromBech32(legacybech32prefix.Bech32PubKeyTypeAccPub, pkstr)
if err == nil {
return pk, nil
}

pk, err = legacybech32prefix.GetPubKeyFromBech32(legacybech32prefix.Bech32PubKeyTypeValPub, pkstr)
if err == nil {
return pk, nil
}

pk, err = legacybech32prefix.GetPubKeyFromBech32(legacybech32prefix.Bech32PubKeyTypeConsPub, pkstr)
if err == nil {
return pk, nil
}

return nil, fmt.Errorf("pubkey '%s' invalid; expected hex, base64, or bech32 of correct size", pkstr)
}

func PubkeyRawCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "pubkey-raw [pubkey] -t [{ed25519, secp256k1}]",
Short: "Decode a ED25519 or secp256k1 pubkey from hex, base64, or bech32",
Long: fmt.Sprintf(`Decode a pubkey from hex, base64, or bech32.
Example:
$ %s debug pubkey-raw TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlz
$ %s debug pubkey-raw cosmos1e0jnq2sun3dzjh8p2xq95kk0expwmd7shwjpfg
`, version.AppName, version.AppName),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx := client.GetClientContextFromCmd(cmd)

pubkeyType, err := cmd.Flags().GetString(flagPubkeyType)
if err != nil {
return err
}
pubkeyType = strings.ToLower(pubkeyType)
if pubkeyType != "secp256k1" && pubkeyType != "ed25519" {
return errors.Wrapf(errors.ErrInvalidType, "invalid pubkey type, expected oneof ed25519 or secp256k1")
}

pk, err := getPubKeyFromRawString(args[0], pubkeyType)
if err != nil {
return err
}

var consensusPub string
edPK, ok := pk.(*ed25519.PubKey)
if ok && pubkeyType == "ed25519" {
consensusPub, err = legacybech32prefix.Bech32ifyPubKey(legacybech32prefix.Bech32PubKeyTypeConsPub, edPK)
if err != nil {
return err
}

cmd.Printf("Hex: %X\n", edPK.Key)
}
cmd.Println("Parsed key as", pk.Type())

pubKeyJSONBytes, err := clientCtx.LegacyAmino.MarshalJSON(pk)
if err != nil {
return err
}
accPub, err := legacybech32prefix.Bech32ifyPubKey(legacybech32prefix.Bech32PubKeyTypeAccPub, pk)
if err != nil {
return err
}
valPub, err := legacybech32prefix.Bech32ifyPubKey(legacybech32prefix.Bech32PubKeyTypeValPub, pk)
if err != nil {
return err
}
cmd.Println("Address:", pk.Address())
cmd.Println("JSON (base64):", string(pubKeyJSONBytes))
cmd.Println("Bech32 Acc:", accPub)
cmd.Println("Bech32 Validator Operator:", valPub)
if pubkeyType == "ed25519" {

cmd.Println("Bech32 Validator Consensus:", consensusPub)
}

return nil
},
}
cmd.Flags().StringP(flagPubkeyType, "t", "ed25519", "Pubkey type to decode (oneof secp256k1, ed25519)")
return cmd
}

func AddrCmd() *cobra.Command {
return &cobra.Command{
Use: "addr [address]",
Expand Down