github.com/iotexproject/iotex-core@v1.14.1-rc1/ioctl/cmd/account/accountcreateadd.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  	"strings"
    12  
    13  	"github.com/spf13/cobra"
    14  	"gopkg.in/yaml.v2"
    15  
    16  	"github.com/iotexproject/iotex-core/ioctl/config"
    17  	"github.com/iotexproject/iotex-core/ioctl/output"
    18  	"github.com/iotexproject/iotex-core/ioctl/validator"
    19  )
    20  
    21  // Multi-language support
    22  var (
    23  	_createAddCmdShorts = map[config.Language]string{
    24  		config.English: "Create new account for ioctl",
    25  		config.Chinese: "为ioctl创建新账户",
    26  	}
    27  	_createAddCmdUses = map[config.Language]string{
    28  		config.English: "createadd ALIAS",
    29  		config.Chinese: "createadd 别名",
    30  	}
    31  )
    32  
    33  // _accountCreateAddCmd represents the account createadd command
    34  var _accountCreateAddCmd = &cobra.Command{
    35  	Use:   config.TranslateInLang(_createAddCmdUses, config.UILanguage),
    36  	Short: config.TranslateInLang(_createAddCmdShorts, config.UILanguage),
    37  	Args:  cobra.ExactArgs(1),
    38  	RunE: func(cmd *cobra.Command, args []string) error {
    39  		cmd.SilenceUsage = true
    40  		err := accountCreateAdd(args)
    41  		return output.PrintError(err)
    42  	},
    43  }
    44  
    45  func accountCreateAdd(args []string) error {
    46  	// Validate inputs
    47  	if err := validator.ValidateAlias(args[0]); err != nil {
    48  		return output.NewError(output.ValidationError, "invalid alias", err)
    49  	}
    50  	alias := args[0]
    51  	if addr, ok := config.ReadConfig.Aliases[alias]; ok {
    52  		var confirm string
    53  		info := fmt.Sprintf("** Alias \"%s\" has already used for %s\n"+
    54  			"Overwriting the account will keep the previous keystore file stay, "+
    55  			"but bind the alias to the new one.\nWould you like to continue?\n", alias, addr)
    56  		message := output.ConfirmationMessage{Info: info, Options: []string{"yes"}}
    57  		fmt.Println(message.String())
    58  		if _, err := fmt.Scanf("%s", &confirm); err != nil {
    59  			return output.NewError(output.InputError, "failed to input yes", err)
    60  		}
    61  		if !strings.EqualFold(confirm, "yes") {
    62  			output.PrintResult("quit")
    63  			return nil
    64  		}
    65  	}
    66  
    67  	var addr string
    68  	var err error
    69  	if CryptoSm2 {
    70  		addr, err = newAccountSm2(alias)
    71  		if err != nil {
    72  			return output.NewError(0, "", err)
    73  		}
    74  	} else {
    75  		addr, err = newAccount(alias)
    76  		if err != nil {
    77  			return output.NewError(0, "", err)
    78  		}
    79  	}
    80  	config.ReadConfig.Aliases[alias] = addr
    81  	out, err := yaml.Marshal(&config.ReadConfig)
    82  	if err != nil {
    83  		return output.NewError(output.SerializationError, "failed to marshal config", err)
    84  	}
    85  	if err := os.WriteFile(config.DefaultConfigFile, out, 0600); err != nil {
    86  		return output.NewError(output.WriteFileError,
    87  			fmt.Sprintf("failed to write to config file %s", config.DefaultConfigFile), err)
    88  	}
    89  	output.PrintResult(fmt.Sprintf("New account \"%s\" is created.\n"+
    90  		"Please Keep your password, or you will lose your private key.", alias))
    91  	return nil
    92  }