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