@0xsequence/guard 1.4.0 → 1.4.1
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.dev.js +323 -68
- package/dist/0xsequence-guard.cjs.prod.js +323 -68
- package/dist/0xsequence-guard.esm.js +321 -65
- package/dist/declarations/src/guard.gen.d.ts +93 -1
- package/dist/declarations/src/index.d.ts +0 -1
- package/dist/declarations/src/signer.d.ts +43 -9
- package/package.json +5 -3
- package/src/guard.gen.ts +177 -4
- package/src/index.ts +0 -1
- package/src/signer.ts +264 -64
|
@@ -2,8 +2,10 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
|
-
var ethers = require('ethers');
|
|
6
5
|
var core = require('@0xsequence/core');
|
|
6
|
+
var signhub = require('@0xsequence/signhub');
|
|
7
|
+
var utils = require('@0xsequence/utils');
|
|
8
|
+
var ethers = require('ethers');
|
|
7
9
|
|
|
8
10
|
function _extends() {
|
|
9
11
|
_extends = Object.assign ? Object.assign.bind() : function (target) {
|
|
@@ -20,22 +22,6 @@ function _extends() {
|
|
|
20
22
|
return _extends.apply(this, arguments);
|
|
21
23
|
}
|
|
22
24
|
|
|
23
|
-
/* eslint-disable */
|
|
24
|
-
// sequence-guard v0.4.0 a29651d1d5f63268e8d03b51e46557e0632c144d
|
|
25
|
-
// --
|
|
26
|
-
// Code generated by webrpc-gen@v0.10.x-dev with typescript generator. DO NOT EDIT.
|
|
27
|
-
//
|
|
28
|
-
// webrpc-gen -schema=guard.ridl -target=typescript -client -out=./clients/guard.gen.ts
|
|
29
|
-
|
|
30
|
-
// WebRPC description and code-gen version
|
|
31
|
-
const WebRPCVersion = 'v1';
|
|
32
|
-
|
|
33
|
-
// Schema version of your RIDL schema
|
|
34
|
-
const WebRPCSchemaVersion = 'v0.4.0';
|
|
35
|
-
|
|
36
|
-
// Schema hash generated from your RIDL schema
|
|
37
|
-
const WebRPCSchemaHash = 'a29651d1d5f63268e8d03b51e46557e0632c144d';
|
|
38
|
-
|
|
39
25
|
//
|
|
40
26
|
// Types
|
|
41
27
|
//
|
|
@@ -99,6 +85,79 @@ class Guard {
|
|
|
99
85
|
});
|
|
100
86
|
});
|
|
101
87
|
};
|
|
88
|
+
this.authMethods = (args, headers) => {
|
|
89
|
+
return this.fetch(this.url('AuthMethods'), createHTTPRequest(args, headers)).then(res => {
|
|
90
|
+
return buildResponse(res).then(_data => {
|
|
91
|
+
return {
|
|
92
|
+
methods: _data.methods
|
|
93
|
+
};
|
|
94
|
+
});
|
|
95
|
+
});
|
|
96
|
+
};
|
|
97
|
+
this.setPIN = (args, headers) => {
|
|
98
|
+
return this.fetch(this.url('SetPIN'), createHTTPRequest(args, headers)).then(res => {
|
|
99
|
+
return buildResponse(res).then(_data => {
|
|
100
|
+
return {};
|
|
101
|
+
});
|
|
102
|
+
});
|
|
103
|
+
};
|
|
104
|
+
this.resetPIN = (args, headers) => {
|
|
105
|
+
return this.fetch(this.url('ResetPIN'), createHTTPRequest(args, headers)).then(res => {
|
|
106
|
+
return buildResponse(res).then(_data => {
|
|
107
|
+
return {};
|
|
108
|
+
});
|
|
109
|
+
});
|
|
110
|
+
};
|
|
111
|
+
this.createTOTP = (args, headers) => {
|
|
112
|
+
return this.fetch(this.url('CreateTOTP'), createHTTPRequest(args, headers)).then(res => {
|
|
113
|
+
return buildResponse(res).then(_data => {
|
|
114
|
+
return {
|
|
115
|
+
uri: _data.uri
|
|
116
|
+
};
|
|
117
|
+
});
|
|
118
|
+
});
|
|
119
|
+
};
|
|
120
|
+
this.commitTOTP = (args, headers) => {
|
|
121
|
+
return this.fetch(this.url('CommitTOTP'), createHTTPRequest(args, headers)).then(res => {
|
|
122
|
+
return buildResponse(res).then(_data => {
|
|
123
|
+
return {
|
|
124
|
+
codes: _data.codes
|
|
125
|
+
};
|
|
126
|
+
});
|
|
127
|
+
});
|
|
128
|
+
};
|
|
129
|
+
this.resetTOTP = (args, headers) => {
|
|
130
|
+
return this.fetch(this.url('ResetTOTP'), createHTTPRequest(args, headers)).then(res => {
|
|
131
|
+
return buildResponse(res).then(_data => {
|
|
132
|
+
return {};
|
|
133
|
+
});
|
|
134
|
+
});
|
|
135
|
+
};
|
|
136
|
+
this.reset2FA = (args, headers) => {
|
|
137
|
+
return this.fetch(this.url('Reset2FA'), createHTTPRequest(args, headers)).then(res => {
|
|
138
|
+
return buildResponse(res).then(_data => {
|
|
139
|
+
return {};
|
|
140
|
+
});
|
|
141
|
+
});
|
|
142
|
+
};
|
|
143
|
+
this.recoveryCodes = (args, headers) => {
|
|
144
|
+
return this.fetch(this.url('RecoveryCodes'), createHTTPRequest(args, headers)).then(res => {
|
|
145
|
+
return buildResponse(res).then(_data => {
|
|
146
|
+
return {
|
|
147
|
+
codes: _data.codes
|
|
148
|
+
};
|
|
149
|
+
});
|
|
150
|
+
});
|
|
151
|
+
};
|
|
152
|
+
this.resetRecoveryCodes = (args, headers) => {
|
|
153
|
+
return this.fetch(this.url('ResetRecoveryCodes'), createHTTPRequest(args, headers)).then(res => {
|
|
154
|
+
return buildResponse(res).then(_data => {
|
|
155
|
+
return {
|
|
156
|
+
codes: _data.codes
|
|
157
|
+
};
|
|
158
|
+
});
|
|
159
|
+
});
|
|
160
|
+
};
|
|
102
161
|
this.hostname = hostname;
|
|
103
162
|
this.fetch = (input, init) => fetch(input, init);
|
|
104
163
|
}
|
|
@@ -137,12 +196,10 @@ const buildResponse = res => {
|
|
|
137
196
|
|
|
138
197
|
const fetch = typeof global === 'object' ? global.fetch : window.fetch;
|
|
139
198
|
class GuardSigner {
|
|
140
|
-
constructor(address, url, appendSuffix = false
|
|
141
|
-
this.requests = new Map();
|
|
199
|
+
constructor(address, url, appendSuffix = false) {
|
|
142
200
|
this.address = address;
|
|
143
201
|
this.url = url;
|
|
144
202
|
this.appendSuffix = appendSuffix;
|
|
145
|
-
this.onError = onError;
|
|
146
203
|
this.guard = new Guard(url, fetch);
|
|
147
204
|
}
|
|
148
205
|
async getAddress() {
|
|
@@ -157,32 +214,18 @@ class GuardSigner {
|
|
|
157
214
|
async decorateTransactions(bundle, _metadata) {
|
|
158
215
|
return bundle;
|
|
159
216
|
}
|
|
160
|
-
async requestSignature(
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
this.requests.set(id, callbacks);
|
|
167
|
-
}
|
|
168
|
-
return true;
|
|
169
|
-
}
|
|
170
|
-
notifyStatusChange(id, status, metadata) {
|
|
171
|
-
if (!this.requests.has(id)) return;
|
|
217
|
+
async requestSignature(_id, message, metadata, callbacks) {
|
|
218
|
+
var _metadata$parts;
|
|
219
|
+
const {
|
|
220
|
+
onSignature,
|
|
221
|
+
onRejection
|
|
222
|
+
} = callbacks;
|
|
172
223
|
if (!core.commons.isWalletSignRequestMetadata(metadata)) {
|
|
173
|
-
|
|
174
|
-
return;
|
|
224
|
+
onRejection('expected sequence signature request metadata');
|
|
225
|
+
return false;
|
|
175
226
|
}
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
packMsgAndSig(address, msg, sig, chainId) {
|
|
179
|
-
return ethers.ethers.utils.defaultAbiCoder.encode(['address', 'uint256', 'bytes', 'bytes'], [address, chainId, msg, sig]);
|
|
180
|
-
}
|
|
181
|
-
keyOfRequest(signer, msg, auxData, chainId) {
|
|
182
|
-
return ethers.ethers.utils.solidityKeccak256(['address', 'uint256', 'bytes', 'bytes'], [signer, chainId, msg, auxData]);
|
|
183
|
-
}
|
|
184
|
-
async evaluateRequest(id, message, _, metadata) {
|
|
185
|
-
var _metadata$parts;
|
|
227
|
+
const guardTotpCode = metadata.guardTotpCode;
|
|
228
|
+
|
|
186
229
|
// Building auxData, notice: this uses the old v1 format
|
|
187
230
|
// TODO: We should update the guard API so we can pass the metadata directly
|
|
188
231
|
const coder = core.universal.genericCoderFor(metadata.config.version);
|
|
@@ -190,41 +233,253 @@ class GuardSigner {
|
|
|
190
233
|
encoded
|
|
191
234
|
} = coder.signature.encodeSigners(metadata.config, (_metadata$parts = metadata.parts) != null ? _metadata$parts : new Map(), [], metadata.chainId);
|
|
192
235
|
try {
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
if (lastAttempt === key) {
|
|
197
|
-
return;
|
|
198
|
-
}
|
|
199
|
-
this.requests.get(id).lastAttempt = key;
|
|
200
|
-
const result = await this.guard.signWith({
|
|
236
|
+
const {
|
|
237
|
+
sig: signature
|
|
238
|
+
} = await this.guard.signWith({
|
|
201
239
|
signer: this.address,
|
|
202
240
|
request: {
|
|
203
241
|
msg: ethers.ethers.utils.hexlify(message),
|
|
204
242
|
auxData: this.packMsgAndSig(metadata.address, metadata.digest, encoded, metadata.chainId),
|
|
205
|
-
chainId: ethers.ethers.BigNumber.from(metadata.chainId).toNumber()
|
|
206
|
-
}
|
|
243
|
+
chainId: ethers.ethers.BigNumber.from(metadata.chainId).toNumber()
|
|
244
|
+
},
|
|
245
|
+
token: guardTotpCode ? {
|
|
246
|
+
id: AuthMethod.TOTP,
|
|
247
|
+
token: guardTotpCode
|
|
248
|
+
} : undefined
|
|
207
249
|
});
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
this.requests.get(id).onSignature(result.sig);
|
|
211
|
-
this.requests.delete(id);
|
|
250
|
+
if (ethers.ethers.utils.arrayify(signature).length === 0) {
|
|
251
|
+
throw new Error('guard response contained no signature data');
|
|
212
252
|
}
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
253
|
+
onSignature(signature);
|
|
254
|
+
return true;
|
|
255
|
+
} catch (error) {
|
|
256
|
+
var _ref, _error$message;
|
|
257
|
+
onRejection(`unable to request guard signature: ${(_ref = (_error$message = error.message) != null ? _error$message : error.msg) != null ? _ref : error}`);
|
|
258
|
+
return false;
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
notifyStatusChange(_id, _status, _metadata) {}
|
|
262
|
+
async getAuthMethods(proof) {
|
|
263
|
+
let response;
|
|
264
|
+
if ('jwt' in proof) {
|
|
265
|
+
response = await this.guard.authMethods({}, {
|
|
266
|
+
Authorization: `BEARER ${proof.jwt}`
|
|
267
|
+
});
|
|
268
|
+
} else {
|
|
269
|
+
const signedProof = await signOwnershipProof(proof);
|
|
270
|
+
response = await this.guard.authMethods({
|
|
271
|
+
proof: {
|
|
272
|
+
wallet: signedProof.walletAddress,
|
|
273
|
+
timestamp: signedProof.timestamp.getTime(),
|
|
274
|
+
signer: signedProof.signerAddress,
|
|
275
|
+
signature: signedProof.signature
|
|
276
|
+
}
|
|
277
|
+
});
|
|
278
|
+
}
|
|
279
|
+
return response.methods.map(parseAuthMethod);
|
|
280
|
+
}
|
|
281
|
+
async setPin(pin, proof) {
|
|
282
|
+
const signedProof = await signAuthUpdateProof(proof);
|
|
283
|
+
if (pin === undefined) {
|
|
284
|
+
await this.guard.resetPIN({
|
|
285
|
+
timestamp: signedProof.timestamp.getTime(),
|
|
286
|
+
signature: signedProof.signature
|
|
287
|
+
}, {
|
|
288
|
+
Authorization: `BEARER ${proof.jwt}`
|
|
289
|
+
});
|
|
290
|
+
} else {
|
|
291
|
+
await this.guard.setPIN({
|
|
292
|
+
pin,
|
|
293
|
+
timestamp: signedProof.timestamp.getTime(),
|
|
294
|
+
signature: signedProof.signature
|
|
295
|
+
}, {
|
|
296
|
+
Authorization: `BEARER ${proof.jwt}`
|
|
297
|
+
});
|
|
219
298
|
}
|
|
220
299
|
}
|
|
300
|
+
resetPin(proof) {
|
|
301
|
+
return this.setPin(undefined, proof);
|
|
302
|
+
}
|
|
303
|
+
async createTotp(proof) {
|
|
304
|
+
const signedProof = await signAuthUpdateProof(proof);
|
|
305
|
+
const {
|
|
306
|
+
uri
|
|
307
|
+
} = await this.guard.createTOTP({
|
|
308
|
+
timestamp: signedProof.timestamp.getTime(),
|
|
309
|
+
signature: signedProof.signature
|
|
310
|
+
}, {
|
|
311
|
+
Authorization: `BEARER ${proof.jwt}`
|
|
312
|
+
});
|
|
313
|
+
return new URL(uri);
|
|
314
|
+
}
|
|
315
|
+
async commitTotp(token, jwt) {
|
|
316
|
+
const {
|
|
317
|
+
codes
|
|
318
|
+
} = await this.guard.commitTOTP({
|
|
319
|
+
token
|
|
320
|
+
}, {
|
|
321
|
+
Authorization: `BEARER ${jwt}`
|
|
322
|
+
});
|
|
323
|
+
return codes;
|
|
324
|
+
}
|
|
325
|
+
async resetTotp(proof) {
|
|
326
|
+
const signedProof = await signAuthUpdateProof(proof);
|
|
327
|
+
await this.guard.resetTOTP({
|
|
328
|
+
timestamp: signedProof.timestamp.getTime(),
|
|
329
|
+
signature: signedProof.signature
|
|
330
|
+
}, {
|
|
331
|
+
Authorization: `BEARER ${proof.jwt}`
|
|
332
|
+
});
|
|
333
|
+
}
|
|
334
|
+
async reset2fa(recoveryCode, proof) {
|
|
335
|
+
if ('jwt' in proof) {
|
|
336
|
+
await this.guard.reset2FA({
|
|
337
|
+
code: recoveryCode
|
|
338
|
+
}, {
|
|
339
|
+
Authorization: `BEARER ${proof.jwt}`
|
|
340
|
+
});
|
|
341
|
+
} else {
|
|
342
|
+
const signedProof = await signOwnershipProof(proof);
|
|
343
|
+
await this.guard.reset2FA({
|
|
344
|
+
code: recoveryCode,
|
|
345
|
+
proof: {
|
|
346
|
+
wallet: signedProof.walletAddress,
|
|
347
|
+
timestamp: signedProof.timestamp.getTime(),
|
|
348
|
+
signer: signedProof.signerAddress,
|
|
349
|
+
signature: signedProof.signature
|
|
350
|
+
}
|
|
351
|
+
});
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
async getRecoveryCodes(proof) {
|
|
355
|
+
const signedProof = await signAuthUpdateProof(proof);
|
|
356
|
+
const {
|
|
357
|
+
codes
|
|
358
|
+
} = await this.guard.recoveryCodes({
|
|
359
|
+
timestamp: signedProof.timestamp.getTime(),
|
|
360
|
+
signature: signedProof.signature
|
|
361
|
+
}, {
|
|
362
|
+
Authorization: `BEARER ${proof.jwt}`
|
|
363
|
+
});
|
|
364
|
+
return codes;
|
|
365
|
+
}
|
|
366
|
+
async resetRecoveryCodes(proof) {
|
|
367
|
+
const signedProof = await signAuthUpdateProof(proof);
|
|
368
|
+
const {
|
|
369
|
+
codes
|
|
370
|
+
} = await this.guard.resetRecoveryCodes({
|
|
371
|
+
timestamp: signedProof.timestamp.getTime(),
|
|
372
|
+
signature: signedProof.signature
|
|
373
|
+
}, {
|
|
374
|
+
Authorization: `BEARER ${proof.jwt}`
|
|
375
|
+
});
|
|
376
|
+
return codes;
|
|
377
|
+
}
|
|
378
|
+
packMsgAndSig(address, msg, sig, chainId) {
|
|
379
|
+
return ethers.ethers.utils.defaultAbiCoder.encode(['address', 'uint256', 'bytes', 'bytes'], [address, chainId, msg, sig]);
|
|
380
|
+
}
|
|
221
381
|
suffix() {
|
|
222
382
|
return this.appendSuffix ? [3] : [];
|
|
223
383
|
}
|
|
224
384
|
}
|
|
385
|
+
let AuthMethod = /*#__PURE__*/function (AuthMethod) {
|
|
386
|
+
AuthMethod["PIN"] = "PIN";
|
|
387
|
+
AuthMethod["TOTP"] = "TOTP";
|
|
388
|
+
return AuthMethod;
|
|
389
|
+
}({});
|
|
390
|
+
function parseAuthMethod(method) {
|
|
391
|
+
switch (method) {
|
|
392
|
+
case AuthMethod.PIN:
|
|
393
|
+
case AuthMethod.TOTP:
|
|
394
|
+
return method;
|
|
395
|
+
default:
|
|
396
|
+
throw new Error(`unknown auth method '${method}'`);
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
function isSignedOwnershipProof(proof) {
|
|
400
|
+
return 'signerAddress' in proof && typeof proof.signerAddress === 'string';
|
|
401
|
+
}
|
|
402
|
+
async function signOwnershipProof(proof) {
|
|
403
|
+
if (isSignedOwnershipProof(proof)) {
|
|
404
|
+
return proof;
|
|
405
|
+
} else {
|
|
406
|
+
const signer = signhub.signers.isSapientSigner(proof.signer) ? proof.signer : new signhub.signers.SignerWrapper(proof.signer);
|
|
407
|
+
const signerAddress = await signer.getAddress();
|
|
408
|
+
const timestamp = new Date();
|
|
409
|
+
const typedData = getOwnershipProofTypedData(proof.walletAddress, timestamp);
|
|
410
|
+
const digest = utils.encodeTypedDataDigest(typedData);
|
|
411
|
+
const randomId = ethers.ethers.utils.hexlify(ethers.ethers.utils.randomBytes(32));
|
|
412
|
+
return new Promise((resolve, reject) => signer.requestSignature(randomId, digest, {}, {
|
|
413
|
+
onSignature(signature) {
|
|
414
|
+
resolve({
|
|
415
|
+
walletAddress: proof.walletAddress,
|
|
416
|
+
timestamp,
|
|
417
|
+
signerAddress,
|
|
418
|
+
signature: ethers.ethers.utils.hexlify(signature)
|
|
419
|
+
});
|
|
420
|
+
},
|
|
421
|
+
onRejection: reject,
|
|
422
|
+
onStatus(_situation) {}
|
|
423
|
+
}));
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
async function signAuthUpdateProof(proof) {
|
|
427
|
+
if ('wallet' in proof) {
|
|
428
|
+
var _typedData$domain$cha;
|
|
429
|
+
const timestamp = new Date();
|
|
430
|
+
const typedData = getAuthUpdateProofTypedData(timestamp);
|
|
431
|
+
const signature = await proof.wallet.signTypedData(typedData.domain, typedData.types, typedData.message, (_typedData$domain$cha = typedData.domain.chainId) != null ? _typedData$domain$cha : 1, 'eip6492');
|
|
432
|
+
return {
|
|
433
|
+
jwt: proof.jwt,
|
|
434
|
+
timestamp,
|
|
435
|
+
signature
|
|
436
|
+
};
|
|
437
|
+
} else {
|
|
438
|
+
return proof;
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
function getOwnershipProofTypedData(wallet, timestamp) {
|
|
442
|
+
return {
|
|
443
|
+
domain,
|
|
444
|
+
types: {
|
|
445
|
+
AuthMethods: [{
|
|
446
|
+
name: 'wallet',
|
|
447
|
+
type: 'address'
|
|
448
|
+
}, {
|
|
449
|
+
name: 'timestamp',
|
|
450
|
+
type: 'string'
|
|
451
|
+
}]
|
|
452
|
+
},
|
|
453
|
+
message: {
|
|
454
|
+
wallet: ethers.ethers.utils.getAddress(wallet),
|
|
455
|
+
timestamp: toUTCString(timestamp)
|
|
456
|
+
}
|
|
457
|
+
};
|
|
458
|
+
}
|
|
459
|
+
function getAuthUpdateProofTypedData(timestamp) {
|
|
460
|
+
return {
|
|
461
|
+
domain,
|
|
462
|
+
types: {
|
|
463
|
+
AuthUpdate: [{
|
|
464
|
+
name: 'timestamp',
|
|
465
|
+
type: 'string'
|
|
466
|
+
}]
|
|
467
|
+
},
|
|
468
|
+
message: {
|
|
469
|
+
timestamp: toUTCString(timestamp)
|
|
470
|
+
}
|
|
471
|
+
};
|
|
472
|
+
}
|
|
473
|
+
const domain = {
|
|
474
|
+
name: 'Sequence Guard',
|
|
475
|
+
version: '1',
|
|
476
|
+
chainId: 1
|
|
477
|
+
};
|
|
478
|
+
function toUTCString(date) {
|
|
479
|
+
return date.toUTCString().replace('GMT', 'UTC');
|
|
480
|
+
}
|
|
225
481
|
|
|
226
|
-
exports.
|
|
482
|
+
exports.AuthMethod = AuthMethod;
|
|
227
483
|
exports.GuardSigner = GuardSigner;
|
|
228
|
-
exports.
|
|
229
|
-
exports.
|
|
230
|
-
exports.WebRPCVersion = WebRPCVersion;
|
|
484
|
+
exports.getAuthUpdateProofTypedData = getAuthUpdateProofTypedData;
|
|
485
|
+
exports.getOwnershipProofTypedData = getOwnershipProofTypedData;
|