Skip to content

Commit

Permalink
fix: add secp256k1 format support to debug pubkey command (#113)
Browse files Browse the repository at this point in the history
  • Loading branch information
daeMOn63 authored Nov 24, 2021
1 parent 3e017e7 commit 2516b5f
Showing 1 changed file with 55 additions and 23 deletions.
78 changes: 55 additions & 23 deletions client/debug/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"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"
Expand All @@ -37,15 +38,25 @@ func Cmd() *cobra.Command {
func getPubKeyFromString(pkstr string) (cryptotypes.PubKey, error) {
bz, err := hex.DecodeString(pkstr)
if err == nil {
if len(bz) == ed25519.PubKeySize {
switch len(bz) {
case ed25519.PubKeySize:
return &ed25519.PubKey{Key: bz}, nil
case secp256k1.PubKeySize:
return &secp256k1.PubKey{Key: bz}, nil
default:
return nil, fmt.Errorf("unsupported pubkey size")
}
}

bz, err = base64.StdEncoding.DecodeString(pkstr)
if err == nil {
if len(bz) == ed25519.PubKeySize {
switch len(bz) {
case ed25519.PubKeySize:
return &ed25519.PubKey{Key: bz}, nil
case secp256k1.PubKeySize:
return &secp256k1.PubKey{Key: bz}, nil
default:
return nil, fmt.Errorf("unsupported pubkey size")
}
}

Expand Down Expand Up @@ -86,30 +97,51 @@ $ %s debug pubkey cosmos1e0jnq2sun3dzjh8p2xq95kk0expwmd7shwjpfg
return err
}

edPK, ok := pk.(*ed25519.PubKey)
if !ok {
return errors.Wrapf(errors.ErrInvalidType, "invalid pubkey type; expected ED25519")
}
var pubKeyJSONBytes, key []byte
var accPub, valPub, consenusPub string

pubKeyJSONBytes, err := clientCtx.LegacyAmino.MarshalJSON(edPK)
if err != nil {
return err
}
accPub, err := sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeAccPub, edPK)
if err != nil {
return err
}
valPub, err := sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeValPub, edPK)
if err != nil {
return err
}
consenusPub, err := sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeConsPub, edPK)
if err != nil {
return err
if edPK, ok := pk.(*ed25519.PubKey); ok {
pubKeyJSONBytes, err = clientCtx.LegacyAmino.MarshalJSON(edPK)
if err != nil {
return err
}
accPub, err = sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeAccPub, edPK)
if err != nil {
return err
}
valPub, err = sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeValPub, edPK)
if err != nil {
return err
}
consenusPub, err = sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeConsPub, edPK)
if err != nil {
return err
}
key = edPK.Key
} else if secpPK, ok := pk.(*secp256k1.PubKey); ok {
pubKeyJSONBytes, err = clientCtx.LegacyAmino.MarshalJSON(secpPK)
if err != nil {
return err
}
accPub, err = sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeAccPub, secpPK)
if err != nil {
return err
}
valPub, err = sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeValPub, secpPK)
if err != nil {
return err
}
consenusPub, err = sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeConsPub, secpPK)
if err != nil {
return err
}
key = secpPK.Key
} else {
return errors.Wrapf(errors.ErrInvalidType, "invalid pubkey type; expected ED25519 or Secp256k1")
}

cmd.Println("Address:", edPK.Address())
cmd.Printf("Hex: %X\n", edPK.Key)
cmd.Println("Address:", pk.Address())
cmd.Printf("Hex: %X\n", key)
cmd.Println("JSON (base64):", string(pubKeyJSONBytes))
cmd.Println("Bech32 Acc:", accPub)
cmd.Println("Bech32 Validator Operator:", valPub)
Expand Down

0 comments on commit 2516b5f

Please sign in to comment.