diff --git a/examples/gateway/common/blocks.go b/examples/gateway/common/blocks.go index a34acae01..a0a0f4702 100644 --- a/examples/gateway/common/blocks.go +++ b/examples/gateway/common/blocks.go @@ -31,7 +31,9 @@ import ( "github.com/ipld/go-ipld-prime" "github.com/ipld/go-ipld-prime/node/basicnode" "github.com/ipld/go-ipld-prime/schema" + "github.com/libp2p/go-libp2p/core/peer" "github.com/libp2p/go-libp2p/core/routing" + mc "github.com/multiformats/go-multicodec" ) func NewBlocksHandler(gw *BlocksGateway, port int) http.Handler { @@ -138,11 +140,23 @@ func (api *BlocksGateway) GetBlock(ctx context.Context, c cid.Cid) (blocks.Block } func (api *BlocksGateway) GetIPNSRecord(ctx context.Context, c cid.Cid) ([]byte, error) { - if api.routing != nil { - return api.routing.GetValue(ctx, "/ipns/"+c.String()) + if api.routing == nil { + return nil, routing.ErrNotSupported } - return nil, routing.ErrNotSupported + // Fails fast if the CID is not an encoded Libp2p Key, avoids wasteful + // round trips to the remote routing provider. + if mc.Code(c.Type()) != mc.Libp2pKey { + return nil, errors.New("provided cid is not an encoded libp2p key") + } + + // The value store expects the key itself to be encoded as a multihash. + id, err := peer.FromCid(c) + if err != nil { + return nil, err + } + + return api.routing.GetValue(ctx, "/ipns/"+string(id)) } func (api *BlocksGateway) GetDNSLinkRecord(ctx context.Context, hostname string) (ifacepath.Path, error) { diff --git a/examples/go.mod b/examples/go.mod index a5025194f..dcac3d4d1 100644 --- a/examples/go.mod +++ b/examples/go.mod @@ -22,6 +22,7 @@ require ( github.com/ipld/go-codec-dagpb v1.5.0 github.com/ipld/go-ipld-prime v0.19.0 github.com/libp2p/go-libp2p v0.25.1 + github.com/multiformats/go-multicodec v0.7.0 github.com/prometheus/client_golang v1.14.0 github.com/stretchr/testify v1.8.1 ) @@ -80,7 +81,6 @@ require ( github.com/multiformats/go-multiaddr v0.8.0 // indirect github.com/multiformats/go-multiaddr-dns v0.3.1 // indirect github.com/multiformats/go-multibase v0.1.1 // indirect - github.com/multiformats/go-multicodec v0.7.0 // indirect github.com/multiformats/go-multihash v0.2.1 // indirect github.com/multiformats/go-multistream v0.4.1 // indirect github.com/multiformats/go-varint v0.0.7 // indirect