Various sign command improvements

- Exit with error if the user is attempting to sign with a key
  whose address is not among those who are expected to sign
  the transaction.

- Add --print-signature-only to output only the generated
  signature.
This commit is contained in:
Alessio Treglia 2018-10-22 15:37:20 -07:00
parent 89047d8ebd
commit bc583ab1d0
No known key found for this signature in database
GPG Key ID: E8A48AE5311D765A
2 changed files with 17 additions and 4 deletions

View File

@ -123,7 +123,8 @@ func SignStdTx(txBldr authtxb.TxBuilder, cliCtx context.CLIContext, name string,
// Check whether the address is a signer
if !isTxSigner(sdk.AccAddress(addr), stdTx.GetSigners()) {
fmt.Fprintf(os.Stderr, "WARNING: The generated transaction's intended signer does not match the given signer: '%v'\n", name)
return signedStdTx, fmt.Errorf(
"The generated transaction's intended signer does not match the given signer: %q", name)
}
if !offline && txBldr.AccountNumber == 0 {

View File

@ -20,6 +20,7 @@ const (
flagAppend = "append"
flagPrintSigs = "print-sigs"
flagOffline = "offline"
flagSigOnly = "print-signature-only"
)
// GetSignCommand returns the sign command
@ -38,6 +39,7 @@ recommended to set such parameters manually.`,
}
cmd.Flags().String(client.FlagName, "", "Name of private key with which to sign")
cmd.Flags().Bool(flagAppend, true, "Append the signature to the existing ones. If disabled, old signatures would be overwritten")
cmd.Flags().Bool(flagSigOnly, false, "Print only the generated signature, then exit.")
cmd.Flags().Bool(flagPrintSigs, false, "Print the addresses that must sign the transaction and those who have already signed it, then exit")
cmd.Flags().Bool(flagOffline, false, "Offline mode. Do not query local cache.")
return cmd
@ -59,14 +61,24 @@ func makeSignCmd(cdc *amino.Codec, decoder auth.AccountDecoder) func(cmd *cobra.
cliCtx := context.NewCLIContext().WithCodec(cdc).WithAccountDecoder(decoder)
txBldr := authtxb.NewTxBuilderFromCLI()
newTx, err := utils.SignStdTx(txBldr, cliCtx, name, stdTx, viper.GetBool(flagAppend), viper.GetBool(flagOffline))
// if --print-signature-only is on, then override --append
generateSignatureOnly := viper.GetBool(flagSigOnly)
append := viper.GetBool(flagAppend) && !generateSignatureOnly
newTx, err := utils.SignStdTx(txBldr, cliCtx, name, stdTx, append, viper.GetBool(flagOffline))
if err != nil {
return err
}
var json []byte
if cliCtx.Indent {
switch {
case generateSignatureOnly && cliCtx.Indent:
json, err = cdc.MarshalJSONIndent(newTx.Signatures[0], "", " ")
case generateSignatureOnly && !cliCtx.Indent:
json, err = cdc.MarshalJSON(newTx.Signatures[0])
case !generateSignatureOnly && cliCtx.Indent:
json, err = cdc.MarshalJSONIndent(newTx, "", " ")
} else {
case !generateSignatureOnly && !cliCtx.Indent:
json, err = cdc.MarshalJSON(newTx)
}
if err != nil {