-
Notifications
You must be signed in to change notification settings - Fork 320
/
accountimport.go
196 lines (187 loc) · 6.57 KB
/
accountimport.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// Copyright (c) 2019 IoTeX Foundation
// 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"
"os"
"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/util"
"github.com/iotexproject/iotex-core/ioctl/validator"
)
// Multi-language support
var (
_importCmdShorts = map[config.Language]string{
config.English: "Import IoTeX private key or keystore into wallet",
config.Chinese: "将IoTeX的私钥或私钥库导入钱包",
}
_importKeyCmdShorts = map[config.Language]string{
config.English: "Import IoTeX private key into wallet",
config.Chinese: "将IoTeX的私钥导入钱包",
}
_importKeyCmdUses = map[config.Language]string{
config.English: "key ALIAS",
config.Chinese: "key 别名",
}
_importKeyStoreCmdShorts = map[config.Language]string{
config.English: "Import IoTeX keystore into wallet",
config.Chinese: "将IoTeX的私钥库导入钱包",
}
_importKeyStoreCmdUses = map[config.Language]string{
config.English: "keystore ALIAS FILEPATH",
config.Chinese: "keystore 别名 文件路径",
}
_importPemCmdShorts = map[config.Language]string{
config.English: "Import IoTeX key from pem file into wallet",
config.Chinese: "将IoTeX私钥从pem文件导入钱包",
}
_importPemCmdUses = map[config.Language]string{
config.English: "pem ALIAS FILEPATH",
config.Chinese: "pem 别名 文件路径",
}
)
var (
// _accountImportCmd represents the account import command
_accountImportCmd = &cobra.Command{
Use: "import",
Short: config.TranslateInLang(_importCmdShorts, config.UILanguage),
}
// _accountImportKeyCmd represents the account import key command
_accountImportKeyCmd = &cobra.Command{
Use: config.TranslateInLang(_importKeyCmdUses, config.UILanguage),
Short: config.TranslateInLang(_importKeyCmdShorts, config.UILanguage),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
err := accountImportKey(args)
return output.PrintError(err)
},
}
// _accountImportKeyStoreCmd represents the account import keystore command
_accountImportKeyStoreCmd = &cobra.Command{
Use: config.TranslateInLang(_importKeyStoreCmdUses, config.UILanguage),
Short: config.TranslateInLang(_importKeyStoreCmdShorts, config.UILanguage),
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
err := accountImportKeyStore(args)
return output.PrintError(err)
},
}
// _accountImportPemCmd represents the account import pem command
_accountImportPemCmd = &cobra.Command{
Use: config.TranslateInLang(_importPemCmdUses, config.UILanguage),
Short: config.TranslateInLang(_importPemCmdShorts, config.UILanguage),
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
err := accountImportPem(args)
return output.PrintError(err)
},
}
)
func init() {
_accountImportCmd.AddCommand(_accountImportKeyCmd)
_accountImportCmd.AddCommand(_accountImportKeyStoreCmd)
_accountImportCmd.AddCommand(_accountImportPemCmd)
}
func validateAlias(alias string) error {
if err := validator.ValidateAlias(alias); err != nil {
return err
}
if addr, ok := config.ReadConfig.Aliases[alias]; ok {
return fmt.Errorf("alias \"%s\" has already used for %s", alias, addr)
}
return nil
}
func writeToFile(alias, addr string) error {
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 := os.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. Keep your password, "+
"or you will lose your private key.", alias))
return nil
}
func readPasswordFromStdin() (string, error) {
password, err := util.ReadSecretFromStdin()
if err != nil {
return "", fmt.Errorf("failed to get password")
}
return password, nil
}
func accountImportKey(args []string) error {
// Validate inputs
alias := args[0]
err := validateAlias(alias)
if err != nil {
return output.NewError(output.ValidationError, "invalid alias", err)
}
output.PrintQuery(fmt.Sprintf("#%s: Enter your private key, "+
"which will not be exposed on the screen.", alias))
privateKey, err := readPasswordFromStdin()
privateKey = util.TrimHexPrefix(privateKey)
if err != nil {
return output.NewError(output.InputError, "failed to get password", err)
}
addr, err := newAccountByKey(alias, privateKey, config.ReadConfig.Wallet)
if err != nil {
return output.NewError(0, "", err)
}
return writeToFile(alias, addr)
}
func accountImportKeyStore(args []string) error {
// Validate inputs
alias := args[0]
err := validateAlias(alias)
if err != nil {
return output.NewError(output.ValidationError, "invalid alias", err)
}
_, err = os.Stat(args[1])
if err != nil {
return output.NewError(output.ReadFileError, "", err)
}
output.PrintQuery(fmt.Sprintf("#%s: Enter your password of keystore, "+
"which will not be exposed on the screen.", alias))
password, err := util.ReadSecretFromStdin()
if err != nil {
return output.NewError(output.InputError, "failed to get password", err)
}
addr, err := newAccountByKeyStore(alias, password, args[1], config.ReadConfig.Wallet)
if err != nil {
return output.NewError(0, "", err)
}
return writeToFile(alias, addr)
}
func accountImportPem(args []string) error {
// Validate inputs
alias := args[0]
err := validateAlias(alias)
if err != nil {
return output.NewError(output.ValidationError, "invalid alias", err)
}
_, err = os.Stat(args[1])
if err != nil {
return output.NewError(output.ReadFileError, "", err)
}
output.PrintQuery(fmt.Sprintf("#%s: Enter your password of pem file, "+
"which will not be exposed on the screen.", alias))
password, err := util.ReadSecretFromStdin()
if err != nil {
return output.NewError(output.InputError, "failed to get password", err)
}
addr, err := newAccountByPem(alias, password, args[1], config.ReadConfig.Wallet)
if err != nil {
return output.NewError(0, "", err)
}
return writeToFile(alias, addr)
}