/
cli.go
149 lines (120 loc) · 3.31 KB
/
cli.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
package cli
import (
"flag"
"fmt"
"log"
"os"
"github.com/Cijin/gochain/pkg/blockchain"
"github.com/Cijin/gochain/pkg/transaction"
)
type CLI struct{}
func (cli *CLI) printUsage() {
fmt.Println("Usage:")
fmt.Println(" printchain - print all the blocks of the blockchain")
fmt.Println(" create -address <WALLET_ADDRESS> - create a new blockchain")
fmt.Println(" getbalance -address <WALLET_ADDRESS> - get your current balance")
fmt.Println(" send -from FROM -to TO -amount AMOUNT - Send AMOUNT of coins from FROM address to TO")
}
func (cli *CLI) validateArgs() {
if len(os.Args) < 2 {
cli.printUsage()
os.Exit(1)
}
}
func (cli *CLI) createBlockchain(address string) {
bc := blockchain.CreateBlockchain(address)
bc.Db.Close()
fmt.Println("Blockchain created!")
}
func (cli *CLI) printChain() {
bc := blockchain.NewBlockchain()
bci := bc.Iterator()
for {
prevBlock := bci.Previous()
fmt.Printf("Previous hash: %x\n", prevBlock.PrevBlockHash)
fmt.Printf("Hash: %x\n", prevBlock.Hash)
fmt.Println()
if len(prevBlock.PrevBlockHash) == 0 {
break
}
}
}
func (cli *CLI) getBalance(address string) {
bc := blockchain.NewBlockchain()
defer bc.Db.Close()
var balance int
unspentTxOutputs := bc.FindUnspentTransactionOutputs(address)
for _, out := range unspentTxOutputs {
balance += out.Value
}
fmt.Printf("Balance of '%s': %d\n", address, balance)
}
func (cli *CLI) send(from, to string, amount int) {
bc := blockchain.NewBlockchain()
defer bc.Db.Close()
tx := blockchain.NewUnspentTxs(from, to, amount, bc)
bc.MineBlock([]*transaction.Transaction{tx})
fmt.Println("Success!")
}
// Run parses command line arguments and processes commands
func (cli *CLI) Run() {
cli.validateArgs()
createChainCmd := flag.NewFlagSet("create", flag.ExitOnError)
sendCmd := flag.NewFlagSet("send", flag.ExitOnError)
printChainCmd := flag.NewFlagSet("printchain", flag.ExitOnError)
getbalanceCmd := flag.NewFlagSet("getbalance", flag.ExitOnError)
createBlockchainAddress := createChainCmd.String("address", "", "The address to send genesis block reward to")
getBalanceAddress := getbalanceCmd.String("address", "", "The address to get balance for")
sendFrom := sendCmd.String("from", "", "Source wallet address")
sendTo := sendCmd.String("to", "", "Destination wallet address")
sendAmount := sendCmd.Int("amount", 0, "Amount to send")
switch os.Args[1] {
case "create":
err := createChainCmd.Parse(os.Args[2:])
if err != nil {
log.Panic(err)
}
case "printchain":
err := printChainCmd.Parse(os.Args[2:])
if err != nil {
log.Panic(err)
}
case "getbalance":
err := getbalanceCmd.Parse(os.Args[2:])
if err != nil {
log.Panic(err)
}
case "send":
err := sendCmd.Parse(os.Args[2:])
if err != nil {
log.Panic(err)
}
default:
cli.printUsage()
os.Exit(1)
}
if createChainCmd.Parsed() {
if *createBlockchainAddress == "" {
createChainCmd.Usage()
os.Exit(1)
}
cli.createBlockchain(*createBlockchainAddress)
}
if getbalanceCmd.Parsed() {
if *getBalanceAddress == "" {
getbalanceCmd.Usage()
os.Exit(1)
}
cli.getBalance(*getBalanceAddress)
}
if printChainCmd.Parsed() {
cli.printChain()
}
if sendCmd.Parsed() {
if *sendFrom == "" || *sendTo == "" || *sendAmount <= 0 {
sendCmd.Usage()
os.Exit(1)
}
cli.send(*sendFrom, *sendTo, *sendAmount)
}
}