-
Notifications
You must be signed in to change notification settings - Fork 320
/
accountupdate.go
136 lines (122 loc) · 4.8 KB
/
accountupdate.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
// 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 (
"bytes"
"fmt"
"github.com/ethereum/go-ethereum/accounts/keystore"
"github.com/spf13/cobra"
"github.com/iotexproject/go-pkgs/crypto"
"github.com/iotexproject/go-pkgs/hash"
"github.com/iotexproject/iotex-address/address"
"github.com/iotexproject/iotex-core/ioctl/config"
"github.com/iotexproject/iotex-core/ioctl/output"
"github.com/iotexproject/iotex-core/ioctl/util"
)
// Multi-language support
var (
_updateCmdShorts = map[config.Language]string{
config.English: "Update password for IoTeX account",
config.Chinese: "为IoTeX账户更新密码",
}
_updateCmdUses = map[config.Language]string{
config.English: "update [ALIAS|ADDRESS]",
config.Chinese: "update [别名|地址]",
}
)
// _accountUpdateCmd represents the account update command
var _accountUpdateCmd = &cobra.Command{
Use: config.TranslateInLang(_updateCmdUses, config.UILanguage),
Short: config.TranslateInLang(_updateCmdShorts, config.UILanguage),
Args: cobra.RangeArgs(0, 1),
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
arg := ""
if len(args) == 1 {
arg = args[0]
}
err := accountUpdate(arg)
return output.PrintError(err)
},
}
func accountUpdate(arg string) error {
account, err := util.GetAddress(arg)
if err != nil {
return output.NewError(output.AddressError, "", err)
}
addr, err := address.FromString(account)
if err != nil {
return output.NewError(output.ConvertError, "failed to convert string into addr", err)
}
if CryptoSm2 {
// find the pem file and update
filePath, err := findSm2PemFile(addr)
if err != nil {
return output.NewError(output.ReadFileError, fmt.Sprintf("crypto file of account #%s not found", addr), err)
}
output.PrintQuery(fmt.Sprintf("#%s: Enter current password\n", account))
currentPassword, err := util.ReadSecretFromStdin()
if err != nil {
return output.NewError(output.InputError, "failed to get current password", err)
}
_, err = crypto.ReadPrivateKeyFromPem(filePath, currentPassword)
if err != nil {
return output.NewError(output.CryptoError, "error occurs when checking current password", err)
}
output.PrintQuery(fmt.Sprintf("#%s: Enter new password\n", account))
password, err := util.ReadSecretFromStdin()
if err != nil {
return output.NewError(output.InputError, "failed to get new password", err)
}
output.PrintQuery(fmt.Sprintf("#%s: Enter new password again\n", account))
passwordAgain, err := util.ReadSecretFromStdin()
if err != nil {
return output.NewError(output.InputError, "failed to get new password", err)
}
if password != passwordAgain {
return output.NewError(output.ValidationError, ErrPasswdNotMatch.Error(), nil)
}
if err := crypto.UpdatePrivateKeyPasswordToPem(filePath, currentPassword, password); err != nil {
return output.NewError(output.KeystoreError, "failed to update pem file", err)
}
} else {
// find the keystore and update
ks := keystore.NewKeyStore(config.ReadConfig.Wallet,
keystore.StandardScryptN, keystore.StandardScryptP)
for _, v := range ks.Accounts() {
if bytes.Equal(addr.Bytes(), v.Address.Bytes()) {
output.PrintQuery(fmt.Sprintf("#%s: Enter current password\n", account))
currentPassword, err := util.ReadSecretFromStdin()
if err != nil {
return output.NewError(output.InputError, "failed to get current password", err)
}
_, err = ks.SignHashWithPassphrase(v, currentPassword, hash.ZeroHash256[:])
if err != nil {
return output.NewError(output.KeystoreError, "error occurs when checking current password", err)
}
output.PrintQuery(fmt.Sprintf("#%s: Enter new password\n", account))
password, err := util.ReadSecretFromStdin()
if err != nil {
return output.NewError(output.InputError, "failed to get new password", err)
}
output.PrintQuery(fmt.Sprintf("#%s: Enter new password again\n", account))
passwordAgain, err := util.ReadSecretFromStdin()
if err != nil {
return output.NewError(output.InputError, "failed to get new password", err)
}
if password != passwordAgain {
return output.NewError(output.ValidationError, ErrPasswdNotMatch.Error(), nil)
}
if err := ks.Update(v, currentPassword, password); err != nil {
return output.NewError(output.KeystoreError, "failed to update keystore", err)
}
output.PrintResult(fmt.Sprintf("Account #%s has been updated.", account))
return nil
}
}
}
output.PrintResult(fmt.Sprintf("Account #%s has been updated.", account))
return nil
}