forked from algorand/go-algorand
-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.go
116 lines (105 loc) · 3.72 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
// Copyright (C) 2019 Algorand, Inc.
// This file is part of go-algorand
//
// go-algorand is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// go-algorand is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with go-algorand. If not, see <https://www.gnu.org/licenses/>.
package config
import (
"encoding/json"
"io/ioutil"
"path/filepath"
"github.com/vincentbdb/go-algorand/util/codecs"
)
const (
kmdConfigFilename = "kmd_config.json"
kmdConfigExampleFilename = kmdConfigFilename + ".example"
defaultSessionLifetimeSecs = 60
defaultScryptN = 65536
defaultScryptR = 1
defaultScryptP = 32
)
// KMDConfig contains global configuration information for kmd
type KMDConfig struct {
DataDir string `json:"-"`
DriverConfig DriverConfig `json:"drivers"`
SessionLifetimeSecs uint64 `json:"session_lifetime_secs"`
Address string `json:"address"`
AllowedOrigins []string `json:"allowed_origins"`
}
// DriverConfig contains config info specific to each wallet driver
type DriverConfig struct {
SQLiteWalletDriverConfig SQLiteWalletDriverConfig `json:"sqlite"`
}
// SQLiteWalletDriverConfig is configuration specific to the SQLiteWalletDriver
type SQLiteWalletDriverConfig struct {
WalletsDir string `json:"wallets_dir"`
UnsafeScrypt bool `json:"allow_unsafe_scrypt"`
ScryptParams ScryptParams `json:"scrypt"`
}
// ScryptParams stores the parameters used for key derivation. This allows
// upgrading security parameters over time
type ScryptParams struct {
ScryptN int `json:"scrypt_n"`
ScryptR int `json:"scrypt_r"`
ScryptP int `json:"scrypt_p"`
}
// defaultConfig returns the default KMDConfig
func defaultConfig(dataDir string) KMDConfig {
return KMDConfig{
DataDir: dataDir,
SessionLifetimeSecs: defaultSessionLifetimeSecs,
DriverConfig: DriverConfig{
SQLiteWalletDriverConfig: SQLiteWalletDriverConfig{
ScryptParams: ScryptParams{
ScryptN: defaultScryptN,
ScryptR: defaultScryptR,
ScryptP: defaultScryptP,
},
},
},
}
}
// Validate ensures that the current configuration is valid, returning an error
// if it's not
func (k KMDConfig) Validate() error {
// If a SQLite Wallet directory is passed, ensure that it is absolute
sqlWalletsDir := k.DriverConfig.SQLiteWalletDriverConfig.WalletsDir
if sqlWalletsDir != "" {
if !filepath.IsAbs(sqlWalletsDir) {
return ErrSQLiteWalletNotAbsolute
}
}
return nil
}
// LoadKMDConfig tries to read the the kmd configuration from disk, merging the
// default kmd configuration with what it finds
func LoadKMDConfig(dataDir string) (cfg KMDConfig, err error) {
cfg = defaultConfig(dataDir)
configFilename := filepath.Join(dataDir, kmdConfigFilename)
dat, err := ioutil.ReadFile(configFilename)
// If there is no config file, then return the default configuration, and dump the default config to disk
if err != nil {
exampleFilename := filepath.Join(dataDir, kmdConfigExampleFilename)
// SaveObjectToFile may return an unhandled error because
// there is nothing to do if an error occurs
codecs.SaveObjectToFile(exampleFilename, cfg, true)
return cfg, nil
}
// Fill in the non-default values
err = json.Unmarshal(dat, &cfg)
if err != nil {
return
}
err = cfg.Validate()
return
}