-
Notifications
You must be signed in to change notification settings - Fork 10
/
config.go
87 lines (70 loc) · 2.01 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
package peer_controller
import (
"github.com/aperturerobotics/bifrost/peer"
"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.ParseToPeer(); err != nil {
return err
}
return nil
}
// ParsePrivateKey parses the private key from the configuration.
// Returns nil, nil if unset.
func (c *Config) ParsePrivateKey() (crypto.PrivKey, error) {
return confparse.ParsePrivateKey(c.GetPrivKey())
}
// ParsePublicKey parses the public key from the configuration.
// Returns nil, nil if unset.
func (c *Config) ParsePublicKey() (crypto.PubKey, error) {
return confparse.ParsePublicKey(c.GetPubKey())
}
// ParsePeerID parses the peer ID.
// may return nil.
func (c *Config) ParsePeerID() (peer.ID, error) {
return confparse.ParsePeerID(c.GetPeerId())
}
// ParseToPeer parses the fields and builds the corresponding Peer.
func (c *Config) ParseToPeer() (peer.Peer, error) {
return confparse.ParsePeer(c.GetPrivKey(), c.GetPubKey(), c.GetPeerId())
}
// _ is a type assertion
var _ config.Config = ((*Config)(nil))