blockchain-poc/cli.go

212 lines
5.3 KiB
Go

package main
import (
"errors"
"flag"
"fmt"
"os"
"strconv"
)
const (
flagBalance = "balance"
flagCreate = "create"
flagSend = "send"
flagLedger = "ledger"
flagCreateWallet = "createwallet"
flagListAddresses = "listaddresses"
)
type CLI struct{}
// Run parses command line arguments and processes commands
func (cli *CLI) Run() {
cli.validateArgs()
// Valid CLI flags
getBalanceCmd := flag.NewFlagSet(flagBalance, flag.ExitOnError)
createBlockchainCmd := flag.NewFlagSet(flagCreate, flag.ExitOnError)
createWalletCmd := flag.NewFlagSet(flagCreateWallet, flag.ExitOnError)
listAddressesCmd := flag.NewFlagSet(flagListAddresses, flag.ExitOnError)
sendCmd := flag.NewFlagSet(flagSend, flag.ExitOnError)
printChainCmd := flag.NewFlagSet(flagLedger, flag.ExitOnError)
getBalanceAddress := getBalanceCmd.String("address", "", "The address to get the balance of")
createBlockchainAddress := createBlockchainCmd.String("address", "", "The address to send the genesis block reward to")
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 flagBalance:
if err := getBalanceCmd.Parse(os.Args[2:]); err != nil {
exitWithError(err)
}
case flagCreate:
if err := createBlockchainCmd.Parse(os.Args[2:]); err != nil {
exitWithError(err)
}
case flagLedger:
if err := printChainCmd.Parse(os.Args[2:]); err != nil {
exitWithError(err)
}
case flagSend:
if err := sendCmd.Parse(os.Args[2:]); err != nil {
exitWithError(err)
}
case flagCreateWallet:
if err := createWalletCmd.Parse(os.Args[2:]); err != nil {
exitWithError(err)
}
case flagListAddresses:
if err := listAddressesCmd.Parse(os.Args[2:]); err != nil {
exitWithError(err)
}
default:
cli.printUsage()
os.Exit(1)
}
if getBalanceCmd.Parsed() {
if *getBalanceAddress == "" {
getBalanceCmd.Usage()
os.Exit(1)
}
cli.getBalance(*getBalanceAddress)
}
if createBlockchainCmd.Parsed() {
if *createBlockchainAddress == "" {
createBlockchainCmd.Usage()
os.Exit(1)
}
cli.createBlockchain(*createBlockchainAddress)
}
if printChainCmd.Parsed() {
cli.printChain()
}
if sendCmd.Parsed() {
if *sendFrom == "" || *sendTo == "" || *sendAmount <= 0 {
sendCmd.Usage()
os.Exit(1)
}
cli.send(*sendFrom, *sendTo, *sendAmount)
}
if createWalletCmd.Parsed() {
cli.createWallet()
}
if listAddressesCmd.Parsed() {
cli.listAddresses()
}
}
// createBlockchain creates a new blockchain with a genesis block
func (cli *CLI) createBlockchain(address string) {
if !ValidateAddress(address) {
exitWithError(errors.New("Address is not valid"))
}
bc := CreateBlockchain(address)
bc.db.Close()
fmt.Println("Done!")
}
// createWallet creates a wallet and outputs the new address
func (cli *CLI) createWallet() {
wallets, _ := NewWallets()
address := wallets.CreateWallet()
wallets.SaveToFile()
fmt.Printf("Your new address: %s\n", address)
}
// getBalance retrieves the transaction balance for an address
func (cli *CLI) getBalance(address string) {
if !ValidateAddress(address) {
exitWithError(errors.New("Invalid Address"))
}
bc := NewBlockchain()
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)
}
// listAddresses outputs all addresses from the wallets file
func (cli *CLI) listAddresses() {
wallets, err := NewWallets()
if err != nil {
exitWithError(err)
}
addresses := wallets.GetAddresses()
for _, address := range addresses {
fmt.Println(address)
}
}
// printChain outputs the entire ledger
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()))
fmt.Println()
if len(block.PrevBlockHash) == 0 {
break
}
}
}
// send Triggers a transaction exchange
func (cli *CLI) send(from, to string, amount int) {
bc := NewBlockchain()
defer bc.db.Close()
tx := NewUTXOTransaction(from, to, amount, bc)
bc.MineBlock([]*Transaction{tx})
fmt.Println("Success!")
}
// printUsage prints the usage of the cli app
func (cli *CLI) printUsage() {
fmt.Println("Usage:")
fmt.Println("\t" + flagBalance + " -address ADDRESS\tGet balance of ADDRESS")
fmt.Println("\t" + flagCreate + " -address ADDRESS\tCreate a blockchain and send genesis block reward to ADDRESS")
fmt.Println("\t" + flagLedger + "\t\t\tprint all the blocks of the blockchain")
fmt.Println("\t" + flagSend + " -from FROM -to TO -amount AMOUNT\tSend AMOUNT of coins from FROM address to TO address")
fmt.Println("\t" + flagCreateWallet + "\t\t\tGenerates a new key-pair and saves it into the wallet store")
fmt.Println("\t" + flagListAddresses + "\t\t\tList all addresses from the wallet store")
}
// validateArgs just makes sure that we've got a couple arguments
func (cli *CLI) validateArgs() {
if len(os.Args) < 2 {
cli.printUsage()
os.Exit(1)
}
}
func exitWithError(err error) {
fmt.Println(err)
os.Exit(1)
}