Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
resolve name handler
- Loading branch information
Showing
2 changed files
with
446 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
// Copyright 2019 Finobo | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package handlers | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
"strings" | ||
|
||
"github.com/ethereum/go-ethereum/common/hexutil" | ||
"github.com/gorilla/mux" | ||
"github.com/mailchain/mailchain/cmd/mailchain/internal/http/params" | ||
"github.com/mailchain/mailchain/errs" | ||
"github.com/mailchain/mailchain/internal/mailbox" | ||
"github.com/mailchain/mailchain/nameservice" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
// api/resolver/ethereum/mainnet | ||
// api/ethereum/mainnet/resolve | ||
// api/ethereum/mainnet/forward-lookup | ||
// api/ethereum/mainnet/address/forward-lookup | ||
// api/ethereum/mainnet/address/reverse-lookup | ||
// api/ethereum/mainnet/address/resolve | ||
// api/ethereum/mainnet/address/reverse | ||
|
||
// GetResolveName returns a handler get spec | ||
func GetResolveName(resolvers map[string]nameservice.ForwardLookup) func(w http.ResponseWriter, r *http.Request) { | ||
// Get swagger:route GET /nameservice/name/{domain-name}/resolve ResolveName NameService GetResolveName | ||
// | ||
// Get public key from an address. | ||
// | ||
// Get the public key. | ||
// | ||
// Responses: | ||
// 200: GetResolveNameResponse | ||
// 404: NotFoundError | ||
// 422: ValidationError | ||
return func(w http.ResponseWriter, hr *http.Request) { | ||
ctx := hr.Context() | ||
protocol, network, domainName, err := parseGetResolveNameRequest(hr) | ||
if err != nil { | ||
errs.JSONWriter(w, http.StatusUnprocessableEntity, errors.WithStack(err)) | ||
return | ||
} | ||
resolver, ok := resolvers[fmt.Sprintf("%s/%s", protocol, network)] | ||
if !ok { | ||
errs.JSONWriter(w, http.StatusUnprocessableEntity, errors.Errorf("no name servier resolver for chain.network configured")) | ||
return | ||
} | ||
|
||
address, err := resolver.ResolveName(ctx, protocol, network, domainName) | ||
if mailbox.IsNetworkNotSupportedError(err) { | ||
errs.JSONWriter(w, http.StatusNotAcceptable, errors.Errorf("%q not supported", protocol+"/"+network)) | ||
return | ||
} | ||
if nameservice.IsInvalidNameError(err) { | ||
errs.JSONWriter(w, http.StatusPreconditionFailed, err) | ||
return | ||
} | ||
if nameservice.IsNoResolverError(err) { | ||
errs.JSONWriter(w, http.StatusNotFound, err) | ||
return | ||
} | ||
if nameservice.IsNotFoundError(err) { | ||
errs.JSONWriter(w, http.StatusNotFound, err) | ||
return | ||
} | ||
if err != nil { | ||
errs.JSONWriter(w, http.StatusInternalServerError, errors.WithStack(err)) | ||
return | ||
} | ||
|
||
w.Header().Set("Content-Type", "application/json") | ||
_ = json.NewEncoder(w).Encode(GetResolveNameResponseBody{ | ||
Address: hexutil.Encode(address), | ||
}) | ||
} | ||
} | ||
|
||
// GetResolveNameRequest pubic key from address request | ||
// swagger:parameters GetResolveName | ||
type GetResolveNameRequest struct { | ||
// name to query to get address for | ||
// | ||
// in: path | ||
// required: true | ||
// example: name.ens | ||
Name string `json:"domain-name"` | ||
|
||
// Network for the name to resolve | ||
// | ||
// enum: mainnet,ropsten,rinkeby,local | ||
// in: path | ||
// required: true | ||
// example: ropsten | ||
Network string `json:"network"` | ||
|
||
// Protocol for the name to resolve | ||
// | ||
// enum: ethereum | ||
// in: path | ||
// required: true | ||
// example: ethereum | ||
Protocol string `json:"protocol"` | ||
} | ||
|
||
// parseGetResolveNameRequest get all the details for the get request | ||
func parseGetResolveNameRequest(r *http.Request) (protocol, network, domain string, err error) { | ||
protocol, err = params.QueryRequireProtocol(r) | ||
if err != nil { | ||
return "", "", "", err | ||
} | ||
network, err = params.QueryRequireNetwork(r) | ||
if err != nil { | ||
return "", "", "", err | ||
} | ||
domain = strings.ToLower(mux.Vars(r)["domain-name"]) | ||
|
||
return protocol, network, domain, nil | ||
} | ||
|
||
// GetResolveNameResponse address of resolved name | ||
// | ||
// swagger:response GetResolveNameResponse | ||
type GetResolveNameResponse struct { | ||
// in: body | ||
Body GetPublicKeyResponseBody | ||
} | ||
|
||
// GetBody body response | ||
// | ||
// swagger:model GetResolveNameResponseBody | ||
type GetResolveNameResponseBody struct { | ||
// The public key | ||
// | ||
// Required: true | ||
// example: 0xde0b295669a9fd93d5f28d9ec85e40f4cb697bae | ||
Address string `json:"address"` | ||
} |
Oops, something went wrong.