-
Notifications
You must be signed in to change notification settings - Fork 1
/
commandline.go
142 lines (113 loc) · 2.55 KB
/
commandline.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
package main
import (
"fmt"
"os"
)
func (cli *CLI) CreateBlockChain(address string) {
if !IsValidAddress(address) {
fmt.Printf("地址无效,请检查\n")
return
}
//3. 调用真正的添加区块函数
bc := CreateBlockChain(address)
defer bc.Db.Close()
}
func (cli *CLI) PrintChain() {
bc := NewBlockChain()
//defer bc.Db.Close()
bc.PrintChain()
}
func (cli *CLI) PrintTx() {
bc := NewBlockChain()
//defer bc.Db.Close()
//bc.PrintChain()
it := bc.NewIterator()
for {
block := it.Next()
fmt.Printf("++++++++++++++++++++++++++++++++++\n")
for _, tx := range block.Transactions {
fmt.Println(tx)
}
if len(block.PrevBlockHash) == 0 {
break
}
}
}
func (cli *CLI) GetBalance(address string) {
if !IsValidAddress(address) {
fmt.Printf("地址无效,请检查\n")
return
}
bc := NewBlockChain()
bc.GetBalance(address)
}
func (cli *CLI) Send(from, to string, amount float64) {
if !IsValidAddress(from) {
fmt.Printf("from : %s 地址无效,请检查\n", from)
return
}
if !IsValidAddress(to) {
fmt.Printf("to : %s 地址无效,请检查\n", to)
return
}
fmt.Printf("%s 向 %s 转账 %f\n", from, to, amount)
bc := NewBlockChain()
if bc == nil {
return
}
//defer bc.Db.Close()
//txs := []*Transaction{coinbase}
//2. 创建普通交易
tx := NewTransaction(from, to, amount, bc)
if tx != nil {
//3. 添加交易到交易数组
gTx = append(gTx, tx)
} else {
fmt.Printf("余额不足,创建交易失败\n")
}
}
func (cli *CLI) Mine(miner, data string) {
//1. 创建挖矿交易
coinbase := NewCoinbaseTx(miner, data)
//先放着最后面
gTx = append(gTx, coinbase)
bc := NewBlockChain()
if bc == nil {
return
}
//defer bc.Db.Close()
//4. 添加交易到区块链AddBlock
bc.AddBlock(gTx)
//应该清楚全局gTx
gTx = []*Transaction{}
fmt.Printf("区块创建成功!\n")
//./blockchian send FROM TO AMOUNT MINER DATA "转账"
}
func (cli *CLI) CreateWallet() {
//w := NewWallet()
//address := w.GetAddress()
ws := NewWallets()
address := ws.CreateWallet()
if address == "" {
fmt.Printf("创建地址失败\n")
os.Exit(1)
}
fmt.Printf("你的新地址为: %s\n", address)
}
func (cli *CLI) ListAllAddress() {
//1. 打开钱包
//2. 遍历里面的map
//3. 将所有的key(地址)返回来
ws := NewWallets()
//返回一个地址的数组
addresses := ws.GetAddresses()
for _, address := range addresses {
fmt.Printf("%s\n", address)
}
}
func (cli *CLI) Help() {
fmt.Printf(Usage)
}
func (cli *CLI)Status() {
fmt.Printf("待确认交易数量: %d\n", len(gTx))
}