-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathHttpClient.cs
More file actions
290 lines (267 loc) · 11.3 KB
/
HttpClient.cs
File metadata and controls
290 lines (267 loc) · 11.3 KB
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Net.Http;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using JetBrains.Annotations;
using Newtonsoft.Json;
using Sequence.ABI;
using Sequence.Authentication;
using Sequence.Config;
using Sequence.Provider;
using Sequence.Utils;
using Sequence.WaaS.DataTypes;
using UnityEngine;
using UnityEngine.Networking;
namespace Sequence.EmbeddedWallet
{
public class HttpClient : IHttpClient
{
private readonly string _url;
private Dictionary<string, string> _defaultHeaders;
private JsonSerializerSettings serializerSettings = new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore
};
private ResponseSignatureValidator _signatureValidator;
private string _waasUrl;
public HttpClient(string url)
{
_signatureValidator = new ResponseSignatureValidator();
_url = url;
this._defaultHeaders = new Dictionary<string, string>();
_defaultHeaders["Content-Type"] = "application/json";
_defaultHeaders["Accept"] = "application/json";
SequenceConfig config = SequenceConfig.GetConfig(SequenceService.WaaS);
_defaultHeaders["X-Access-Key"] = config.BuilderAPIKey;
_defaultHeaders["Accept-Signature"] = "sig=()";
if (string.IsNullOrWhiteSpace(config.BuilderAPIKey))
{
throw SequenceConfig.MissingConfigError("Builder API Key");
}
ConfigJwt configJwt = SequenceConfig.GetConfigJwt();
string rpcUrl = configJwt.rpcServer;
if (string.IsNullOrWhiteSpace(rpcUrl))
{
throw SequenceConfig.MissingConfigError("RPC Server");
}
_waasUrl = rpcUrl;
}
public void AddDefaultHeader(string key, string value)
{
this._defaultHeaders[key] = value;
}
public (UnityWebRequest, string, string) BuildRequest<T>(string path, T args,
[CanBeNull] Dictionary<string, string> headers = null, string overrideUrl = null)
{
string url = _url.AppendTrailingSlashIfNeeded() + path;
url = url.RemoveTrailingSlash();
if (overrideUrl != null)
{
url = overrideUrl.AppendTrailingSlashIfNeeded() + path;
}
UnityWebRequest request = UnityWebRequest.Get(url);
request.method = UnityWebRequest.kHttpVerbPOST;
SetHeaders(request, headers);
string requestJson = SetRequestData(request, args);
string method = request.method;
string headersString = ExtractHeaders(request);
string curlRequest = $"curl -X {method} '{url}' {headersString} -d '{requestJson}'";
if (requestJson == "")
{
curlRequest = $"curl -X {method} '{url}' {headersString}";
}
return (request, curlRequest, url);
}
private void SetHeaders(UnityWebRequest request, Dictionary<string, string> headers)
{
if (headers == null)
{
headers = _defaultHeaders;
}
else
{
foreach (string key in _defaultHeaders.Keys)
{
if (!headers.ContainsKey(key))
{
headers[key] = _defaultHeaders[key];
}
}
}
foreach (string key in headers.Keys)
{
if (headers[key] == null)
{
continue;
}
request.SetRequestHeader(key, headers[key]);
}
}
private string SetRequestData<T>(UnityWebRequest request, T args)
{
string requestJson = "";
if (typeof(T) == typeof(string))
{
requestJson = args as string;
}
else
{
requestJson = JsonConvert.SerializeObject(args, serializerSettings);
}
byte[] requestData = Encoding.UTF8.GetBytes(requestJson);
request.uploadHandler = new UploadHandlerRaw(requestData);
request.uploadHandler.contentType = "application/json";
return requestJson;
}
public async Task<T2> SendRequest<T, T2>(string path, T args, [CanBeNull] Dictionary<string, string> headers = null, string overrideUrl = null)
{
(UnityWebRequest, string, string) newRequest = BuildRequest(path, args, headers, overrideUrl);
UnityWebRequest request = newRequest.Item1;
string curlRequest = newRequest.Item2;
string url = newRequest.Item3;
try
{
await request.SendWebRequest();
if (request.error != null || request.result != UnityWebRequest.Result.Success ||
request.responseCode < 200 || request.responseCode > 299)
{
throw new Exception($"Error sending request to {url}: {request.responseCode} {request.error}");
}
else
{
while (!_signatureValidator.PublicKeyFetched)
{
await Task.Yield();
}
string responseJson = "";
if (url.Contains(_waasUrl))
{
try
{
responseJson = _signatureValidator.ValidateResponse(request);
}
catch (Exception e)
{
throw new Exception("Error validating response: " + e.Message + " Warning: this response may have been tampered with!");
}
}
else
{
byte[] results = request.downloadHandler.data;
responseJson = Encoding.UTF8.GetString(results);
}
try
{
T2 result = JsonConvert.DeserializeObject<T2>(responseJson);
return result;
}
catch (Exception e)
{
throw new Exception(
$"Error unmarshalling response from {url}: {e.Message} | given: {responseJson}");
}
}
}
catch (HttpRequestException e)
{
throw new Exception("HTTP Request failed: " + e.Message + GetRequestErrorIfAvailable(request) +
"\nCurl-equivalent request: " + curlRequest);
}
catch (FormatException e)
{
throw new Exception("Invalid URL format: " + e.Message + GetRequestErrorIfAvailable(request) +
"\nCurl-equivalent request: " + curlRequest);
}
catch (FileLoadException e)
{
string errorReason = GetRequestErrorIfAvailable(request);
string exceptionMessage = "File load exception: " + e.Message + " response: " + errorReason +
"\nCurl-equivalent request: " + curlRequest;
if (errorReason.Contains("intent is invalid: intent expired") || errorReason.Contains("intent is invalid: intent issued in the future"))
{
string dateHeader = request.GetResponseHeader("date");
if (string.IsNullOrWhiteSpace(dateHeader))
{
exceptionMessage += "\nNo date header found in response";
throw new Exception(exceptionMessage);
}
if (DateTime.TryParseExact(dateHeader, "ddd, dd MMM yyyy HH:mm:ss 'GMT'",
CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal, out DateTime dateTime))
{
long currentTimeAccordingToServer = ((DateTimeOffset)dateTime).ToUnixTimeSeconds();
throw new TimeMismatchException(exceptionMessage, currentTimeAccordingToServer);
}
else
{
exceptionMessage += "\nUnable to parse server time from date header in response";
throw new Exception(exceptionMessage);
}
}
else if (errorReason.Contains("JWT validation: aud not satisfied"))
{
exceptionMessage = "File load exception: " + e.Message + " response: " + errorReason +
" Please make sure you've whitelisted the associated login method and associated configuration values in your Embedded Wallet configuration in the Sequence Builder!"
+ "\nCurl-equivalent request: " + curlRequest;
}
throw new Exception(exceptionMessage);
}
catch (Exception e)
{
throw new Exception("An unexpected error occurred: " + e.Message + GetRequestErrorIfAvailable(request) +
"\nCurl-equivalent request: " + curlRequest);
}
finally
{
request.Dispose();
}
}
public async Task<TimeSpan> GetTimeShift()
{
UnityWebRequest request = UnityWebRequest.Get(_waasUrl.AppendTrailingSlashIfNeeded() + "status");
request.method = UnityWebRequest.kHttpVerbGET;
try
{
await request.SendWebRequest();
DateTime serverTime = DateTime.Parse(request.GetResponseHeader("date")).ToUniversalTime();
DateTime localTime = DateTime.UtcNow;
TimeSpan timeShift = serverTime - localTime;
return timeShift;
}
catch (Exception e)
{
Debug.LogError("Error getting time shift: " + e.Message);
return TimeSpan.Zero;
}
finally
{
request.Dispose();
}
}
private string GetRequestErrorIfAvailable(UnityWebRequest request)
{
if (request.downloadHandler != null && request.downloadHandler.data != null)
{
return " " + Encoding.UTF8.GetString(request.downloadHandler.data);
}
return "";
}
private string ExtractHeaders(UnityWebRequest request)
{
StringBuilder headerBuilder = new StringBuilder();
foreach (string headerKey in new string[]{"Content-Type", "Accept", "Authorization", "X-Sequence-Tenant", "X-Access-Key"})
{
string headerValue = request.GetRequestHeader(headerKey);
if (string.IsNullOrEmpty(headerValue))
{
continue;
}
headerBuilder.Append($"-H '{headerKey}: {headerValue}' ");
}
return headerBuilder.ToString();
}
}
}