OSDN Git Service

381bef5ed928ba130b18ea9dfc640e1722e9ee39
[bytom/bytom.git] / cmd / bytomd / commands / init.go
1 package commands
2
3 import (
4         "os"
5         "encoding/hex"
6
7         "github.com/spf13/cobra"
8         log "github.com/sirupsen/logrus"
9         cmn "github.com/tendermint/tmlibs/common"
10
11         "github.com/bytom/types"
12         cfg "github.com/bytom/config"
13         "github.com/bytom/crypto/ed25519/chainkd"
14 )
15
16 var initFilesCmd = &cobra.Command{
17         Use:   "init",
18         Short: "Initialize blockchain",
19         Run:   initFiles,
20 }
21
22 func init() {
23         initFilesCmd.Flags().String("chain_id", config.ChainID, "Select [mainnet] or [testnet]")
24
25         RootCmd.AddCommand(initFilesCmd)
26 }
27
28 func initFiles(cmd *cobra.Command, args []string) {
29         if config.ChainID == "mainnet" {
30                 cfg.EnsureRoot(config.RootDir, "mainnet")
31         } else {
32                 cfg.EnsureRoot(config.RootDir, "testnet")
33         }
34
35         genFile := config.GenesisFile()
36         if _, err := os.Stat(genFile); !os.IsNotExist(err) {
37                 log.WithField("genesis", config.GenesisFile()).Info("Already exits config file.")
38                 return
39         }
40         xprv, err := chainkd.NewXPrv(nil)
41         if err != nil {
42                 log.WithField("error", err).Error("Spawn node's key failed.")
43                 return
44         }
45         genDoc := types.GenesisDoc{
46                 ChainID:    cmn.Fmt(config.ChainID),
47                 PrivateKey: hex.EncodeToString(xprv.Bytes()),
48         }
49         genDoc.SaveAs(genFile)
50         log.WithField("genesis", config.GenesisFile()).Info("Initialized bytom")
51 }