@@ -2,6 +2,7 @@ package main
22
33import (
44 "context"
5+ "errors"
56 "fmt"
67 gopath "path"
78
@@ -28,7 +29,9 @@ import (
2829 "github.com/ipld/go-ipld-prime"
2930 "github.com/ipld/go-ipld-prime/node/basicnode"
3031 "github.com/ipld/go-ipld-prime/schema"
32+ "github.com/libp2p/go-libp2p/core/peer"
3133 "github.com/libp2p/go-libp2p/core/routing"
34+ mc "github.com/multiformats/go-multicodec"
3235)
3336
3437type bifrostGateway struct {
@@ -112,7 +115,19 @@ func (api *bifrostGateway) GetBlock(ctx context.Context, c cid.Cid) (blocks.Bloc
112115}
113116
114117func (api * bifrostGateway ) GetIPNSRecord (ctx context.Context , c cid.Cid ) ([]byte , error ) {
115- return api .routing .GetValue (ctx , "/ipns/" + c .String ())
118+ // Fails fast if the CID is not an encoded Libp2p Key, avoids wasteful
119+ // round trips to the remote routing provider.
120+ if mc .Code (c .Type ()) != mc .Libp2pKey {
121+ return nil , errors .New ("provided cid is not an encoded libp2p key" )
122+ }
123+
124+ // The value store expects the key itself to be encoded as a multihash.
125+ id , err := peer .FromCid (c )
126+ if err != nil {
127+ return nil , err
128+ }
129+
130+ return api .routing .GetValue (ctx , "/ipns/" + string (id ))
116131}
117132
118133func (api * bifrostGateway ) GetDNSLinkRecord (ctx context.Context , hostname string ) (ifacepath.Path , error ) {
0 commit comments