github.com/cosmos/cosmos-sdk@v0.50.10/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/cosmos/cosmos-sdk/client"
     9  	"github.com/cosmos/cosmos-sdk/client/flags"
    10  	authclient "github.com/cosmos/cosmos-sdk/x/auth/client"
    11  )
    12  
    13  // GetEncodeCommand returns the encode command to take a JSONified transaction and turn it into
    14  // Amino-serialized bytes
    15  func GetEncodeCommand() *cobra.Command {
    16  	cmd := &cobra.Command{
    17  		Use:   "encode [file]",
    18  		Short: "Encode transactions generated offline",
    19  		Long: `Encode transactions created with the --generate-only flag or signed with the sign command.
    20  Read a transaction from <file>, serialize it to the Protobuf wire protocol, and output it as base64.
    21  If you supply a dash (-) argument in place of an input filename, the command reads from standard input.`,
    22  		Args: cobra.ExactArgs(1),
    23  		RunE: func(cmd *cobra.Command, args []string) error {
    24  			clientCtx := client.GetClientContextFromCmd(cmd)
    25  
    26  			tx, err := authclient.ReadTxFromFile(clientCtx, args[0])
    27  			if err != nil {
    28  				return err
    29  			}
    30  
    31  			// re-encode it
    32  			txBytes, err := clientCtx.TxConfig.TxEncoder()(tx)
    33  			if err != nil {
    34  				return err
    35  			}
    36  
    37  			// base64 encode the encoded tx bytes
    38  			txBytesBase64 := base64.StdEncoding.EncodeToString(txBytes)
    39  
    40  			return clientCtx.PrintString(txBytesBase64 + "\n")
    41  		},
    42  	}
    43  
    44  	flags.AddTxFlagsToCmd(cmd)
    45  	_ = cmd.Flags().MarkHidden(flags.FlagOutput) // encoding makes sense to output only json
    46  
    47  	return cmd
    48  }