Skip to content

Commit

Permalink
feat: accept a CID or a multihash to lookup (#68)
Browse files Browse the repository at this point in the history
Closes #67
  • Loading branch information
gammazero committed Sep 20, 2024
1 parent 6e3c677 commit 5d597a2
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
12 changes: 10 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/ipfs/go-cid"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/multiformats/go-multiaddr"
"github.com/multiformats/go-multihash"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/collectors"
"github.com/prometheus/client_golang/prometheus/promhttp"
Expand Down Expand Up @@ -108,8 +109,15 @@ func startServer(ctx context.Context, d *daemon, tcpListener, metricsUsername, m
}
cidKey, err := cid.Decode(cidStr)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
mh, mhErr := multihash.FromB58String(cidStr)
if mhErr != nil {
mh, mhErr = multihash.FromHexString(cidStr)
if mhErr != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
}
cidKey = cid.NewCidV1(cid.Raw, mh)
}

checkTimeout := defaultCheckTimeout
Expand Down
4 changes: 2 additions & 2 deletions web/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ <h1 class="dib pa3 ma0 lh-tight">
</section>
<section class="bg-near-white">
<form id="queryForm" class="mw8 center lh-copy dark-gray br2 pv4 ph2 ph4-ns">
<label class="db mt3 f6 fw6" for="cid">CID</label>
<label class="db mt3 f6 fw6" for="cid">CID or multihash</label>
<input class="db w-100 pa2" type="text" id="cid" name="cid" required>
<label class="db mt3 f6 fw6" for="ma">Multiaddr (optional)</label>
<input class="db w-100 pa2" type="text" id="multiaddr" name="multiaddr" placeholder="/p2p/12D3Koo..." />
Expand Down Expand Up @@ -273,7 +273,7 @@ <h2 class="f4">What does it mean if I get an error?</h2>
outText += "IPNI\n"
}
} else {
outText += "❌ Could not find the multihash in DHR or IPNI\n"
outText += "❌ Could not find the multihash in DHT or IPNI\n"
}

if (respObj.DataAvailableOverBitswap.Error !== "") {
Expand Down

0 comments on commit 5d597a2

Please sign in to comment.