github.com/iotexproject/iotex-core@v1.14.1-rc1/ioctl/newcmd/account/accountimport.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  	"fmt"
    10  	"os"
    11  
    12  	"github.com/pkg/errors"
    13  	"github.com/spf13/cobra"
    14  
    15  	"github.com/iotexproject/iotex-core/ioctl"
    16  	"github.com/iotexproject/iotex-core/ioctl/config"
    17  	"github.com/iotexproject/iotex-core/ioctl/util"
    18  	"github.com/iotexproject/iotex-core/ioctl/validator"
    19  )
    20  
    21  // Multi-language support
    22  var (
    23  	importCmdShorts = map[config.Language]string{
    24  		config.English: "Import IoTeX private key or keystore into wallet",
    25  		config.Chinese: "将IoTeX的私钥或私钥库导入钱包",
    26  	}
    27  	importKeyCmdShorts = map[config.Language]string{
    28  		config.English: "Import IoTeX private key into wallet",
    29  		config.Chinese: "将IoTeX的私钥导入钱包",
    30  	}
    31  	importKeyCmdUses = map[config.Language]string{
    32  		config.English: "key ALIAS",
    33  		config.Chinese: "key 别名",
    34  	}
    35  	importKeyStoreCmdShorts = map[config.Language]string{
    36  		config.English: "Import IoTeX keystore into wallet",
    37  		config.Chinese: "将IoTeX的私钥库导入钱包",
    38  	}
    39  	importKeyStoreCmdUses = map[config.Language]string{
    40  		config.English: "keystore ALIAS FILEPATH",
    41  		config.Chinese: "keystore 别名 文件路径",
    42  	}
    43  	importPemCmdShorts = map[config.Language]string{
    44  		config.English: "Import IoTeX key from pem file into wallet",
    45  		config.Chinese: "将IoTeX私钥从pem文件导入钱包",
    46  	}
    47  	importPemCmdUses = map[config.Language]string{
    48  		config.English: "pem ALIAS FILEPATH",
    49  		config.Chinese: "pem 别名 文件路径",
    50  	}
    51  )
    52  
    53  // NewAccountImportCmd combines three account import command
    54  func NewAccountImportCmd(client ioctl.Client) *cobra.Command {
    55  	importShorts, _ := client.SelectTranslation(importCmdShorts)
    56  	accountImportCmd := &cobra.Command{
    57  		Use:   "import",
    58  		Short: importShorts,
    59  	}
    60  
    61  	accountImportCmd.AddCommand(NewAccountImportKeyCmd(client))
    62  	accountImportCmd.AddCommand(NewAccountImportKeyStoreCmd(client))
    63  	accountImportCmd.AddCommand(NewAccountImportPemCmd(client))
    64  
    65  	return accountImportCmd
    66  }
    67  
    68  // NewAccountImportKeyCmd represents the account import key command
    69  func NewAccountImportKeyCmd(client ioctl.Client) *cobra.Command {
    70  	importKeyUses, _ := client.SelectTranslation(importKeyCmdUses)
    71  	importKeyShorts, _ := client.SelectTranslation(importKeyCmdShorts)
    72  	return &cobra.Command{
    73  		Use:   importKeyUses,
    74  		Short: importKeyShorts,
    75  		Args:  cobra.ExactArgs(1),
    76  		RunE: func(cmd *cobra.Command, args []string) error {
    77  			cmd.SilenceUsage = true
    78  			alias := args[0]
    79  
    80  			if err := validateAlias(client, alias); err != nil {
    81  				return errors.Wrap(err, "invalid alias")
    82  			}
    83  			cmd.Println(fmt.Sprintf("#%s: Enter your private key, "+
    84  				"which will not be exposed on the screen.", alias))
    85  			privateKey, err := client.ReadSecret()
    86  			privateKey = util.TrimHexPrefix(privateKey)
    87  			if err != nil {
    88  				return errors.Wrap(err, "failed to get password")
    89  			}
    90  			addr, err := newAccountByKey(client, cmd, alias, privateKey)
    91  			if err != nil {
    92  				return err
    93  			}
    94  			return client.SetAliasAndSave(alias, addr)
    95  		},
    96  	}
    97  }
    98  
    99  // NewAccountImportKeyStoreCmd represents the account import keystore command
   100  func NewAccountImportKeyStoreCmd(client ioctl.Client) *cobra.Command {
   101  	importKeyStoreUses, _ := client.SelectTranslation(importKeyStoreCmdUses)
   102  	importKeyStoreShorts, _ := client.SelectTranslation(importKeyStoreCmdShorts)
   103  	return &cobra.Command{
   104  		Use:   importKeyStoreUses,
   105  		Short: importKeyStoreShorts,
   106  		Args:  cobra.ExactArgs(2),
   107  		RunE: func(cmd *cobra.Command, args []string) error {
   108  			cmd.SilenceUsage = true
   109  			alias := args[0]
   110  
   111  			if err := validateAlias(client, alias); err != nil {
   112  				return errors.Wrap(err, "invalid alias")
   113  			}
   114  			if _, err := os.Stat(args[1]); err != nil {
   115  				return err
   116  			}
   117  
   118  			cmd.Println(fmt.Sprintf("#%s: Enter your password of keystore, "+
   119  				"which will not be exposed on the screen.", alias))
   120  			password, err := client.ReadSecret()
   121  			if err != nil {
   122  				return errors.Wrap(err, "failed to get password")
   123  			}
   124  			addr, err := newAccountByKeyStore(client, cmd, alias, password, args[1])
   125  			if err != nil {
   126  				return err
   127  			}
   128  			return client.SetAliasAndSave(alias, addr)
   129  		},
   130  	}
   131  }
   132  
   133  // NewAccountImportPemCmd represents the account import pem command
   134  func NewAccountImportPemCmd(client ioctl.Client) *cobra.Command {
   135  	importPemUses, _ := client.SelectTranslation(importPemCmdUses)
   136  	importPemShorts, _ := client.SelectTranslation(importPemCmdShorts)
   137  	return &cobra.Command{
   138  		Use:   importPemUses,
   139  		Short: importPemShorts,
   140  		Args:  cobra.ExactArgs(2),
   141  		RunE: func(cmd *cobra.Command, args []string) error {
   142  			cmd.SilenceUsage = true
   143  			alias := args[0]
   144  
   145  			if err := validateAlias(client, alias); err != nil {
   146  				return errors.Wrap(err, "invalid alias")
   147  			}
   148  			if _, err := os.Stat(args[1]); err != nil {
   149  				return err
   150  			}
   151  
   152  			cmd.Println(fmt.Sprintf("#%s: Enter your password of pem file, "+
   153  				"which will not be exposed on the screen.", alias))
   154  			password, err := client.ReadSecret()
   155  			if err != nil {
   156  				return errors.Wrap(err, "failed to get password")
   157  			}
   158  			addr, err := newAccountByPem(client, cmd, alias, password, args[1])
   159  			if err != nil {
   160  				return err
   161  			}
   162  			return client.SetAliasAndSave(alias, addr)
   163  		},
   164  	}
   165  }
   166  
   167  func validateAlias(client ioctl.Client, alias string) error {
   168  	if err := validator.ValidateAlias(alias); err != nil {
   169  		return err
   170  	}
   171  	if addr, ok := client.Config().Aliases[alias]; ok {
   172  		return fmt.Errorf("alias \"%s\" has already used for %s", alias, addr)
   173  	}
   174  	return nil
   175  }