/
resolve.go
153 lines (139 loc) · 4.35 KB
/
resolve.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
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"`
}