-
Notifications
You must be signed in to change notification settings - Fork 16
/
root.go
178 lines (153 loc) · 4.41 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
package cmd
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"sync"
"github.com/0chain/gosdk/core/conf"
"github.com/0chain/gosdk/core/transaction"
"github.com/0chain/gosdk/core/zcncrypto"
"github.com/0chain/gosdk/zcncore"
"github.com/mitchellh/go-homedir"
"github.com/spf13/cobra"
)
var cfgFile string
var networkFile string
var walletFile string
var cDir string
var bSilent bool
var clientConfig string
var rootCmd = &cobra.Command{
Use: "zwallet",
Short: "Use Zwallet to store, send and execute smart contract on 0Chain platform",
Long: `Use Zwallet to store, send and execute smart contract on 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 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(&cDir, "configDir", "", "configuration directory (default is $HOME/.zcn)")
rootCmd.PersistentFlags().BoolVar(&bSilent, "silent", false, "Do not print sdk logs in stderr (prints by default)")
}
func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
func getConfigDir() string {
if cDir != "" {
return cDir
}
var configDir string
// Find home directory.
home, err := homedir.Dir()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
configDir = home + "/.zcn"
return configDir
}
func initConfig() {
var configDir string
if cDir != "" {
configDir = cDir
} else {
configDir = getConfigDir()
}
if cfgFile == "" {
cfgFile = "config.yaml"
}
cfg, err := conf.LoadConfigFile(filepath.Join(configDir, cfgFile))
if err != nil {
ExitWithError("Can't read config:", err)
}
transaction.SetConfig(&cfg)
if networkFile == "" {
networkFile = "network.yaml"
}
network, _ := conf.LoadNetworkFile(filepath.Join(configDir, networkFile))
//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.json"
}
//set the log file
zcncore.SetLogFile("cmdlog.log", !bSilent)
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 {
ExitWithError(err.Error())
}
if network.IsValid() {
zcncore.SetNetwork(network.Miners, network.Sharders)
}
// is freshly created wallet?
var fresh bool
if _, err := os.Stat(walletFilePath); os.IsNotExist(err) {
fmt.Println("No wallet in path ", walletFilePath, "found. Creating wallet...")
wg := &sync.WaitGroup{}
statusBar := &ZCNStatus{wg: wg}
wg.Add(1)
err = zcncore.CreateWallet(statusBar)
if err == nil {
wg.Wait()
} else {
ExitWithError(err.Error())
}
if len(statusBar.walletString) == 0 || !statusBar.success {
ExitWithError("Error creating the wallet." + statusBar.errMsg)
}
fmt.Println("ZCN wallet created!!")
clientConfig = string(statusBar.walletString)
file, err := os.Create(walletFilePath)
if err != nil {
ExitWithError(err.Error())
}
defer file.Close()
fmt.Fprintf(file, clientConfig)
fresh = true
} else {
f, err := os.Open(walletFilePath)
if err != nil {
ExitWithError("Error opening the wallet", err)
}
clientBytes, err := ioutil.ReadAll(f)
if err != nil {
ExitWithError("Error reading the wallet", err)
}
clientConfig = string(clientBytes)
}
wallet := &zcncrypto.Wallet{}
err = json.Unmarshal([]byte(clientConfig), wallet)
clientWallet = wallet
if err != nil {
ExitWithError("Invalid wallet at path:" + walletFilePath)
}
wg := &sync.WaitGroup{}
err = zcncore.SetWalletInfo(clientConfig, false)
if err == nil {
wg.Wait()
} else {
ExitWithError(err.Error())
}
if fresh {
log.Print("Creating related read pool for storage smart-contract...")
if err = createReadPool(); err != nil {
log.Fatalf("Failed to create read pool: %v", err)
}
log.Printf("Read pool created successfully")
}
}