-
Notifications
You must be signed in to change notification settings - Fork 18
/
command.go
200 lines (180 loc) · 4.71 KB
/
command.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
197
198
199
200
package main
import (
"strings"
"time"
"github.com/ethereum/go-ethereum/ethclient"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var (
number int
node string
)
// EtherScan 配置
type EtherScan struct {
Key string
URL string
}
type configure struct {
ElasticURL string
ElasticSniff bool
EthRPC string
MaxBalance float64
To []string
NetMode string
RawTx string
SignedTx string
DB string
GethRPC string
ParityRPC string
EtherscanRPC string
}
// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "ethereum-service",
Short: "Generate Ethereum account and sign tx",
}
// apiCmd represents the chain command
var genAccountCmd = &cobra.Command{
Use: "genaccount",
Short: "Generate ethereum account",
Run: func(cmd *cobra.Command, args []string) {
timeFormat := time.Now().Format("2006-01-02_15-04-05")
timeDir := strings.Join([]string{"version_1", timeFormat}, "_")
accountDir, err := mkdirBySlice([]string{HomeDir(), "account"})
if err != nil {
log.Fatalln("Fail to create account directory")
}
addresses := []*csvAddress{}
for index := 0; index < number; index++ {
address, err := createAccount(*accountDir, timeDir)
if err != nil {
log.Fatalln(err.Error())
}
addresses = append(addresses, &csvAddress{Address: *address})
}
export2CSV(addresses, *accountDir)
},
}
var syncCmd = &cobra.Command{
Use: "sync",
Short: "sync chain data to elasticsearch",
Run: func(cmd *cobra.Command, args []string) {
config.InitConfig()
sync()
},
}
var subscribeNewBlockCmd = &cobra.Command{
Use: "sub",
Short: "subscribe new block event",
Run: func(cmd *cobra.Command, args []string) {
config.InitConfig()
subNewBlockCmd()
},
}
var constructCmd = &cobra.Command{
Use: "construct",
Short: "construct transactio",
Run: func(cmd *cobra.Command, args []string) {
config.InitConfig()
if !Contains([]string{"geth", "parity", "etherscan"}, node) {
log.Errorln("Only support geth, parity, etherscan")
return
}
constructTxCmd()
},
}
var signCmd = &cobra.Command{
Use: "sign",
Short: "sigin transactio",
Run: func(cmd *cobra.Command, args []string) {
config.InitConfig()
signTxCmd()
},
}
var sendCmd = &cobra.Command{
Use: "send",
Short: "broadcast signex transaction to ethereum network",
Run: func(cmd *cobra.Command, args []string) {
config.InitConfig()
nodeClient, err := ethclient.Dial(config.EthRPC)
if err != nil {
log.Fatalln(err.Error())
}
sendTxCmd(nodeClient)
},
}
// Execute 命令行入口
func Execute() {
if err := rootCmd.Execute(); err != nil {
log.Fatalf(err.Error())
}
}
func (conf *configure) InitConfig() {
viper.SetConfigType("yaml")
viper.AddConfigPath(HomeDir())
viper.SetConfigName("ethereum-cold-wallet")
viper.AutomaticEnv() // read in environment variables that match
// If a config file is found, read it in.
err := viper.ReadInConfig()
if err == nil {
log.WithFields(log.Fields{
"Using Configure file": viper.ConfigFileUsed(),
"Time:": time.Now().Format("Mon Jan _2 15:04:05 2006"),
}).Info()
} else {
log.Fatal("Error: ethereum-cold-wallet.yml not found in: ", HomeDir())
}
for key, value := range viper.AllSettings() {
switch key {
case "elastic_url":
conf.ElasticURL = value.(string)
case "elastic_sniff":
conf.ElasticSniff = value.(bool)
case "eth_rpc":
conf.EthRPC = value.(string)
case "max_balance":
conf.MaxBalance = value.(float64)
case "to":
conf.To = viper.GetStringSlice(key)
case "net_mode":
conf.NetMode = value.(string)
case "raw_tx_path":
conf.RawTx = value.(string)
case "signed_tx_path":
conf.SignedTx = value.(string)
case "db_mysql":
conf.DB = value.(string)
case "geth_rpc":
conf.GethRPC = value.(string)
case "parity_rpc":
conf.ParityRPC = value.(string)
case "etherscan_rpc":
subv := viper.Sub("etherscan_rpc")
for subKey, subValue := range subv.AllSettings() {
switch subKey {
case "key":
etherscan.Key = subValue.(string)
case "url":
etherscan.URL = subValue.(string)
}
}
}
}
}
func init() {
config = new(configure)
etherscan = new(EtherScan)
initLogger()
rootCmd.AddCommand(genAccountCmd)
rootCmd.AddCommand(subscribeNewBlockCmd)
rootCmd.AddCommand(constructCmd)
rootCmd.AddCommand(signCmd)
rootCmd.AddCommand(sendCmd)
// rootCmd.AddCommand(syncCmd)
genAccountCmd.Flags().IntVarP(&number, "number", "n", 10, "Generate ethereum accounts")
genAccountCmd.MarkFlagRequired("number")
constructCmd.Flags().StringVarP(&node, "node", "n", "parity", "Ethereum node type, support geth, parity, etherscan")
constructCmd.MarkFlagRequired("node")
}