-
Notifications
You must be signed in to change notification settings - Fork 10
/
config.go
65 lines (52 loc) · 1.34 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
package peer_controller
import (
"github.com/aperturerobotics/bifrost/util/confparse"
"github.com/aperturerobotics/controllerbus/config"
"github.com/libp2p/go-libp2p-core/crypto"
)
// ConfigID is the identifier for the config type.
const ConfigID = ControllerID
// NewConfigWithPrivKey builds a new configuration with a private key
func NewConfigWithPrivKey(pk crypto.PrivKey) (*Config, error) {
privKeyStr, err := confparse.MarshalPrivateKey(pk)
if err != nil {
return nil, err
}
return &Config{
PrivKey: privKeyStr,
}, nil
}
// GetConfigID returns the config identifier.
func (c *Config) GetConfigID() string {
return ConfigID
}
// EqualsConfig checks equality between two configs.
func (c *Config) EqualsConfig(c2 config.Config) bool {
oc, ok := c2.(*Config)
if !ok {
return false
}
if c.GetPrivKey() == oc.GetPrivKey() {
return true
}
pk1, err := c.ParsePrivateKey()
if err != nil {
return false
}
pk2, err := oc.ParsePrivateKey()
if err != nil {
return false
}
return pk2.Equals(pk1)
}
// Validate validates the configuration.
func (c *Config) Validate() error {
if _, err := c.ParsePrivateKey(); err != nil {
return err
}
return nil
}
// ParsePrivateKey parses the private key from the configuration.
func (c *Config) ParsePrivateKey() (crypto.PrivKey, error) {
return confparse.ParsePrivateKey(c.GetPrivKey())
}