cli: add three cli command for exploring
This commit is contained in:
parent
f9935daa83
commit
782db48492
48
cli.go
48
cli.go
|
@ -21,6 +21,11 @@ func (cli *CLI) printUsage() {
|
||||||
fmt.Println(" reindexutxo - Rebuilds the UTXO set")
|
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(" 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(" 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() {
|
func (cli *CLI) validateArgs() {
|
||||||
|
@ -48,6 +53,9 @@ func (cli *CLI) Run() {
|
||||||
reindexUTXOCmd := flag.NewFlagSet("reindexutxo", flag.ExitOnError)
|
reindexUTXOCmd := flag.NewFlagSet("reindexutxo", flag.ExitOnError)
|
||||||
sendCmd := flag.NewFlagSet("send", flag.ExitOnError)
|
sendCmd := flag.NewFlagSet("send", flag.ExitOnError)
|
||||||
startNodeCmd := flag.NewFlagSet("startnode", 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")
|
getBalanceAddress := getBalanceCmd.String("address", "", "The address to get balance for")
|
||||||
createBlockchainAddress := createBlockchainCmd.String("address", "", "The address to send genesis block reward to")
|
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")
|
sendAmount := sendCmd.Int("amount", 0, "Amount to send")
|
||||||
sendMine := sendCmd.Bool("mine", false, "Mine immediately on the same node")
|
sendMine := sendCmd.Bool("mine", false, "Mine immediately on the same node")
|
||||||
startNodeMiner := startNodeCmd.String("miner", "", "Enable mining mode and send reward to ADDRESS")
|
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] {
|
switch os.Args[1] {
|
||||||
case "getbalance":
|
case "getbalance":
|
||||||
|
@ -98,6 +108,21 @@ func (cli *CLI) Run() {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Panic(err)
|
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:
|
default:
|
||||||
cli.printUsage()
|
cli.printUsage()
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
|
@ -152,4 +177,27 @@ func (cli *CLI) Run() {
|
||||||
}
|
}
|
||||||
cli.startNode(nodeID, *startNodeMiner)
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -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!")
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue