InitializeDataDirectory() populates the data directory with configuration and data files if missing
(dataDirPath string, log lib.LoggerI)
| 153 | |
| 154 | // InitializeDataDirectory() populates the data directory with configuration and data files if missing |
| 155 | func InitializeDataDirectory(dataDirPath string, log lib.LoggerI) (c lib.Config, privateValKey crypto.PrivateKeyI) { |
| 156 | // make the data dir if missing |
| 157 | if err := os.MkdirAll(dataDirPath, os.ModePerm); err != nil { |
| 158 | log.Fatal(err.Error()) |
| 159 | } |
| 160 | // make the config.json file if missing |
| 161 | configFilePath := filepath.Join(dataDirPath, lib.ConfigFilePath) |
| 162 | if _, err := os.Stat(configFilePath); errors.Is(err, os.ErrNotExist) { |
| 163 | log.Infof("Creating %s file", lib.ConfigFilePath) |
| 164 | if err = lib.DefaultConfig().WriteToFile(configFilePath); err != nil { |
| 165 | log.Fatal(err.Error()) |
| 166 | } |
| 167 | } |
| 168 | // make the private key file if missing |
| 169 | privateValKeyPath := filepath.Join(dataDirPath, lib.ValKeyPath) |
| 170 | if _, err := os.Stat(privateValKeyPath); errors.Is(err, os.ErrNotExist) { |
| 171 | blsPrivateKey, _ := crypto.NewBLS12381PrivateKey() |
| 172 | log.Infof("Creating %s file", lib.ValKeyPath) |
| 173 | if err = crypto.PrivateKeyToFile(blsPrivateKey, privateValKeyPath); err != nil { |
| 174 | log.Fatal(err.Error()) |
| 175 | } |
| 176 | pwd = getFirstPassword(log) |
| 177 | // allow flag config to skip initial nickname |
| 178 | if nick == "" { |
| 179 | // get nickname from the user |
| 180 | log.Infof("Enter nickname for your new private key:") |
| 181 | _, e := fmt.Scanln(&nick) |
| 182 | if e != nil { |
| 183 | log.Fatal(e.Error()) |
| 184 | } |
| 185 | } |
| 186 | // load the keystore from file |
| 187 | k, e := crypto.NewKeystoreFromFile(dataDirPath) |
| 188 | if e != nil { |
| 189 | log.Fatal(e.Error()) |
| 190 | } |
| 191 | // import the validator key |
| 192 | address, e := k.ImportRaw(blsPrivateKey.Bytes(), pwd, crypto.ImportRawOpts{ |
| 193 | Nickname: nick, |
| 194 | }) |
| 195 | if e != nil { |
| 196 | log.Fatal(e.Error()) |
| 197 | } |
| 198 | // save keystore to the file |
| 199 | if e = k.SaveToFile(dataDirPath); e != nil { |
| 200 | log.Fatal(e.Error()) |
| 201 | } |
| 202 | log.Infof("Imported validator key %s to keystore", address) |
| 203 | } |
| 204 | // make the proposals.json file if missing |
| 205 | if _, err := os.Stat(filepath.Join(dataDirPath, lib.ProposalsFilePath)); errors.Is(err, os.ErrNotExist) { |
| 206 | log.Infof("Creating %s file", lib.ProposalsFilePath) |
| 207 | // create an example proposal |
| 208 | blsPrivateKey, _ := crypto.NewBLS12381PrivateKey() |
| 209 | proposals := make(fsm.GovProposals) |
| 210 | a, _ := lib.NewAny(&lib.StringWrapper{Value: "example"}) |
| 211 | tx, e := fsm.NewTransaction(blsPrivateKey, &fsm.MessageChangeParameter{ |
| 212 | ParameterSpace: fsm.ParamSpaceCons + "|" + fsm.ParamSpaceFee + "|" + fsm.ParamSpaceVal + "|" + fsm.ParamSpaceGov, |
no test coverage detected