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

     1  package cli
     2  
     3  import (
     4  	"encoding/base64"
     5  
     6  	"github.com/spf13/cobra"
     7  
     8  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/client/context"
     9  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/client/flags"
    10  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/codec"
    11  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/auth/client/utils"
    12  )
    13  
    14  // txEncodeRespStr implements a simple Stringer wrapper for a encoded tx.
    15  type txEncodeRespStr string
    16  
    17  func (txr txEncodeRespStr) String() string {
    18  	return string(txr)
    19  }
    20  
    21  // GetEncodeCommand returns the encode command to take a JSONified transaction and turn it into
    22  // Amino-serialized bytes
    23  func GetEncodeCommand(cdc *codec.Codec) *cobra.Command {
    24  	cmd := &cobra.Command{
    25  		Use:   "encode [file]",
    26  		Short: "Encode transactions generated offline",
    27  		Long: `Encode transactions created with the --generate-only flag and signed with the sign command.
    28  Read a transaction from <file>, serialize it to the Amino wire protocol, and output it as base64.
    29  If you supply a dash (-) argument in place of an input filename, the command reads from standard input.`,
    30  		Args: cobra.ExactArgs(1),
    31  		RunE: func(cmd *cobra.Command, args []string) (err error) {
    32  			cliCtx := context.NewCLIContext().WithCodec(cdc)
    33  
    34  			stdTx, err := utils.ReadStdTxFromFile(cliCtx.Codec, args[0])
    35  			if err != nil {
    36  				return
    37  			}
    38  
    39  			// re-encode it via the Amino wire protocol
    40  			txBytes, err := cliCtx.Codec.MarshalBinaryLengthPrefixed(stdTx)
    41  			if err != nil {
    42  				return err
    43  			}
    44  
    45  			// base64 encode the encoded tx bytes
    46  			txBytesBase64 := base64.StdEncoding.EncodeToString(txBytes)
    47  
    48  			response := txEncodeRespStr(txBytesBase64)
    49  			return cliCtx.PrintOutput(response)
    50  		},
    51  	}
    52  
    53  	return flags.PostCommands(cmd)[0]
    54  }