-
Notifications
You must be signed in to change notification settings - Fork 80
/
config.go
98 lines (88 loc) · 2.92 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
package config
import (
"crypto/ecdsa"
"os"
"path/filepath"
"strings"
"github.com/0xPolygon/cdk-data-availability/config/types"
"github.com/0xPolygon/cdk-data-availability/db"
"github.com/0xPolygon/cdk-data-availability/log"
"github.com/0xPolygon/cdk-data-availability/rpc"
"github.com/ethereum/go-ethereum/accounts/keystore"
"github.com/mitchellh/mapstructure"
"github.com/spf13/viper"
"github.com/urfave/cli/v2"
)
const (
// FlagCfg flag used for config aka cfg
FlagCfg = "cfg"
)
// Config represents the full configuration of the data node
type Config struct {
PrivateKey types.KeystoreFileConfig
DB db.Config
Log log.Config
RPC rpc.Config
L1 L1Config
}
// L1Config is a struct that defines L1 contract and service settings
type L1Config struct {
WsURL string `mapstructure:"WsURL"`
RpcURL string `mapstructure:"RpcURL"`
CDKValidiumAddress string `mapstructure:"CDKValidiumAddress"`
DataCommitteeAddress string `mapstructure:"DataCommitteeAddress"`
Timeout types.Duration `mapstructure:"Timeout"`
RetryPeriod types.Duration `mapstructure:"RetryPeriod"`
BlockBatchSize uint `mapstructure:"BlockBatchSize"`
}
// Load loads the configuration baseed on the cli context
func Load(ctx *cli.Context) (*Config, error) {
cfg, err := Default()
if err != nil {
return nil, err
}
configFilePath := ctx.String(FlagCfg)
if configFilePath != "" {
dirName, fileName := filepath.Split(configFilePath)
fileExtension := strings.TrimPrefix(filepath.Ext(fileName), ".")
fileNameWithoutExtension := strings.TrimSuffix(fileName, "."+fileExtension)
viper.AddConfigPath(dirName)
viper.SetConfigName(fileNameWithoutExtension)
viper.SetConfigType(fileExtension)
}
viper.AutomaticEnv()
replacer := strings.NewReplacer(".", "_")
viper.SetEnvKeyReplacer(replacer)
viper.SetEnvPrefix("DATA_NODE")
err = viper.ReadInConfig()
if err != nil {
_, ok := err.(viper.ConfigFileNotFoundError)
if ok {
log.Infof("config file not found")
} else {
log.Infof("error reading config file: ", err)
return nil, err
}
}
decodeHooks := []viper.DecoderConfigOption{
// this allows arrays to be decoded from env var separated by ",", example: MY_VAR="value1,value2,value3"
viper.DecodeHook(mapstructure.ComposeDecodeHookFunc(mapstructure.TextUnmarshallerHookFunc(), mapstructure.StringToSliceHookFunc(","))),
}
err = viper.Unmarshal(&cfg, decodeHooks...)
return cfg, err
}
// NewKeyFromKeystore creates a private key from a keystore file
func NewKeyFromKeystore(cfg types.KeystoreFileConfig) (*ecdsa.PrivateKey, error) {
if cfg.Path == "" && cfg.Password == "" {
return nil, nil
}
keystoreEncrypted, err := os.ReadFile(filepath.Clean(cfg.Path))
if err != nil {
return nil, err
}
key, err := keystore.DecryptKey(keystoreEncrypted, cfg.Password)
if err != nil {
return nil, err
}
return key.PrivateKey, nil
}