github.com/Finschia/finschia-sdk@v0.48.1/x/genutil/utils.go (about)

     1  package genutil
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"path/filepath"
     7  	"time"
     8  
     9  	cfg "github.com/Finschia/ostracon/config"
    10  	osted25519 "github.com/Finschia/ostracon/crypto/ed25519"
    11  	ostos "github.com/Finschia/ostracon/libs/os"
    12  	"github.com/Finschia/ostracon/p2p"
    13  	"github.com/Finschia/ostracon/privval"
    14  	octypes "github.com/Finschia/ostracon/types"
    15  	"github.com/cosmos/go-bip39"
    16  
    17  	cryptocodec "github.com/Finschia/finschia-sdk/crypto/codec"
    18  	cryptotypes "github.com/Finschia/finschia-sdk/crypto/types"
    19  )
    20  
    21  // ExportGenesisFile creates and writes the genesis configuration to disk. An
    22  // error is returned if building or writing the configuration to file fails.
    23  func ExportGenesisFile(genDoc *octypes.GenesisDoc, genFile string) error {
    24  	if err := genDoc.ValidateAndComplete(); err != nil {
    25  		return err
    26  	}
    27  
    28  	return genDoc.SaveAs(genFile)
    29  }
    30  
    31  // ExportGenesisFileWithTime creates and writes the genesis configuration to disk.
    32  // An error is returned if building or writing the configuration to file fails.
    33  func ExportGenesisFileWithTime(
    34  	genFile, chainID string, validators []octypes.GenesisValidator,
    35  	appState json.RawMessage, genTime time.Time,
    36  ) error {
    37  	genDoc := octypes.GenesisDoc{
    38  		GenesisTime: genTime,
    39  		ChainID:     chainID,
    40  		Validators:  validators,
    41  		AppState:    appState,
    42  	}
    43  
    44  	if err := genDoc.ValidateAndComplete(); err != nil {
    45  		return err
    46  	}
    47  
    48  	return genDoc.SaveAs(genFile)
    49  }
    50  
    51  // InitializeNodeValidatorFiles creates private validator and p2p configuration files.
    52  func InitializeNodeValidatorFiles(config *cfg.Config) (nodeID string, valPubKey cryptotypes.PubKey, err error) {
    53  	return InitializeNodeValidatorFilesFromMnemonic(config, "")
    54  }
    55  
    56  // InitializeNodeValidatorFiles creates private validator and p2p configuration files using the given mnemonic.
    57  // If no valid mnemonic is given, a random one will be used instead.
    58  func InitializeNodeValidatorFilesFromMnemonic(config *cfg.Config, mnemonic string) (nodeID string, valPubKey cryptotypes.PubKey, err error) {
    59  	if len(mnemonic) > 0 && !bip39.IsMnemonicValid(mnemonic) {
    60  		return "", nil, fmt.Errorf("invalid mnemonic")
    61  	}
    62  
    63  	nodeKey, err := p2p.LoadOrGenNodeKey(config.NodeKeyFile())
    64  	if err != nil {
    65  		return "", nil, err
    66  	}
    67  
    68  	nodeID = string(nodeKey.ID())
    69  
    70  	pvKeyFile := config.PrivValidatorKeyFile()
    71  	if err := ostos.EnsureDir(filepath.Dir(pvKeyFile), 0777); err != nil {
    72  		return "", nil, err
    73  	}
    74  
    75  	pvStateFile := config.PrivValidatorStateFile()
    76  	if err := ostos.EnsureDir(filepath.Dir(pvStateFile), 0777); err != nil {
    77  		return "", nil, err
    78  	}
    79  
    80  	var filePV *privval.FilePV
    81  	if len(mnemonic) == 0 {
    82  		filePV = privval.LoadOrGenFilePV(pvKeyFile, pvStateFile)
    83  	} else {
    84  		privKey := osted25519.GenPrivKeyFromSecret([]byte(mnemonic))
    85  		filePV = privval.NewFilePV(privKey, pvKeyFile, pvStateFile)
    86  	}
    87  
    88  	tmValPubKey, err := filePV.GetPubKey()
    89  	if err != nil {
    90  		return "", nil, err
    91  	}
    92  
    93  	valPubKey, err = cryptocodec.FromOcPubKeyInterface(tmValPubKey)
    94  	if err != nil {
    95  		return "", nil, err
    96  	}
    97  
    98  	return nodeID, valPubKey, nil
    99  }