Merge pull request #1371 from cosmos/bucky/gaiadebug-bech32
gaiadebug: support bech32
This commit is contained in:
commit
7f59aa259f
|
@ -11,6 +11,7 @@ import (
|
|||
"strings"
|
||||
|
||||
gaia "github.com/cosmos/cosmos-sdk/cmd/gaia/app"
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
"github.com/cosmos/cosmos-sdk/x/auth"
|
||||
"github.com/spf13/cobra"
|
||||
crypto "github.com/tendermint/go-crypto"
|
||||
|
@ -19,6 +20,7 @@ import (
|
|||
func init() {
|
||||
rootCmd.AddCommand(txCmd)
|
||||
rootCmd.AddCommand(pubkeyCmd)
|
||||
rootCmd.AddCommand(addrCmd)
|
||||
rootCmd.AddCommand(hackCmd)
|
||||
rootCmd.AddCommand(rawBytesCmd)
|
||||
}
|
||||
|
@ -37,10 +39,16 @@ var txCmd = &cobra.Command{
|
|||
|
||||
var pubkeyCmd = &cobra.Command{
|
||||
Use: "pubkey",
|
||||
Short: "Decode a pubkey from hex or base64",
|
||||
Short: "Decode a pubkey from hex, base64, or bech32",
|
||||
RunE: runPubKeyCmd,
|
||||
}
|
||||
|
||||
var addrCmd = &cobra.Command{
|
||||
Use: "addr",
|
||||
Short: "Convert an address between hex and bech32",
|
||||
RunE: runAddrCmd,
|
||||
}
|
||||
|
||||
var hackCmd = &cobra.Command{
|
||||
Use: "hack",
|
||||
Short: "Boilerplate to Hack on an existing state by scripting some Go...",
|
||||
|
@ -80,30 +88,103 @@ func runPubKeyCmd(cmd *cobra.Command, args []string) error {
|
|||
}
|
||||
|
||||
pubkeyString := args[0]
|
||||
var pubKeyI crypto.PubKey
|
||||
|
||||
// try hex, then base64
|
||||
// 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 {
|
||||
return fmt.Errorf(`Expected hex or base64. Got errors:
|
||||
var err3 error
|
||||
pubKeyI, err3 = sdk.GetAccPubKeyBech32(pubkeyString)
|
||||
if err3 != nil {
|
||||
var err4 error
|
||||
pubKeyI, err4 = sdk.GetValPubKeyBech32(pubkeyString)
|
||||
|
||||
if err4 != nil {
|
||||
return fmt.Errorf(`Expected hex, base64, or bech32. Got errors:
|
||||
hex: %v,
|
||||
base64: %v
|
||||
`, err, err2)
|
||||
bech32 acc: %v
|
||||
bech32 val: %v
|
||||
`, err, err2, err3, err4)
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
cdc := gaia.MakeCodec()
|
||||
var pubKey crypto.PubKeyEd25519
|
||||
copy(pubKey[:], pubkeyBytes)
|
||||
if pubKeyI == nil {
|
||||
copy(pubKey[:], pubkeyBytes)
|
||||
} else {
|
||||
pubKey = pubKeyI.(crypto.PubKeyEd25519)
|
||||
pubkeyBytes = pubKey[:]
|
||||
}
|
||||
|
||||
cdc := gaia.MakeCodec()
|
||||
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
|
||||
}
|
||||
fmt.Println("Address:", pubKey.Address())
|
||||
fmt.Printf("Hex: %X\n", pubkeyBytes)
|
||||
fmt.Println("JSON (base64):", string(pubKeyJSONBytes))
|
||||
fmt.Println("Bech32 Acc:", accPub)
|
||||
fmt.Println("Bech32 Val:", valPub)
|
||||
return nil
|
||||
}
|
||||
|
||||
func runAddrCmd(cmd *cobra.Command, args []string) error {
|
||||
if len(args) != 1 {
|
||||
return fmt.Errorf("Expected single arg")
|
||||
}
|
||||
|
||||
addrString := args[0]
|
||||
var addr sdk.Address
|
||||
|
||||
// try hex, then bech32
|
||||
var err error
|
||||
addr, err = hex.DecodeString(addrString)
|
||||
if err != nil {
|
||||
var err2 error
|
||||
addr, err2 = sdk.GetAccAddressBech32(addrString)
|
||||
if err2 != nil {
|
||||
var err3 error
|
||||
addr, err3 = sdk.GetValAddressBech32(addrString)
|
||||
|
||||
if err3 != nil {
|
||||
return fmt.Errorf(`Expected hex or bech32. Got errors:
|
||||
hex: %v,
|
||||
bech32 acc: %v
|
||||
bech32 val: %v
|
||||
`, err, err2, err3)
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
accAddr, err := sdk.Bech32ifyAcc(addr)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
valAddr, err := sdk.Bech32ifyVal(addr)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Println("Address:", addr)
|
||||
fmt.Println("Bech32 Acc:", accAddr)
|
||||
fmt.Println("Bech32 Val:", valAddr)
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue