11 log "github.com/sirupsen/logrus"
13 "github.com/vapor/crypto/ed25519/chainkd"
17 // CommonConfig means config object
22 // Top level options use an anonymous struct
23 BaseConfig `mapstructure:",squash"`
24 // Options for services
25 P2P *P2PConfig `mapstructure:"p2p"`
26 Wallet *WalletConfig `mapstructure:"wallet"`
27 Auth *RPCAuthConfig `mapstructure:"auth"`
28 Web *WebConfig `mapstructure:"web"`
29 Websocket *WebsocketConfig `mapstructure:"ws"`
30 Federation *FederationConfig `mapstructure:"federation"`
33 // Default configurable parameters.
34 func DefaultConfig() *Config {
36 BaseConfig: DefaultBaseConfig(),
37 P2P: DefaultP2PConfig(),
38 Wallet: DefaultWalletConfig(),
39 Auth: DefaultRPCAuthConfig(),
40 Web: DefaultWebConfig(),
41 Websocket: DefaultWebsocketConfig(),
42 Federation: DefaultFederationConfig(),
46 // Set the RootDir for all Config structs
47 func (cfg *Config) SetRoot(root string) *Config {
48 cfg.BaseConfig.RootDir = root
52 // NodeKey retrieves the currently configured private key of the node, checking
53 // first any manually set key, falling back to the one found in the configured
54 // data folder. If no key can be found, a new one is generated.
55 func (cfg *Config) PrivateKey() *chainkd.XPrv {
60 filePath := rootify(cfg.PrivateKeyFile, cfg.BaseConfig.RootDir)
61 fildReader, err := os.Open(filePath)
63 log.WithField("err", err).Panic("fail on open private key file")
66 defer fildReader.Close()
67 buf := make([]byte, 128)
68 if _, err = io.ReadFull(fildReader, buf); err != nil {
69 log.WithField("err", err).Panic("fail on read private key file")
73 if _, err := hex.Decode(xprv[:], buf); err != nil {
74 log.WithField("err", err).Panic("fail on decode private key")
78 xpub := cfg.XPrv.XPub()
83 //-----------------------------------------------------------------------------
85 type BaseConfig struct {
86 // The root directory for all data.
87 // This should be set in viper so it can unmarshal into this struct
88 RootDir string `mapstructure:"home"`
90 //The ID of the network to json
91 ChainID string `mapstructure:"chain_id"`
94 LogLevel string `mapstructure:"log_level"`
96 // A custom human readable name for this node
97 Moniker string `mapstructure:"moniker"`
99 // TCP or UNIX socket address for the profiling server to listen on
100 ProfListenAddress string `mapstructure:"prof_laddr"`
102 Mining bool `mapstructure:"mining"`
104 // Database backend: leveldb | memdb
105 DBBackend string `mapstructure:"db_backend"`
107 // Database directory
108 DBPath string `mapstructure:"db_dir"`
110 // Keystore directory
111 KeysPath string `mapstructure:"keys_dir"`
113 ApiAddress string `mapstructure:"api_addr"`
115 VaultMode bool `mapstructure:"vault_mode"`
118 LogFile string `mapstructure:"log_file"`
120 PrivateKeyFile string `mapstructure:"private_key_file"`
124 // Federation file name
125 FederationFileName string `mapstructure:"federation_file"`
128 // Default configurable base parameters.
129 func DefaultBaseConfig() BaseConfig {
131 Moniker: "anonymous",
132 ProfListenAddress: "",
134 DBBackend: "leveldb",
136 KeysPath: "keystore",
137 PrivateKeyFile: "node_key.txt",
138 FederationFileName: "federation.json",
142 func (b BaseConfig) DBDir() string {
143 return rootify(b.DBPath, b.RootDir)
146 func (b BaseConfig) KeysDir() string {
147 return rootify(b.KeysPath, b.RootDir)
150 func (b BaseConfig) FederationFile() string {
151 return rootify(b.FederationFileName, b.RootDir)
155 type P2PConfig struct {
156 ListenAddress string `mapstructure:"laddr"`
157 Seeds string `mapstructure:"seeds"`
158 SkipUPNP bool `mapstructure:"skip_upnp"`
159 LANDiscover bool `mapstructure:"lan_discoverable"`
160 MaxNumPeers int `mapstructure:"max_num_peers"`
161 HandshakeTimeout int `mapstructure:"handshake_timeout"`
162 DialTimeout int `mapstructure:"dial_timeout"`
163 ProxyAddress string `mapstructure:"proxy_address"`
164 ProxyUsername string `mapstructure:"proxy_username"`
165 ProxyPassword string `mapstructure:"proxy_password"`
166 KeepDial string `mapstructure:"keep_dial"`
169 // Default configurable p2p parameters.
170 func DefaultP2PConfig() *P2PConfig {
172 ListenAddress: "tcp://0.0.0.0:46656",
176 HandshakeTimeout: 30,
184 //-----------------------------------------------------------------------------
185 type WalletConfig struct {
186 Disable bool `mapstructure:"disable"`
187 Rescan bool `mapstructure:"rescan"`
188 TxIndex bool `mapstructure:"txindex"`
189 MaxTxFee uint64 `mapstructure:"max_tx_fee"`
192 type RPCAuthConfig struct {
193 Disable bool `mapstructure:"disable"`
196 type WebConfig struct {
197 Closed bool `mapstructure:"closed"`
200 type WebsocketConfig struct {
201 MaxNumWebsockets int `mapstructure:"max_num_websockets"`
202 MaxNumConcurrentReqs int `mapstructure:"max_num_concurrent_reqs"`
205 type FederationConfig struct {
206 Xpubs []chainkd.XPub `json:"xpubs"`
207 Quorum int `json:"quorum"`
210 // Default configurable rpc's auth parameters.
211 func DefaultRPCAuthConfig() *RPCAuthConfig {
212 return &RPCAuthConfig{
217 // Default configurable web parameters.
218 func DefaultWebConfig() *WebConfig {
224 // Default configurable wallet parameters.
225 func DefaultWalletConfig() *WalletConfig {
226 return &WalletConfig{
230 MaxTxFee: uint64(1000000000),
234 func DefaultWebsocketConfig() *WebsocketConfig {
235 return &WebsocketConfig{
236 MaxNumWebsockets: 25,
237 MaxNumConcurrentReqs: 20,
241 func DefaultFederationConfig() *FederationConfig {
242 return &FederationConfig{
243 Xpubs: []chainkd.XPub{
244 xpub("50ef22b3a3fca7bc08916187cc9ec2f4005c9c6b1353aa1decbd4be3f3bb0fbe1967589f0d9dec13a388c0412002d2c267bdf3b920864e1ddc50581be5604ce1"),
250 func xpub(str string) (xpub chainkd.XPub) {
251 if err := xpub.UnmarshalText([]byte(str)); err != nil {
252 log.Panicf("Fail converts a string to xpub")
257 //-----------------------------------------------------------------------------
260 // helper function to make config creation independent of root dir
261 func rootify(path, root string) string {
262 if filepath.IsAbs(path) {
265 return filepath.Join(root, path)
268 // DefaultDataDir is the default data directory to use for the databases and other
269 // persistence requirements.
270 func DefaultDataDir() string {
271 // Try to place the data folder in the user's home dir
276 switch runtime.GOOS {
278 return filepath.Join(home, "Library", "Application Support", "Vapor")
280 return filepath.Join(home, "AppData", "Roaming", "Vapor")
282 return filepath.Join(home, ".vapor")
286 func isFolderNotExists(path string) bool {
287 _, err := os.Stat(path)
288 return os.IsNotExist(err)
291 func homeDir() string {
292 if home := os.Getenv("HOME"); home != "" {
295 if usr, err := user.Current(); err == nil {