Extract CLI commands into separate files

This commit is contained in:
Ivan Kuznetsov 2017-09-10 12:53:06 +07:00
parent bb70b4924b
commit 5f9e6c0c91
7 changed files with 99 additions and 76 deletions

76
cli.go
View File

@ -5,87 +5,11 @@ import (
"fmt"
"log"
"os"
"strconv"
)
// CLI responsible for processing command line arguments
type CLI struct{}
func (cli *CLI) createBlockchain(address string) {
bc := CreateBlockchain(address)
bc.db.Close()
fmt.Println("Done!")
}
func (cli *CLI) createWallet() {
wallets, _ := NewWallets()
address := wallets.CreateWallet()
wallets.SaveToFile()
fmt.Printf("Your new address: %s\n", address)
}
func (cli *CLI) getBalance(address string) {
bc := NewBlockchain(address)
defer bc.db.Close()
balance := 0
pubKeyHash := Base58Decode([]byte(address))
pubKeyHash = pubKeyHash[1 : len(pubKeyHash)-4]
UTXOs := bc.FindUTXO(pubKeyHash)
for _, out := range UTXOs {
balance += out.Value
}
fmt.Printf("Balance of '%s': %d\n", address, balance)
}
func (cli *CLI) listAddresses() {
wallets, err := NewWallets()
if err != nil {
log.Panic(err)
}
addresses := wallets.GetAddresses()
for _, address := range addresses {
fmt.Println(address)
}
}
func (cli *CLI) printChain() {
bc := NewBlockchain("")
defer bc.db.Close()
bci := bc.Iterator()
for {
block := bci.Next()
fmt.Printf("Prev. hash: %x\n", block.PrevBlockHash)
fmt.Printf("Hash: %x\n", block.Hash)
pow := NewProofOfWork(block)
fmt.Printf("PoW: %s\n", strconv.FormatBool(pow.Validate()))
for _, tx := range block.Transactions {
fmt.Println(tx)
}
fmt.Println()
if len(block.PrevBlockHash) == 0 {
break
}
}
}
func (cli *CLI) send(from, to string, amount int) {
bc := NewBlockchain(from)
defer bc.db.Close()
tx := NewUTXOTransaction(from, to, amount, bc)
bc.MineBlock([]*Transaction{tx})
fmt.Println("Success!")
}
func (cli *CLI) printUsage() {
fmt.Println("Usage:")
fmt.Println(" createblockchain -address ADDRESS - Create a blockchain and send genesis block reward to ADDRESS")

9
cli_createblockchain.go Normal file
View File

@ -0,0 +1,9 @@
package main
import "fmt"
func (cli *CLI) createBlockchain(address string) {
bc := CreateBlockchain(address)
bc.db.Close()
fmt.Println("Done!")
}

11
cli_createwallet.go Normal file
View File

@ -0,0 +1,11 @@
package main
import "fmt"
func (cli *CLI) createWallet() {
wallets, _ := NewWallets()
address := wallets.CreateWallet()
wallets.SaveToFile()
fmt.Printf("Your new address: %s\n", address)
}

19
cli_getbalance.go Normal file
View File

@ -0,0 +1,19 @@
package main
import "fmt"
func (cli *CLI) getBalance(address string) {
bc := NewBlockchain(address)
defer bc.db.Close()
balance := 0
pubKeyHash := Base58Decode([]byte(address))
pubKeyHash = pubKeyHash[1 : len(pubKeyHash)-4]
UTXOs := bc.FindUTXO(pubKeyHash)
for _, out := range UTXOs {
balance += out.Value
}
fmt.Printf("Balance of '%s': %d\n", address, balance)
}

18
cli_listaddress.go Normal file
View File

@ -0,0 +1,18 @@
package main
import (
"fmt"
"log"
)
func (cli *CLI) listAddresses() {
wallets, err := NewWallets()
if err != nil {
log.Panic(err)
}
addresses := wallets.GetAddresses()
for _, address := range addresses {
fmt.Println(address)
}
}

30
cli_printchain.go Normal file
View File

@ -0,0 +1,30 @@
package main
import (
"fmt"
"strconv"
)
func (cli *CLI) printChain() {
bc := NewBlockchain("")
defer bc.db.Close()
bci := bc.Iterator()
for {
block := bci.Next()
fmt.Printf("Prev. hash: %x\n", block.PrevBlockHash)
fmt.Printf("Hash: %x\n", block.Hash)
pow := NewProofOfWork(block)
fmt.Printf("PoW: %s\n", strconv.FormatBool(pow.Validate()))
for _, tx := range block.Transactions {
fmt.Println(tx)
}
fmt.Println()
if len(block.PrevBlockHash) == 0 {
break
}
}
}

12
cli_send.go Normal file
View File

@ -0,0 +1,12 @@
package main
import "fmt"
func (cli *CLI) send(from, to string, amount int) {
bc := NewBlockchain(from)
defer bc.db.Close()
tx := NewUTXOTransaction(from, to, amount, bc)
bc.MineBlock([]*Transaction{tx})
fmt.Println("Success!")
}