diff --git a/cli.go b/cli.go index e01794da..e0f17a70 100644 --- a/cli.go +++ b/cli.go @@ -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") @@ -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) @@ -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") @@ -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) } @@ -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() { diff --git a/cli_explore.go b/cli_explore.go index c6e39a51..28494af5 100644 --- a/cli_explore.go +++ b/cli_explore.go @@ -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) {