github.com/cheng762/platon-go@v1.8.17-0.20190529111256-7deff2d7be26/cmd/ethkey/genkeypair.go (about) 1 package main 2 3 import ( 4 "github.com/PlatONnetwork/PlatON-Go/cmd/utils" 5 "github.com/PlatONnetwork/PlatON-Go/crypto" 6 "crypto/ecdsa" 7 "encoding/hex" 8 "fmt" 9 "gopkg.in/urfave/cli.v1" 10 ) 11 12 type outputGenkeypair struct { 13 Address string 14 PrivateKey string 15 PublicKey string 16 } 17 18 var commandGenkeypair = cli.Command{ 19 Name: "genkeypair", 20 Usage: "generate new private key pair", 21 ArgsUsage: "[ ]", 22 Description: ` 23 Generate a new private key pair. 24 . 25 `, 26 Flags: []cli.Flag{ 27 jsonFlag, 28 }, 29 Action: func(ctx *cli.Context) error { 30 // Check if keyfile path given and make sure it doesn't already exist. 31 var privateKey *ecdsa.PrivateKey 32 var err error 33 // generate random. 34 privateKey, err = crypto.GenerateKey() 35 if err != nil { 36 utils.Fatalf("Failed to generate random private key: %v", err) 37 } 38 39 // Output some information. 40 out := outputGenkeypair{ 41 Address: crypto.PubkeyToAddress(privateKey.PublicKey).Hex(), 42 PublicKey: hex.EncodeToString(crypto.FromECDSAPub(&privateKey.PublicKey)[1:]), 43 PrivateKey: hex.EncodeToString(crypto.FromECDSA(privateKey)), 44 } 45 if ctx.Bool(jsonFlag.Name) { 46 mustPrintJSON(out) 47 } else { 48 fmt.Println("Address : ", out.Address) 49 fmt.Println("PrivateKey: ", out.PrivateKey) 50 fmt.Println("PublicKey : ", out.PublicKey) 51 } 52 return nil 53 }, 54 }