github.com/iotexproject/iotex-core@v1.14.1-rc1/ioctl/newcmd/account/accountcreateadd.go (about) 1 // Copyright (c) 2022 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 11 "github.com/pkg/errors" 12 "github.com/spf13/cobra" 13 14 "github.com/iotexproject/iotex-core/ioctl" 15 "github.com/iotexproject/iotex-core/ioctl/config" 16 "github.com/iotexproject/iotex-core/ioctl/validator" 17 ) 18 19 // Multi-language support 20 var ( 21 _createAddCmdShorts = map[config.Language]string{ 22 config.English: "Create new account for ioctl", 23 config.Chinese: "为ioctl创建新账户", 24 } 25 _createAddCmdUses = map[config.Language]string{ 26 config.English: "createadd ALIAS", 27 config.Chinese: "createadd 别名", 28 } 29 _invalidAlias = map[config.Language]string{ 30 config.English: "invalid alias", 31 config.Chinese: "无效别名", 32 } 33 _aliasHasAlreadyUsed = map[config.Language]string{ 34 config.English: "** Alias \"%s\" has already used for %s\n" + 35 "Overwriting the account will keep the previous keystore file stay, " + 36 "but bind the alias to the new one.\nWould you like to continue?\n", 37 config.Chinese: "** 这个别名 \"%s\" 已被 %s 使用!\n" + 38 "复写帐户后先前的 keystore 文件将会留存!\n" + 39 "但底下的别名将绑定为新的。您是否要继续?", 40 } 41 _outputMessage = map[config.Language]string{ 42 config.English: "New account \"%s\" is created.\n" + 43 "Please Keep your password, or you will lose your private key.", 44 config.Chinese: "新帐户 \"%s\" 已建立。\n" + 45 "请保护好您的密码,否则您会失去您的私钥。", 46 } 47 ) 48 49 // NewAccountCreateAdd represents the account createadd command 50 func NewAccountCreateAdd(client ioctl.Client) *cobra.Command { 51 use, _ := client.SelectTranslation(_createAddCmdUses) 52 short, _ := client.SelectTranslation(_createAddCmdShorts) 53 _invalidAlias, _ := client.SelectTranslation(_invalidAlias) 54 _aliasHasAlreadyUsed, _ := client.SelectTranslation(_aliasHasAlreadyUsed) 55 infoQuit, _ := client.SelectTranslation(_infoQuit) 56 failToWriteToConfigFile, _ := client.SelectTranslation(_failToWriteToConfigFile) 57 failToGenerateNewPrivateKey, _ := client.SelectTranslation(_failToGenerateNewPrivateKey) 58 failToGenerateNewPrivateKeySm2, _ := client.SelectTranslation(_failToGenerateNewPrivateKeySm2) 59 _outputMessage, _ := client.SelectTranslation(_outputMessage) 60 61 return &cobra.Command{ 62 Use: use, 63 Short: short, 64 Args: cobra.ExactArgs(1), 65 RunE: func(cmd *cobra.Command, args []string) error { 66 cmd.SilenceUsage = true 67 68 if err := validator.ValidateAlias(args[0]); err != nil { 69 return errors.Wrap(err, _invalidAlias) 70 } 71 72 if addr, ok := client.Config().Aliases[args[0]]; ok { 73 confirmed, err := client.AskToConfirm(fmt.Sprintf(_aliasHasAlreadyUsed, args[0], addr)) 74 if err != nil { 75 return errors.Wrap(err, "failed to ask confirm") 76 } 77 if !confirmed { 78 cmd.Println(infoQuit) 79 return nil 80 } 81 } 82 83 var addr string 84 var err error 85 if client.IsCryptoSm2() { 86 addr, err = newAccountSm2(client, cmd, args[0]) 87 if err != nil { 88 return errors.Wrap(err, failToGenerateNewPrivateKey) 89 } 90 } else { 91 addr, err = newAccount(client, cmd, args[0]) 92 if err != nil { 93 return errors.Wrap(err, failToGenerateNewPrivateKeySm2) 94 } 95 } 96 if err := client.SetAliasAndSave(args[0], addr); err != nil { 97 return errors.Wrapf(err, failToWriteToConfigFile) 98 } 99 cmd.Println(fmt.Sprintf(_outputMessage, args[0])) 100 return nil 101 }, 102 } 103 }