Extract CLI commands into separate files
This commit is contained in:
parent
bb70b4924b
commit
5f9e6c0c91
76
cli.go
76
cli.go
|
@ -5,87 +5,11 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// CLI responsible for processing command line arguments
|
// CLI responsible for processing command line arguments
|
||||||
type CLI struct{}
|
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() {
|
func (cli *CLI) printUsage() {
|
||||||
fmt.Println("Usage:")
|
fmt.Println("Usage:")
|
||||||
fmt.Println(" createblockchain -address ADDRESS - Create a blockchain and send genesis block reward to ADDRESS")
|
fmt.Println(" createblockchain -address ADDRESS - Create a blockchain and send genesis block reward to ADDRESS")
|
||||||
|
|
|
@ -0,0 +1,9 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
func (cli *CLI) createBlockchain(address string) {
|
||||||
|
bc := CreateBlockchain(address)
|
||||||
|
bc.db.Close()
|
||||||
|
fmt.Println("Done!")
|
||||||
|
}
|
|
@ -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)
|
||||||
|
}
|
|
@ -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)
|
||||||
|
}
|
|
@ -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)
|
||||||
|
}
|
||||||
|
}
|
|
@ -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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -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!")
|
||||||
|
}
|
Loading…
Reference in New Issue