github.com/Gessiux/neatchain@v1.3.1/chain/neatchain/create_private_validator.go (about) 1 package main 2 3 import ( 4 "fmt" 5 "os" 6 "path/filepath" 7 8 "github.com/Gessiux/go-crypto" 9 "github.com/Gessiux/go-wire" 10 "github.com/Gessiux/neatchain/chain/consensus/neatcon/types" 11 "github.com/Gessiux/neatchain/chain/log" 12 "github.com/Gessiux/neatchain/params" 13 "github.com/Gessiux/neatchain/utilities/common" 14 "github.com/Gessiux/neatchain/utilities/utils" 15 "gopkg.in/urfave/cli.v1" 16 ) 17 18 type PrivValidatorForConsole struct { 19 // NeatChain Account Address 20 Address string `json:"address"` 21 // NeatChain Consensus Public Key, in BLS format 22 PubKey crypto.PubKey `json:"consensus_pub_key"` 23 // NeatChain Consensus Private Key, in BLS format 24 // PrivKey should be empty if a Signer other than the default is being used. 25 PrivKey crypto.PrivKey `json:"consensus_priv_key"` 26 } 27 28 func CreatePrivateValidatorCmd(ctx *cli.Context) error { 29 var consolePrivVal *PrivValidatorForConsole 30 address := ctx.Args().First() 31 32 if address == "" { 33 log.Info("address is empty, need an address") 34 return nil 35 } 36 37 datadir := ctx.GlobalString(utils.DataDirFlag.Name) 38 if err := os.MkdirAll(datadir, 0700); err != nil { 39 return err 40 } 41 42 chainId := params.MainnetChainConfig.NeatChainId 43 44 if ctx.GlobalIsSet(utils.TestnetFlag.Name) { 45 chainId = params.TestnetChainConfig.NeatChainId 46 } 47 48 privValFilePath := filepath.Join(ctx.GlobalString(utils.DataDirFlag.Name), chainId) 49 privValFile := filepath.Join(ctx.GlobalString(utils.DataDirFlag.Name), chainId, "priv_validator.json") 50 51 err := os.MkdirAll(privValFilePath, os.ModePerm) 52 if err != nil { 53 panic(err) 54 } 55 56 validator := types.GenPrivValidatorKey(common.StringToAddress(address)) 57 58 consolePrivVal = &PrivValidatorForConsole{ 59 Address: validator.Address.String(), 60 PubKey: validator.PubKey, 61 PrivKey: validator.PrivKey, 62 } 63 64 fmt.Printf(string(wire.JSONBytesPretty(consolePrivVal))) 65 validator.SetFile(privValFile) 66 validator.Save() 67 68 return nil 69 }