/
ipfs_trustless_gateway.go
54 lines (44 loc) · 1.38 KB
/
ipfs_trustless_gateway.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
package metadata
import (
"bytes"
"fmt"
"io"
"github.com/multiformats/go-multicodec"
"github.com/multiformats/go-varint"
)
var (
ipfsGatewayHttpBytes = append(varint.ToUvarint(uint64(multicodec.TransportIpfsGatewayHttp)), ipfsGatewayHttpPayloadLenBytes...)
ipfsGatewayHttpPayloadLenBytes = varint.ToUvarint(0)
_ Protocol = (*IpfsGatewayHttp)(nil)
)
// IpfsGatewayHttp represents the indexing metadata that uses multicodec.TransportIpfsGatewayHttp.
type IpfsGatewayHttp struct {
}
func (b IpfsGatewayHttp) ID() multicodec.Code {
return multicodec.TransportIpfsGatewayHttp
}
func (b IpfsGatewayHttp) MarshalBinary() ([]byte, error) {
return ipfsGatewayHttpBytes, nil
}
func (b IpfsGatewayHttp) UnmarshalBinary(data []byte) error {
if !bytes.Equal(data, ipfsGatewayHttpBytes) {
return fmt.Errorf("transport ID does not match %s", multicodec.TransportIpfsGatewayHttp)
}
return nil
}
func (b IpfsGatewayHttp) ReadFrom(r io.Reader) (n int64, err error) {
wantLen := len(ipfsGatewayHttpBytes)
buf := make([]byte, wantLen)
read, err := r.Read(buf)
bRead := int64(read)
if err != nil {
return bRead, err
}
if wantLen != read {
return bRead, fmt.Errorf("expected %d readable bytes but read %d", wantLen, read)
}
if !bytes.Equal(ipfsGatewayHttpBytes, buf) {
return bRead, fmt.Errorf("transport ID does not match %s", multicodec.TransportIpfsGatewayHttp)
}
return bRead, nil
}