From 522042fd1200fbdbf49c76e55b4bceeebb80a0f9 Mon Sep 17 00:00:00 2001 From: Ethan Buchman Date: Tue, 12 Jun 2018 22:17:11 -0700 Subject: [PATCH] gaiadebug tool --- Makefile | 5 +- cmd/gaia/cmd/gaiadebug/main.go | 122 +++++++++++++++++++++++++++++++++ 2 files changed, 126 insertions(+), 1 deletion(-) create mode 100644 cmd/gaia/cmd/gaiadebug/main.go diff --git a/Makefile b/Makefile index d6444b0b62..c0d18c4a39 100644 --- a/Makefile +++ b/Makefile @@ -46,6 +46,9 @@ install_examples: go install $(BUILD_FLAGS) ./examples/democoin/cmd/democoind go install $(BUILD_FLAGS) ./examples/democoin/cmd/democli +install_debug: + go install $(BUILD_FLAGS) ./cmd/gaia/cmd/gaiadebug + dist: @bash publish/dist.sh @bash publish/publish.sh @@ -157,4 +160,4 @@ remotenet-status: # To avoid unintended conflicts with file names, always add to .PHONY # unless there is a reason not to. # https://www.gnu.org/software/make/manual/html_node/Phony-Targets.html -.PHONY: build build_examples install install_examples dist check_tools get_tools get_vendor_deps draw_deps test test_cli test_unit test_cover test_lint benchmark devdoc_init devdoc devdoc_save devdoc_update remotenet-start remotenet-stop remotenet-status +.PHONY: build build_examples install install_examples install_debug dist check_tools get_tools get_vendor_deps draw_deps test test_cli test_unit test_cover test_lint benchmark devdoc_init devdoc devdoc_save devdoc_update remotenet-start remotenet-stop remotenet-status diff --git a/cmd/gaia/cmd/gaiadebug/main.go b/cmd/gaia/cmd/gaiadebug/main.go new file mode 100644 index 0000000000..ed5344bdd0 --- /dev/null +++ b/cmd/gaia/cmd/gaiadebug/main.go @@ -0,0 +1,122 @@ +package main + +import ( + "bytes" + "encoding/base64" + "encoding/hex" + "encoding/json" + "fmt" + "os" + + gaia "github.com/cosmos/cosmos-sdk/cmd/gaia/app" + "github.com/cosmos/cosmos-sdk/x/auth" + "github.com/spf13/cobra" + crypto "github.com/tendermint/go-crypto" +) + +func init() { + rootCmd.AddCommand(txCmd) + rootCmd.AddCommand(pubkeyCmd) +} + +var rootCmd = &cobra.Command{ + Use: "gaiadebug", + Short: "Gaia debug tool", + SilenceUsage: true, +} + +var txCmd = &cobra.Command{ + Use: "tx", + Short: "Decode a gaia tx from hex or base64", + RunE: runTxCmd, +} + +var pubkeyCmd = &cobra.Command{ + Use: "pubkey", + Short: "Decode a pubkey from hex or base64", + RunE: runPubKeyCmd, +} + +func runPubKeyCmd(cmd *cobra.Command, args []string) error { + if len(args) != 1 { + return fmt.Errorf("Expected single arg") + } + + pubkeyString := args[0] + + // try hex, then base64 + 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: + hex: %v, + base64: %v + `, err, err2) + } + } + + cdc := gaia.MakeCodec() + var pubKey crypto.PubKeyEd25519 + copy(pubKey[:], pubkeyBytes) + pubKeyJSONBytes, err := cdc.MarshalJSON(pubKey) + if err != nil { + return err + } + fmt.Println("Address:", pubKey.Address()) + fmt.Printf("Hex: %X\n", pubkeyBytes) + fmt.Println("JSON (base64):", string(pubKeyJSONBytes)) + return nil +} + +func runTxCmd(cmd *cobra.Command, args []string) error { + if len(args) != 1 { + return fmt.Errorf("Expected single arg") + } + + 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 = auth.StdTx{} + cdc := gaia.MakeCodec() + + err = cdc.UnmarshalBinary(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 { + return err + } + + fmt.Println(buf.String()) + return nil +} + +func main() { + err := rootCmd.Execute() + if err != nil { + os.Exit(1) + } + os.Exit(0) +}