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

Add debug commands #5244

Merged
merged 10 commits into from
Nov 11, 2019
178 changes: 178 additions & 0 deletions client/debug/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
package debug

import (
"encoding/base64"
"encoding/hex"
"fmt"
"strings"

"github.com/cosmos/cosmos-sdk/codec"

"strconv"
tac0turtle marked this conversation as resolved.
Show resolved Hide resolved

"github.com/cosmos/cosmos-sdk/client"
"github.com/spf13/cobra"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/tendermint/tendermint/crypto"
"github.com/tendermint/tendermint/crypto/ed25519"
)

func Cmd(cdc *codec.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "debug",
Short: "tool for helping with debugging your application",
tac0turtle marked this conversation as resolved.
Show resolved Hide resolved
RunE: client.ValidateCmd,
}

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

return cmd
}

tac0turtle marked this conversation as resolved.
Show resolved Hide resolved
func PubkeyCmd(cdc *codec.Codec) *cobra.Command {
alexanderbez marked this conversation as resolved.
Show resolved Hide resolved
return &cobra.Command{
Use: "pubkey",
Short: "Decode a pubkey from hex, base64, or bech32",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {

pubkeyString := args[0]
var pubKeyI crypto.PubKey

// try hex, then base64, then bech32
pubkeyBytes, err := hex.DecodeString(pubkeyString)
if err != nil {
var err2 error
pubkeyBytes, err2 = base64.StdEncoding.DecodeString(pubkeyString)
if err2 != nil {
var err3 error
pubKeyI, err3 = sdk.GetAccPubKeyBech32(pubkeyString)
if err3 != nil {
var err4 error
pubKeyI, err4 = sdk.GetValPubKeyBech32(pubkeyString)

if err4 != nil {
var err5 error
pubKeyI, err5 = sdk.GetConsPubKeyBech32(pubkeyString)
if err5 != nil {
return fmt.Errorf(`expected hex, base64, or bech32. Got errors:
hex: %v,
tac0turtle marked this conversation as resolved.
Show resolved Hide resolved
base64: %v
bech32 Acc: %v
bech32 Val: %v
bech32 Cons: %v`,
err, err2, err3, err4, err5)
}

}
}

}
}

var pubKey ed25519.PubKeyEd25519
if pubKeyI == nil {
copy(pubKey[:], pubkeyBytes)
} else {
pubKey = pubKeyI.(ed25519.PubKeyEd25519)
pubkeyBytes = pubKey[:]
}

pubKeyJSONBytes, err := cdc.MarshalJSON(pubKey)
if err != nil {
return err
}
accPub, err := sdk.Bech32ifyAccPub(pubKey)
if err != nil {
return err
}
valPub, err := sdk.Bech32ifyValPub(pubKey)
if err != nil {
return err
}

consenusPub, err := sdk.Bech32ifyConsPub(pubKey)
if err != nil {
return err
}
fmt.Println("Address:", pubKey.Address())
tac0turtle marked this conversation as resolved.
Show resolved Hide resolved
fmt.Printf("Hex: %X\n", pubkeyBytes)
tac0turtle marked this conversation as resolved.
Show resolved Hide resolved
fmt.Println("JSON (base64):", string(pubKeyJSONBytes))
fmt.Println("Bech32 Acc:", accPub)
fmt.Println("Bech32 Validator Operator:", valPub)
fmt.Println("Bech32 Validator Consensus:", consenusPub)
return nil
},
}
}

func AddrCmd() *cobra.Command {
return &cobra.Command{
Use: "addr",
Short: "Convert an address between hex and bech32",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {

addrString := args[0]
var addr []byte

// try hex, then bech32
var err error
addr, err = hex.DecodeString(addrString)
if err != nil {
var err2 error
addr, err2 = sdk.AccAddressFromBech32(addrString)
if err2 != nil {
var err3 error
addr, err3 = sdk.ValAddressFromBech32(addrString)

if err3 != nil {
return fmt.Errorf(`expected hex or bech32. Got errors:
hex: %v,
tac0turtle marked this conversation as resolved.
Show resolved Hide resolved
bech32 acc: %v
bech32 val: %v
`, err, err2, err3)

}
}
}

accAddr := sdk.AccAddress(addr)
valAddr := sdk.ValAddress(addr)

fmt.Println("Address:", addr)
tac0turtle marked this conversation as resolved.
Show resolved Hide resolved
fmt.Printf("Address (hex): %X\n", addr)
fmt.Printf("Bech32 Acc: %s\n", accAddr)
fmt.Printf("Bech32 Val: %s\n", valAddr)
return nil
},
}
}

func RawBytesCmd() *cobra.Command {
return &cobra.Command{
Use: "raw-bytes",
Short: "Convert raw bytes output (eg. [10 21 13 255]) to hex",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
stringBytes := args[0]
stringBytes = strings.Trim(stringBytes, "[")
stringBytes = strings.Trim(stringBytes, "]")
spl := strings.Split(stringBytes, " ")

byteArray := []byte{}
for _, s := range spl {
b, err := strconv.Atoi(s)
if err != nil {
return err
}
byteArray = append(byteArray, byte(b))
}
fmt.Printf("%X\n", byteArray)
return nil
},
}
}
53 changes: 53 additions & 0 deletions x/auth/client/cli/decode.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
package cli

import (
"bytes"
"encoding/base64"
"encoding/hex"
"encoding/json"
"fmt"

"github.com/spf13/cobra"
"github.com/tendermint/go-amino"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/x/auth/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
)

Expand Down Expand Up @@ -38,3 +44,50 @@ func GetDecodeCommand(codec *amino.Codec) *cobra.Command {

return client.PostCommands(cmd)[0]
}

// DecodeTxCmd - returns the command to decode a tx from hex or base64
func DecodeTxCmd(cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
Use: "tx",
Short: "Decode a tx from hex or base64",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {

txString := args[0]

// try hex, then base64
txBytes, err := hex.DecodeString(txString)
if err != nil {
var err2 error
txBytes, err2 = base64.StdEncoding.DecodeString(txString)
if err2 != nil {
return fmt.Errorf(`expected hex or base64. Got errors:
hex: %v,
base64: %v
`, err, err2)
}
}

var tx = types.StdTx{}

err = cdc.UnmarshalBinaryLengthPrefixed(txBytes, &tx)
if err != nil {
return err
}

bz, err := cdc.MarshalJSON(tx)
if err != nil {
return err
}

buf := bytes.NewBuffer([]byte{})
err = json.Indent(buf, bz, "", " ")
if err != nil {
tac0turtle marked this conversation as resolved.
Show resolved Hide resolved
return err
}

fmt.Println(buf.String())
return nil
},
}
}