Skip to content

Commit ca2d8c4

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 ca2d8c4

4 files changed

Lines changed: 56 additions & 3 deletions

File tree

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

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)