2017-08-28 09:03:43 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2017-08-29 00:09:47 -05:00
|
|
|
"flag"
|
2017-08-28 09:03:43 -05:00
|
|
|
"fmt"
|
2017-08-29 00:09:47 -05:00
|
|
|
"log"
|
2017-08-28 09:03:43 -05:00
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
|
|
|
// CLI responsible for processing command line arguments
|
2017-09-02 23:17:10 -05:00
|
|
|
type CLI struct{}
|
|
|
|
|
2017-09-07 21:53:26 -05:00
|
|
|
func (cli *CLI) printUsage() {
|
|
|
|
fmt.Println("Usage:")
|
|
|
|
fmt.Println(" createblockchain -address ADDRESS - Create a blockchain and send genesis block reward to ADDRESS")
|
|
|
|
fmt.Println(" createwallet - Generates a new key-pair and saves it into the wallet file")
|
|
|
|
fmt.Println(" getbalance -address ADDRESS - Get balance of ADDRESS")
|
|
|
|
fmt.Println(" listaddresses - Lists all addresses from the wallet file")
|
|
|
|
fmt.Println(" printchain - Print all the blocks of the blockchain")
|
|
|
|
fmt.Println(" send -from FROM -to TO -amount AMOUNT - Send AMOUNT of coins from FROM address to TO")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cli *CLI) validateArgs() {
|
|
|
|
if len(os.Args) < 2 {
|
|
|
|
cli.printUsage()
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-29 00:09:47 -05:00
|
|
|
// Run parses command line arguments and processes commands
|
|
|
|
func (cli *CLI) Run() {
|
2017-08-28 09:03:43 -05:00
|
|
|
cli.validateArgs()
|
|
|
|
|
2017-09-02 23:02:46 -05:00
|
|
|
getBalanceCmd := flag.NewFlagSet("getbalance", flag.ExitOnError)
|
2017-09-02 23:17:10 -05:00
|
|
|
createBlockchainCmd := flag.NewFlagSet("createblockchain", flag.ExitOnError)
|
2017-09-07 02:18:12 -05:00
|
|
|
createWalletCmd := flag.NewFlagSet("createwallet", flag.ExitOnError)
|
2017-09-07 21:51:44 -05:00
|
|
|
listAddressesCmd := flag.NewFlagSet("listaddresses", flag.ExitOnError)
|
2017-09-02 22:05:18 -05:00
|
|
|
sendCmd := flag.NewFlagSet("send", flag.ExitOnError)
|
2017-08-29 00:09:47 -05:00
|
|
|
printChainCmd := flag.NewFlagSet("printchain", flag.ExitOnError)
|
|
|
|
|
2017-09-02 23:02:46 -05:00
|
|
|
getBalanceAddress := getBalanceCmd.String("address", "", "The address to get balance for")
|
2017-09-02 23:17:10 -05:00
|
|
|
createBlockchainAddress := createBlockchainCmd.String("address", "", "The address to send genesis block reward to")
|
2017-09-02 22:05:18 -05:00
|
|
|
sendFrom := sendCmd.String("from", "", "Source wallet address")
|
|
|
|
sendTo := sendCmd.String("to", "", "Destination wallet address")
|
|
|
|
sendAmount := sendCmd.Int("amount", 0, "Amount to send")
|
2017-08-29 00:09:47 -05:00
|
|
|
|
2017-08-28 09:03:43 -05:00
|
|
|
switch os.Args[1] {
|
2017-09-02 23:02:46 -05:00
|
|
|
case "getbalance":
|
|
|
|
err := getBalanceCmd.Parse(os.Args[2:])
|
|
|
|
if err != nil {
|
|
|
|
log.Panic(err)
|
|
|
|
}
|
2017-09-02 23:17:10 -05:00
|
|
|
case "createblockchain":
|
|
|
|
err := createBlockchainCmd.Parse(os.Args[2:])
|
|
|
|
if err != nil {
|
|
|
|
log.Panic(err)
|
|
|
|
}
|
2017-09-07 02:18:12 -05:00
|
|
|
case "createwallet":
|
|
|
|
err := createWalletCmd.Parse(os.Args[2:])
|
|
|
|
if err != nil {
|
|
|
|
log.Panic(err)
|
|
|
|
}
|
2017-09-07 21:51:44 -05:00
|
|
|
case "listaddresses":
|
|
|
|
err := listAddressesCmd.Parse(os.Args[2:])
|
|
|
|
if err != nil {
|
|
|
|
log.Panic(err)
|
|
|
|
}
|
2017-09-02 22:05:18 -05:00
|
|
|
case "printchain":
|
|
|
|
err := printChainCmd.Parse(os.Args[2:])
|
2017-08-29 00:09:47 -05:00
|
|
|
if err != nil {
|
|
|
|
log.Panic(err)
|
|
|
|
}
|
2017-09-02 22:05:18 -05:00
|
|
|
case "send":
|
|
|
|
err := sendCmd.Parse(os.Args[2:])
|
2017-08-29 00:09:47 -05:00
|
|
|
if err != nil {
|
|
|
|
log.Panic(err)
|
|
|
|
}
|
2017-08-28 09:03:43 -05:00
|
|
|
default:
|
|
|
|
cli.printUsage()
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2017-08-29 00:09:47 -05:00
|
|
|
|
2017-09-02 23:02:46 -05:00
|
|
|
if getBalanceCmd.Parsed() {
|
|
|
|
if *getBalanceAddress == "" {
|
|
|
|
getBalanceCmd.Usage()
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
cli.getBalance(*getBalanceAddress)
|
|
|
|
}
|
|
|
|
|
2017-09-02 23:17:10 -05:00
|
|
|
if createBlockchainCmd.Parsed() {
|
|
|
|
if *createBlockchainAddress == "" {
|
|
|
|
createBlockchainCmd.Usage()
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
cli.createBlockchain(*createBlockchainAddress)
|
|
|
|
}
|
|
|
|
|
2017-09-07 02:18:12 -05:00
|
|
|
if createWalletCmd.Parsed() {
|
|
|
|
cli.createWallet()
|
|
|
|
}
|
|
|
|
|
2017-09-07 21:51:44 -05:00
|
|
|
if listAddressesCmd.Parsed() {
|
|
|
|
cli.listAddresses()
|
|
|
|
}
|
|
|
|
|
2017-09-02 22:05:18 -05:00
|
|
|
if printChainCmd.Parsed() {
|
|
|
|
cli.printChain()
|
|
|
|
}
|
|
|
|
|
|
|
|
if sendCmd.Parsed() {
|
|
|
|
if *sendFrom == "" || *sendTo == "" || *sendAmount <= 0 {
|
|
|
|
sendCmd.Usage()
|
2017-08-29 00:09:47 -05:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2017-09-02 22:05:18 -05:00
|
|
|
cli.send(*sendFrom, *sendTo, *sendAmount)
|
2017-08-29 00:09:47 -05:00
|
|
|
}
|
2017-08-28 09:03:43 -05:00
|
|
|
}
|