2018-09-07 03:59:16 -07:00
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2018-10-22 17:51:46 -07:00
|
|
|
"github.com/pkg/errors"
|
2018-09-07 03:59:16 -07:00
|
|
|
"github.com/spf13/viper"
|
2018-10-22 16:11:13 -07:00
|
|
|
"io/ioutil"
|
2018-09-07 03:59:16 -07:00
|
|
|
|
|
|
|
"github.com/cosmos/cosmos-sdk/client"
|
|
|
|
"github.com/cosmos/cosmos-sdk/client/context"
|
|
|
|
"github.com/cosmos/cosmos-sdk/client/utils"
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
"github.com/cosmos/cosmos-sdk/x/auth"
|
2018-09-07 10:49:12 -07:00
|
|
|
authtxb "github.com/cosmos/cosmos-sdk/x/auth/client/txbuilder"
|
2018-09-07 03:59:16 -07:00
|
|
|
"github.com/spf13/cobra"
|
2018-10-22 16:11:13 -07:00
|
|
|
"github.com/tendermint/go-amino"
|
2018-09-07 03:59:16 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2018-10-22 16:59:19 -07:00
|
|
|
flagAppend = "append"
|
2018-10-29 08:15:59 -07:00
|
|
|
flagValidateSigs = "validate-signatures"
|
2018-10-22 16:59:19 -07:00
|
|
|
flagOffline = "offline"
|
2018-10-29 08:15:59 -07:00
|
|
|
flagSigOnly = "signature-only"
|
2018-09-07 03:59:16 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
// GetSignCommand returns the sign command
|
|
|
|
func GetSignCommand(codec *amino.Codec, decoder auth.AccountDecoder) *cobra.Command {
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "sign <file>",
|
2018-09-26 07:00:49 -07:00
|
|
|
Short: "Sign transactions generated offline",
|
2018-09-07 03:59:16 -07:00
|
|
|
Long: `Sign transactions created with the --generate-only flag.
|
2018-10-19 11:00:27 -07:00
|
|
|
Read a transaction from <file>, sign it, and print its JSON encoding.
|
|
|
|
|
2018-10-23 12:41:24 -07:00
|
|
|
If the flag --signature-only flag is on, it outputs a JSON representation
|
2018-10-22 16:52:54 -07:00
|
|
|
of the generated signature only.
|
|
|
|
|
2018-10-31 06:50:01 -07:00
|
|
|
If the flag --validate-signatures is on, then the command would check whether all required
|
|
|
|
signers have signed the transactions and whether the signatures were collected in the right
|
|
|
|
order.
|
|
|
|
|
2018-10-19 11:00:27 -07:00
|
|
|
The --offline flag makes sure that the client will not reach out to the local cache.
|
|
|
|
Thus account number or sequence number lookups will not be performed and it is
|
|
|
|
recommended to set such parameters manually.`,
|
2018-09-07 03:59:16 -07:00
|
|
|
RunE: makeSignCmd(codec, decoder),
|
|
|
|
Args: cobra.ExactArgs(1),
|
|
|
|
}
|
|
|
|
cmd.Flags().String(client.FlagName, "", "Name of private key with which to sign")
|
2018-10-23 11:53:47 -07:00
|
|
|
cmd.Flags().Bool(flagAppend, true,
|
|
|
|
"Append the signature to the existing ones. If disabled, old signatures would be overwritten")
|
2018-10-29 08:15:59 -07:00
|
|
|
cmd.Flags().Bool(flagSigOnly, false, "Print only the generated signature, then exit.")
|
|
|
|
cmd.Flags().Bool(flagValidateSigs, false, "Print the addresses that must sign the transaction, "+
|
2018-10-23 11:53:47 -07:00
|
|
|
"those who have already signed it, and make sure that signatures are in the correct order.")
|
2018-10-19 11:00:27 -07:00
|
|
|
cmd.Flags().Bool(flagOffline, false, "Offline mode. Do not query local cache.")
|
2018-09-07 03:59:16 -07:00
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
func makeSignCmd(cdc *amino.Codec, decoder auth.AccountDecoder) func(cmd *cobra.Command, args []string) error {
|
|
|
|
return func(cmd *cobra.Command, args []string) (err error) {
|
|
|
|
stdTx, err := readAndUnmarshalStdTx(cdc, args[0])
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-10-29 08:15:59 -07:00
|
|
|
if viper.GetBool(flagValidateSigs) {
|
2018-10-22 16:11:13 -07:00
|
|
|
if !printSignatures(stdTx) {
|
|
|
|
return fmt.Errorf("signatures validation failed")
|
|
|
|
}
|
2018-09-07 03:59:16 -07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
name := viper.GetString(client.FlagName)
|
2018-10-22 17:51:46 -07:00
|
|
|
if name == "" {
|
|
|
|
return errors.New("required flag \"name\" has not been set")
|
|
|
|
}
|
2018-09-07 03:59:16 -07:00
|
|
|
cliCtx := context.NewCLIContext().WithCodec(cdc).WithAccountDecoder(decoder)
|
2018-09-07 10:49:12 -07:00
|
|
|
txBldr := authtxb.NewTxBuilderFromCLI()
|
2018-09-07 03:59:16 -07:00
|
|
|
|
2018-10-23 12:41:24 -07:00
|
|
|
// if --signature-only is on, then override --append
|
2018-10-29 08:15:59 -07:00
|
|
|
generateSignatureOnly := viper.GetBool(flagSigOnly)
|
2018-10-29 08:11:32 -07:00
|
|
|
appendSig := viper.GetBool(flagAppend) && !generateSignatureOnly
|
|
|
|
newTx, err := utils.SignStdTx(txBldr, cliCtx, name, stdTx, appendSig, viper.GetBool(flagOffline))
|
2018-09-07 03:59:16 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-10-22 15:37:20 -07:00
|
|
|
|
2018-10-04 04:00:24 -07:00
|
|
|
var json []byte
|
2018-10-22 15:37:20 -07:00
|
|
|
|
2018-10-29 08:34:11 -07:00
|
|
|
switch generateSignatureOnly {
|
|
|
|
case true:
|
|
|
|
switch cliCtx.Indent {
|
|
|
|
case true:
|
|
|
|
json, err = cdc.MarshalJSONIndent(newTx.Signatures[0], "", " ")
|
|
|
|
default:
|
|
|
|
json, err = cdc.MarshalJSON(newTx.Signatures[0])
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
switch cliCtx.Indent {
|
|
|
|
case true:
|
|
|
|
json, err = cdc.MarshalJSONIndent(newTx, "", " ")
|
|
|
|
default:
|
|
|
|
json, err = cdc.MarshalJSON(newTx)
|
|
|
|
}
|
2018-10-04 04:00:24 -07:00
|
|
|
}
|
2018-09-07 03:59:16 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fmt.Printf("%s\n", json)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-22 16:11:13 -07:00
|
|
|
func printSignatures(stdTx auth.StdTx) bool {
|
2018-09-07 03:59:16 -07:00
|
|
|
fmt.Println("Signers:")
|
2018-10-22 15:52:19 -07:00
|
|
|
signers := stdTx.GetSigners()
|
|
|
|
for i, signer := range signers {
|
2018-09-07 03:59:16 -07:00
|
|
|
fmt.Printf(" %v: %v\n", i, signer.String())
|
|
|
|
}
|
2018-10-22 16:11:13 -07:00
|
|
|
|
|
|
|
sigs := stdTx.GetSignatures()
|
2018-09-07 03:59:16 -07:00
|
|
|
fmt.Println("")
|
|
|
|
fmt.Println("Signatures:")
|
2018-10-22 16:11:13 -07:00
|
|
|
success := true
|
|
|
|
if len(sigs) != len(signers) {
|
|
|
|
success = false
|
|
|
|
}
|
2018-09-07 03:59:16 -07:00
|
|
|
for i, sig := range stdTx.GetSignatures() {
|
2018-10-22 15:52:19 -07:00
|
|
|
sigAddr := sdk.AccAddress(sig.Address())
|
|
|
|
sigSanity := "OK"
|
|
|
|
if i >= len(signers) || !sigAddr.Equals(signers[i]) {
|
2018-10-22 16:11:13 -07:00
|
|
|
sigSanity = fmt.Sprintf("ERROR: signature %d does not match its respective signer", i)
|
2018-10-29 08:13:36 -07:00
|
|
|
success = false
|
2018-10-22 15:52:19 -07:00
|
|
|
}
|
|
|
|
fmt.Printf(" %v: %v\t[%s]\n", i, sigAddr.String(), sigSanity)
|
2018-09-07 03:59:16 -07:00
|
|
|
}
|
2018-10-22 16:11:13 -07:00
|
|
|
fmt.Println("")
|
|
|
|
return success
|
2018-09-07 03:59:16 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func readAndUnmarshalStdTx(cdc *amino.Codec, filename string) (stdTx auth.StdTx, err error) {
|
|
|
|
var bytes []byte
|
|
|
|
if bytes, err = ioutil.ReadFile(filename); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err = cdc.UnmarshalJSON(bytes, &stdTx); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|