From 5f9e6c0c91962143106c2b3db8092e85d1c49803 Mon Sep 17 00:00:00 2001 From: Ivan Kuznetsov Date: Sun, 10 Sep 2017 12:53:06 +0700 Subject: [PATCH] Extract CLI commands into separate files --- cli.go | 76 ----------------------------------------- cli_createblockchain.go | 9 +++++ cli_createwallet.go | 11 ++++++ cli_getbalance.go | 19 +++++++++++ cli_listaddress.go | 18 ++++++++++ cli_printchain.go | 30 ++++++++++++++++ cli_send.go | 12 +++++++ 7 files changed, 99 insertions(+), 76 deletions(-) create mode 100644 cli_createblockchain.go create mode 100644 cli_createwallet.go create mode 100644 cli_getbalance.go create mode 100644 cli_listaddress.go create mode 100644 cli_printchain.go create mode 100644 cli_send.go diff --git a/cli.go b/cli.go index 2b01163..863ffd1 100644 --- a/cli.go +++ b/cli.go @@ -5,87 +5,11 @@ import ( "fmt" "log" "os" - "strconv" ) // CLI responsible for processing command line arguments 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() { fmt.Println("Usage:") fmt.Println(" createblockchain -address ADDRESS - Create a blockchain and send genesis block reward to ADDRESS") diff --git a/cli_createblockchain.go b/cli_createblockchain.go new file mode 100644 index 0000000..c2acb48 --- /dev/null +++ b/cli_createblockchain.go @@ -0,0 +1,9 @@ +package main + +import "fmt" + +func (cli *CLI) createBlockchain(address string) { + bc := CreateBlockchain(address) + bc.db.Close() + fmt.Println("Done!") +} diff --git a/cli_createwallet.go b/cli_createwallet.go new file mode 100644 index 0000000..b42a142 --- /dev/null +++ b/cli_createwallet.go @@ -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) +} diff --git a/cli_getbalance.go b/cli_getbalance.go new file mode 100644 index 0000000..76ab0d0 --- /dev/null +++ b/cli_getbalance.go @@ -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) +} diff --git a/cli_listaddress.go b/cli_listaddress.go new file mode 100644 index 0000000..96fd282 --- /dev/null +++ b/cli_listaddress.go @@ -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) + } +} diff --git a/cli_printchain.go b/cli_printchain.go new file mode 100644 index 0000000..4c2e865 --- /dev/null +++ b/cli_printchain.go @@ -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 + } + } +} diff --git a/cli_send.go b/cli_send.go new file mode 100644 index 0000000..ac3e001 --- /dev/null +++ b/cli_send.go @@ -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!") +}