-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.go
73 lines (65 loc) · 1.21 KB
/
util.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
package tentacle
import (
"errors"
"github.com/driftluo/tentacle-go/secio"
ma "github.com/multiformats/go-multiaddr"
)
func deleteSlice(source []ma.Multiaddr, item ma.Multiaddr) []ma.Multiaddr {
j := 0
for _, val := range source {
if val != item {
source[j] = val
j++
}
}
return source[:j]
}
func protectRun(entry func(), report func()) {
defer func() {
err := recover()
if err != nil {
if report != nil {
report()
}
}
}()
entry()
}
func isSupport(addr ma.Multiaddr) bool {
ps := addr.Protocols()
if len(ps) < 2 {
return false
}
for _, p := range ps {
switch p.Name {
case "tcp", "ip", "ip6", "ip4", "dns", "dns4", "dns6", "p2p", "ws":
continue
default:
return false
}
}
return true
}
// ExtractPeerID get peer id from multiaddr
func ExtractPeerID(addr ma.Multiaddr) (secio.PeerID, error) {
var peerid secio.PeerID
var has bool
var err error
ma.ForEach(addr, func(c ma.Component) bool {
switch c.Protocol().Code {
case ma.P_P2P:
peerid, err = secio.PeerIDFromBytes(c.RawValue())
if err != nil {
has = false
}
has = true
return false
default:
return true
}
})
if has {
return peerid, nil
}
return nil, errors.New("Can't find")
}