From b0771a98d1af7f61634b1cd5f1ea55802630471f Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Wed, 8 Feb 2023 09:53:40 +0100 Subject: [PATCH] fix: example get ipns record with routing api --- examples/gateway/common/blocks.go | 20 +++++++++++++++++--- examples/go.mod | 2 +- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/examples/gateway/common/blocks.go b/examples/gateway/common/blocks.go index a34acae01d..a0a0f47025 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 b6d0b1372a..59b57d4477 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.24.2 + github.com/multiformats/go-multicodec v0.7.0 github.com/prometheus/client_golang v1.14.0 github.com/stretchr/testify v1.8.1 ) @@ -82,7 +83,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-varint v0.0.7 // indirect github.com/opentracing/opentracing-go v1.2.0 // indirect