-
Notifications
You must be signed in to change notification settings - Fork 2
/
config.go
113 lines (98 loc) · 2.44 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
package overlay
import (
"encoding/json"
"fmt"
"io/ioutil"
"strings"
crypto "github.com/libp2p/go-libp2p-crypto"
maddr "github.com/multiformats/go-multiaddr"
)
// A new type we need for writing a custom flag parser
type addrList []maddr.Multiaddr
func (al *addrList) String() string {
strs := make([]string, len(*al))
for i, addr := range *al {
strs[i] = addr.String()
}
return strings.Join(strs, ",")
}
func (al *addrList) Set(value string) error {
addr, err := maddr.NewMultiaddr(value)
if err != nil {
return err
}
*al = append(*al, addr)
return nil
}
// StringsToAddrs ...
func StringsToAddrs(addrStrings []string) (maddrs []maddr.Multiaddr, err error) {
for _, addrString := range addrStrings {
addr, err := maddr.NewMultiaddr(addrString)
if err != nil {
return maddrs, err
}
maddrs = append(maddrs, addr)
}
return
}
// Config provides the configuration required to start the overlay nodes
type Config struct {
RendezvousString string
BootstrapPeers addrList
ListenAddresses addrList
OverlayListenAddresses addrList
ProtocolID string
}
// Identity is the private keys of the two peers.
type Identity struct {
PublicPrivKey crypto.PrivKey
OverlayPrivKey crypto.PrivKey
}
type jsonIdentity struct {
PublicPrivKey, OverlayPrivKey []byte
}
// Save writes config to the provided path.
func (c *Identity) Save(path string) error {
if c == nil {
return fmt.Errorf("Identity is nil")
}
if c.PublicPrivKey == nil {
return fmt.Errorf("PublicPrivKey is nil")
}
byPubPrivKey, err := crypto.MarshalPrivateKey(c.PublicPrivKey)
if err != nil {
return err
}
byOverlayPrivKey, err := crypto.MarshalPrivateKey(c.OverlayPrivKey)
if err != nil {
return err
}
jsonID := &jsonIdentity{PublicPrivKey: byPubPrivKey, OverlayPrivKey: byOverlayPrivKey}
file, err := json.MarshalIndent(jsonID, "", " ")
if err != nil {
return err
}
return ioutil.WriteFile(path, file, 0644)
}
// LoadIdentity loads a json config from a path.
func LoadIdentity(path string) (*Identity, error) {
file, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
jsonID := &jsonIdentity{}
err = json.Unmarshal(file, jsonID)
if err != nil {
return nil, err
}
c := &Identity{}
c.PublicPrivKey, err = crypto.UnmarshalPrivateKey(jsonID.PublicPrivKey)
if err != nil {
return nil, err
}
c.OverlayPrivKey, err = crypto.UnmarshalPrivateKey(jsonID.OverlayPrivKey)
if err != nil {
return nil, err
}
return c, nil
}