-
Notifications
You must be signed in to change notification settings - Fork 2
/
account.go
91 lines (77 loc) · 2.23 KB
/
account.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
package cli
import (
"fmt"
"github.com/ethereum/go-ethereum/accounts/keystore"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
func (cli *CLI) buildAccountCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "account [new|list]",
Short: "Manage NewChain accounts",
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
return
},
}
cmd.AddCommand(cli.buildAccountNewCmd())
cmd.AddCommand(cli.buildAccountListCmd())
return cmd
}
func (cli *CLI) buildAccountNewCmd() *cobra.Command {
accountNewCmd := &cobra.Command{
Use: "new [-n number] [--faucet]",
Short: "create a new account",
Args: cobra.MinimumNArgs(0),
DisableFlagsInUseLine: true,
Run: func(cmd *cobra.Command, args []string) {
walletPath := cli.walletPath
wallet := keystore.NewKeyStore(walletPath,
keystore.LightScryptN, keystore.LightScryptP)
walletPassword, err := getPassPhrase("Your new account is locked with a password. Please give a password. Do not forget this password.", true)
if err != nil {
fmt.Println("Error: ", err)
return
}
numOfNew, err := cmd.Flags().GetInt("numOfNew")
if err != nil {
numOfNew = viper.GetInt("account.numOfNew")
}
if numOfNew <= 0 {
fmt.Printf("number[%d] of new account less then 1\n", numOfNew)
numOfNew = 1
}
for i := 0; i < numOfNew; i++ {
account, err := wallet.NewAccount(walletPassword)
if err != nil {
fmt.Println("Account error:", err)
return
}
fmt.Println(account.Address.Hex())
}
},
}
accountNewCmd.Flags().IntP("numOfNew", "n", 1, "number of the new account")
return accountNewCmd
}
func (cli *CLI) buildAccountListCmd() *cobra.Command {
accountListCmd := &cobra.Command{
Use: "list",
Short: "list all accounts in the wallet path",
Args: cobra.MinimumNArgs(0),
DisableFlagsInUseLine: true,
Run: func(cmd *cobra.Command, args []string) {
walletPath := cli.walletPath
wallet := keystore.NewKeyStore(walletPath,
keystore.LightScryptN, keystore.LightScryptP)
if len(wallet.Accounts()) == 0 {
fmt.Println("Empty wallet, create account first.")
return
}
for _, account := range wallet.Accounts() {
fmt.Println(account.Address.Hex())
}
},
}
return accountListCmd
}