github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/libs/cosmos-sdk/client/debug/main.go (about)

     1  package debug
     2  
     3  import (
     4  	"encoding/base64"
     5  	"encoding/hex"
     6  	"fmt"
     7  	"strconv"
     8  	"strings"
     9  
    10  	"github.com/spf13/cobra"
    11  
    12  	"github.com/fibonacci-chain/fbc/libs/tendermint/crypto"
    13  	"github.com/fibonacci-chain/fbc/libs/tendermint/crypto/ed25519"
    14  
    15  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/client"
    16  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/codec"
    17  	sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types"
    18  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/version"
    19  )
    20  
    21  func Cmd(cdc *codec.Codec) *cobra.Command {
    22  	cmd := &cobra.Command{
    23  		Use:   "debug",
    24  		Short: "Tool for helping with debugging your application",
    25  		RunE:  client.ValidateCmd,
    26  	}
    27  
    28  	cmd.AddCommand(PubkeyCmd(cdc))
    29  	cmd.AddCommand(AddrCmd())
    30  	cmd.AddCommand(RawBytesCmd())
    31  
    32  	return cmd
    33  }
    34  
    35  // getPubKeyFromString returns a Tendermint PubKey (PubKeyEd25519) by attempting
    36  // to decode the pubkey string from hex, base64, and finally bech32. If all
    37  // encodings fail, an error is returned.
    38  func getPubKeyFromString(pkstr string) (crypto.PubKey, error) {
    39  	var pubKey ed25519.PubKeyEd25519
    40  
    41  	bz, err := hex.DecodeString(pkstr)
    42  	if err == nil {
    43  		copy(pubKey[:], bz)
    44  		return pubKey, nil
    45  	}
    46  
    47  	bz, err = base64.StdEncoding.DecodeString(pkstr)
    48  	if err == nil {
    49  		copy(pubKey[:], bz)
    50  		return pubKey, nil
    51  	}
    52  
    53  	pk, err := sdk.GetPubKeyFromBech32(sdk.Bech32PubKeyTypeAccPub, pkstr)
    54  	if err == nil {
    55  		return pk, nil
    56  	}
    57  
    58  	pk, err = sdk.GetPubKeyFromBech32(sdk.Bech32PubKeyTypeValPub, pkstr)
    59  	if err == nil {
    60  		return pk, nil
    61  	}
    62  
    63  	pk, err = sdk.GetPubKeyFromBech32(sdk.Bech32PubKeyTypeConsPub, pkstr)
    64  	if err == nil {
    65  		return pk, nil
    66  	}
    67  
    68  	return nil, fmt.Errorf("pubkey '%s' invalid; expected hex, base64, or bech32", pubKey)
    69  }
    70  
    71  func PubkeyCmd(cdc *codec.Codec) *cobra.Command {
    72  	return &cobra.Command{
    73  		Use:   "pubkey [pubkey]",
    74  		Short: "Decode a ED25519 pubkey from hex, base64, or bech32",
    75  		Long: fmt.Sprintf(`Decode a pubkey from hex, base64, or bech32.
    76  
    77  Example:
    78  $ %s debug pubkey TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlz
    79  $ %s debug pubkey cosmos1e0jnq2sun3dzjh8p2xq95kk0expwmd7shwjpfg
    80  			`, version.ClientName, version.ClientName),
    81  		Args: cobra.ExactArgs(1),
    82  		RunE: func(cmd *cobra.Command, args []string) error {
    83  			pk, err := getPubKeyFromString(args[0])
    84  			if err != nil {
    85  				return err
    86  			}
    87  
    88  			edPK, ok := pk.(ed25519.PubKeyEd25519)
    89  			if !ok {
    90  				return fmt.Errorf("invalid pubkey type; expected ED25519")
    91  			}
    92  
    93  			pubKeyJSONBytes, err := cdc.MarshalJSON(edPK)
    94  			if err != nil {
    95  				return err
    96  			}
    97  			accPub, err := sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeAccPub, edPK)
    98  			if err != nil {
    99  				return err
   100  			}
   101  			valPub, err := sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeValPub, edPK)
   102  			if err != nil {
   103  				return err
   104  			}
   105  			consenusPub, err := sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeConsPub, edPK)
   106  			if err != nil {
   107  				return err
   108  			}
   109  
   110  			cmd.Println("Address:", edPK.Address())
   111  			cmd.Printf("Hex: %X\n", edPK[:])
   112  			cmd.Println("JSON (base64):", string(pubKeyJSONBytes))
   113  			cmd.Println("Bech32 Acc:", accPub)
   114  			cmd.Println("Bech32 Validator Operator:", valPub)
   115  			cmd.Println("Bech32 Validator Consensus:", consenusPub)
   116  
   117  			return nil
   118  		},
   119  	}
   120  }
   121  
   122  func AddrCmd() *cobra.Command {
   123  	return &cobra.Command{
   124  		Use:   "addr [address]",
   125  		Short: "Convert an address between hex and bech32",
   126  		Long: fmt.Sprintf(`Convert an address between hex encoding and bech32.
   127  			
   128  Example:
   129  $ %s debug addr cosmos1e0jnq2sun3dzjh8p2xq95kk0expwmd7shwjpfg
   130  			`, version.ClientName),
   131  		Args: cobra.ExactArgs(1),
   132  		RunE: func(cmd *cobra.Command, args []string) error {
   133  
   134  			addrString := args[0]
   135  			var addr []byte
   136  
   137  			// try hex, then bech32
   138  			var err error
   139  			addr, err = hex.DecodeString(addrString)
   140  			if err != nil {
   141  				var err2 error
   142  				addr, err2 = sdk.AccAddressFromBech32(addrString)
   143  				if err2 != nil {
   144  					var err3 error
   145  					addr, err3 = sdk.ValAddressFromBech32(addrString)
   146  
   147  					if err3 != nil {
   148  						return fmt.Errorf("expected hex or bech32. Got errors: hex: %v, bech32 acc: %v, bech32 val: %v", err, err2, err3)
   149  
   150  					}
   151  				}
   152  			}
   153  
   154  			accAddr := sdk.AccAddress(addr)
   155  			valAddr := sdk.ValAddress(addr)
   156  
   157  			cmd.Println("Address:", addr)
   158  			cmd.Printf("Address (hex): %X\n", addr)
   159  			cmd.Printf("Bech32 Acc: %s\n", accAddr)
   160  			cmd.Printf("Bech32 Val: %s\n", valAddr)
   161  			return nil
   162  		},
   163  	}
   164  }
   165  
   166  func RawBytesCmd() *cobra.Command {
   167  	return &cobra.Command{
   168  		Use:   "raw-bytes [raw-bytes]",
   169  		Short: "Convert raw bytes output (eg. [10 21 13 255]) to hex",
   170  		Long: fmt.Sprintf(`Convert raw-bytes to hex.
   171  			
   172  Example:
   173  $ %s debug raw-bytes [72 101 108 108 111 44 32 112 108 97 121 103 114 111 117 110 100]
   174  			`, version.ClientName),
   175  		Args: cobra.ExactArgs(1),
   176  		RunE: func(cmd *cobra.Command, args []string) error {
   177  			stringBytes := args[0]
   178  			stringBytes = strings.Trim(stringBytes, "[")
   179  			stringBytes = strings.Trim(stringBytes, "]")
   180  			spl := strings.Split(stringBytes, " ")
   181  
   182  			byteArray := []byte{}
   183  			for _, s := range spl {
   184  				b, err := strconv.Atoi(s)
   185  				if err != nil {
   186  					return err
   187  				}
   188  				byteArray = append(byteArray, byte(b))
   189  			}
   190  			fmt.Printf("%X\n", byteArray)
   191  			return nil
   192  		},
   193  	}
   194  }