wallets
This commit is contained in:
9 files changed
+396
-162
No files matched your search
+180
@@ -0,0 +1,180 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
"runtime"
|
||||
"strconv"
|
||||
|
||||
"github.com/SowinskiBraeden/golang-blockchain/blockchain"
|
||||
"github.com/SowinskiBraeden/golang-blockchain/wallet"
|
||||
)
|
||||
|
||||
type CommandLine struct{}
|
||||
|
||||
func (cli *CommandLine) printUsage() {
|
||||
fmt.Println("Usage:")
|
||||
fmt.Println(" getbalance -address ADDRESS - get the balance for an address")
|
||||
fmt.Println(" createblockchain -address ADDRESS create a blockchain")
|
||||
fmt.Println(" printchain - Prints the blocks in the chain")
|
||||
fmt.Println(" send -from FROM -to TO -amount AMOUNT - Send amount to an address")
|
||||
fmt.Println(" createwallet - CReates a new Wallet")
|
||||
fmt.Println(" listaddresses - Lists the addresses in our wallet file")
|
||||
}
|
||||
|
||||
func (cli *CommandLine) validateArgs() {
|
||||
if len(os.Args) < 2 {
|
||||
cli.printUsage()
|
||||
runtime.Goexit()
|
||||
}
|
||||
}
|
||||
|
||||
func (cli *CommandLine) listAddresses() {
|
||||
wallets, _ := wallet.CreateWallets()
|
||||
addresses := wallets.GetAllAddresses()
|
||||
|
||||
for _, address := range addresses {
|
||||
fmt.Println(address)
|
||||
}
|
||||
}
|
||||
|
||||
func (cli *CommandLine) createWallet() {
|
||||
wallets, _ := wallet.CreateWallets()
|
||||
address := wallets.AddWallet()
|
||||
wallets.SaveFile()
|
||||
|
||||
fmt.Printf("New address is: %s\n", address)
|
||||
}
|
||||
|
||||
func (cli *CommandLine) printChain() {
|
||||
chain := blockchain.ContinueBlockChain("")
|
||||
defer chain.Database.Close()
|
||||
iter := chain.Iterator()
|
||||
|
||||
for {
|
||||
block := iter.Next()
|
||||
|
||||
fmt.Printf("Prev. Hash: %x\n", block.PrevHash)
|
||||
fmt.Printf("Hash: %x\n", block.Hash)
|
||||
pow := blockchain.NewProof(block)
|
||||
fmt.Printf("PoW: %s\n", strconv.FormatBool(pow.Validate()))
|
||||
fmt.Println()
|
||||
|
||||
if len(block.PrevHash) == 0 {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (cli *CommandLine) createBlockChain(address string) {
|
||||
chain := blockchain.InitBlockChain(address)
|
||||
chain.Database.Close()
|
||||
fmt.Println("Finished!")
|
||||
}
|
||||
|
||||
func (cli *CommandLine) getBalance(address string) {
|
||||
chain := blockchain.ContinueBlockChain(address)
|
||||
defer chain.Database.Close()
|
||||
|
||||
balance := 0
|
||||
UTXOs := chain.FindUTXO(address)
|
||||
|
||||
for _, out := range UTXOs {
|
||||
balance += out.Value
|
||||
}
|
||||
|
||||
fmt.Printf("Balance of %s: %d\n", address, balance)
|
||||
}
|
||||
|
||||
func (cli *CommandLine) send(from, to string, amount int) {
|
||||
chain := blockchain.ContinueBlockChain(from)
|
||||
defer chain.Database.Close()
|
||||
|
||||
tx := blockchain.NewTransaction(from, to, amount, chain)
|
||||
chain.AddBlock([]*blockchain.Transaction{tx})
|
||||
fmt.Println("Success!")
|
||||
}
|
||||
|
||||
func (cli *CommandLine) Run() {
|
||||
cli.validateArgs()
|
||||
|
||||
getBalanceCmd := flag.NewFlagSet("getbalance", flag.ExitOnError)
|
||||
createBlockchainCmd := flag.NewFlagSet("createblockchain", flag.ExitOnError)
|
||||
sendCmd := flag.NewFlagSet("send", flag.ExitOnError)
|
||||
printChainCmd := flag.NewFlagSet("printchain", flag.ExitOnError)
|
||||
createWalletCmd := flag.NewFlagSet("createwallet", flag.ExitOnError)
|
||||
listAddressesCmd := flag.NewFlagSet("listaddresses", flag.ExitOnError)
|
||||
|
||||
getBalanceAddress := getBalanceCmd.String("address", "", "The address to get balance of")
|
||||
createBlockchainAddress := createBlockchainCmd.String("address", "", "The address to create a blockchain for")
|
||||
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 "getbalance":
|
||||
err := getBalanceCmd.Parse(os.Args[2:])
|
||||
blockchain.Handle(err)
|
||||
|
||||
case "createblockchain":
|
||||
err := createBlockchainCmd.Parse(os.Args[2:])
|
||||
blockchain.Handle(err)
|
||||
|
||||
case "listaddresses":
|
||||
err := listAddressesCmd.Parse(os.Args[2:])
|
||||
blockchain.Handle(err)
|
||||
|
||||
case "createwallet":
|
||||
err := createWalletCmd.Parse(os.Args[2:])
|
||||
blockchain.Handle(err)
|
||||
|
||||
case "printchain":
|
||||
err := printChainCmd.Parse(os.Args[2:])
|
||||
blockchain.Handle(err)
|
||||
|
||||
case "send":
|
||||
err := sendCmd.Parse(os.Args[4:])
|
||||
blockchain.Handle(err)
|
||||
|
||||
default:
|
||||
cli.printUsage()
|
||||
runtime.Goexit()
|
||||
}
|
||||
|
||||
if getBalanceCmd.Parsed() {
|
||||
if *getBalanceAddress == "" {
|
||||
getBalanceCmd.Usage()
|
||||
runtime.Goexit()
|
||||
}
|
||||
cli.getBalance(*getBalanceAddress)
|
||||
}
|
||||
|
||||
if createBlockchainCmd.Parsed() {
|
||||
if *createBlockchainAddress == "" {
|
||||
createBlockchainCmd.Usage()
|
||||
runtime.Goexit()
|
||||
}
|
||||
cli.createBlockChain(*createBlockchainAddress)
|
||||
}
|
||||
|
||||
if listAddressesCmd.Parsed() {
|
||||
cli.listAddresses()
|
||||
}
|
||||
|
||||
if createWalletCmd.Parsed() {
|
||||
cli.createWallet()
|
||||
}
|
||||
|
||||
if printChainCmd.Parsed() {
|
||||
cli.printChain()
|
||||
}
|
||||
|
||||
if sendCmd.Parsed() {
|
||||
if *sendFrom == "" || *sendTo == "" || *sendAmount == 0 {
|
||||
sendCmd.Usage()
|
||||
runtime.Goexit()
|
||||
}
|
||||
cli.send(*sendFrom, *sendTo, *sendAmount)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user