-
Notifications
You must be signed in to change notification settings - Fork 320
/
accountlist.go
99 lines (85 loc) · 2.59 KB
/
accountlist.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
// 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"
"strings"
"github.com/ethereum/go-ethereum/accounts/keystore"
"github.com/spf13/cobra"
"github.com/iotexproject/iotex-address/address"
"github.com/iotexproject/iotex-core/ioctl/cmd/alias"
"github.com/iotexproject/iotex-core/ioctl/config"
"github.com/iotexproject/iotex-core/ioctl/output"
)
// Multi-language support
var (
_listCmdShorts = map[config.Language]string{
config.English: "List existing account for ioctl",
config.Chinese: "列出ioctl中已存在的账户",
}
)
// _accountListCmd represents the account list command
var _accountListCmd = &cobra.Command{
Use: "list",
Short: config.TranslateInLang(_listCmdShorts, config.UILanguage),
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
err := accountList()
return output.PrintError(err)
},
}
type listMessage struct {
Accounts []account `json:"accounts"`
}
type account struct {
Address string `json:"address"`
Alias string `json:"alias"`
}
func accountList() error {
message := listMessage{}
aliases := alias.GetAliasMap()
if CryptoSm2 {
sm2Accounts, err := listSm2Account()
if err != nil {
return output.NewError(output.ReadFileError, "failed to get sm2 accounts", err)
}
for _, addr := range sm2Accounts {
message.Accounts = append(message.Accounts, account{
Address: addr,
Alias: aliases[addr],
})
}
} else {
ks := keystore.NewKeyStore(config.ReadConfig.Wallet,
keystore.StandardScryptN, keystore.StandardScryptP)
for _, v := range ks.Accounts() {
addr, err := address.FromBytes(v.Address.Bytes())
if err != nil {
return output.NewError(output.ConvertError, "failed to convert bytes into address", err)
}
message.Accounts = append(message.Accounts, account{
Address: addr.String(),
Alias: aliases[addr.String()],
})
}
}
fmt.Println(message.String())
return nil
}
func (m *listMessage) String() string {
if output.Format == "" {
lines := make([]string, 0)
for _, account := range m.Accounts {
line := account.Address
if account.Alias != "" {
line += " - " + account.Alias
}
lines = append(lines, line)
}
return strings.Join(lines, "\n")
}
return output.FormatString(output.Result, m)
}