@0xsequence/guard 2.3.19 → 2.3.21
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/0xsequence-guard.cjs.d.ts +2 -2
- package/dist/0xsequence-guard.cjs.dev.js +768 -0
- package/dist/0xsequence-guard.cjs.js +6 -15
- package/dist/0xsequence-guard.cjs.prod.js +768 -0
- package/dist/0xsequence-guard.esm.js +758 -0
- package/dist/declarations/src/guard.gen.d.ts +382 -0
- package/dist/declarations/src/index.d.ts +2 -0
- package/dist/declarations/src/signer.d.ts +66 -0
- package/package.json +5 -5
|
@@ -0,0 +1,758 @@
|
|
|
1
|
+
import { commons, universal } from '@0xsequence/core';
|
|
2
|
+
import { signers } from '@0xsequence/signhub';
|
|
3
|
+
import { encodeTypedDataDigest } from '@0xsequence/utils';
|
|
4
|
+
import { ethers } from 'ethers';
|
|
5
|
+
|
|
6
|
+
function _extends() {
|
|
7
|
+
return _extends = Object.assign ? Object.assign.bind() : function (n) {
|
|
8
|
+
for (var e = 1; e < arguments.length; e++) {
|
|
9
|
+
var t = arguments[e];
|
|
10
|
+
for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]);
|
|
11
|
+
}
|
|
12
|
+
return n;
|
|
13
|
+
}, _extends.apply(null, arguments);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/* eslint-disable */
|
|
17
|
+
// sequence-guard v0.4.0 776b307c2145ac7a994eec63240acae042c96067
|
|
18
|
+
// --
|
|
19
|
+
// Code generated by webrpc-gen@v0.25.3 with typescript generator. DO NOT EDIT.
|
|
20
|
+
//
|
|
21
|
+
// webrpc-gen -schema=guard.ridl -target=typescript -client -out=./clients/guard.gen.ts
|
|
22
|
+
|
|
23
|
+
const WebrpcHeader = 'Webrpc';
|
|
24
|
+
const WebrpcHeaderValue = 'webrpc@v0.25.3;gen-typescript@v0.17.0;sequence-guard@v0.4.0';
|
|
25
|
+
|
|
26
|
+
//
|
|
27
|
+
// Types
|
|
28
|
+
//
|
|
29
|
+
|
|
30
|
+
//
|
|
31
|
+
// Client
|
|
32
|
+
//
|
|
33
|
+
class Guard {
|
|
34
|
+
constructor(hostname, fetch) {
|
|
35
|
+
this.hostname = void 0;
|
|
36
|
+
this.fetch = void 0;
|
|
37
|
+
this.path = '/rpc/Guard/';
|
|
38
|
+
this.ping = (headers, signal) => {
|
|
39
|
+
return this.fetch(this.url('Ping'), createHTTPRequest({}, headers, signal)).then(res => {
|
|
40
|
+
return buildResponse(res).then(_data => {
|
|
41
|
+
return {
|
|
42
|
+
status: _data.status
|
|
43
|
+
};
|
|
44
|
+
});
|
|
45
|
+
}, error => {
|
|
46
|
+
throw WebrpcRequestFailedError.new({
|
|
47
|
+
cause: `fetch(): ${error.message || ''}`
|
|
48
|
+
});
|
|
49
|
+
});
|
|
50
|
+
};
|
|
51
|
+
this.version = (headers, signal) => {
|
|
52
|
+
return this.fetch(this.url('Version'), createHTTPRequest({}, headers, signal)).then(res => {
|
|
53
|
+
return buildResponse(res).then(_data => {
|
|
54
|
+
return {
|
|
55
|
+
version: _data.version
|
|
56
|
+
};
|
|
57
|
+
});
|
|
58
|
+
}, error => {
|
|
59
|
+
throw WebrpcRequestFailedError.new({
|
|
60
|
+
cause: `fetch(): ${error.message || ''}`
|
|
61
|
+
});
|
|
62
|
+
});
|
|
63
|
+
};
|
|
64
|
+
this.runtimeStatus = (headers, signal) => {
|
|
65
|
+
return this.fetch(this.url('RuntimeStatus'), createHTTPRequest({}, headers, signal)).then(res => {
|
|
66
|
+
return buildResponse(res).then(_data => {
|
|
67
|
+
return {
|
|
68
|
+
status: _data.status
|
|
69
|
+
};
|
|
70
|
+
});
|
|
71
|
+
}, error => {
|
|
72
|
+
throw WebrpcRequestFailedError.new({
|
|
73
|
+
cause: `fetch(): ${error.message || ''}`
|
|
74
|
+
});
|
|
75
|
+
});
|
|
76
|
+
};
|
|
77
|
+
this.getSignerConfig = (args, headers, signal) => {
|
|
78
|
+
return this.fetch(this.url('GetSignerConfig'), createHTTPRequest(args, headers, signal)).then(res => {
|
|
79
|
+
return buildResponse(res).then(_data => {
|
|
80
|
+
return {
|
|
81
|
+
signerConfig: _data.signerConfig
|
|
82
|
+
};
|
|
83
|
+
});
|
|
84
|
+
}, error => {
|
|
85
|
+
throw WebrpcRequestFailedError.new({
|
|
86
|
+
cause: `fetch(): ${error.message || ''}`
|
|
87
|
+
});
|
|
88
|
+
});
|
|
89
|
+
};
|
|
90
|
+
this.sign = (args, headers, signal) => {
|
|
91
|
+
return this.fetch(this.url('Sign'), createHTTPRequest(args, headers, signal)).then(res => {
|
|
92
|
+
return buildResponse(res).then(_data => {
|
|
93
|
+
return {
|
|
94
|
+
sig: _data.sig
|
|
95
|
+
};
|
|
96
|
+
});
|
|
97
|
+
}, error => {
|
|
98
|
+
throw WebrpcRequestFailedError.new({
|
|
99
|
+
cause: `fetch(): ${error.message || ''}`
|
|
100
|
+
});
|
|
101
|
+
});
|
|
102
|
+
};
|
|
103
|
+
this.signWith = (args, headers, signal) => {
|
|
104
|
+
return this.fetch(this.url('SignWith'), createHTTPRequest(args, headers, signal)).then(res => {
|
|
105
|
+
return buildResponse(res).then(_data => {
|
|
106
|
+
return {
|
|
107
|
+
sig: _data.sig
|
|
108
|
+
};
|
|
109
|
+
});
|
|
110
|
+
}, error => {
|
|
111
|
+
throw WebrpcRequestFailedError.new({
|
|
112
|
+
cause: `fetch(): ${error.message || ''}`
|
|
113
|
+
});
|
|
114
|
+
});
|
|
115
|
+
};
|
|
116
|
+
this.patch = (args, headers, signal) => {
|
|
117
|
+
return this.fetch(this.url('Patch'), createHTTPRequest(args, headers, signal)).then(res => {
|
|
118
|
+
return buildResponse(res).then(_data => {
|
|
119
|
+
return {
|
|
120
|
+
txs: _data.txs
|
|
121
|
+
};
|
|
122
|
+
});
|
|
123
|
+
}, error => {
|
|
124
|
+
throw WebrpcRequestFailedError.new({
|
|
125
|
+
cause: `fetch(): ${error.message || ''}`
|
|
126
|
+
});
|
|
127
|
+
});
|
|
128
|
+
};
|
|
129
|
+
this.authMethods = (args, headers, signal) => {
|
|
130
|
+
return this.fetch(this.url('AuthMethods'), createHTTPRequest(args, headers, signal)).then(res => {
|
|
131
|
+
return buildResponse(res).then(_data => {
|
|
132
|
+
return {
|
|
133
|
+
methods: _data.methods,
|
|
134
|
+
active: _data.active
|
|
135
|
+
};
|
|
136
|
+
});
|
|
137
|
+
}, error => {
|
|
138
|
+
throw WebrpcRequestFailedError.new({
|
|
139
|
+
cause: `fetch(): ${error.message || ''}`
|
|
140
|
+
});
|
|
141
|
+
});
|
|
142
|
+
};
|
|
143
|
+
this.setPIN = (args, headers, signal) => {
|
|
144
|
+
return this.fetch(this.url('SetPIN'), createHTTPRequest(args, headers, signal)).then(res => {
|
|
145
|
+
return buildResponse(res).then(_data => {
|
|
146
|
+
return {};
|
|
147
|
+
});
|
|
148
|
+
}, error => {
|
|
149
|
+
throw WebrpcRequestFailedError.new({
|
|
150
|
+
cause: `fetch(): ${error.message || ''}`
|
|
151
|
+
});
|
|
152
|
+
});
|
|
153
|
+
};
|
|
154
|
+
this.resetPIN = (args, headers, signal) => {
|
|
155
|
+
return this.fetch(this.url('ResetPIN'), createHTTPRequest(args, headers, signal)).then(res => {
|
|
156
|
+
return buildResponse(res).then(_data => {
|
|
157
|
+
return {};
|
|
158
|
+
});
|
|
159
|
+
}, error => {
|
|
160
|
+
throw WebrpcRequestFailedError.new({
|
|
161
|
+
cause: `fetch(): ${error.message || ''}`
|
|
162
|
+
});
|
|
163
|
+
});
|
|
164
|
+
};
|
|
165
|
+
this.createTOTP = (args, headers, signal) => {
|
|
166
|
+
return this.fetch(this.url('CreateTOTP'), createHTTPRequest(args, headers, signal)).then(res => {
|
|
167
|
+
return buildResponse(res).then(_data => {
|
|
168
|
+
return {
|
|
169
|
+
uri: _data.uri
|
|
170
|
+
};
|
|
171
|
+
});
|
|
172
|
+
}, error => {
|
|
173
|
+
throw WebrpcRequestFailedError.new({
|
|
174
|
+
cause: `fetch(): ${error.message || ''}`
|
|
175
|
+
});
|
|
176
|
+
});
|
|
177
|
+
};
|
|
178
|
+
this.commitTOTP = (args, headers, signal) => {
|
|
179
|
+
return this.fetch(this.url('CommitTOTP'), createHTTPRequest(args, headers, signal)).then(res => {
|
|
180
|
+
return buildResponse(res).then(_data => {
|
|
181
|
+
return {
|
|
182
|
+
codes: _data.codes
|
|
183
|
+
};
|
|
184
|
+
});
|
|
185
|
+
}, error => {
|
|
186
|
+
throw WebrpcRequestFailedError.new({
|
|
187
|
+
cause: `fetch(): ${error.message || ''}`
|
|
188
|
+
});
|
|
189
|
+
});
|
|
190
|
+
};
|
|
191
|
+
this.resetTOTP = (args, headers, signal) => {
|
|
192
|
+
return this.fetch(this.url('ResetTOTP'), createHTTPRequest(args, headers, signal)).then(res => {
|
|
193
|
+
return buildResponse(res).then(_data => {
|
|
194
|
+
return {};
|
|
195
|
+
});
|
|
196
|
+
}, error => {
|
|
197
|
+
throw WebrpcRequestFailedError.new({
|
|
198
|
+
cause: `fetch(): ${error.message || ''}`
|
|
199
|
+
});
|
|
200
|
+
});
|
|
201
|
+
};
|
|
202
|
+
this.reset2FA = (args, headers, signal) => {
|
|
203
|
+
return this.fetch(this.url('Reset2FA'), createHTTPRequest(args, headers, signal)).then(res => {
|
|
204
|
+
return buildResponse(res).then(_data => {
|
|
205
|
+
return {};
|
|
206
|
+
});
|
|
207
|
+
}, error => {
|
|
208
|
+
throw WebrpcRequestFailedError.new({
|
|
209
|
+
cause: `fetch(): ${error.message || ''}`
|
|
210
|
+
});
|
|
211
|
+
});
|
|
212
|
+
};
|
|
213
|
+
this.recoveryCodes = (args, headers, signal) => {
|
|
214
|
+
return this.fetch(this.url('RecoveryCodes'), createHTTPRequest(args, headers, signal)).then(res => {
|
|
215
|
+
return buildResponse(res).then(_data => {
|
|
216
|
+
return {
|
|
217
|
+
codes: _data.codes
|
|
218
|
+
};
|
|
219
|
+
});
|
|
220
|
+
}, error => {
|
|
221
|
+
throw WebrpcRequestFailedError.new({
|
|
222
|
+
cause: `fetch(): ${error.message || ''}`
|
|
223
|
+
});
|
|
224
|
+
});
|
|
225
|
+
};
|
|
226
|
+
this.resetRecoveryCodes = (args, headers, signal) => {
|
|
227
|
+
return this.fetch(this.url('ResetRecoveryCodes'), createHTTPRequest(args, headers, signal)).then(res => {
|
|
228
|
+
return buildResponse(res).then(_data => {
|
|
229
|
+
return {
|
|
230
|
+
codes: _data.codes
|
|
231
|
+
};
|
|
232
|
+
});
|
|
233
|
+
}, error => {
|
|
234
|
+
throw WebrpcRequestFailedError.new({
|
|
235
|
+
cause: `fetch(): ${error.message || ''}`
|
|
236
|
+
});
|
|
237
|
+
});
|
|
238
|
+
};
|
|
239
|
+
this.hostname = hostname.replace(/\/*$/, '');
|
|
240
|
+
this.fetch = (input, init) => fetch(input, init);
|
|
241
|
+
}
|
|
242
|
+
url(name) {
|
|
243
|
+
return this.hostname + this.path + name;
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
const createHTTPRequest = (body = {}, headers = {}, signal = null) => {
|
|
247
|
+
const reqHeaders = _extends({}, headers, {
|
|
248
|
+
'Content-Type': 'application/json'
|
|
249
|
+
});
|
|
250
|
+
reqHeaders[WebrpcHeader] = WebrpcHeaderValue;
|
|
251
|
+
return {
|
|
252
|
+
method: 'POST',
|
|
253
|
+
headers: reqHeaders,
|
|
254
|
+
body: JSON.stringify(body || {}),
|
|
255
|
+
signal
|
|
256
|
+
};
|
|
257
|
+
};
|
|
258
|
+
const buildResponse = res => {
|
|
259
|
+
return res.text().then(text => {
|
|
260
|
+
let data;
|
|
261
|
+
try {
|
|
262
|
+
data = JSON.parse(text);
|
|
263
|
+
} catch (error) {
|
|
264
|
+
let message = '';
|
|
265
|
+
if (error instanceof Error) {
|
|
266
|
+
message = error.message;
|
|
267
|
+
}
|
|
268
|
+
throw WebrpcBadResponseError.new({
|
|
269
|
+
status: res.status,
|
|
270
|
+
cause: `JSON.parse(): ${message}: response text: ${text}`
|
|
271
|
+
});
|
|
272
|
+
}
|
|
273
|
+
if (!res.ok) {
|
|
274
|
+
const code = typeof data.code === 'number' ? data.code : 0;
|
|
275
|
+
throw (webrpcErrorByCode[code] || WebrpcError).new(data);
|
|
276
|
+
}
|
|
277
|
+
return data;
|
|
278
|
+
});
|
|
279
|
+
};
|
|
280
|
+
|
|
281
|
+
//
|
|
282
|
+
// Errors
|
|
283
|
+
//
|
|
284
|
+
|
|
285
|
+
class WebrpcError extends Error {
|
|
286
|
+
constructor(name, code, message, status, cause) {
|
|
287
|
+
super(message);
|
|
288
|
+
this.name = void 0;
|
|
289
|
+
this.code = void 0;
|
|
290
|
+
this.message = void 0;
|
|
291
|
+
this.status = void 0;
|
|
292
|
+
this.cause = void 0;
|
|
293
|
+
/** @deprecated Use message instead of msg. Deprecated in webrpc v0.11.0. */
|
|
294
|
+
this.msg = void 0;
|
|
295
|
+
this.name = name || 'WebrpcError';
|
|
296
|
+
this.code = typeof code === 'number' ? code : 0;
|
|
297
|
+
this.message = message || `endpoint error ${this.code}`;
|
|
298
|
+
this.msg = this.message;
|
|
299
|
+
this.status = typeof status === 'number' ? status : 0;
|
|
300
|
+
this.cause = cause;
|
|
301
|
+
Object.setPrototypeOf(this, WebrpcError.prototype);
|
|
302
|
+
}
|
|
303
|
+
static new(payload) {
|
|
304
|
+
return new this(payload.error, payload.code, payload.message || payload.msg, payload.status, payload.cause);
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
// Webrpc errors
|
|
309
|
+
|
|
310
|
+
class WebrpcEndpointError extends WebrpcError {
|
|
311
|
+
constructor(name = 'WebrpcEndpoint', code = 0, message = `endpoint error`, status = 0, cause) {
|
|
312
|
+
super(name, code, message, status, cause);
|
|
313
|
+
Object.setPrototypeOf(this, WebrpcEndpointError.prototype);
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
class WebrpcRequestFailedError extends WebrpcError {
|
|
317
|
+
constructor(name = 'WebrpcRequestFailed', code = -1, message = `request failed`, status = 0, cause) {
|
|
318
|
+
super(name, code, message, status, cause);
|
|
319
|
+
Object.setPrototypeOf(this, WebrpcRequestFailedError.prototype);
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
class WebrpcBadRouteError extends WebrpcError {
|
|
323
|
+
constructor(name = 'WebrpcBadRoute', code = -2, message = `bad route`, status = 0, cause) {
|
|
324
|
+
super(name, code, message, status, cause);
|
|
325
|
+
Object.setPrototypeOf(this, WebrpcBadRouteError.prototype);
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
class WebrpcBadMethodError extends WebrpcError {
|
|
329
|
+
constructor(name = 'WebrpcBadMethod', code = -3, message = `bad method`, status = 0, cause) {
|
|
330
|
+
super(name, code, message, status, cause);
|
|
331
|
+
Object.setPrototypeOf(this, WebrpcBadMethodError.prototype);
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
class WebrpcBadRequestError extends WebrpcError {
|
|
335
|
+
constructor(name = 'WebrpcBadRequest', code = -4, message = `bad request`, status = 0, cause) {
|
|
336
|
+
super(name, code, message, status, cause);
|
|
337
|
+
Object.setPrototypeOf(this, WebrpcBadRequestError.prototype);
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
class WebrpcBadResponseError extends WebrpcError {
|
|
341
|
+
constructor(name = 'WebrpcBadResponse', code = -5, message = `bad response`, status = 0, cause) {
|
|
342
|
+
super(name, code, message, status, cause);
|
|
343
|
+
Object.setPrototypeOf(this, WebrpcBadResponseError.prototype);
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
class WebrpcServerPanicError extends WebrpcError {
|
|
347
|
+
constructor(name = 'WebrpcServerPanic', code = -6, message = `server panic`, status = 0, cause) {
|
|
348
|
+
super(name, code, message, status, cause);
|
|
349
|
+
Object.setPrototypeOf(this, WebrpcServerPanicError.prototype);
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
class WebrpcInternalErrorError extends WebrpcError {
|
|
353
|
+
constructor(name = 'WebrpcInternalError', code = -7, message = `internal error`, status = 0, cause) {
|
|
354
|
+
super(name, code, message, status, cause);
|
|
355
|
+
Object.setPrototypeOf(this, WebrpcInternalErrorError.prototype);
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
class WebrpcClientDisconnectedError extends WebrpcError {
|
|
359
|
+
constructor(name = 'WebrpcClientDisconnected', code = -8, message = `client disconnected`, status = 0, cause) {
|
|
360
|
+
super(name, code, message, status, cause);
|
|
361
|
+
Object.setPrototypeOf(this, WebrpcClientDisconnectedError.prototype);
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
class WebrpcStreamLostError extends WebrpcError {
|
|
365
|
+
constructor(name = 'WebrpcStreamLost', code = -9, message = `stream lost`, status = 0, cause) {
|
|
366
|
+
super(name, code, message, status, cause);
|
|
367
|
+
Object.setPrototypeOf(this, WebrpcStreamLostError.prototype);
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
class WebrpcStreamFinishedError extends WebrpcError {
|
|
371
|
+
constructor(name = 'WebrpcStreamFinished', code = -10, message = `stream finished`, status = 0, cause) {
|
|
372
|
+
super(name, code, message, status, cause);
|
|
373
|
+
Object.setPrototypeOf(this, WebrpcStreamFinishedError.prototype);
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
// Schema errors
|
|
378
|
+
|
|
379
|
+
class UnauthorizedError extends WebrpcError {
|
|
380
|
+
constructor(name = 'Unauthorized', code = 1000, message = `Unauthorized access`, status = 0, cause) {
|
|
381
|
+
super(name, code, message, status, cause);
|
|
382
|
+
Object.setPrototypeOf(this, UnauthorizedError.prototype);
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
class PermissionDeniedError extends WebrpcError {
|
|
386
|
+
constructor(name = 'PermissionDenied', code = 1001, message = `Permission denied`, status = 0, cause) {
|
|
387
|
+
super(name, code, message, status, cause);
|
|
388
|
+
Object.setPrototypeOf(this, PermissionDeniedError.prototype);
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
class SessionExpiredError extends WebrpcError {
|
|
392
|
+
constructor(name = 'SessionExpired', code = 1002, message = `Session expired`, status = 0, cause) {
|
|
393
|
+
super(name, code, message, status, cause);
|
|
394
|
+
Object.setPrototypeOf(this, SessionExpiredError.prototype);
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
class MethodNotFoundError extends WebrpcError {
|
|
398
|
+
constructor(name = 'MethodNotFound', code = 1003, message = `Method not found`, status = 0, cause) {
|
|
399
|
+
super(name, code, message, status, cause);
|
|
400
|
+
Object.setPrototypeOf(this, MethodNotFoundError.prototype);
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
class RequestConflictError extends WebrpcError {
|
|
404
|
+
constructor(name = 'RequestConflict', code = 1004, message = `Conflict with target resource`, status = 0, cause) {
|
|
405
|
+
super(name, code, message, status, cause);
|
|
406
|
+
Object.setPrototypeOf(this, RequestConflictError.prototype);
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
class AbortedError extends WebrpcError {
|
|
410
|
+
constructor(name = 'Aborted', code = 1005, message = `Request aborted`, status = 0, cause) {
|
|
411
|
+
super(name, code, message, status, cause);
|
|
412
|
+
Object.setPrototypeOf(this, AbortedError.prototype);
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
class GeoblockedError extends WebrpcError {
|
|
416
|
+
constructor(name = 'Geoblocked', code = 1006, message = `Geoblocked region`, status = 0, cause) {
|
|
417
|
+
super(name, code, message, status, cause);
|
|
418
|
+
Object.setPrototypeOf(this, GeoblockedError.prototype);
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
class RateLimitedError extends WebrpcError {
|
|
422
|
+
constructor(name = 'RateLimited', code = 1007, message = `Rate-limited. Please slow down.`, status = 0, cause) {
|
|
423
|
+
super(name, code, message, status, cause);
|
|
424
|
+
Object.setPrototypeOf(this, RateLimitedError.prototype);
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
class InvalidArgumentError extends WebrpcError {
|
|
428
|
+
constructor(name = 'InvalidArgument', code = 2001, message = `Invalid argument`, status = 0, cause) {
|
|
429
|
+
super(name, code, message, status, cause);
|
|
430
|
+
Object.setPrototypeOf(this, InvalidArgumentError.prototype);
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
class UnavailableError extends WebrpcError {
|
|
434
|
+
constructor(name = 'Unavailable', code = 2002, message = `Unavailable resource`, status = 0, cause) {
|
|
435
|
+
super(name, code, message, status, cause);
|
|
436
|
+
Object.setPrototypeOf(this, UnavailableError.prototype);
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
class QueryFailedError extends WebrpcError {
|
|
440
|
+
constructor(name = 'QueryFailed', code = 2003, message = `Query failed`, status = 0, cause) {
|
|
441
|
+
super(name, code, message, status, cause);
|
|
442
|
+
Object.setPrototypeOf(this, QueryFailedError.prototype);
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
class ValidationFailedError extends WebrpcError {
|
|
446
|
+
constructor(name = 'ValidationFailed', code = 2004, message = `Validation Failed`, status = 0, cause) {
|
|
447
|
+
super(name, code, message, status, cause);
|
|
448
|
+
Object.setPrototypeOf(this, ValidationFailedError.prototype);
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
class NotFoundError extends WebrpcError {
|
|
452
|
+
constructor(name = 'NotFound', code = 3000, message = `Resource not found`, status = 0, cause) {
|
|
453
|
+
super(name, code, message, status, cause);
|
|
454
|
+
Object.setPrototypeOf(this, NotFoundError.prototype);
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
const webrpcErrorByCode = {
|
|
458
|
+
[0]: WebrpcEndpointError,
|
|
459
|
+
[-1]: WebrpcRequestFailedError,
|
|
460
|
+
[-2]: WebrpcBadRouteError,
|
|
461
|
+
[-3]: WebrpcBadMethodError,
|
|
462
|
+
[-4]: WebrpcBadRequestError,
|
|
463
|
+
[-5]: WebrpcBadResponseError,
|
|
464
|
+
[-6]: WebrpcServerPanicError,
|
|
465
|
+
[-7]: WebrpcInternalErrorError,
|
|
466
|
+
[-8]: WebrpcClientDisconnectedError,
|
|
467
|
+
[-9]: WebrpcStreamLostError,
|
|
468
|
+
[-10]: WebrpcStreamFinishedError,
|
|
469
|
+
[1000]: UnauthorizedError,
|
|
470
|
+
[1001]: PermissionDeniedError,
|
|
471
|
+
[1002]: SessionExpiredError,
|
|
472
|
+
[1003]: MethodNotFoundError,
|
|
473
|
+
[1004]: RequestConflictError,
|
|
474
|
+
[1005]: AbortedError,
|
|
475
|
+
[1006]: GeoblockedError,
|
|
476
|
+
[1007]: RateLimitedError,
|
|
477
|
+
[2001]: InvalidArgumentError,
|
|
478
|
+
[2002]: UnavailableError,
|
|
479
|
+
[2003]: QueryFailedError,
|
|
480
|
+
[2004]: ValidationFailedError,
|
|
481
|
+
[3000]: NotFoundError
|
|
482
|
+
};
|
|
483
|
+
|
|
484
|
+
class GuardSigner {
|
|
485
|
+
constructor(address, url, appendSuffix = false, projectAccessKey) {
|
|
486
|
+
this.address = address;
|
|
487
|
+
this.url = url;
|
|
488
|
+
this.appendSuffix = appendSuffix;
|
|
489
|
+
this.projectAccessKey = projectAccessKey;
|
|
490
|
+
this.guard = void 0;
|
|
491
|
+
this._fetch = (input, init) => {
|
|
492
|
+
const headers = {};
|
|
493
|
+
const projectAccessKey = this.projectAccessKey;
|
|
494
|
+
if (projectAccessKey && projectAccessKey.length > 0) {
|
|
495
|
+
headers['X-Access-Key'] = projectAccessKey;
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
// before the request is made
|
|
499
|
+
init.headers = _extends({}, init.headers, headers);
|
|
500
|
+
return fetch(input, init);
|
|
501
|
+
};
|
|
502
|
+
this.guard = new Guard(url, this._fetch);
|
|
503
|
+
}
|
|
504
|
+
async getAddress() {
|
|
505
|
+
return this.address;
|
|
506
|
+
}
|
|
507
|
+
async buildDeployTransaction(_metadata) {
|
|
508
|
+
return undefined;
|
|
509
|
+
}
|
|
510
|
+
async predecorateSignedTransactions(_metadata) {
|
|
511
|
+
return [];
|
|
512
|
+
}
|
|
513
|
+
async decorateTransactions(bundle, _metadata) {
|
|
514
|
+
return bundle;
|
|
515
|
+
}
|
|
516
|
+
async sign(message, metadata) {
|
|
517
|
+
var _metadata$parts;
|
|
518
|
+
if (!commons.isWalletSignRequestMetadata(metadata)) {
|
|
519
|
+
throw new Error('expected sequence signature request metadata');
|
|
520
|
+
}
|
|
521
|
+
const guardTotpCode = metadata.guardTotpCode;
|
|
522
|
+
|
|
523
|
+
// Building auxData, notice: this uses the old v1 format
|
|
524
|
+
// TODO: We should update the guard API so we can pass the metadata directly
|
|
525
|
+
const coder = universal.genericCoderFor(metadata.config.version);
|
|
526
|
+
const {
|
|
527
|
+
encoded
|
|
528
|
+
} = coder.signature.encodeSigners(metadata.config, (_metadata$parts = metadata.parts) != null ? _metadata$parts : new Map(), [], metadata.chainId);
|
|
529
|
+
return (await this.guard.signWith({
|
|
530
|
+
signer: this.address,
|
|
531
|
+
request: {
|
|
532
|
+
msg: ethers.hexlify(message),
|
|
533
|
+
auxData: this.packMsgAndSig(metadata.address, metadata.digest, encoded, metadata.chainId),
|
|
534
|
+
chainId: Number(metadata.chainId)
|
|
535
|
+
},
|
|
536
|
+
token: guardTotpCode ? {
|
|
537
|
+
id: AuthMethod.TOTP,
|
|
538
|
+
token: guardTotpCode
|
|
539
|
+
} : undefined
|
|
540
|
+
})).sig;
|
|
541
|
+
}
|
|
542
|
+
notifyStatusChange(_id, _status, _metadata) {}
|
|
543
|
+
async getAuthMethods(proof) {
|
|
544
|
+
let response;
|
|
545
|
+
if ('jwt' in proof) {
|
|
546
|
+
response = await this.guard.authMethods({}, {
|
|
547
|
+
Authorization: `BEARER ${proof.jwt}`
|
|
548
|
+
});
|
|
549
|
+
} else {
|
|
550
|
+
const signedProof = await signOwnershipProof(proof);
|
|
551
|
+
response = await this.guard.authMethods({
|
|
552
|
+
proof: {
|
|
553
|
+
wallet: signedProof.walletAddress,
|
|
554
|
+
timestamp: signedProof.timestamp.getTime(),
|
|
555
|
+
signer: signedProof.signerAddress,
|
|
556
|
+
signature: signedProof.signature
|
|
557
|
+
}
|
|
558
|
+
});
|
|
559
|
+
}
|
|
560
|
+
return _extends({}, response, {
|
|
561
|
+
methods: response.methods.map(parseAuthMethod)
|
|
562
|
+
});
|
|
563
|
+
}
|
|
564
|
+
async setPin(pin, proof) {
|
|
565
|
+
const signedProof = await signAuthUpdateProof(proof);
|
|
566
|
+
if (pin === undefined) {
|
|
567
|
+
await this.guard.resetPIN({
|
|
568
|
+
timestamp: signedProof.timestamp.getTime(),
|
|
569
|
+
signature: signedProof.signature
|
|
570
|
+
}, {
|
|
571
|
+
Authorization: `BEARER ${proof.jwt}`
|
|
572
|
+
});
|
|
573
|
+
} else {
|
|
574
|
+
await this.guard.setPIN({
|
|
575
|
+
pin,
|
|
576
|
+
timestamp: signedProof.timestamp.getTime(),
|
|
577
|
+
signature: signedProof.signature
|
|
578
|
+
}, {
|
|
579
|
+
Authorization: `BEARER ${proof.jwt}`
|
|
580
|
+
});
|
|
581
|
+
}
|
|
582
|
+
}
|
|
583
|
+
resetPin(proof) {
|
|
584
|
+
return this.setPin(undefined, proof);
|
|
585
|
+
}
|
|
586
|
+
async createTotp(proof) {
|
|
587
|
+
const signedProof = await signAuthUpdateProof(proof);
|
|
588
|
+
const {
|
|
589
|
+
uri
|
|
590
|
+
} = await this.guard.createTOTP({
|
|
591
|
+
timestamp: signedProof.timestamp.getTime(),
|
|
592
|
+
signature: signedProof.signature
|
|
593
|
+
}, {
|
|
594
|
+
Authorization: `BEARER ${proof.jwt}`
|
|
595
|
+
});
|
|
596
|
+
return new URL(uri);
|
|
597
|
+
}
|
|
598
|
+
async commitTotp(token, jwt) {
|
|
599
|
+
const {
|
|
600
|
+
codes
|
|
601
|
+
} = await this.guard.commitTOTP({
|
|
602
|
+
token
|
|
603
|
+
}, {
|
|
604
|
+
Authorization: `BEARER ${jwt}`
|
|
605
|
+
});
|
|
606
|
+
return codes;
|
|
607
|
+
}
|
|
608
|
+
async resetTotp(proof) {
|
|
609
|
+
const signedProof = await signAuthUpdateProof(proof);
|
|
610
|
+
await this.guard.resetTOTP({
|
|
611
|
+
timestamp: signedProof.timestamp.getTime(),
|
|
612
|
+
signature: signedProof.signature
|
|
613
|
+
}, {
|
|
614
|
+
Authorization: `BEARER ${proof.jwt}`
|
|
615
|
+
});
|
|
616
|
+
}
|
|
617
|
+
async reset2fa(recoveryCode, proof) {
|
|
618
|
+
if ('jwt' in proof) {
|
|
619
|
+
await this.guard.reset2FA({
|
|
620
|
+
code: recoveryCode
|
|
621
|
+
}, {
|
|
622
|
+
Authorization: `BEARER ${proof.jwt}`
|
|
623
|
+
});
|
|
624
|
+
} else {
|
|
625
|
+
const signedProof = await signOwnershipProof(proof);
|
|
626
|
+
await this.guard.reset2FA({
|
|
627
|
+
code: recoveryCode,
|
|
628
|
+
proof: {
|
|
629
|
+
wallet: signedProof.walletAddress,
|
|
630
|
+
timestamp: signedProof.timestamp.getTime(),
|
|
631
|
+
signer: signedProof.signerAddress,
|
|
632
|
+
signature: signedProof.signature
|
|
633
|
+
}
|
|
634
|
+
});
|
|
635
|
+
}
|
|
636
|
+
}
|
|
637
|
+
async getRecoveryCodes(proof) {
|
|
638
|
+
const signedProof = await signAuthUpdateProof(proof);
|
|
639
|
+
const {
|
|
640
|
+
codes
|
|
641
|
+
} = await this.guard.recoveryCodes({
|
|
642
|
+
timestamp: signedProof.timestamp.getTime(),
|
|
643
|
+
signature: signedProof.signature
|
|
644
|
+
}, {
|
|
645
|
+
Authorization: `BEARER ${proof.jwt}`
|
|
646
|
+
});
|
|
647
|
+
return codes;
|
|
648
|
+
}
|
|
649
|
+
async resetRecoveryCodes(proof) {
|
|
650
|
+
const signedProof = await signAuthUpdateProof(proof);
|
|
651
|
+
const {
|
|
652
|
+
codes
|
|
653
|
+
} = await this.guard.resetRecoveryCodes({
|
|
654
|
+
timestamp: signedProof.timestamp.getTime(),
|
|
655
|
+
signature: signedProof.signature
|
|
656
|
+
}, {
|
|
657
|
+
Authorization: `BEARER ${proof.jwt}`
|
|
658
|
+
});
|
|
659
|
+
return codes;
|
|
660
|
+
}
|
|
661
|
+
packMsgAndSig(address, msg, sig, chainId) {
|
|
662
|
+
return ethers.AbiCoder.defaultAbiCoder().encode(['address', 'uint256', 'bytes', 'bytes'], [address, chainId, msg, sig]);
|
|
663
|
+
}
|
|
664
|
+
suffix() {
|
|
665
|
+
return new Uint8Array(this.appendSuffix ? [3] : []);
|
|
666
|
+
}
|
|
667
|
+
}
|
|
668
|
+
let AuthMethod = /*#__PURE__*/function (AuthMethod) {
|
|
669
|
+
AuthMethod["PIN"] = "PIN";
|
|
670
|
+
AuthMethod["TOTP"] = "TOTP";
|
|
671
|
+
return AuthMethod;
|
|
672
|
+
}({});
|
|
673
|
+
function parseAuthMethod(method) {
|
|
674
|
+
switch (method) {
|
|
675
|
+
case AuthMethod.PIN:
|
|
676
|
+
case AuthMethod.TOTP:
|
|
677
|
+
return method;
|
|
678
|
+
default:
|
|
679
|
+
throw new Error(`unknown auth method '${method}'`);
|
|
680
|
+
}
|
|
681
|
+
}
|
|
682
|
+
function isSignedOwnershipProof(proof) {
|
|
683
|
+
return 'signerAddress' in proof && typeof proof.signerAddress === 'string';
|
|
684
|
+
}
|
|
685
|
+
async function signOwnershipProof(proof) {
|
|
686
|
+
if (isSignedOwnershipProof(proof)) {
|
|
687
|
+
return proof;
|
|
688
|
+
} else {
|
|
689
|
+
const signer = signers.isSapientSigner(proof.signer) ? proof.signer : new signers.SignerWrapper(proof.signer);
|
|
690
|
+
const signerAddress = await signer.getAddress();
|
|
691
|
+
const timestamp = new Date();
|
|
692
|
+
const typedData = getOwnershipProofTypedData(proof.walletAddress, timestamp);
|
|
693
|
+
const digest = encodeTypedDataDigest(typedData);
|
|
694
|
+
return {
|
|
695
|
+
walletAddress: proof.walletAddress,
|
|
696
|
+
timestamp,
|
|
697
|
+
signerAddress,
|
|
698
|
+
signature: ethers.hexlify(await signer.sign(digest, {}))
|
|
699
|
+
};
|
|
700
|
+
}
|
|
701
|
+
}
|
|
702
|
+
async function signAuthUpdateProof(proof) {
|
|
703
|
+
if ('wallet' in proof) {
|
|
704
|
+
var _typedData$domain$cha;
|
|
705
|
+
const timestamp = new Date();
|
|
706
|
+
const typedData = getAuthUpdateProofTypedData(timestamp);
|
|
707
|
+
const signature = await proof.wallet.signTypedData(typedData.domain, typedData.types, typedData.message, (_typedData$domain$cha = typedData.domain.chainId) != null ? _typedData$domain$cha : 1, 'eip6492');
|
|
708
|
+
return {
|
|
709
|
+
jwt: proof.jwt,
|
|
710
|
+
timestamp,
|
|
711
|
+
signature
|
|
712
|
+
};
|
|
713
|
+
} else {
|
|
714
|
+
return proof;
|
|
715
|
+
}
|
|
716
|
+
}
|
|
717
|
+
function getOwnershipProofTypedData(wallet, timestamp) {
|
|
718
|
+
return {
|
|
719
|
+
domain,
|
|
720
|
+
types: {
|
|
721
|
+
AuthMethods: [{
|
|
722
|
+
name: 'wallet',
|
|
723
|
+
type: 'address'
|
|
724
|
+
}, {
|
|
725
|
+
name: 'timestamp',
|
|
726
|
+
type: 'string'
|
|
727
|
+
}]
|
|
728
|
+
},
|
|
729
|
+
message: {
|
|
730
|
+
wallet: ethers.getAddress(wallet),
|
|
731
|
+
timestamp: toUTCString(timestamp)
|
|
732
|
+
}
|
|
733
|
+
};
|
|
734
|
+
}
|
|
735
|
+
function getAuthUpdateProofTypedData(timestamp) {
|
|
736
|
+
return {
|
|
737
|
+
domain,
|
|
738
|
+
types: {
|
|
739
|
+
AuthUpdate: [{
|
|
740
|
+
name: 'timestamp',
|
|
741
|
+
type: 'string'
|
|
742
|
+
}]
|
|
743
|
+
},
|
|
744
|
+
message: {
|
|
745
|
+
timestamp: toUTCString(timestamp)
|
|
746
|
+
}
|
|
747
|
+
};
|
|
748
|
+
}
|
|
749
|
+
const domain = {
|
|
750
|
+
name: 'Sequence Guard',
|
|
751
|
+
version: '1',
|
|
752
|
+
chainId: 1
|
|
753
|
+
};
|
|
754
|
+
function toUTCString(date) {
|
|
755
|
+
return date.toUTCString().replace('GMT', 'UTC');
|
|
756
|
+
}
|
|
757
|
+
|
|
758
|
+
export { AuthMethod, Guard, GuardSigner, getAuthUpdateProofTypedData, getOwnershipProofTypedData, isSignedOwnershipProof, signOwnershipProof };
|