github.com/intfoundation/intchain@v0.0.0-20220727031208-4316ad31ca73/cmd/intchain/create_private_validator.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/intfoundation/go-crypto"
     6  	"github.com/intfoundation/go-wire"
     7  	"github.com/intfoundation/intchain/cmd/utils"
     8  	"github.com/intfoundation/intchain/common"
     9  	"github.com/intfoundation/intchain/consensus/ipbft/types"
    10  	"github.com/intfoundation/intchain/log"
    11  	"github.com/intfoundation/intchain/params"
    12  	"gopkg.in/urfave/cli.v1"
    13  	"os"
    14  	"path/filepath"
    15  )
    16  
    17  type PrivValidatorForConsole struct {
    18  	// IntChain Account Address
    19  	Address string `json:"address"`
    20  	// IntChain Consensus Public Key, in BLS format
    21  	PubKey crypto.PubKey `json:"consensus_pub_key"`
    22  	// IntChain Consensus Private Key, in BLS format
    23  	// PrivKey should be empty if a Signer other than the default is being used.
    24  	PrivKey crypto.PrivKey `json:"consensus_priv_key"`
    25  }
    26  
    27  func CreatePrivateValidatorCmd(ctx *cli.Context) error {
    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.IntChainId
    41  
    42  	if ctx.GlobalIsSet(utils.TestnetFlag.Name) {
    43  		chainId = params.TestnetChainConfig.IntChainId
    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  	fmt.Printf(string(wire.JSONBytesPretty(validator)))
    57  	validator.SetFile(privValFile)
    58  	validator.Save()
    59  
    60  	return nil
    61  }