github.com/neatio-net/neatio@v1.7.3-0.20231114194659-f4d7a2226baa/chain/neatio/create_private_validator.go (about)

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