mirror of
https://github.com/ipfs/kubo.git
synced 2026-02-22 02:47:48 +08:00
37 lines
623 B
Go
37 lines
623 B
Go
package peer
|
|
|
|
import (
|
|
ma "github.com/jbenet/go-multiaddr"
|
|
mh "github.com/jbenet/go-multihash"
|
|
)
|
|
|
|
type PeerId mh.Multihash
|
|
|
|
// have to map string : *Peer because slices are not comparable.
|
|
type PeerBook map[string]*Peer
|
|
|
|
type Peer struct {
|
|
Id PeerId
|
|
Addresses []*ma.Multiaddr
|
|
}
|
|
|
|
func (p *Peer) AddAddress(a *ma.Multiaddr) {
|
|
p.Addresses = append(p.Addresses, a)
|
|
}
|
|
|
|
func (p *Peer) NetAddress(n string) *ma.Multiaddr {
|
|
for _, a := range p.Addresses {
|
|
ps, err := a.Protocols()
|
|
if err != nil {
|
|
continue // invalid addr
|
|
}
|
|
|
|
for _, p := range ps {
|
|
if p.Name == n {
|
|
return a
|
|
}
|
|
}
|
|
}
|
|
return nil
|
|
}
|