Skip to content
This repository has been archived by the owner on Mar 28, 2023. It is now read-only.

GET /ob/resolveipns/:peerid can perform self-lookups #1629

Merged
merged 5 commits into from
Jun 13, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 49 additions & 5 deletions api/jsonapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import (
"encoding/hex"
"encoding/json"
"fmt"
"gx/ipfs/QmQmhotPUzVrMEWNK3x1R5jQ5ZHWyL7tVUrmRPjrBrvyCb/go-ipfs-files"
"gx/ipfs/QmXLwxifxwfc2bAwq6rdjbYqAsGzWsDE9RM5TWMGtykyj6/interface-go-ipfs-core"
"io/ioutil"
"net/http"
"net/http/httputil"
Expand All @@ -24,9 +22,13 @@ import (
"time"

ipnspath "gx/ipfs/QmQAgv6Gaoe2tQpcabqwKXKChp2MZ7i3UXv9DqTTaxCaTR/go-path"
files "gx/ipfs/QmQmhotPUzVrMEWNK3x1R5jQ5ZHWyL7tVUrmRPjrBrvyCb/go-ipfs-files"
cid "gx/ipfs/QmTbxNB1NwDesLmKTscr4udL2tVP7MaxvXnD1D9yX7g3PN/go-cid"
datastore "gx/ipfs/QmUadX5EcvrBmxAV9sE7wUWtWSqxns5K84qKJBixmcT1w9/go-datastore"
ipns "gx/ipfs/QmUwMnKKjH3JwGKNVZ3TcP37W93xzqNA4ECFFiMo6sXkkc/go-ipns"
iface "gx/ipfs/QmXLwxifxwfc2bAwq6rdjbYqAsGzWsDE9RM5TWMGtykyj6/interface-go-ipfs-core"
peer "gx/ipfs/QmYVXrKrKHDC9FobgmcmshCDyWwdrfwfanNQN4oxJ9Fk3h/go-libp2p-peer"
routing "gx/ipfs/QmYxUdYY9S6yg5tSPVin5GFTvtfsLauVcr7reHDD3dM8xf/go-libp2p-routing"
ps "gx/ipfs/QmaCTz9RkrU13bm9kMB54f7atgqM4qkjDZpRwRoJiWXEqs/go-libp2p-peerstore"
mh "gx/ipfs/QmerPMzPk1mJVowm8KgmoknWa4yCYvvugMPsgWmDNUvDLW/go-multihash"

Expand Down Expand Up @@ -3809,6 +3811,34 @@ func (i *jsonAPIHandler) GETIPNS(w http.ResponseWriter, r *http.Request) {

func (i *jsonAPIHandler) GETResolveIPNS(w http.ResponseWriter, r *http.Request) {
_, peerID := path.Split(r.URL.Path)
if len(peerID) == 0 || peerID == "resolveipns" {
peerID = i.node.IpfsNode.Identity.Pretty()
}

type respType struct {
PeerID string `json:"peerid"`
Record struct {
Hex string `json:"hex"`
} `json:"record"`
}
var response = respType{PeerID: peerID}

if i.node.IpfsNode.Identity.Pretty() == peerID {
ipnsBytes, err := i.node.IpfsNode.Repo.Datastore().Get(namesys.IpnsDsKey(i.node.IpfsNode.Identity))
if err != nil {
ErrorResponse(w, http.StatusInternalServerError, fmt.Sprintf("retrieving self from datastore: %s", err))
return
}
response.Record.Hex = hex.EncodeToString(ipnsBytes)
b, err := json.MarshalIndent(response, "", " ")
if err != nil {
ErrorResponse(w, http.StatusInternalServerError, fmt.Sprintf("marshal json error: %s", err))
return
}

SanitizedResponse(w, string(b))
return
}

pid, err := peer.IDB58Decode(peerID)
if err != nil {
Expand All @@ -3817,15 +3847,29 @@ func (i *jsonAPIHandler) GETResolveIPNS(w http.ResponseWriter, r *http.Request)
}

ctx, cancel := context.WithTimeout(context.Background(), time.Second*180)
defer cancel()
_, err = routing.GetPublicKey(i.node.IpfsNode.Routing, ctx, pid)
cancel()
if err != nil {
ErrorResponse(w, http.StatusBadRequest, err.Error())
return
}

ipnsEntryBytes, err := i.node.IpfsNode.Routing.GetValue(ctx, "/ipns/"+pid.String())
ctx, cancel = context.WithTimeout(context.Background(), time.Second*180)
ipnsBytes, err := i.node.IpfsNode.Routing.GetValue(ctx, ipns.RecordKey(pid))
cancel()
if err != nil {
ErrorResponse(w, http.StatusBadRequest, err.Error())
return
}

SanitizedResponse(w, hex.EncodeToString(ipnsEntryBytes))
response.Record.Hex = hex.EncodeToString(ipnsBytes)
b, err := json.MarshalIndent(response, "", " ")
if err != nil {
ErrorResponse(w, http.StatusInternalServerError, fmt.Sprintf("marshal json error: %s", err))
return
}

SanitizedResponse(w, string(b))
}

func (i *jsonAPIHandler) POSTTestEmailNotifications(w http.ResponseWriter, r *http.Request) {
Expand Down