Skip to content

Commit e20eabc

Browse files
committed
Add IPFS Gateway HTTP transport metadata as a well-known multicodec
Add SDK to aid parsing IPNI metadata that represents IPFS Gateway HTTP transport protocol. Relates to: - multiformats/multicodec#321 Fixes #21
1 parent f4876a4 commit e20eabc

File tree

4 files changed

+58
-3
lines changed

4 files changed

+58
-3
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ require (
1616
github.com/libp2p/go-msgio v0.3.0
1717
github.com/mr-tron/base58 v1.2.0
1818
github.com/multiformats/go-multiaddr v0.9.0
19-
github.com/multiformats/go-multicodec v0.8.1
19+
github.com/multiformats/go-multicodec v0.9.0
2020
github.com/multiformats/go-multihash v0.2.1
2121
github.com/multiformats/go-multistream v0.4.1
2222
github.com/multiformats/go-varint v0.0.7

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -401,8 +401,8 @@ github.com/multiformats/go-multibase v0.0.1/go.mod h1:bja2MqRZ3ggyXtZSEDKpl0uO/g
401401
github.com/multiformats/go-multibase v0.0.3/go.mod h1:5+1R4eQrT3PkYZ24C3W2Ue2tPwIdYQD509ZjSb5y9Oc=
402402
github.com/multiformats/go-multibase v0.2.0 h1:isdYCVLvksgWlMW9OZRYJEa9pZETFivncJHmHnnd87g=
403403
github.com/multiformats/go-multibase v0.2.0/go.mod h1:bFBZX4lKCA/2lyOFSAoKH5SS6oPyjtnzK/XTFDPkNuk=
404-
github.com/multiformats/go-multicodec v0.8.1 h1:ycepHwavHafh3grIbR1jIXnKCsFm0fqsfEOsJ8NtKE8=
405-
github.com/multiformats/go-multicodec v0.8.1/go.mod h1:L3QTQvMIaVBkXOXXtVmYE+LI16i14xuaojr/H7Ai54k=
404+
github.com/multiformats/go-multicodec v0.9.0 h1:pb/dlPnzee/Sxv/j4PmkDRxCOi3hXTz3IbPKOXWJkmg=
405+
github.com/multiformats/go-multicodec v0.9.0/go.mod h1:L3QTQvMIaVBkXOXXtVmYE+LI16i14xuaojr/H7Ai54k=
406406
github.com/multiformats/go-multihash v0.0.1/go.mod h1:w/5tugSrLEbWqlcgJabL3oHFKTwfvkofsjW2Qa1ct4U=
407407
github.com/multiformats/go-multihash v0.0.8/go.mod h1:YSLudS+Pi8NHE7o6tb3D8vrpKa63epEDmG8nTduyAew=
408408
github.com/multiformats/go-multihash v0.0.9/go.mod h1:YSLudS+Pi8NHE7o6tb3D8vrpKa63epEDmG8nTduyAew=

metadata/ipfs_trustless_gateway.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package metadata
2+
3+
import (
4+
"bytes"
5+
"fmt"
6+
"io"
7+
8+
"github.com/multiformats/go-multicodec"
9+
"github.com/multiformats/go-varint"
10+
)
11+
12+
var (
13+
ipfsGatewayHttpBytes = append(varint.ToUvarint(uint64(multicodec.TransportIpfsGatewayHttp)), ipfsGatewayHttpPayloadLenBytes...)
14+
ipfsGatewayHttpPayloadLenBytes = varint.ToUvarint(0)
15+
16+
_ Protocol = (*IpfsGatewayHttp)(nil)
17+
)
18+
19+
// IpfsGatewayHttp represents the indexing metadata that uses multicodec.TransportIpfsGatewayHttp.
20+
type IpfsGatewayHttp struct {
21+
}
22+
23+
func (b IpfsGatewayHttp) ID() multicodec.Code {
24+
return multicodec.TransportIpfsGatewayHttp
25+
}
26+
27+
func (b IpfsGatewayHttp) MarshalBinary() ([]byte, error) {
28+
return ipfsGatewayHttpBytes, nil
29+
}
30+
31+
func (b IpfsGatewayHttp) UnmarshalBinary(data []byte) error {
32+
if !bytes.Equal(data, ipfsGatewayHttpBytes) {
33+
return fmt.Errorf("transport ID does not match %s", multicodec.TransportIpfsGatewayHttp)
34+
}
35+
return nil
36+
}
37+
38+
func (b IpfsGatewayHttp) ReadFrom(r io.Reader) (n int64, err error) {
39+
wantLen := len(ipfsGatewayHttpBytes)
40+
buf := make([]byte, wantLen)
41+
read, err := r.Read(buf)
42+
bRead := int64(read)
43+
if err != nil {
44+
return bRead, err
45+
}
46+
if wantLen != read {
47+
return bRead, fmt.Errorf("expected %d readable bytes but read %d", wantLen, read)
48+
}
49+
50+
if !bytes.Equal(ipfsGatewayHttpBytes, buf) {
51+
return bRead, fmt.Errorf("transport ID does not match %s", multicodec.TransportIpfsGatewayHttp)
52+
}
53+
return bRead, nil
54+
}

metadata/metadata.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ func init() {
6464
}
6565
d.protocols[multicodec.TransportBitswap] = func() Protocol { return &Bitswap{} }
6666
d.protocols[multicodec.TransportGraphsyncFilecoinv1] = func() Protocol { return &GraphsyncFilecoinV1{} }
67+
d.protocols[multicodec.TransportIpfsGatewayHttp] = func() Protocol { return &IpfsGatewayHttp{} }
6768
Default = &d
6869
}
6970

0 commit comments

Comments
 (0)