Skip to content

Commit

Permalink
Add IPFS Gateway HTTP transport metadata as a well-known multicodec
Browse files Browse the repository at this point in the history
Add SDK to aid parsing IPNI metadata that represents IPFS Gateway HTTP
transport protocol.

Relates to:
 - multiformats/multicodec#321

Fixes #21
  • Loading branch information
masih committed Apr 25, 2023
1 parent f4876a4 commit d2811a7
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 3 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ require (
github.com/libp2p/go-msgio v0.3.0
github.com/mr-tron/base58 v1.2.0
github.com/multiformats/go-multiaddr v0.9.0
github.com/multiformats/go-multicodec v0.8.1
github.com/multiformats/go-multicodec v0.9.0
github.com/multiformats/go-multihash v0.2.1
github.com/multiformats/go-multistream v0.4.1
github.com/multiformats/go-varint v0.0.7
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -401,8 +401,8 @@ github.com/multiformats/go-multibase v0.0.1/go.mod h1:bja2MqRZ3ggyXtZSEDKpl0uO/g
github.com/multiformats/go-multibase v0.0.3/go.mod h1:5+1R4eQrT3PkYZ24C3W2Ue2tPwIdYQD509ZjSb5y9Oc=
github.com/multiformats/go-multibase v0.2.0 h1:isdYCVLvksgWlMW9OZRYJEa9pZETFivncJHmHnnd87g=
github.com/multiformats/go-multibase v0.2.0/go.mod h1:bFBZX4lKCA/2lyOFSAoKH5SS6oPyjtnzK/XTFDPkNuk=
github.com/multiformats/go-multicodec v0.8.1 h1:ycepHwavHafh3grIbR1jIXnKCsFm0fqsfEOsJ8NtKE8=
github.com/multiformats/go-multicodec v0.8.1/go.mod h1:L3QTQvMIaVBkXOXXtVmYE+LI16i14xuaojr/H7Ai54k=
github.com/multiformats/go-multicodec v0.9.0 h1:pb/dlPnzee/Sxv/j4PmkDRxCOi3hXTz3IbPKOXWJkmg=
github.com/multiformats/go-multicodec v0.9.0/go.mod h1:L3QTQvMIaVBkXOXXtVmYE+LI16i14xuaojr/H7Ai54k=
github.com/multiformats/go-multihash v0.0.1/go.mod h1:w/5tugSrLEbWqlcgJabL3oHFKTwfvkofsjW2Qa1ct4U=
github.com/multiformats/go-multihash v0.0.8/go.mod h1:YSLudS+Pi8NHE7o6tb3D8vrpKa63epEDmG8nTduyAew=
github.com/multiformats/go-multihash v0.0.9/go.mod h1:YSLudS+Pi8NHE7o6tb3D8vrpKa63epEDmG8nTduyAew=
Expand Down
54 changes: 54 additions & 0 deletions metadata/ipfs_trustless_gateway.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,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
}
1 change: 1 addition & 0 deletions metadata/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ func init() {
}
d.protocols[multicodec.TransportBitswap] = func() Protocol { return &Bitswap{} }
d.protocols[multicodec.TransportGraphsyncFilecoinv1] = func() Protocol { return &GraphsyncFilecoinV1{} }
d.protocols[multicodec.TransportIpfsGatewayHttp] = func() Protocol { return &IpfsGatewayHttp{} }
Default = &d
}

Expand Down

0 comments on commit d2811a7

Please sign in to comment.