-
Notifications
You must be signed in to change notification settings - Fork 58
/
config.go
134 lines (124 loc) · 3.6 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
// Package conf parse config to configuration
package conf
import (
"bytes"
"fmt"
"log"
"os"
"strings"
"github.com/spf13/viper"
)
// Config contains configuration items for sdk
type Config struct {
IsHTTP bool
ChainID int64
CAFile string
Key string
Cert string
IsSMCrypto bool
PrivateKey []byte
GroupID int
NodeURL string
}
// ParseConfigFile parses the configuration from toml config file
func ParseConfigFile(cfgFile string) ([]Config, error) {
file, err := os.Open(cfgFile)
if err != nil {
return nil, fmt.Errorf("open file failed, err: %v", err)
}
defer func() {
err = file.Close()
if err != nil {
log.Fatalf("close file failed, err: %v", err)
}
}()
fileInfo, err := file.Stat()
if err != nil {
return nil, fmt.Errorf("file is not found, err: %v", err)
}
fileSize := fileInfo.Size()
buffer := make([]byte, fileSize)
_, err = file.Read(buffer)
if err != nil {
return nil, fmt.Errorf("read file failed, err: %v", err)
}
return ParseConfig(buffer)
}
// ParseConfig parses the configuration from []byte
func ParseConfig(buffer []byte) ([]Config, error) {
viper.SetConfigType("toml")
err := viper.ReadConfig(bytes.NewBuffer(buffer))
if err != nil {
return nil, fmt.Errorf("viper .ReadConfig failed, err: %v", err)
}
config := new(Config)
var configs []Config
viper.SetDefault("SMCrypto", false)
viper.SetDefault("Network.Type", "rpc")
viper.SetDefault("Network.CAFile", "ca.crt")
viper.SetDefault("Network.Key", "sdk.key")
viper.SetDefault("Network.Cert", "sdk.crt")
if viper.IsSet("Chain") {
if viper.IsSet("Chain.ChainID") {
config.ChainID = int64(viper.GetInt("Chain.ChainID"))
} else {
return nil, fmt.Errorf("Chain.ChainID has not been set")
}
if viper.IsSet("Chain.SMCrypto") {
config.IsSMCrypto = viper.GetBool("Chain.SMCrypto")
} else {
return nil, fmt.Errorf("SMCrypto has not been set")
}
} else {
return nil, fmt.Errorf("chain has not been set")
}
if viper.IsSet("Account") {
accountKeyFile := viper.GetString("Account.KeyFile")
keyBytes, curve, err := LoadECPrivateKeyFromPEM(accountKeyFile)
if err != nil {
return nil, fmt.Errorf("parse private key failed, err: %v", err)
}
if config.IsSMCrypto && curve != sm2p256v1 {
return nil, fmt.Errorf("smcrypto must use sm2p256v1 private key, but found %s", curve)
}
if !config.IsSMCrypto && curve != secp256k1 {
return nil, fmt.Errorf("must use secp256k1 private key, but found %s", curve)
}
config.PrivateKey = keyBytes
} else {
return nil, fmt.Errorf("network has not been set")
}
if viper.IsSet("Network") {
connectionType := viper.GetString("Network.Type")
if strings.EqualFold(connectionType, "rpc") {
config.IsHTTP = true
} else if strings.EqualFold(connectionType, "channel") {
config.IsHTTP = false
} else {
fmt.Printf("Network.Type %s is not supported, use channel", connectionType)
}
config.CAFile = viper.GetString("Network.CAFile")
config.Key = viper.GetString("Network.Key")
config.Cert = viper.GetString("Network.Cert")
var connections []struct {
GroupID int
NodeURL string
}
if viper.IsSet("Network.Connection") {
err := viper.UnmarshalKey("Network.Connection", &connections)
if err != nil {
return nil, fmt.Errorf("parse Network.Connection failed. err: %v", err)
}
for i := range connections {
configs = append(configs, *config)
configs[i].GroupID = connections[i].GroupID
configs[i].NodeURL = connections[i].NodeURL
}
} else {
return nil, fmt.Errorf("Network.Connection has not been set")
}
} else {
return nil, fmt.Errorf("network has not been set")
}
return configs, nil
}