104 lines
3.0 KiB
Go
104 lines
3.0 KiB
Go
package genutil
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"path/filepath"
|
|
"time"
|
|
|
|
"github.com/cosmos/go-bip39"
|
|
cfg "github.com/tendermint/tendermint/config"
|
|
tmed25519 "github.com/tendermint/tendermint/crypto/ed25519"
|
|
tmos "github.com/tendermint/tendermint/libs/os"
|
|
"github.com/tendermint/tendermint/privval"
|
|
tmtypes "github.com/tendermint/tendermint/types"
|
|
|
|
cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
|
|
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
|
|
)
|
|
|
|
// ExportGenesisFile creates and writes the genesis configuration to disk. An
|
|
// error is returned if building or writing the configuration to file fails.
|
|
func ExportGenesisFile(genDoc *tmtypes.GenesisDoc, genFile string) error {
|
|
if err := genDoc.ValidateAndComplete(); err != nil {
|
|
return err
|
|
}
|
|
|
|
return genDoc.SaveAs(genFile)
|
|
}
|
|
|
|
// ExportGenesisFileWithTime creates and writes the genesis configuration to disk.
|
|
// An error is returned if building or writing the configuration to file fails.
|
|
func ExportGenesisFileWithTime(
|
|
genFile, chainID string, validators []tmtypes.GenesisValidator,
|
|
appState json.RawMessage, genTime time.Time,
|
|
) error {
|
|
|
|
genDoc := tmtypes.GenesisDoc{
|
|
GenesisTime: genTime,
|
|
ChainID: chainID,
|
|
Validators: validators,
|
|
AppState: appState,
|
|
}
|
|
|
|
if err := genDoc.ValidateAndComplete(); err != nil {
|
|
return err
|
|
}
|
|
|
|
return genDoc.SaveAs(genFile)
|
|
}
|
|
|
|
// InitializeNodeValidatorFiles creates private validator and p2p configuration files.
|
|
func InitializeNodeValidatorFiles(config *cfg.Config) (nodeID string, valPubKey cryptotypes.PubKey, err error) {
|
|
return InitializeNodeValidatorFilesFromMnemonic(config, "")
|
|
}
|
|
|
|
// InitializeNodeValidatorFilesFromMnemonic creates private validator and p2p configuration files using the given mnemonic.
|
|
// If no valid mnemonic is given, a random one will be used instead.
|
|
func InitializeNodeValidatorFilesFromMnemonic(config *cfg.Config, mnemonic string) (nodeID string, valPubKey cryptotypes.PubKey, err error) {
|
|
if len(mnemonic) > 0 && !bip39.IsMnemonicValid(mnemonic) {
|
|
return "", nil, fmt.Errorf("invalid mnemonic")
|
|
}
|
|
nodeKey, err := tmtypes.LoadOrGenNodeKey(config.NodeKeyFile())
|
|
if err != nil {
|
|
return "", nil, err
|
|
}
|
|
|
|
nodeID = string(nodeKey.ID)
|
|
|
|
pvKeyFile := config.PrivValidator.KeyFile()
|
|
if err := tmos.EnsureDir(filepath.Dir(pvKeyFile), 0777); err != nil {
|
|
return "", nil, err
|
|
}
|
|
|
|
pvStateFile := config.PrivValidator.StateFile()
|
|
if err := tmos.EnsureDir(filepath.Dir(pvStateFile), 0777); err != nil {
|
|
return "", nil, err
|
|
}
|
|
|
|
var filePV *privval.FilePV
|
|
if len(mnemonic) == 0 {
|
|
filePV, err = privval.LoadOrGenFilePV(pvKeyFile, pvStateFile)
|
|
if err != nil {
|
|
return "", nil, err
|
|
}
|
|
} else {
|
|
privKey := tmed25519.GenPrivKeyFromSecret([]byte(mnemonic))
|
|
filePV = privval.NewFilePV(privKey, pvKeyFile, pvStateFile)
|
|
filePV.Save()
|
|
}
|
|
|
|
tmValPubKey, err := filePV.GetPubKey(context.TODO())
|
|
if err != nil {
|
|
return "", nil, err
|
|
}
|
|
|
|
valPubKey, err = cryptocodec.FromTmPubKeyInterface(tmValPubKey)
|
|
if err != nil {
|
|
return "", nil, err
|
|
}
|
|
|
|
return nodeID, valPubKey, nil
|
|
}
|