github.com/iotexproject/iotex-core@v1.14.1-rc1/ioctl/newcmd/account/accountexportpublic.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  	_exportPublicCmdShorts = map[config.Language]string{
    20  		config.English: "Export IoTeX public key from wallet",
    21  		config.Chinese: "从钱包导出IoTeX的公钥",
    22  	}
    23  	_exportPublicCmdUses = map[config.Language]string{
    24  		config.English: "exportpublic (ALIAS|ADDRESS)",
    25  		config.Chinese: "exportpublic (别名|地址)",
    26  	}
    27  )
    28  
    29  // NewAccountExportPublic represents the account export public command
    30  func NewAccountExportPublic(client ioctl.Client) *cobra.Command {
    31  	use, _ := client.SelectTranslation(_exportPublicCmdUses)
    32  	short, _ := client.SelectTranslation(_exportPublicCmdShorts)
    33  
    34  	return &cobra.Command{
    35  		Use:   use,
    36  		Short: short,
    37  		Args:  cobra.ExactArgs(1),
    38  		RunE: func(cmd *cobra.Command, args []string) error {
    39  			cmd.SilenceUsage = true
    40  			arg := args[0]
    41  
    42  			var (
    43  				addr string
    44  				err  error
    45  			)
    46  			if util.AliasIsHdwalletKey(arg) {
    47  				addr = arg
    48  			} else {
    49  				addr, err = client.Address(arg)
    50  				if err != nil {
    51  					return errors.Wrap(err, "failed to get address")
    52  				}
    53  			}
    54  			prvKey, err := PrivateKeyFromSigner(client, cmd, addr, "")
    55  			if err != nil {
    56  				return errors.Wrap(err, "failed to get private key from keystore")
    57  			}
    58  			cmd.Println(prvKey.PublicKey().HexString())
    59  			prvKey.Zero()
    60  			return nil
    61  		},
    62  	}
    63  }