2019-06-15 05:34:11 -07:00
|
|
|
package cli
|
2019-02-26 03:34:27 -08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/base64"
|
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
|
|
|
"github.com/cosmos/cosmos-sdk/client/context"
|
2019-05-28 01:44:04 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/client/flags"
|
2019-02-26 03:34:27 -08:00
|
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
2019-06-15 05:34:11 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/auth/client/utils"
|
2019-02-26 03:34:27 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
// txEncodeRespStr implements a simple Stringer wrapper for a encoded tx.
|
|
|
|
type txEncodeRespStr string
|
|
|
|
|
|
|
|
func (txr txEncodeRespStr) String() string {
|
|
|
|
return string(txr)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetEncodeCommand returns the encode command to take a JSONified transaction and turn it into
|
|
|
|
// Amino-serialized bytes
|
2019-06-05 16:26:17 -07:00
|
|
|
func GetEncodeCommand(cdc *codec.Codec) *cobra.Command {
|
2019-02-26 03:34:27 -08:00
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "encode [file]",
|
|
|
|
Short: "Encode transactions generated offline",
|
|
|
|
Long: `Encode transactions created with the --generate-only flag and signed with the sign command.
|
|
|
|
Read a transaction from <file>, serialize it to the Amino wire protocol, and output it as base64.
|
|
|
|
If you supply a dash (-) argument in place of an input filename, the command reads from standard input.`,
|
|
|
|
Args: cobra.ExactArgs(1),
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) (err error) {
|
2019-06-05 16:26:17 -07:00
|
|
|
cliCtx := context.NewCLIContext().WithCodec(cdc)
|
2019-02-26 03:34:27 -08:00
|
|
|
|
|
|
|
stdTx, err := utils.ReadStdTxFromFile(cliCtx.Codec, args[0])
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// re-encode it via the Amino wire protocol
|
|
|
|
txBytes, err := cliCtx.Codec.MarshalBinaryLengthPrefixed(stdTx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// base64 encode the encoded tx bytes
|
|
|
|
txBytesBase64 := base64.StdEncoding.EncodeToString(txBytes)
|
|
|
|
|
|
|
|
response := txEncodeRespStr(txBytesBase64)
|
2019-09-19 06:21:38 -07:00
|
|
|
return cliCtx.PrintOutput(response)
|
2019-02-26 03:34:27 -08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-05-28 01:44:04 -07:00
|
|
|
return flags.PostCommands(cmd)[0]
|
2019-02-26 03:34:27 -08:00
|
|
|
}
|