-
Notifications
You must be signed in to change notification settings - Fork 0
/
addr.go
67 lines (59 loc) · 1.49 KB
/
addr.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
package conv
import (
"strings"
"github.com/multiformats/go-multiaddr"
"github.com/gridfoundation/furyint/config"
)
// TranslateAddresses updates conf by changing Cosmos-style addresses to Multiaddr format.
func TranslateAddresses(conf *config.NodeConfig) error {
if conf.P2P.ListenAddress != "" {
addr, err := GetMultiAddr(conf.P2P.ListenAddress)
if err != nil {
return err
}
conf.P2P.ListenAddress = addr.String()
}
seeds := strings.Split(conf.P2P.Seeds, ",")
for i, seed := range seeds {
if seed != "" {
addr, err := GetMultiAddr(seed)
if err != nil {
return err
}
seeds[i] = addr.String()
}
}
conf.P2P.Seeds = strings.Join(seeds, ",")
return nil
}
// GetMultiAddr converts single Cosmos-style network address into Multiaddr.
// Input format: [protocol://][<NODE_ID>@]<IPv4>:<PORT>
func GetMultiAddr(addr string) (multiaddr.Multiaddr, error) {
var err error
var p2pID multiaddr.Multiaddr
parts := strings.Split(addr, "://")
proto := "tcp"
if len(parts) == 2 {
proto = parts[0]
addr = parts[1]
}
if at := strings.IndexRune(addr, '@'); at != -1 {
p2pID, err = multiaddr.NewMultiaddr("/p2p/" + addr[:at])
if err != nil {
return nil, err
}
addr = addr[at+1:]
}
parts = strings.Split(addr, ":")
if len(parts) != 2 {
return nil, errInvalidAddress
}
maddr, err := multiaddr.NewMultiaddr("/ip4/" + parts[0] + "/" + proto + "/" + parts[1])
if err != nil {
return nil, err
}
if p2pID != nil {
maddr = maddr.Encapsulate(p2pID)
}
return maddr, nil
}