-
Notifications
You must be signed in to change notification settings - Fork 320
/
accountcreateadd.go
91 lines (83 loc) · 2.93 KB
/
accountcreateadd.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// Copyright (c) 2019 IoTeX Foundation
// This is an alpha (internal) release and is not suitable for production. This source code is provided 'as is' and no
// warranties are given as to title or non-infringement, merchantability or fitness for purpose and, to the extent
// permitted by law, all liability for your use of the code is disclaimed. This source code is governed by Apache
// License 2.0 that can be found in the LICENSE file.
package account
import (
"fmt"
"io/ioutil"
"strings"
"github.com/spf13/cobra"
"gopkg.in/yaml.v2"
"github.com/iotexproject/iotex-core/ioctl/config"
"github.com/iotexproject/iotex-core/ioctl/output"
"github.com/iotexproject/iotex-core/ioctl/validator"
)
// Multi-language support
var (
createAddCmdShorts = map[config.Language]string{
config.English: "Create new account for ioctl",
config.Chinese: "为ioctl创建新账户",
}
createAddCmdUses = map[config.Language]string{
config.English: "createadd ALIAS",
config.Chinese: "createadd 别名",
}
)
// accountCreateAddCmd represents the account createadd command
var accountCreateAddCmd = &cobra.Command{
Use: config.TranslateInLang(createAddCmdUses, config.UILanguage),
Short: config.TranslateInLang(createAddCmdShorts, config.UILanguage),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
err := accountCreateAdd(args)
return output.PrintError(err)
},
}
func accountCreateAdd(args []string) error {
// Validate inputs
if err := validator.ValidateAlias(args[0]); err != nil {
return output.NewError(output.ValidationError, "invalid alias", err)
}
alias := args[0]
if addr, ok := config.ReadConfig.Aliases[alias]; ok {
var confirm string
info := fmt.Sprintf("** Alias \"%s\" has already used for %s\n"+
"Overwriting the account will keep the previous keystore file stay, "+
"but bind the alias to the new one.\nWould you like to continue?\n", alias, addr)
message := output.ConfirmationMessage{Info: info, Options: []string{"yes"}}
fmt.Println(message.String())
fmt.Scanf("%s", &confirm)
if !strings.EqualFold(confirm, "yes") {
output.PrintResult("quit")
return nil
}
}
var addr string
var err error
if CryptoSm2 {
addr, err = newAccountSm2(alias)
if err != nil {
return output.NewError(0, "", err)
}
} else {
addr, err = newAccount(alias)
if err != nil {
return output.NewError(0, "", err)
}
}
config.ReadConfig.Aliases[alias] = addr
out, err := yaml.Marshal(&config.ReadConfig)
if err != nil {
return output.NewError(output.SerializationError, "failed to marshal config", err)
}
if err := ioutil.WriteFile(config.DefaultConfigFile, out, 0600); err != nil {
return output.NewError(output.WriteFileError,
fmt.Sprintf("failed to write to config file %s", config.DefaultConfigFile), err)
}
output.PrintResult(fmt.Sprintf("New account \"%s\" is created.\n"+
"Please Keep your password, or you will lose your private key.", alias))
return nil
}