github.com/iotexproject/iotex-core@v1.14.1-rc1/ioctl/cmd/account/accountexport.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/spf13/cobra"
    10  
    11  	"github.com/iotexproject/iotex-core/ioctl/config"
    12  	"github.com/iotexproject/iotex-core/ioctl/output"
    13  	"github.com/iotexproject/iotex-core/ioctl/util"
    14  )
    15  
    16  // Multi-language support
    17  var (
    18  	_exportCmdShorts = map[config.Language]string{
    19  		config.English: "Export IoTeX private key from wallet",
    20  		config.Chinese: "从钱包导出IoTeX的私钥",
    21  	}
    22  	_exportCmdUses = map[config.Language]string{
    23  		config.English: "export (ALIAS|ADDRESS)",
    24  		config.Chinese: "export (别名|地址)",
    25  	}
    26  )
    27  
    28  // _accountExportCmd represents the account export command
    29  var _accountExportCmd = &cobra.Command{
    30  	Use:   config.TranslateInLang(_exportCmdUses, config.UILanguage),
    31  	Short: config.TranslateInLang(_exportCmdShorts, config.UILanguage),
    32  	Args:  cobra.ExactArgs(1),
    33  	RunE: func(cmd *cobra.Command, args []string) error {
    34  		cmd.SilenceUsage = true
    35  		err := accountExport(args[0])
    36  		return output.PrintError(err)
    37  	},
    38  }
    39  
    40  func accountExport(arg string) error {
    41  	var (
    42  		addr string
    43  		err  error
    44  	)
    45  	if util.AliasIsHdwalletKey(arg) {
    46  		addr = arg
    47  	} else {
    48  		addr, err = util.Address(arg)
    49  		if err != nil {
    50  			return output.NewError(output.AddressError, "failed to get address", err)
    51  		}
    52  	}
    53  	prvKey, err := PrivateKeyFromSigner(addr, "")
    54  	if err != nil {
    55  		return output.NewError(output.KeystoreError, "failed to get private key from keystore", err)
    56  	}
    57  	output.PrintResult(prvKey.HexString())
    58  	prvKey.Zero()
    59  	return nil
    60  }