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",
138 PrivateKeyFile: "node_key.txt",
139 FederationFileName: "federation.json",
143 func (b BaseConfig) DBDir() string {
144 return rootify(b.DBPath, b.RootDir)
147 func (b BaseConfig) LogDir() string {
148 return rootify(b.LogFile, b.RootDir)
151 func (b BaseConfig) KeysDir() string {
152 return rootify(b.KeysPath, b.RootDir)
155 func (b BaseConfig) FederationFile() string {
156 return rootify(b.FederationFileName, b.RootDir)
160 type P2PConfig struct {
161 ListenAddress string `mapstructure:"laddr"`
162 Seeds string `mapstructure:"seeds"`
163 SkipUPNP bool `mapstructure:"skip_upnp"`
164 LANDiscover bool `mapstructure:"lan_discoverable"`
165 MaxNumPeers int `mapstructure:"max_num_peers"`
166 HandshakeTimeout int `mapstructure:"handshake_timeout"`
167 DialTimeout int `mapstructure:"dial_timeout"`
168 ProxyAddress string `mapstructure:"proxy_address"`
169 ProxyUsername string `mapstructure:"proxy_username"`
170 ProxyPassword string `mapstructure:"proxy_password"`
171 KeepDial string `mapstructure:"keep_dial"`
172 Compression string `mapstructure:"compression_backend"`
175 // Default configurable p2p parameters.
176 func DefaultP2PConfig() *P2PConfig {
178 ListenAddress: "tcp://0.0.0.0:56656",
182 HandshakeTimeout: 30,
187 Compression: "snappy",
191 //-----------------------------------------------------------------------------
192 type WalletConfig struct {
193 Disable bool `mapstructure:"disable"`
194 Rescan bool `mapstructure:"rescan"`
195 TxIndex bool `mapstructure:"txindex"`
196 MaxTxFee uint64 `mapstructure:"max_tx_fee"`
199 type RPCAuthConfig struct {
200 Disable bool `mapstructure:"disable"`
203 type WebConfig struct {
204 Closed bool `mapstructure:"closed"`
207 type WebsocketConfig struct {
208 MaxNumWebsockets int `mapstructure:"max_num_websockets"`
209 MaxNumConcurrentReqs int `mapstructure:"max_num_concurrent_reqs"`
212 type FederationConfig struct {
213 Xpubs []chainkd.XPub `json:"xpubs"`
214 Quorum int `json:"quorum"`
217 // Default configurable rpc's auth parameters.
218 func DefaultRPCAuthConfig() *RPCAuthConfig {
219 return &RPCAuthConfig{
224 // Default configurable web parameters.
225 func DefaultWebConfig() *WebConfig {
231 // Default configurable wallet parameters.
232 func DefaultWalletConfig() *WalletConfig {
233 return &WalletConfig{
237 MaxTxFee: uint64(1000000000),
241 func DefaultWebsocketConfig() *WebsocketConfig {
242 return &WebsocketConfig{
243 MaxNumWebsockets: 25,
244 MaxNumConcurrentReqs: 20,
248 func DefaultFederationConfig() *FederationConfig {
249 return &FederationConfig{
250 Xpubs: []chainkd.XPub{
251 xpub("580daf48fa8962100047cb1391da890bb7f2c849fdbc9b368cb4394a4c7cbb0977e2e7ebbf055dc0ef90af6a0d2af01ce7ec56b735d016aab597815ec48552e5"),
252 xpub("f3f6bcf61b65fa9d1566455a5688ca8b395efdc22e654963134b5e5cb0a45d8be522d21abc384a73177a7b9d64eba915fcfe2862d86a508a3c46dc410bdd72ad"),
253 xpub("53559612f2b7bcada18948b7de39d63947a0e2bd7336d07db1350c54ba5743996b84bf9d18ff7a2457e1a5c70ce5013e4a3b62666ddb03294c53051d5f5c70c0"),
254 xpub("7c88cc58adfc71818b08308d43c29de22460b0ea6895449cbec6e458d7dc09e0aea243fa5075ee6621da0d805bd047f6bb207329c5bd2ca3253b172fb323b512"),
260 func xpub(str string) (xpub chainkd.XPub) {
261 if err := xpub.UnmarshalText([]byte(str)); err != nil {
262 log.Panicf("Fail converts a string to xpub")
267 //-----------------------------------------------------------------------------
270 // helper function to make config creation independent of root dir
271 func rootify(path, root string) string {
272 if filepath.IsAbs(path) {
275 return filepath.Join(root, path)
278 // DefaultDataDir is the default data directory to use for the databases and other
279 // persistence requirements.
280 func DefaultDataDir() string {
281 // Try to place the data folder in the user's home dir
286 switch runtime.GOOS {
288 return filepath.Join(home, "Library", "Application Support", "Vapor")
290 return filepath.Join(home, "AppData", "Roaming", "Vapor")
292 return filepath.Join(home, ".vapor")
296 func isFolderNotExists(path string) bool {
297 _, err := os.Stat(path)
298 return os.IsNotExist(err)
301 func homeDir() string {
302 if home := os.Getenv("HOME"); home != "" {
305 if usr, err := user.Current(); err == nil {