cosmos-sdk/x/auth/client/cli/sign.go

152 lines
4.4 KiB
Go
Raw Normal View History

package cli
import (
"fmt"
"io/ioutil"
2018-10-22 17:51:46 -07:00
"github.com/pkg/errors"
"github.com/spf13/viper"
"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"
authtxb "github.com/cosmos/cosmos-sdk/x/auth/client/txbuilder"
"github.com/spf13/cobra"
2018-10-22 16:11:13 -07:00
"github.com/tendermint/go-amino"
)
const (
flagAppend = "append"
2018-10-29 08:15:59 -07:00
flagValidateSigs = "validate-signatures"
flagOffline = "offline"
2018-10-29 08:15:59 -07:00
flagSigOnly = "signature-only"
)
// GetSignCommand returns the sign command
func GetSignCommand(codec *amino.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "sign <file>",
Short: "Sign transactions generated offline",
Long: `Sign transactions created with the --generate-only flag.
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.
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.
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.`,
RunE: makeSignCmd(codec),
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.")
cmd.Flags().Bool(flagOffline, false, "Offline mode. Do not query local cache.")
// Add the flags here and return the command
return client.PostCommands(cmd)[0]
}
func makeSignCmd(cdc *amino.Codec) 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")
}
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")
}
cliCtx := context.NewCLIContext().WithCodec(cdc).WithAccountDecoder(cdc)
txBldr := authtxb.NewTxBuilderFromCLI()
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))
if err != nil {
return err
}
var json []byte
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)
}
}
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 {
fmt.Println("Signers:")
signers := stdTx.GetSigners()
for i, signer := range signers {
fmt.Printf(" %v: %v\n", i, signer.String())
}
2018-10-22 16:11:13 -07:00
sigs := stdTx.GetSignatures()
fmt.Println("")
fmt.Println("Signatures:")
2018-10-22 16:11:13 -07:00
success := true
if len(sigs) != len(signers) {
success = false
}
for i, sig := range stdTx.GetSignatures() {
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
}
fmt.Printf(" %v: %v\t[%s]\n", i, sigAddr.String(), sigSanity)
}
2018-10-22 16:11:13 -07:00
fmt.Println("")
return success
}
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
}