github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/libs/cosmos-sdk/x/auth/client/cli/decode.go (about)

     1  package cli
     2  
     3  import (
     4  	"encoding/base64"
     5  	"encoding/hex"
     6  
     7  	"github.com/spf13/cobra"
     8  	"github.com/spf13/viper"
     9  	"github.com/tendermint/go-amino"
    10  
    11  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/client/context"
    12  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/client/flags"
    13  	authtypes "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/auth/types"
    14  )
    15  
    16  const flagHex = "hex"
    17  
    18  // GetDecodeCommand returns the decode command to take Amino-serialized bytes
    19  // and turn it into a JSONified transaction.
    20  func GetDecodeCommand(codec *amino.Codec) *cobra.Command {
    21  	cmd := &cobra.Command{
    22  		Use:   "decode [amino-byte-string]",
    23  		Short: "Decode an amino-encoded transaction string.",
    24  		Args:  cobra.ExactArgs(1),
    25  		RunE:  runDecodeTxString(codec),
    26  	}
    27  
    28  	cmd.Flags().BoolP(flagHex, "x", false, "Treat input as hexadecimal instead of base64")
    29  	return flags.PostCommands(cmd)[0]
    30  }
    31  
    32  func runDecodeTxString(codec *amino.Codec) func(cmd *cobra.Command, args []string) (err error) {
    33  	return func(cmd *cobra.Command, args []string) (err error) {
    34  		cliCtx := context.NewCLIContext().WithCodec(codec).WithOutput(cmd.OutOrStdout())
    35  		var txBytes []byte
    36  
    37  		if viper.GetBool(flagHex) {
    38  			txBytes, err = hex.DecodeString(args[0])
    39  		} else {
    40  			txBytes, err = base64.StdEncoding.DecodeString(args[0])
    41  		}
    42  		if err != nil {
    43  			return err
    44  		}
    45  
    46  		var stdTx authtypes.StdTx
    47  		err = cliCtx.Codec.UnmarshalBinaryLengthPrefixed(txBytes, &stdTx)
    48  		if err != nil {
    49  			return err
    50  		}
    51  
    52  		return cliCtx.PrintOutput(stdTx)
    53  	}
    54  }