cli: add three cli command for exploring

This commit is contained in:
Wei Yang 2017-10-16 17:07:22 +08:00
parent f9935daa83
commit 782db48492
2 changed files with 83 additions and 0 deletions

48
cli.go
View File

@ -21,6 +21,11 @@ func (cli *CLI) printUsage() {
fmt.Println(" reindexutxo - Rebuilds the UTXO set")
fmt.Println(" send -from FROM -to TO -amount AMOUNT -mine - Send AMOUNT of coins from FROM address to TO. Mine on the same node, when -mine is set.")
fmt.Println(" startnode -miner ADDRESS - Start a node with ID specified in NODE_ID env. var. -miner enables mining")
fmt.Println()
fmt.Println("Exploring cmds:")
fmt.Println(" generateKey - generate KeyPair for exploring")
fmt.Println(" getAddress -pubKey PUBKEY - convert pubKey to address")
fmt.Println(" validateAddress -addr Address - validate an address")
}
func (cli *CLI) validateArgs() {
@ -48,6 +53,9 @@ func (cli *CLI) Run() {
reindexUTXOCmd := flag.NewFlagSet("reindexutxo", flag.ExitOnError)
sendCmd := flag.NewFlagSet("send", flag.ExitOnError)
startNodeCmd := flag.NewFlagSet("startnode", flag.ExitOnError)
generateKeyCmd := flag.NewFlagSet("generateKey", flag.ExitOnError)
getAddressCmd := flag.NewFlagSet("getAddress", flag.ExitOnError)
validateAddrCmd := flag.NewFlagSet("validateAddress", flag.ExitOnError)
getBalanceAddress := getBalanceCmd.String("address", "", "The address to get balance for")
createBlockchainAddress := createBlockchainCmd.String("address", "", "The address to send genesis block reward to")
@ -56,6 +64,8 @@ func (cli *CLI) Run() {
sendAmount := sendCmd.Int("amount", 0, "Amount to send")
sendMine := sendCmd.Bool("mine", false, "Mine immediately on the same node")
startNodeMiner := startNodeCmd.String("miner", "", "Enable mining mode and send reward to ADDRESS")
pubKey := getAddressCmd.String("pubKey", "", "the key where address generated")
address := validateAddrCmd.String("addr", "", "the public address")
switch os.Args[1] {
case "getbalance":
@ -98,6 +108,21 @@ func (cli *CLI) Run() {
if err != nil {
log.Panic(err)
}
case "validateAddress":
err := validateAddrCmd.Parse(os.Args[2:])
if err != nil {
log.Panic(err)
}
case "generateKey":
err := generateKeyCmd.Parse(os.Args[2:])
if err != nil {
log.Panic(err)
}
case "getAddress":
err := getAddressCmd.Parse(os.Args[2:])
if err != nil {
log.Panic(err)
}
default:
cli.printUsage()
os.Exit(1)
@ -152,4 +177,27 @@ func (cli *CLI) Run() {
}
cli.startNode(nodeID, *startNodeMiner)
}
if generateKeyCmd.Parsed() {
cli.generateKey()
}
if getAddressCmd.Parsed() {
if *pubKey == "" {
getAddressCmd.Usage()
os.Exit(1)
}
cli.getAddress(*pubKey)
}
if validateAddrCmd.Parsed() {
if *address == "" {
validateAddrCmd.Usage()
os.Exit(1)
}
cli.validateAddr(*address)
}
}

35
cli_explore.go Normal file
View File

@ -0,0 +1,35 @@
package main
import (
"encoding/hex"
"fmt"
)
func (cli *CLI) generateKey() {
_, public := newKeyPair()
fmt.Println("Public Key:")
fmt.Println(hex.EncodeToString(public))
}
func (cli *CLI) getAddress(pubKey string) {
public, _ := hex.DecodeString(pubKey)
pubKeyHash := HashPubKey(public)
versionedPayload := append([]byte{version}, pubKeyHash...)
checksum := checksum(versionedPayload)
fullPayload := append(versionedPayload, checksum...)
address := Base58Encode(fullPayload)
fmt.Println()
fmt.Printf("PubKey : %s\nAddress: %s\n", pubKey, address)
}
func (cli *CLI) validateAddr(address string) {
fmt.Printf("Address: %s\n", address)
if !ValidateAddress(address) {
fmt.Println("Not valid!")
} else {
fmt.Println("Valid!")
}
}