/
transports.go
53 lines (43 loc) · 1.22 KB
/
transports.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
package boosttypes
import (
_ "embed"
"fmt"
"github.com/ipld/go-ipld-prime/node/bindnode"
bindnoderegistry "github.com/ipld/go-ipld-prime/node/bindnode/registry"
"github.com/multiformats/go-multiaddr"
)
type Protocol struct {
// The name of the transport protocol eg "libp2p" or "http"
Name string
// The address of the endpoint in multiaddr format
Addresses []multiaddr.Multiaddr
}
type QueryResponse struct {
Protocols []Protocol
}
//go:embed transports.ipldsch
var embedSchema []byte
func multiAddrFromBytes(b []byte) (interface{}, error) {
ma, err := multiaddr.NewMultiaddrBytes(b)
if err != nil {
return nil, err
}
return &ma, err
}
func multiAddrToBytes(iface interface{}) ([]byte, error) {
ma, ok := iface.(*multiaddr.Multiaddr)
if !ok {
return nil, fmt.Errorf("expected *Multiaddr value")
}
return (*ma).Bytes(), nil
}
var BindnodeRegistry = bindnoderegistry.NewRegistry()
func init() {
var dummyMa multiaddr.Multiaddr
var bindnodeOptions = []bindnode.Option{
bindnode.TypedBytesConverter(&dummyMa, multiAddrFromBytes, multiAddrToBytes),
}
if err := BindnodeRegistry.RegisterType((*QueryResponse)(nil), string(embedSchema), "QueryResponse", bindnodeOptions...); err != nil {
panic(err.Error())
}
}