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

Commit

Permalink
Merge pull request #1629 from OpenBazaar/1601-ipns-self-resolve
Browse files Browse the repository at this point in the history
GET /ob/resolveipns/:peerid can perform self-lookups
  • Loading branch information
placer14 authored Jun 13, 2019
2 parents 7945d11 + cc6b44f commit ef06d07
Showing 1 changed file with 49 additions and 5 deletions.
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

0 comments on commit ef06d07

Please sign in to comment.