-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommandLine.go
More file actions
76 lines (68 loc) · 1.54 KB
/
Copy pathCommandLine.go
File metadata and controls
76 lines (68 loc) · 1.54 KB
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
package main
import (
"fmt"
)
func (cli *CLI) PrintBlockChain() {
cli.bc.PrintChain()
fmt.Println("Print blockchain success")
}
func (cli *CLI) PrintBlockChainReverse() {
bc := cli.bc
it := bc.NewIterator()
for {
block := it.Next()
for _, tx := range block.Transactions {
fmt.Println(tx)
}
if len(block.PrevHash) == 0 {
fmt.Printf("==Blockchain range end==")
break
}
}
}
func (cli *CLI) GetBalance(address string) {
if !IsVailAddress(address) {
fmt.Printf("Address invalid: %s\n", address)
return
}
pubKeyHash := GetPubKeyFromAddress(address)
utxos := cli.bc.FindUTXOs(pubKeyHash)
total := 0.0
for _, utxo := range utxos {
total += utxo.Value
}
fmt.Printf("%s have balance: %f\n", address, total)
}
func (cli *CLI) Transfer(from, to string, amount float64, miner, data string) {
if !IsVailAddress(from) {
fmt.Printf("From address invalid: %s\n", from)
return
}
if !IsVailAddress(to) {
fmt.Printf("To address invalid: %s\n", to)
return
}
if !IsVailAddress(miner) {
fmt.Printf("Miner address invalid: %s\n", miner)
return
}
mining := NewMiningTX(miner, data)
tx := NewTransaction(from, to, amount, cli.bc)
if tx == nil {
return
}
cli.bc.AddBlock([]*Transaction{mining, tx})
fmt.Println("Transfer success!")
}
func (cli *CLI) CliNewWallet() {
ws := NewWallets()
address := ws.CreateWallet()
fmt.Printf("Address: %s\n", address)
}
func (cli *CLI) ListAddresses() {
ws := NewWallets()
addresses := ws.ListAllAddresses()
for _, address := range addresses {
fmt.Printf("Address: %s\n", address)
}
}