github.com/iotexproject/iotex-core@v1.14.1-rc1/ioctl/newcmd/account/accountsign.go (about)

     1  // Copyright (c) 2019 IoTeX Foundation
     2  // This source code is provided 'as is' and no warranties are given as to title or non-infringement, merchantability
     3  // or fitness for purpose and, to the extent permitted by law, all liability for your use of the code is disclaimed.
     4  // This source code is governed by Apache License 2.0 that can be found in the LICENSE file.
     5  
     6  package account
     7  
     8  import (
     9  	"github.com/pkg/errors"
    10  	"github.com/spf13/cobra"
    11  
    12  	"github.com/iotexproject/iotex-core/ioctl"
    13  	"github.com/iotexproject/iotex-core/ioctl/config"
    14  	"github.com/iotexproject/iotex-core/ioctl/util"
    15  )
    16  
    17  // Multi-language support
    18  var (
    19  	_signCmdShorts = map[config.Language]string{
    20  		config.English: "Sign message with private key from wallet",
    21  		config.Chinese: "用钱包中的私钥对信息签名",
    22  	}
    23  	_signCmdUses = map[config.Language]string{
    24  		config.English: "sign MESSAGE [-s SIGNER]",
    25  		config.Chinese: "sign 信息 [-s 签署人]",
    26  	}
    27  	_flagSignerUsages = map[config.Language]string{
    28  		config.English: "choose a signing account",
    29  		config.Chinese: "选择一个签名账户",
    30  	}
    31  )
    32  
    33  // NewAccountSign represents the account sign command
    34  func NewAccountSign(client ioctl.Client) *cobra.Command {
    35  	var _signer string
    36  	use, _ := client.SelectTranslation(_signCmdUses)
    37  	short, _ := client.SelectTranslation(_signCmdShorts)
    38  	usage, _ := client.SelectTranslation(_flagSignerUsages)
    39  
    40  	cmd := &cobra.Command{
    41  		Use:   use,
    42  		Short: short,
    43  		Args:  cobra.ExactArgs(1),
    44  		RunE: func(cmd *cobra.Command, args []string) error {
    45  			cmd.SilenceUsage = true
    46  			msg := args[0]
    47  
    48  			var (
    49  				addr string
    50  				err  error
    51  			)
    52  			if util.AliasIsHdwalletKey(_signer) {
    53  				addr = _signer
    54  			} else {
    55  				addr, err = client.Address(_signer)
    56  				if err != nil {
    57  					return errors.Wrap(err, "failed to get address")
    58  				}
    59  			}
    60  			signedMessage, err := Sign(client, cmd, addr, "", msg)
    61  			if err != nil {
    62  				return errors.Wrap(err, "failed to sign message")
    63  			}
    64  			cmd.Println(signedMessage)
    65  			return nil
    66  		},
    67  	}
    68  	cmd.Flags().StringVarP(&_signer, "signer", "s", "", usage)
    69  	return cmd
    70  }