/
config.go
212 lines (181 loc) · 6.11 KB
/
config.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
207
208
209
210
211
212
package zcnbridge
import (
"flag"
"fmt"
"path"
"github.com/0chain/gosdk/zcncore"
"github.com/0chain/gosdk/core/common"
"github.com/0chain/gosdk/zcnbridge/log"
"github.com/spf13/viper"
)
type BridgeSDKConfig struct {
LogLevel *string
LogPath *string
ConfigBridgeFile *string
ConfigChainFile *string
ConfigDir *string
Development *bool
}
type ContractsRegistry struct {
// Address of Ethereum bridge contract
BridgeAddress string
// Address of WZCN Ethereum wrapped token
WzcnAddress string
// Address of Ethereum authorizers contract
AuthorizersAddress string
}
type BridgeConfig struct {
ConsensusThreshold float64
}
type EthereumConfig struct {
// URL of ethereum RPC node (infura or alchemy)
EthereumNodeURL string
// Gas limit to execute ethereum transaction
GasLimit uint64
// Value to execute Ethereum smart contracts (default = 0)
Value int64
}
type BridgeClientConfig struct {
ContractsRegistry
EthereumConfig
EthereumAddress string
Password string
Homedir string
}
type Instance struct {
//zcnWallet *wallet.Wallet
startTime common.Timestamp
nonce int64
}
type BridgeClient struct {
*BridgeConfig
*BridgeClientConfig
*Instance
}
type BridgeOwner struct {
*BridgeClientConfig
*Instance
}
// ReadClientConfigFromCmd reads config from command line
// Bridge has several configs:
// Chain config at ~/.zcn/config.json
// User 0Chain wallet config at ~/.zcn/wallet.json
// User EthBridge config ~/.zcn/bridge.json
// Owner EthBridge config ~/.zcn/bridgeowner.json
func ReadClientConfigFromCmd() *BridgeSDKConfig {
// reading from bridge.yaml
cmd := &BridgeSDKConfig{}
cmd.Development = flag.Bool("development", false, "development mode")
cmd.LogPath = flag.String("logs", "./logs", "log folder")
cmd.ConfigDir = flag.String("path", GetConfigDir(), "config home folder")
cmd.ConfigBridgeFile = flag.String("bridge_config", BridgeClientConfigName, "bridge config file")
cmd.ConfigChainFile = flag.String("chain_config", ZChainsClientConfigName, "chain config file")
cmd.LogLevel = flag.String("loglevel", "debug", "log level")
flag.Parse()
return cmd
}
func CreateBridgeOwner(cfg *viper.Viper) *BridgeOwner {
owner := cfg.Get(OwnerConfigKeyName)
if owner == nil {
ExitWithError("CreateBridgeOwner: can't read config with `owner` key")
}
fileUsed := cfg.ConfigFileUsed()
homedir := path.Dir(fileUsed)
if homedir == "" {
ExitWithError("CreateBridgeOwner: homedir is required")
}
return &BridgeOwner{
BridgeClientConfig: &BridgeClientConfig{
ContractsRegistry: ContractsRegistry{
BridgeAddress: cfg.GetString(fmt.Sprintf("%s.BridgeAddress", OwnerConfigKeyName)),
WzcnAddress: cfg.GetString(fmt.Sprintf("%s.WzcnAddress", OwnerConfigKeyName)),
AuthorizersAddress: cfg.GetString(fmt.Sprintf("%s.AuthorizersAddress", OwnerConfigKeyName)),
},
EthereumConfig: EthereumConfig{
EthereumNodeURL: cfg.GetString(fmt.Sprintf("%s.EthereumNodeURL", OwnerConfigKeyName)),
GasLimit: cfg.GetUint64(fmt.Sprintf("%s.GasLimit", OwnerConfigKeyName)),
Value: cfg.GetInt64(fmt.Sprintf("%s.Value", OwnerConfigKeyName)),
},
EthereumAddress: cfg.GetString(fmt.Sprintf("%s.EthereumAddress", OwnerConfigKeyName)),
Password: cfg.GetString(fmt.Sprintf("%s.Password", OwnerConfigKeyName)),
Homedir: homedir,
},
Instance: &Instance{
startTime: common.Now(),
},
}
}
func CreateBridgeClient(cfg *viper.Viper) *BridgeClient {
fileUsed := cfg.ConfigFileUsed()
homedir := path.Dir(fileUsed)
if homedir == "" {
ExitWithError("homedir is required")
}
bridge := cfg.Get(ClientConfigKeyName)
if bridge == nil {
ExitWithError(fmt.Sprintf("Can't read config with '%s' key", ClientConfigKeyName))
}
return &BridgeClient{
BridgeClientConfig: &BridgeClientConfig{
ContractsRegistry: ContractsRegistry{
BridgeAddress: cfg.GetString(fmt.Sprintf("%s.BridgeAddress", ClientConfigKeyName)),
WzcnAddress: cfg.GetString(fmt.Sprintf("%s.WzcnAddress", ClientConfigKeyName)),
AuthorizersAddress: cfg.GetString(fmt.Sprintf("%s.AuthorizersAddress", ClientConfigKeyName)),
},
EthereumConfig: EthereumConfig{
EthereumNodeURL: cfg.GetString(fmt.Sprintf("%s.EthereumNodeURL", ClientConfigKeyName)),
GasLimit: cfg.GetUint64(fmt.Sprintf("%s.GasLimit", ClientConfigKeyName)),
Value: cfg.GetInt64(fmt.Sprintf("%s.Value", ClientConfigKeyName)),
},
EthereumAddress: cfg.GetString(fmt.Sprintf("%s.EthereumAddress", ClientConfigKeyName)),
Password: cfg.GetString(fmt.Sprintf("%s.Password", ClientConfigKeyName)),
Homedir: homedir,
},
BridgeConfig: &BridgeConfig{
ConsensusThreshold: cfg.GetFloat64(fmt.Sprintf("%s.ConsensusThreshold", ClientConfigKeyName)),
},
Instance: &Instance{
startTime: common.Now(),
},
}
}
// ID returns id of Node.
func (b *BridgeClient) ID() string {
return zcncore.GetClientWalletID()
//return b.zcnWallet.ID()
}
// ID returns id of Node.
func (b *BridgeOwner) ID() string {
return zcncore.GetClientWalletID()
//return b.zcnWallet.ID()
}
// PublicKey returns public key of Node
//func (b *BridgeClient) PublicKey() string {
// return b.zcnWallet.PublicKey()
//}
//
//func (b *BridgeClient) PrivateKey() string {
// return b.zcnWallet.PrivateKey()
//}
func (b *BridgeClient) IncrementNonce() int64 {
b.nonce++
return b.nonce
}
//func (b *BridgeClient) SetupZCNClient(cfg *BridgeSDKConfig) {
// b.SetupZCNSDK(*cfg.LogPath, *cfg.LogLevel)
// b.SetupZCNWallet(EthereumWalletClientConfigName)
//}
// SetupBridgeClientSDK Use this from standalone application
// 0Chain SDK initialization is required
func SetupBridgeClientSDK(cfg *BridgeSDKConfig) *BridgeClient {
log.InitLogging(*cfg.Development, *cfg.LogPath, *cfg.LogLevel)
//initChainFromConfig(initChainConfig(cfg))
bridgeClient := CreateBridgeClient(initBridgeConfig(cfg))
return bridgeClient
}
// SetupBridgeOwnerSDK Use this from standalone application to initialize bridge owner.
// 0Chain SDK initialization is not required in this case
func SetupBridgeOwnerSDK(cfg *BridgeSDKConfig) *BridgeOwner {
bridgeOwner := CreateBridgeOwner(initBridgeConfig(cfg))
return bridgeOwner
}