-
Notifications
You must be signed in to change notification settings - Fork 20
/
root.go
147 lines (132 loc) · 3.81 KB
/
root.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package cmd
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"sync"
"github.com/0chain/gosdk/core/zcncrypto"
"github.com/mitchellh/go-homedir"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/0chain/gosdk/zboxcore/sdk"
"github.com/0chain/gosdk/zcncore"
)
var cfgFile string
var walletFile string
var bVerbose bool
var sharders []string
var miners []string
var clientConfig string
var configDir string
var rootCmd = &cobra.Command{
Use: "zbox",
Short: "0Box is a decentralized storage application written on the 0Chain platform",
Long: `0Box is a decentralized storage application written on the 0Chain platform.
Complete documentation is available at https://0chain.net`,
}
var clientWallet *zcncrypto.Wallet
func init() {
cobra.OnInitialize(initConfig)
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.zcn/nodes.yaml)")
rootCmd.PersistentFlags().StringVar(&walletFile, "wallet", "", "wallet file (default is $HOME/.zcn/wallet.txt)")
rootCmd.PersistentFlags().BoolVar(&bVerbose, "verbose", false, "prints sdk log in stdio (default false)")
}
func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
func initConfig() {
nodeConfig := viper.New()
// Find home directory.
home, err := homedir.Dir()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
configDir = home + "/.zcn"
// Search config in home directory with name ".cobra" (without extension).
nodeConfig.AddConfigPath(configDir)
if &cfgFile != nil && len(cfgFile) > 0 {
nodeConfig.SetConfigName(cfgFile)
} else {
nodeConfig.SetConfigName("nodes")
}
if err := nodeConfig.ReadInConfig(); err != nil {
fmt.Println("Can't read config:", err)
os.Exit(1)
}
sharders = nodeConfig.GetStringSlice("sharders")
miners = nodeConfig.GetStringSlice("miners")
signScheme := nodeConfig.GetString("signature_scheme")
chainID := nodeConfig.GetString("chain_id")
//TODO: move the private key storage to the keychain or secure storage
var walletFilePath string
if &walletFile != nil && len(walletFile) > 0 {
walletFilePath = configDir + "/" + walletFile
} else {
walletFilePath = configDir + "/wallet.txt"
}
//set the log file
zcncore.SetLogFile("cmdlog.log", bVerbose)
sdk.SetLogFile("cmdlog.log", bVerbose)
zcncore.InitZCNSDK(miners, sharders, signScheme)
if _, err := os.Stat(walletFilePath); os.IsNotExist(err) {
wg := &sync.WaitGroup{}
statusBar := &ZCNStatus{wg: wg}
wg.Add(1)
err = zcncore.CreateWallet(statusBar)
if err == nil {
wg.Wait()
} else {
fmt.Println(err.Error())
os.Exit(1)
}
if len(statusBar.walletString) == 0 || !statusBar.success {
fmt.Println("Error creating the wallet." + statusBar.errMsg)
os.Exit(1)
}
fmt.Println("ZCN wallet created")
clientConfig = string(statusBar.walletString)
file, err := os.Create(walletFilePath)
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
defer file.Close()
fmt.Fprintf(file, clientConfig)
} else {
f, err := os.Open(walletFilePath)
if err != nil {
fmt.Println("Error opening the wallet", err)
os.Exit(1)
}
clientBytes, err := ioutil.ReadAll(f)
if err != nil {
fmt.Println("Error reading the wallet", err)
os.Exit(1)
}
clientConfig = string(clientBytes)
}
//minerjson, _ := json.Marshal(miners)
//sharderjson, _ := json.Marshal(sharders)
wallet := &zcncrypto.Wallet{}
err = json.Unmarshal([]byte(clientConfig), wallet)
clientWallet = wallet
if err != nil {
fmt.Println("Invalid wallet at path:" + walletFilePath)
os.Exit(1)
}
//init the storage sdk with the known miners, sharders and client wallet info
err = sdk.InitStorageSDK(clientConfig, miners, sharders, chainID, signScheme)
if err != nil {
fmt.Println("Error in sdk init", err)
os.Exit(1)
}
sdk.SetNumBlockDownloads(10)
}
func getConfigDir() string {
return configDir
}