-
Notifications
You must be signed in to change notification settings - Fork 2
/
init.go
115 lines (102 loc) · 3.33 KB
/
init.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
package cli
import (
"fmt"
"strings"
"github.com/ethereum/go-ethereum/accounts/keystore"
"github.com/ethereum/go-ethereum/console"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
func (cli *CLI) buildInitCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "init",
Short: "Initialize config file and bank",
DisableFlagsInUseLine: true,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("Initialize config file")
prompt := fmt.Sprintf("Enter file in which to save (%s): ", defaultConfigFile)
configPath, err := console.Stdin.PromptInput(prompt)
if err != nil {
fmt.Println("PromptInput err:", err)
}
if configPath == "" {
configPath = defaultConfigFile
}
cli.config = configPath
walletPathV := viper.GetString("walletPath")
prompt = fmt.Sprintf("Enter the wallet storage directory (%s): ", walletPathV)
walletPath, err := console.Stdin.PromptInput(prompt)
if err != nil {
fmt.Println("PromptInput err:", err)
}
if walletPath == "" {
walletPath = walletPathV
}
viper.Set("walletPath", walletPath)
rpcURLV := viper.GetString("rpcURL")
prompt = fmt.Sprintf("Enter NewChain json rpc or ipc url (%s): ", rpcURLV)
rpcURL, err := console.Stdin.PromptInput(prompt)
if err != nil {
fmt.Println("PromptInput err:", err)
}
if rpcURL == "" {
rpcURL = rpcURLV
}
viper.Set("rpcURL", rpcURL)
privateKeyV := viper.GetString("Bank.RSAPEMPrivateKeyFile")
prompt = fmt.Sprintf("Enter path of Private Key RSA Pem file(%s): ", privateKeyV)
privateKey, err := console.Stdin.PromptInput(prompt)
if err != nil {
fmt.Println("PromptInput err:", err)
}
if privateKey == "" {
privateKey = privateKeyV
}
viper.Set("Bank.RSAPEMPrivateKeyFile", privateKey)
publicKeyV := viper.GetString("Bank.RSAPEMPublicKeyFile")
prompt = fmt.Sprintf("Enter path of Public Key RSA Pem file(%s): ", publicKeyV)
publicKey, err := console.Stdin.PromptInput(prompt)
if err != nil {
fmt.Println("PromptInput err:", err)
}
if publicKey == "" {
publicKey = publicKeyV
}
viper.Set("Bank.RSAPEMPublicKeyFile", publicKey)
prompt = fmt.Sprintf("Create a new bank account or not: [Y/n] ")
createNewAddress, err := console.Stdin.PromptInput(prompt)
if err != nil {
fmt.Println("PromptInput err:", err)
}
if len(createNewAddress) <= 0 {
createNewAddress = "Y"
}
if strings.ToUpper(createNewAddress[:1]) == "Y" {
wallet := keystore.NewKeyStore(walletPath,
keystore.StandardScryptN, keystore.StandardScryptP)
walletPassword, err := getPassPhrase("Your new account is locked with a password. Please give a password. Do not forget this password.", true)
if err == nil {
account, err := wallet.NewAccount(walletPassword)
if err == nil {
fromAddress := account.Address.String()
fmt.Println(fromAddress)
viper.Set("Bank.Address", fromAddress)
} else {
fmt.Println("Account error:", err)
fmt.Println("Just create your account later.")
}
} else {
fmt.Println("Error: ", err)
fmt.Println("Just create your account later.")
}
}
err = viper.WriteConfigAs(configPath)
if err != nil {
fmt.Println("WriteConfig:", err)
} else {
fmt.Println("Your configuration has been saved in ", configPath)
}
},
}
return cmd
}