@@ -3,19 +3,38 @@ package cli
33import (
44 "flag"
55 "fmt"
6- "github.com/sasaxie/go-blockchain/core"
76 "log"
87 "os"
9- "strconv"
108)
119
10+ // CLI responsible for processing command line arguments
1211type CLI struct {}
1312
13+ func (cli * CLI ) printUsage () {
14+ fmt .Println ("Usage:" )
15+ fmt .Println (" createblockchain -address ADDRESS - Create a blockchain and send genesis block reward to ADDRESS" )
16+ fmt .Println (" createwallet - Generates a new key-pair and saves it into the wallet file" )
17+ fmt .Println (" getbalance -address ADDRESS - Get balance of ADDRESS" )
18+ fmt .Println (" listaddresses - Lists all addresses from the wallet file" )
19+ fmt .Println (" printchain - Print all the blocks of the blockchain" )
20+ fmt .Println (" send -from FROM -to TO -amount AMOUNT - Send AMOUNT of coins from FROM address to TO" )
21+ }
22+
23+ func (cli * CLI ) validateArgs () {
24+ if len (os .Args ) < 2 {
25+ cli .printUsage ()
26+ os .Exit (1 )
27+ }
28+ }
29+
30+ // Run parses command line arguments and processes commands
1431func (cli * CLI ) Run () {
1532 cli .validateArgs ()
1633
1734 getBalanceCmd := flag .NewFlagSet ("getbalance" , flag .ExitOnError )
1835 createBlockchainCmd := flag .NewFlagSet ("createblockchain" , flag .ExitOnError )
36+ createWalletCmd := flag .NewFlagSet ("createwallet" , flag .ExitOnError )
37+ listAddressesCmd := flag .NewFlagSet ("listaddresses" , flag .ExitOnError )
1938 sendCmd := flag .NewFlagSet ("send" , flag .ExitOnError )
2039 printChainCmd := flag .NewFlagSet ("printchain" , flag .ExitOnError )
2140
@@ -36,6 +55,16 @@ func (cli *CLI) Run() {
3655 if err != nil {
3756 log .Panic (err )
3857 }
58+ case "createwallet" :
59+ err := createWalletCmd .Parse (os .Args [2 :])
60+ if err != nil {
61+ log .Panic (err )
62+ }
63+ case "listaddresses" :
64+ err := listAddressesCmd .Parse (os .Args [2 :])
65+ if err != nil {
66+ log .Panic (err )
67+ }
3968 case "printchain" :
4069 err := printChainCmd .Parse (os .Args [2 :])
4170 if err != nil {
@@ -67,6 +96,14 @@ func (cli *CLI) Run() {
6796 cli .createBlockchain (* createBlockchainAddress )
6897 }
6998
99+ if createWalletCmd .Parsed () {
100+ cli .createWallet ()
101+ }
102+
103+ if listAddressesCmd .Parsed () {
104+ cli .listAddresses ()
105+ }
106+
70107 if printChainCmd .Parsed () {
71108 cli .printChain ()
72109 }
@@ -80,68 +117,3 @@ func (cli *CLI) Run() {
80117 cli .send (* sendFrom , * sendTo , * sendAmount )
81118 }
82119}
83-
84- func (cli * CLI ) validateArgs () {
85- if len (os .Args ) < 2 {
86- cli .printUsage ()
87- os .Exit (1 )
88- }
89- }
90-
91- func (cli * CLI ) printUsage () {
92- fmt .Println ("Usage:" )
93- fmt .Println (" getbalance -address ADDRESS - Get balance of ADDRESS" )
94- fmt .Println (" createblockchain -address ADDRESS - Create a blockchain and send genesis block reward to ADDRESS" )
95- fmt .Println (" printchain - Print all the blocks of the blockchain" )
96- fmt .Println (" send -from FROM -to TO -amount AMOUNT - Send AMOUNT of coins from FROM address to TO" )
97- }
98-
99- func (cli * CLI ) printChain () {
100- bc := core .NewBlockchain ("" )
101- defer bc .DB .Close ()
102-
103- bci := bc .Iterator ()
104-
105- for {
106- block := bci .Next ()
107-
108- fmt .Printf ("Prev. hash: %x\n " , block .PrevBlockHash )
109- fmt .Printf ("Hash: %x\n " , block .Hash )
110- pow := core .NewProofOfWork (block )
111- fmt .Printf ("PoW: %s\n " , strconv .FormatBool (pow .Validate ()))
112- fmt .Println ()
113-
114- if len (block .PrevBlockHash ) == 0 {
115- break
116- }
117- }
118- }
119-
120- func (cli * CLI ) getBalance (address string ) {
121- bc := core .NewBlockchain (address )
122- defer bc .DB .Close ()
123-
124- balance := 0
125- UTXOs := bc .FindUTXO (address )
126-
127- for _ , out := range UTXOs {
128- balance += out .Value
129- }
130-
131- fmt .Printf ("Balance of '%s': %d\n " , address , balance )
132- }
133-
134- func (cli * CLI ) send (from , to string , amount int ) {
135- bc := core .NewBlockchain (from )
136- defer bc .DB .Close ()
137-
138- tx := core .NewUTXOTransaction (from , to , amount , bc )
139- bc .MineBlock ([]* core.Transaction {tx })
140- fmt .Println ("Success!" )
141- }
142-
143- func (cli * CLI ) createBlockchain (address string ) {
144- bc := core .CreateBlockchain (address )
145- bc .DB .Close ()
146- fmt .Println ("Done!" )
147- }
0 commit comments