-
Notifications
You must be signed in to change notification settings - Fork 6
/
message.go
55 lines (49 loc) · 1.64 KB
/
message.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
package message
import (
"errors"
"strings"
"github.com/ipfs/go-cid"
"github.com/multiformats/go-multiaddr"
)
var ErrBadEncoding = errors.New("invalid message encoding")
// Message announces the availability of an IPNI advertisement..
type Message struct {
// Cid identifies the advertisement being announced.
Cid cid.Cid
// Addrs contains a set of multiaddrs that specify where the announced
// advertisement is available. See SetAddrs and GetAddrs.
Addrs [][]byte
// ExtraData is optional data indended for a certain recipients. For
// example, a publisher may include its storage provider ID for validation
// by a gateway.
ExtraData []byte
// The OrigPeer field may or may not be present in the serialized data, and
// the CBOR serializer/deserializer is able to detect that. Only messages
// that are re-published by an indexer, for consumption by othen indexers,
// contain this field.
OrigPeer string
}
// SetAddrs writes a slice of Multiaddr into the Message as a slice of []byte.
func (m *Message) SetAddrs(addrs []multiaddr.Multiaddr) {
m.Addrs = make([][]byte, len(addrs))
for i, a := range addrs {
m.Addrs[i] = a.Bytes()
}
}
// GetAddrs reads the slice of Multiaddr that is stored in the Message as a
// slice of []byte.
func (m *Message) GetAddrs() ([]multiaddr.Multiaddr, error) {
addrs := make([]multiaddr.Multiaddr, 0, len(m.Addrs))
for _, addrBytes := range m.Addrs {
addr, err := multiaddr.NewMultiaddrBytes(addrBytes)
if err != nil {
if strings.Contains(err.Error(), "no protocol with code") {
// Ignore unknown protocols.
continue
}
return nil, err
}
addrs = append(addrs, addr)
}
return addrs, nil
}