Skip to content

Commit

Permalink
cli: generatePrivKey
Browse files Browse the repository at this point in the history
  • Loading branch information
RichardWeiYang committed Oct 21, 2017
1 parent 64cf474 commit 04318d7
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 11 deletions.
28 changes: 22 additions & 6 deletions cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ func (cli *CLI) printUsage() {
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(" generatePrivKey - generate KeyPair for exploring")
fmt.Println(" getPubKey -privKey PRIKEY - generate PubKey from privateKey")
fmt.Println(" getAddress -pubKey PUBKEY - convert pubKey to address")
fmt.Println(" getPubKeyHash -address Address - get pubKeyHash of an address")
fmt.Println(" validateAddress -addr Address - validate an address")
Expand Down Expand Up @@ -55,7 +56,8 @@ 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)
generatePrivKeyCmd := flag.NewFlagSet("generatePrivKey", flag.ExitOnError)
getPubKeyCmd := flag.NewFlagSet("getPubKey", flag.ExitOnError)
getAddressCmd := flag.NewFlagSet("getAddress", flag.ExitOnError)
getPubKeyHashCmd := flag.NewFlagSet("getPubKeyHash", flag.ExitOnError)
validateAddrCmd := flag.NewFlagSet("validateAddress", flag.ExitOnError)
Expand All @@ -68,6 +70,7 @@ 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")
privateKey := getPubKeyCmd.String("privKey", "", "generate PubKey based on this")
pubKey := getAddressCmd.String("pubKey", "", "the key where address generated")
pubKeyAddress := getPubKeyHashCmd.String("address", "", "the pub address")
address := validateAddrCmd.String("addr", "", "the public address")
Expand Down Expand Up @@ -119,8 +122,13 @@ func (cli *CLI) Run() {
if err != nil {
log.Panic(err)
}
case "generateKey":
err := generateKeyCmd.Parse(os.Args[2:])
case "generatePrivKey":
err := generatePrivKeyCmd.Parse(os.Args[2:])
if err != nil {
log.Panic(err)
}
case "getPubKey":
err := getPubKeyCmd.Parse(os.Args[2:])
if err != nil {
log.Panic(err)
}
Expand Down Expand Up @@ -194,8 +202,16 @@ func (cli *CLI) Run() {
cli.startNode(nodeID, *startNodeMiner)
}

if generateKeyCmd.Parsed() {
cli.generateKey()
if generatePrivKeyCmd.Parsed() {
cli.generatePrivKey()
}

if getPubKeyCmd.Parsed() {
if *privateKey == "" {
getPubKeyCmd.Usage()
os.Exit(1)
}
cli.getPubKey(*privateKey)
}

if getAddressCmd.Parsed() {
Expand Down
16 changes: 11 additions & 5 deletions cli_explore.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
package main

import (
"crypto/elliptic"
"encoding/hex"
"fmt"
)

func (cli *CLI) generateKey() {
private, public := newKeyPair()
fmt.Println("Private Key:")
func (cli *CLI) getPubKey(privateKey string) {
curve := elliptic.P256()
priv_key, _ := hex.DecodeString(privateKey)
x, y := curve.ScalarBaseMult(priv_key)
pubKey := append(x.Bytes(), y.Bytes()...)
fmt.Println(hex.EncodeToString(pubKey))
}

func (cli *CLI) generatePrivKey() {
private, _ := newKeyPair()
fmt.Println(hex.EncodeToString(private.D.Bytes()))
fmt.Println("Public Key:")
fmt.Println(hex.EncodeToString(public))
}

func (cli *CLI) getAddress(pubKey string) {
Expand Down

0 comments on commit 04318d7

Please sign in to comment.