160 lines
3.8 KiB
Go
160 lines
3.8 KiB
Go
package main
|
|
|
|
import (
|
|
"flag"
|
|
"fmt"
|
|
"os"
|
|
"strconv"
|
|
)
|
|
|
|
const (
|
|
flagBalance = "balance"
|
|
flagCreate = "create"
|
|
flagSend = "send"
|
|
flagLedger = "ledger"
|
|
)
|
|
|
|
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)
|
|
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)
|
|
}
|
|
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)
|
|
}
|
|
}
|
|
|
|
// createBlockchain creates a new blockchain with a genesis block
|
|
func (cli *CLI) createBlockchain(address string) {
|
|
bc := CreateBlockchain(address)
|
|
bc.db.Close()
|
|
fmt.Println("Done!")
|
|
}
|
|
|
|
// 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
|
|
}
|
|
}
|
|
}
|
|
|
|
// getBalance retrieves the transaction balance for an address
|
|
func (cli *CLI) getBalance(address string) {
|
|
bc := NewBlockchain()
|
|
defer bc.db.Close()
|
|
|
|
balance := 0
|
|
UTXOs := bc.FindUTXO(address)
|
|
|
|
for _, out := range UTXOs {
|
|
balance += out.Value
|
|
}
|
|
|
|
fmt.Printf("Balance of '%s': %d\n", address, balance)
|
|
}
|
|
|
|
// 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")
|
|
}
|
|
|
|
// 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)
|
|
}
|