-
Notifications
You must be signed in to change notification settings - Fork 19
/
api.go
229 lines (186 loc) · 8.25 KB
/
api.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
// Copyright (C) 2023 Gobalsky Labs Limited
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package v2
import (
"context"
"fmt"
"net/http"
"net/url"
"strings"
"code.vegaprotocol.io/vega/libs/jsonrpc"
"code.vegaprotocol.io/vega/wallet/api"
"code.vegaprotocol.io/vega/wallet/service/v2/connections"
"code.vegaprotocol.io/vega/wallet/wallet"
"go.uber.org/zap"
)
// Generates mocks
//go:generate go run github.com/golang/mock/mockgen -destination mocks/mocks.go -package mocks code.vegaprotocol.io/vega/wallet/service/v2 ClientAPI
type ClientAPI interface {
ConnectWallet(ctx context.Context, hostname string) (wallet.Wallet, *jsonrpc.ErrorDetails)
GetChainID(ctx context.Context) (jsonrpc.Result, *jsonrpc.ErrorDetails)
ListKeys(ctx context.Context, connectedWallet api.ConnectedWallet) (jsonrpc.Result, *jsonrpc.ErrorDetails)
CheckTransaction(ctx context.Context, params jsonrpc.Params, connectedWallet api.ConnectedWallet) (jsonrpc.Result, *jsonrpc.ErrorDetails)
SignTransaction(ctx context.Context, rawParams jsonrpc.Params, connectedWallet api.ConnectedWallet) (jsonrpc.Result, *jsonrpc.ErrorDetails)
SendTransaction(ctx context.Context, rawParams jsonrpc.Params, connectedWallet api.ConnectedWallet) (jsonrpc.Result, *jsonrpc.ErrorDetails)
}
type Command func(ctx context.Context, lw *responseWriter, httpRequest *http.Request, rpcRequest jsonrpc.Request) (jsonrpc.Result, *jsonrpc.ErrorDetails)
type API struct {
log *zap.Logger
commands map[string]Command
}
// NewAPI builds the wallet JSON-RPC API with specific methods that are
// intended to be publicly exposed to third-party applications in a
// non-trustable environment.
// Because of the nature of the environment from where these methods are called,
// (the "wild, wild web"), no administration methods are exposed. We don't want
// malicious third-party applications to leverage administration capabilities
// that could expose the user and/or compromise his wallets.
func NewAPI(log *zap.Logger, clientAPI ClientAPI, connectionsManager *connections.Manager) *API {
commands := map[string]Command{}
commands["client.connect_wallet"] = func(ctx context.Context, lw *responseWriter, httpRequest *http.Request, rpcRequest jsonrpc.Request) (jsonrpc.Result, *jsonrpc.ErrorDetails) {
hostname, err := resolveHostname(httpRequest)
if err != nil {
return nil, jsonrpc.NewServerError(api.ErrorCodeHostnameResolutionFailure, err)
}
selectedWallet, errDetails := clientAPI.ConnectWallet(ctx, hostname)
if errDetails != nil {
return nil, errDetails
}
token, err := connectionsManager.StartSession(hostname, selectedWallet)
if err != nil {
return nil, jsonrpc.NewServerError(api.ErrorCodeAuthenticationFailure, err)
}
lw.SetAuthorization(AsVWT(token))
return nil, nil
}
commands["client.disconnect_wallet"] = func(ctx context.Context, _ *responseWriter, httpRequest *http.Request, rpcRequest jsonrpc.Request) (jsonrpc.Result, *jsonrpc.ErrorDetails) {
vwt, err := ExtractVWT(httpRequest)
if err != nil {
return nil, jsonrpc.NewServerError(api.ErrorCodeAuthenticationFailure, err)
}
connectionsManager.EndSessionConnectionWithToken(vwt.Token())
return nil, nil
}
commands["client.get_chain_id"] = func(ctx context.Context, _ *responseWriter, httpRequest *http.Request, rpcRequest jsonrpc.Request) (jsonrpc.Result, *jsonrpc.ErrorDetails) {
return clientAPI.GetChainID(ctx)
}
commands["client.list_keys"] = func(ctx context.Context, _ *responseWriter, httpRequest *http.Request, rpcRequest jsonrpc.Request) (jsonrpc.Result, *jsonrpc.ErrorDetails) {
hostname, err := resolveHostname(httpRequest)
if err != nil {
return nil, jsonrpc.NewServerError(api.ErrorCodeHostnameResolutionFailure, err)
}
vwt, err := ExtractVWT(httpRequest)
if err != nil {
return nil, jsonrpc.NewServerError(api.ErrorCodeAuthenticationFailure, err)
}
connectedWallet, errDetails := connectionsManager.ConnectedWallet(ctx, hostname, vwt.Token())
if errDetails != nil {
return nil, errDetails
}
return clientAPI.ListKeys(ctx, connectedWallet)
}
commands["client.sign_transaction"] = func(ctx context.Context, _ *responseWriter, httpRequest *http.Request, rpcRequest jsonrpc.Request) (jsonrpc.Result, *jsonrpc.ErrorDetails) {
hostname, err := resolveHostname(httpRequest)
if err != nil {
return nil, jsonrpc.NewServerError(api.ErrorCodeHostnameResolutionFailure, err)
}
vwt, err := ExtractVWT(httpRequest)
if err != nil {
return nil, jsonrpc.NewServerError(api.ErrorCodeAuthenticationFailure, err)
}
connectedWallet, errDetails := connectionsManager.ConnectedWallet(ctx, hostname, vwt.Token())
if errDetails != nil {
return nil, errDetails
}
return clientAPI.SignTransaction(ctx, rpcRequest.Params, connectedWallet)
}
commands["client.send_transaction"] = func(ctx context.Context, _ *responseWriter, httpRequest *http.Request, rpcRequest jsonrpc.Request) (jsonrpc.Result, *jsonrpc.ErrorDetails) {
hostname, err := resolveHostname(httpRequest)
if err != nil {
return nil, jsonrpc.NewServerError(api.ErrorCodeHostnameResolutionFailure, err)
}
vwt, err := ExtractVWT(httpRequest)
if err != nil {
return nil, jsonrpc.NewServerError(api.ErrorCodeAuthenticationFailure, err)
}
connectedWallet, errDetails := connectionsManager.ConnectedWallet(ctx, hostname, vwt.Token())
if errDetails != nil {
return nil, errDetails
}
return clientAPI.SendTransaction(ctx, rpcRequest.Params, connectedWallet)
}
commands["client.check_transaction"] = func(ctx context.Context, _ *responseWriter, httpRequest *http.Request, rpcRequest jsonrpc.Request) (jsonrpc.Result, *jsonrpc.ErrorDetails) {
hostname, err := resolveHostname(httpRequest)
if err != nil {
return nil, jsonrpc.NewServerError(api.ErrorCodeHostnameResolutionFailure, err)
}
vwt, err := ExtractVWT(httpRequest)
if err != nil {
return nil, jsonrpc.NewServerError(api.ErrorCodeAuthenticationFailure, err)
}
connectedWallet, errDetails := connectionsManager.ConnectedWallet(ctx, hostname, vwt.Token())
if errDetails != nil {
return nil, errDetails
}
return clientAPI.CheckTransaction(ctx, rpcRequest.Params, connectedWallet)
}
return &API{
log: log,
commands: commands,
}
}
// resolveHostname attempts to resolve the source of the request by parsing the
// Origin (and if absent, the Referer) header.
// If it fails to resolve the hostname, it returns an error.
func resolveHostname(r *http.Request) (string, error) {
origin := r.Header.Get("Origin")
if origin != "" {
parsedOrigin, err := url.Parse(origin)
if err != nil {
return origin, nil //nolint:nilerr
}
if parsedOrigin.Host != "" {
return parsedOrigin.Host, nil
}
return normalizeHostname(origin)
}
// In some scenario, the Origin can be set to null by the browser for privacy
// reasons. Since we are not trying to fingerprint or spoof anyone, we
// attempt to parse the Referer.
referer := r.Header.Get("Referer")
if referer != "" {
parsedReferer, err := url.Parse(referer)
if err != nil {
return "", fmt.Errorf("could not parse the Referer header: %w", err)
}
return normalizeHostname(parsedReferer.Host)
}
// If none of the Origin and Referer headers are present, we just report that
// the missing Origin header as we should, ideally, only rely on this header.
// The Referer is just a "desperate" attempt to get information about the
// origin of the request and minimize the friction with future privacy
// settings.
return "", ErrOriginHeaderIsRequired
}
func normalizeHostname(host string) (string, error) {
host = trimBlankCharacters(host)
if host == "" {
return "", ErrOriginHeaderIsRequired
}
return host, nil
}
func trimBlankCharacters(host string) string {
return strings.Trim(host, " \r\n\t")
}