-
Notifications
You must be signed in to change notification settings - Fork 19
/
root.go
206 lines (174 loc) · 5.66 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package cmd
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"github.com/0chain/gosdk/core/conf"
"github.com/0chain/gosdk/core/logger"
"github.com/spf13/cobra"
"github.com/0chain/gosdk/zboxcore/blockchain"
"github.com/0chain/zboxcli/util"
"github.com/0chain/gosdk/core/zcncrypto"
"github.com/0chain/gosdk/zboxcore/sdk"
"github.com/0chain/gosdk/zcncore"
)
var cfgFile string
var networkFile string
var walletFile string
var walletClientID string
var walletClientKey string
var cDir string
var nonce int64
var txFee float64
var bSilent bool
var allocUnderRepair bool
var walletJSON string
var rootCmd = &cobra.Command{
Use: "zbox",
Short: "zbox is a decentralized storage application written on the 0Chain platform",
Long: `zbox is a decentralized storage application written on the 0Chain platform.
Complete documentation is available at https://docs.zus.network/guides/zbox-cli`,
}
var clientWallet *zcncrypto.Wallet
func init() {
InstallDLLs()
cobra.OnInitialize(initConfig)
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is config.yaml)")
rootCmd.PersistentFlags().StringVar(&networkFile, "network", "", "network file to overwrite the network details (if required, default is network.yaml)")
rootCmd.PersistentFlags().StringVar(&walletFile, "wallet", "", "wallet file (default is wallet.json)")
rootCmd.PersistentFlags().StringVar(&walletClientID, "wallet_client_id", "", "wallet client_id")
rootCmd.PersistentFlags().StringVar(&walletClientKey, "wallet_client_key", "", "wallet client_key")
rootCmd.PersistentFlags().Int64Var(&nonce, "withNonce", 0, "nonce that will be used in transaction (default is 0)")
rootCmd.PersistentFlags().StringVar(&cDir, "configDir", "", "configuration directory (default is $HOME/.zcn)")
rootCmd.PersistentFlags().BoolVar(&bSilent, "silent", false, "(default false) Do not show interactive sdk logs (shown by default)")
rootCmd.PersistentFlags().Float64Var(&txFee, "fee", 0, "transaction fee for the given transaction (if unset, it will be set to blockchain min fee)")
}
func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
func initConfig() {
configDir := util.SetConfigDir(cDir)
if cfgFile == "" {
cfgFile = "config.yaml"
}
cfg, err := conf.LoadConfigFile(filepath.Join(configDir, cfgFile))
if err != nil {
fmt.Println("Can't read config:", err)
os.Exit(1)
}
if networkFile == "" {
networkFile = "network.yaml"
}
network, _ := conf.LoadNetworkFile(filepath.Join(configDir, networkFile))
// syncing loggers
logger.SyncLoggers([]*logger.Logger{zcncore.GetLogger(), sdk.GetLogger()})
// set the log file
zcncore.SetLogFile("cmdlog.log", !bSilent)
sdk.SetLogFile("cmdlog.log", !bSilent)
sdk.SetMinSubmit(cfg.MinSubmit)
if network.IsValid() {
zcncore.SetNetwork(network.Miners, network.Sharders)
conf.InitChainNetwork(&conf.Network{
Miners: network.Miners,
Sharders: network.Sharders,
})
}
err = zcncore.InitZCNSDK(cfg.BlockWorker, cfg.SignatureScheme,
zcncore.WithChainID(cfg.ChainID),
zcncore.WithMinSubmit(cfg.MinSubmit),
zcncore.WithMinConfirmation(cfg.MinConfirmation),
zcncore.WithConfirmationChainLength(cfg.ConfirmationChainLength))
if err != nil {
fmt.Println("Error initializing core SDK.", err)
os.Exit(1)
}
// is freshly created wallet?
//var fresh bool
wallet := &zcncrypto.Wallet{}
if (&walletClientID != nil) && (len(walletClientID) > 0) && (&walletClientKey != nil) && (len(walletClientKey) > 0) {
wallet.ClientID = walletClientID
wallet.ClientKey = walletClientKey
var clientBytes []byte
clientBytes, err = json.Marshal(wallet)
walletJSON = string(clientBytes)
if err != nil {
fmt.Println("Invalid wallet data passed:" + walletClientID + " " + walletClientKey)
os.Exit(1)
}
clientWallet = wallet
//fresh = false
} else {
var walletFilePath string
if &walletFile != nil && len(walletFile) > 0 {
if filepath.IsAbs(walletFile) {
walletFilePath = walletFile
} else {
walletFilePath = configDir + string(os.PathSeparator) + walletFile
}
} else {
walletFilePath = configDir + string(os.PathSeparator) + "wallet.json"
}
if _, err = os.Stat(walletFilePath); os.IsNotExist(err) {
wallet, err := zcncore.CreateWalletOffline()
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
fmt.Println("ZCN wallet created")
walletJSON = wallet
file, err := os.Create(walletFilePath)
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
defer file.Close()
fmt.Fprint(file, walletJSON)
} 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)
}
walletJSON = string(clientBytes)
}
//minerjson, _ := json.Marshal(miners)
//sharderjson, _ := json.Marshal(sharders)
err = json.Unmarshal([]byte(walletJSON), 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
if err = sdk.InitStorageSDK(
walletJSON,
cfg.BlockWorker,
cfg.ChainID,
cfg.SignatureScheme,
cfg.PreferredBlobbers,
nonce,
zcncore.ConvertToValue(txFee),
); err != nil {
fmt.Println("Error in sdk init", err)
os.Exit(1)
}
// additional settings depending network latency
blockchain.SetMaxTxnQuery(cfg.MaxTxnQuery)
blockchain.SetQuerySleepTime(cfg.QuerySleepTime)
conf.InitClientConfig(&cfg)
if network.IsValid() {
sdk.SetNetwork(network.Miners, network.Sharders)
}
sdk.SetNumBlockDownloads(10)
}