-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
141 lines (133 loc) · 2.59 KB
/
main.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
package main
import (
"fmt"
"github.com/diiyw/chaingo/cli"
"log"
"os"
"strconv"
)
const VERSION = "0.0.1"
func main() {
usage := `Chaingo - an blockchain write by golang
Usage:
chaingo [command] options
Commands:
mine start mining
account account manage
chain print blockchain
version show chaingo version
help shows this help.
Options:
-h show command help
Author:
cheuk <zhuohong@live.com>`
if len(os.Args) == 1 {
fmt.Println(usage)
return
}
if os.Args[1] == "mine" {
var (
mineUsage = `Usage:
chaingo mine [-ip 0.0.0.0 -port 2048 -address]`
ip = "0.0.0.0"
port = 2048
)
if len(os.Args) < 8 || os.Args[1] == "-h" {
fmt.Println(mineUsage)
return
}
if os.Args[2] == "-ip" {
if len(os.Args) == 3 {
log.Println("ERROR: unspecified ip")
return
}
ip = os.Args[3]
}
if os.Args[4] == "-port" {
if len(os.Args) == 5 {
log.Println("ERROR: unspecified ip")
return
}
p, err := strconv.ParseInt(os.Args[5], 10, 32)
if err != nil {
log.Println("ERROR: error port")
}
port = int(p)
}
if os.Args[6] == "-address" {
if len(os.Args) == 7 {
log.Println("ERROR: unspecified address")
return
}
cli.StartMine(ip, port, os.Args[7])
return
}
fmt.Println(mineUsage)
}
if os.Args[1] == "account" {
accountUsage := `Usage:
chaingo account [-new|-list|-fund|-send]`
if len(os.Args) == 2 || os.Args[1] == "-h" {
fmt.Println(accountUsage)
return
}
if os.Args[2] == "-list" {
cli.ListAccount()
return
}
if os.Args[2] == "-new" {
if err := cli.NewAccount(); err != nil {
log.Fatal(err)
}
return
}
if os.Args[2] == "-fund" {
if len(os.Args) == 3 {
log.Println("ERROR: please enter an address")
return
}
cli.Fund(os.Args[3])
return
}
if os.Args[2] == "-send" {
if len(os.Args) <= 5 {
fmt.Println(`Usage:
chaingo account -send (amount) (from) (to)`)
return
}
p, err := strconv.ParseInt(os.Args[3], 10, 32)
if err != nil {
log.Println("ERROR: error amount")
}
cli.Send(int(p), os.Args[4], os.Args[5])
return
}
fmt.Println(accountUsage)
}
if os.Args[1] == "version" {
fmt.Println("Version:", VERSION)
return
}
if os.Args[1] == "chain" {
accountUsage := `Usage:
chaingo chain [print|create]`
if len(os.Args) == 2 || os.Args[1] == "-h" {
fmt.Println(accountUsage)
return
}
if os.Args[2] == "print" {
cli.PrintChain()
return
}
if os.Args[2] == "create" {
cli.CreateGenesisBlock()
return
}
fmt.Println(accountUsage)
}
if os.Args[1] == "help" {
fmt.Println(usage)
return
}
fmt.Println(usage)
}