@portal-hq/web 3.7.0 → 3.8.0

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.
@@ -9,7 +9,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
9
9
  };
10
10
  import { PortalMpcError } from './errors';
11
11
  import { BackupMethods, } from '../index';
12
- const WEB_SDK_VERSION = '3.7.0';
12
+ const WEB_SDK_VERSION = '3.8.0';
13
13
  class Mpc {
14
14
  get ready() {
15
15
  return this._ready;
@@ -52,172 +52,64 @@ class Mpc {
52
52
  // Noop
53
53
  }) {
54
54
  return __awaiter(this, void 0, void 0, function* () {
55
- const message = {
56
- type: 'portal:wasm:backup',
57
- data,
58
- };
59
55
  // validates password config for password backup
60
56
  this.validateBackupConfig(data);
61
- return new Promise((resolve, reject) => {
62
- // Create a function to be bound when backup is triggered
63
- const handleBackup = (event) => {
64
- const { type, data: result } = event.data;
65
- const { origin } = event;
66
- // ignore any broadcast postMessages
67
- if (origin !== this.getOrigin()) {
68
- return;
69
- }
70
- if (type === 'portal:wasm:backupError') {
71
- // Remove the event listeners
72
- window.removeEventListener('message', handleBackup);
73
- window.removeEventListener('message', handleProgress);
74
- // Reject the promise with the error
75
- return reject(new PortalMpcError(result));
76
- }
77
- else if (type === 'portal:wasm:backupResult') {
78
- // Remove the event listeners
79
- window.removeEventListener('message', handleBackup);
80
- window.removeEventListener('message', handleProgress);
81
- const resultData = result;
82
- const backupIds = Array.isArray(resultData.backupIds)
83
- ? resultData.backupIds
84
- : [];
85
- const storageCallback = () => __awaiter(this, void 0, void 0, function* () {
86
- yield this.setBackupStatus('STORED_CLIENT_BACKUP_SHARE', backupIds);
87
- });
88
- // Resolve the promise with the result
89
- resolve({
90
- cipherText: resultData.cipherText,
91
- encryptionKey: typeof resultData.encryptionKey === 'string'
92
- ? resultData.encryptionKey
93
- : undefined,
94
- storageCallback,
95
- });
96
- }
97
- };
98
- const handleProgress = (message) => {
99
- const { type, data: status } = message.data;
100
- const { origin } = message;
101
- // ignore any broadcast postMessages
102
- if (origin !== this.getOrigin()) {
103
- return;
104
- }
105
- if (type === 'portal:wasm:backupProgress') {
106
- void progress(status);
107
- }
108
- };
109
- // Bind the function to the message event
110
- window.addEventListener('message', handleBackup);
111
- window.addEventListener('message', handleProgress);
112
- // Send the request to the iframe
113
- this.postMessage(message);
57
+ return this.handleRequestToIframeAndPost({
58
+ methodMessage: 'portal:wasm:backup',
59
+ errorMessage: 'portal:wasm:backupError',
60
+ resultMessage: 'portal:wasm:backupResult',
61
+ data,
62
+ progressMessage: 'portal:wasm:backupProgress',
63
+ progressCallback: progress,
64
+ mapReturnValue: (result) => {
65
+ const resultData = result;
66
+ const backupIds = Array.isArray(resultData.backupIds)
67
+ ? resultData.backupIds
68
+ : [];
69
+ const storageCallback = () => __awaiter(this, void 0, void 0, function* () {
70
+ yield this.setBackupStatus('STORED_CLIENT_BACKUP_SHARE', backupIds);
71
+ });
72
+ return {
73
+ cipherText: result.cipherText,
74
+ encryptionKey: typeof resultData.encryptionKey === 'string'
75
+ ? resultData.encryptionKey
76
+ : undefined,
77
+ storageCallback,
78
+ };
79
+ },
114
80
  });
115
81
  });
116
82
  }
117
83
  clearLocalWallet() {
118
- return new Promise((resolve) => {
119
- const handleClearLocalWallet = (event) => {
120
- const { type } = event.data;
121
- const { origin } = event;
122
- // ignore any broadcast postMessages
123
- if (origin !== this.getOrigin()) {
124
- return;
125
- }
126
- if (type === 'portal:destroyResult') {
127
- // Remove the event listener
128
- window.removeEventListener('message', handleClearLocalWallet);
129
- // Resolve the promise with the result
130
- resolve(true);
131
- }
132
- };
133
- // Bind the function to the message event
134
- window.addEventListener('message', handleClearLocalWallet);
135
- // Send the request to the iframe
136
- this.postMessage({
137
- type: 'portal:destroy',
138
- data: {},
139
- });
84
+ return this.handleRequestToIframeAndPost({
85
+ methodMessage: 'portal:destroy',
86
+ errorMessage: 'portal:destroyError',
87
+ resultMessage: 'portal:destroyResult',
88
+ data: {},
89
+ mapReturnValue: () => true,
140
90
  });
141
91
  }
142
92
  generate(data, progress = () => {
143
93
  // Noop
144
94
  }) {
145
95
  return __awaiter(this, void 0, void 0, function* () {
146
- const message = {
147
- type: 'portal:wasm:generate',
96
+ return this.handleRequestToIframeAndPost({
97
+ methodMessage: 'portal:wasm:generate',
98
+ errorMessage: 'portal:wasm:generateError',
99
+ resultMessage: 'portal:wasm:generateResult',
148
100
  data,
149
- };
150
- return new Promise((resolve, reject) => {
151
- // Create a function to be bound when generate is triggered
152
- const handleGenerate = (event) => {
153
- const { type, data: result } = event.data;
154
- const { origin } = event;
155
- // ignore any broadcast postMessages
156
- if (origin !== this.getOrigin()) {
157
- return;
158
- }
159
- // Handle errors
160
- if (type === 'portal:wasm:generateError') {
161
- // Remove the event listeners
162
- window.removeEventListener('message', handleGenerate);
163
- window.removeEventListener('message', handleProgress);
164
- // Reject the promise with the error
165
- return reject(new PortalMpcError(result));
166
- }
167
- else if (type === 'portal:wasm:generateResult') {
168
- // Remove the event listeners
169
- window.removeEventListener('message', handleGenerate);
170
- window.removeEventListener('message', handleProgress);
171
- // Resolve the promise with the result
172
- resolve(result);
173
- }
174
- };
175
- const handleProgress = (message) => {
176
- const { type, data: status } = message.data;
177
- const { origin } = message;
178
- // ignore any broadcast postMessages
179
- if (origin !== this.getOrigin()) {
180
- return;
181
- }
182
- if (type === 'portal:wasm:generateProgress') {
183
- void progress(status);
184
- }
185
- };
186
- // Bind the function to the message event
187
- window.addEventListener('message', handleGenerate);
188
- window.addEventListener('message', handleProgress);
189
- // Send the request to the iframe
190
- this.postMessage(message);
101
+ progressMessage: 'portal:wasm:generateProgress',
102
+ progressCallback: progress,
191
103
  });
192
104
  });
193
105
  }
194
106
  getAddress() {
195
107
  return __awaiter(this, void 0, void 0, function* () {
196
- const message = {
197
- type: 'portal:address',
108
+ return this.handleRequestToIframeAndPost({
109
+ methodMessage: 'portal:address',
110
+ errorMessage: 'portal:addressError',
111
+ resultMessage: 'portal:addressResult',
198
112
  data: {},
199
- };
200
- return new Promise((resolve) => {
201
- // Create a function to be bound when getAddress is triggered
202
- const handleGetAddress = (event) => {
203
- const { type, data: result } = event.data;
204
- const { origin } = event;
205
- // ignore any broadcast postMessages
206
- if (origin !== this.getOrigin()) {
207
- return;
208
- }
209
- // Check that the message is the one we're looking for
210
- if (type === 'portal:addressResult') {
211
- // Remove the event listener
212
- window.removeEventListener('message', handleGetAddress);
213
- // Resolve the promise with the result
214
- resolve(result);
215
- }
216
- };
217
- // Bind the function to the message event
218
- window.addEventListener('message', handleGetAddress);
219
- // Send the request to the iframe
220
- this.postMessage(message);
221
113
  });
222
114
  });
223
115
  }
@@ -225,138 +117,46 @@ class Mpc {
225
117
  // Noop
226
118
  }) {
227
119
  return __awaiter(this, void 0, void 0, function* () {
228
- return new Promise((resolve, reject) => {
229
- const handleRecover = (message) => {
230
- const { type, data } = message.data;
231
- const { origin } = message;
232
- // ignore any broadcast postMessages
233
- if (origin !== this.getOrigin()) {
234
- return;
235
- }
236
- if (type === 'portal:wasm:recoverError') {
237
- // Remove the event listeners
238
- window.removeEventListener('message', handleRecover);
239
- window.removeEventListener('message', handleProgress);
240
- reject(new PortalMpcError(data));
241
- }
242
- else if (type === 'portal:wasm:recoverResult') {
243
- // Remove the event listeners
244
- window.removeEventListener('message', handleRecover);
245
- window.removeEventListener('message', handleProgress);
246
- resolve(data);
247
- }
248
- };
249
- const handleProgress = (message) => {
250
- const { type, data: status } = message.data;
251
- const { origin } = message;
252
- // ignore any broadcast postMessages
253
- if (origin !== this.getOrigin()) {
254
- return;
255
- }
256
- if (type === 'portal:wasm:recoverProgress') {
257
- void progress(status);
258
- }
259
- };
260
- window.addEventListener('message', handleRecover);
261
- window.addEventListener('message', handleProgress);
262
- this.postMessage({
263
- type: 'portal:wasm:recover',
264
- data,
265
- });
120
+ return this.handleRequestToIframeAndPost({
121
+ methodMessage: 'portal:wasm:recover',
122
+ errorMessage: 'portal:wasm:recoverError',
123
+ resultMessage: 'portal:wasm:recoverResult',
124
+ data,
125
+ progressMessage: 'portal:wasm:recoverProgress',
126
+ progressCallback: progress,
266
127
  });
267
128
  });
268
129
  }
269
130
  eject(data) {
270
131
  return __awaiter(this, void 0, void 0, function* () {
271
- return new Promise((resolve, reject) => {
272
- const handleEject = (message) => {
273
- const { type, data: result } = message.data;
274
- const { origin } = message;
275
- // ignore any broadcast postMessages
276
- if (origin !== this.getOrigin()) {
277
- return;
278
- }
279
- if (type === 'portal:wasm:ejectError') {
280
- // Remove the event listeners
281
- window.removeEventListener('message', handleEject);
282
- reject(new PortalMpcError(result));
283
- }
284
- else if (type === 'portal:wasm:ejectResult') {
285
- // Remove the event listeners
286
- window.removeEventListener('message', handleEject);
287
- resolve(result);
288
- }
289
- };
290
- window.addEventListener('message', handleEject);
291
- this.postMessage({
292
- type: 'portal:wasm:eject',
293
- data,
294
- });
132
+ return this.handleRequestToIframeAndPost({
133
+ methodMessage: 'portal:wasm:eject',
134
+ errorMessage: 'portal:wasm:ejectError',
135
+ resultMessage: 'portal:wasm:ejectResult',
136
+ data,
295
137
  });
296
138
  });
297
139
  }
298
140
  ejectPrivateKeys(data) {
299
141
  return __awaiter(this, void 0, void 0, function* () {
300
- return new Promise((resolve, reject) => {
301
- const handleEject = (message) => {
302
- const { type, data: result } = message.data;
303
- const { origin } = message;
304
- // ignore any broadcast postMessages
305
- if (origin !== this.getOrigin()) {
306
- return;
307
- }
308
- if (type === 'portal:wasm:ejectPrivateKeysError') {
309
- // Remove the event listeners
310
- window.removeEventListener('message', handleEject);
311
- reject(new PortalMpcError(result));
312
- }
313
- else if (type === 'portal:wasm:ejectPrivateKeysResult') {
314
- // Remove the event listeners
315
- window.removeEventListener('message', handleEject);
316
- resolve(result);
317
- }
318
- };
319
- window.addEventListener('message', handleEject);
320
- this.postMessage({
321
- type: 'portal:wasm:ejectPrivateKeys',
322
- data,
323
- });
142
+ return this.handleRequestToIframeAndPost({
143
+ methodMessage: 'portal:wasm:ejectPrivateKeys',
144
+ errorMessage: 'portal:wasm:ejectPrivateKeysError',
145
+ resultMessage: 'portal:wasm:ejectPrivateKeysResult',
146
+ data,
324
147
  });
325
148
  });
326
149
  }
327
150
  rawSign(curve, param) {
328
151
  return __awaiter(this, void 0, void 0, function* () {
329
- return new Promise((resolve, reject) => {
330
- const handleRawSign = (event) => {
331
- const { type, data: result } = event.data;
332
- const { origin } = event;
333
- // ignore any broadcast postMessages
334
- if (origin !== this.getOrigin()) {
335
- return;
336
- }
337
- if (type === 'portal:mpc:rawSignError') {
338
- // Remove the event listeners
339
- window.removeEventListener('message', handleRawSign);
340
- // Reject the promise with the error
341
- return reject(new PortalMpcError(result));
342
- }
343
- else if (type === 'portal:mpc:rawSignResult') {
344
- // Remove the event listeners
345
- window.removeEventListener('message', handleRawSign);
346
- // Resolve the promise with the result
347
- resolve(result);
348
- }
349
- };
350
- // Bind the function to the message event
351
- window.addEventListener('message', handleRawSign);
352
- // Send the request to the iframe
353
- this.postMessage({
354
- type: 'portal:mpc:rawSign',
355
- data: {
356
- curve,
357
- param,
358
- },
359
- });
152
+ return this.handleRequestToIframeAndPost({
153
+ methodMessage: 'portal:mpc:rawSign',
154
+ errorMessage: 'portal:mpc:rawSignError',
155
+ resultMessage: 'portal:mpc:rawSignResult',
156
+ data: {
157
+ curve,
158
+ param,
159
+ },
360
160
  });
361
161
  });
362
162
  }
@@ -364,82 +164,23 @@ class Mpc {
364
164
  // Noop
365
165
  }) {
366
166
  return __awaiter(this, void 0, void 0, function* () {
367
- return new Promise((resolve, reject) => {
368
- // Create a function to be bound when sign is triggered
369
- const handleSign = (event) => {
370
- const { type, data: result } = event.data;
371
- const { origin } = event;
372
- // ignore any broadcast postMessages
373
- if (origin !== this.getOrigin()) {
374
- return;
375
- }
376
- if (type === 'portal:wasm:signError') {
377
- // Remove the event listeners
378
- window.removeEventListener('message', handleSign);
379
- window.removeEventListener('message', handleProgress);
380
- // Reject the promise with the error
381
- return reject(new PortalMpcError(result));
382
- }
383
- else if (type === 'portal:wasm:signResult') {
384
- // Remove the event listeners
385
- window.removeEventListener('message', handleSign);
386
- window.removeEventListener('message', handleProgress);
387
- // Resolve the promise with the result
388
- resolve(result);
389
- }
390
- };
391
- const handleProgress = (message) => {
392
- const { type, data: status } = message.data;
393
- const { origin } = message;
394
- // ignore any broadcast postMessages
395
- if (origin !== this.getOrigin()) {
396
- return;
397
- }
398
- if (type === 'portal:wasm:signProgress') {
399
- void progress(status);
400
- }
401
- };
402
- // Bind the functions to the message event
403
- window.addEventListener('message', handleSign);
404
- window.addEventListener('message', handleProgress);
405
- // Send the request to the iframe
406
- this.postMessage({
407
- type: 'portal:wasm:sign',
408
- data,
409
- });
167
+ return this.handleRequestToIframeAndPost({
168
+ methodMessage: 'portal:wasm:sign',
169
+ errorMessage: 'portal:wasm:signError',
170
+ resultMessage: 'portal:wasm:signResult',
171
+ data,
172
+ progressMessage: 'portal:wasm:signProgress',
173
+ progressCallback: progress,
410
174
  });
411
175
  });
412
176
  }
413
177
  checkSharesOnDevice() {
414
178
  return __awaiter(this, void 0, void 0, function* () {
415
- return new Promise((resolve, reject) => {
416
- const handleCheckSharesOnDevice = (event) => {
417
- const { type, data } = event.data;
418
- const { origin } = event;
419
- // ignore any broadcast postMessages
420
- if (origin !== this.getOrigin()) {
421
- return;
422
- }
423
- if (type === 'portal:checkSharesOnDeviceError') {
424
- // Remove the event listener
425
- window.removeEventListener('message', handleCheckSharesOnDevice);
426
- // Reject the promise with the error
427
- return reject(new PortalMpcError(data));
428
- }
429
- else if (type === 'portal:checkSharesOnDeviceResult') {
430
- // Remove the event listener
431
- window.removeEventListener('message', handleCheckSharesOnDevice);
432
- // Resolve the promise with the result
433
- resolve(data);
434
- }
435
- };
436
- // Bind the function to the message event
437
- window.addEventListener('message', handleCheckSharesOnDevice);
438
- // Send the request to the iframe
439
- this.postMessage({
440
- type: 'portal:checkSharesOnDevice',
441
- data: {},
442
- });
179
+ return this.handleRequestToIframeAndPost({
180
+ methodMessage: 'portal:checkSharesOnDevice',
181
+ errorMessage: 'portal:checkSharesOnDeviceError',
182
+ resultMessage: 'portal:checkSharesOnDeviceResult',
183
+ data: {},
443
184
  });
444
185
  });
445
186
  }
@@ -448,551 +189,185 @@ class Mpc {
448
189
  *******************************/
449
190
  getBalances(chainId) {
450
191
  return __awaiter(this, void 0, void 0, function* () {
451
- return new Promise((resolve, reject) => {
452
- const handleGetBalances = (event) => {
453
- const { type, data } = event.data;
454
- const { origin } = event;
455
- // ignore any broadcast postMessages
456
- if (origin !== this.getOrigin()) {
457
- return;
458
- }
459
- if (type === 'portal:getBalancesError') {
460
- // Remove the event listener
461
- window.removeEventListener('message', handleGetBalances);
462
- // Reject the promise with the error
463
- return reject(new PortalMpcError(data));
464
- }
465
- else if (type === 'portal:getBalancesResult') {
466
- // Remove the event listener
467
- window.removeEventListener('message', handleGetBalances);
468
- // Resolve the promise with the result
469
- resolve(data);
470
- }
471
- };
472
- // Bind the function to the message event
473
- window.addEventListener('message', handleGetBalances);
474
- // Send the request to the iframe
475
- this.postMessage({
476
- type: 'portal:getBalances',
477
- data: { chainId },
478
- });
192
+ return this.handleRequestToIframeAndPost({
193
+ methodMessage: 'portal:getBalances',
194
+ errorMessage: 'portal:getBalancesError',
195
+ resultMessage: 'portal:getBalancesResult',
196
+ data: { chainId },
479
197
  });
480
198
  });
481
199
  }
482
200
  fund(chainId, params) {
483
201
  return __awaiter(this, void 0, void 0, function* () {
484
- return new Promise((resolve, reject) => {
485
- const handleFund = (event) => {
486
- const { type, data } = event.data;
487
- const { origin } = event;
488
- // Ignore any broadcast postMessages
489
- if (origin !== this.getOrigin()) {
490
- return;
491
- }
492
- if (type === 'portal:fundError') {
493
- // Remove the event listener
494
- window.removeEventListener('message', handleFund);
495
- // Reject the promise with the error
496
- return reject(new PortalMpcError(data));
497
- }
498
- else if (type === 'portal:fundResult') {
499
- // Remove the event listener
500
- window.removeEventListener('message', handleFund);
501
- // Resolve the promise with the result
502
- resolve(data);
503
- }
504
- };
505
- // Bind the function to the message event
506
- window.addEventListener('message', handleFund);
507
- // Send the request to the iframe
508
- this.postMessage({
509
- type: 'portal:fund',
510
- data: { chainId, params },
511
- });
202
+ return this.handleRequestToIframeAndPost({
203
+ methodMessage: 'portal:fund',
204
+ errorMessage: 'portal:fundError',
205
+ resultMessage: 'portal:fundResult',
206
+ data: { chainId, params },
512
207
  });
513
208
  });
514
209
  }
515
210
  getClient() {
516
211
  return __awaiter(this, void 0, void 0, function* () {
517
- return new Promise((resolve, reject) => {
518
- const handleGetClient = (event) => {
519
- const { type, data } = event.data;
520
- const { origin } = event;
521
- // ignore any broadcast postMessages
522
- if (origin !== this.getOrigin()) {
523
- return;
524
- }
525
- if (type === 'portal:getClientError') {
526
- // Remove the event listener
527
- window.removeEventListener('message', handleGetClient);
528
- // Reject the promise with the error
529
- return reject(new PortalMpcError(data));
530
- }
531
- else if (type === 'portal:getClientResult') {
532
- // Remove the event listener
533
- window.removeEventListener('message', handleGetClient);
534
- // Resolve the promise with the result
535
- resolve(data);
536
- }
537
- };
538
- // Bind the function to the message event
539
- window.addEventListener('message', handleGetClient);
540
- // Send the request to the iframe
541
- this.postMessage({
542
- type: 'portal:getClient',
543
- data: {},
544
- });
212
+ return this.handleRequestToIframeAndPost({
213
+ methodMessage: 'portal:getClient',
214
+ errorMessage: 'portal:getClientError',
215
+ resultMessage: 'portal:getClientResult',
216
+ data: {},
545
217
  });
546
218
  });
547
219
  }
548
220
  getNFTs(chainId) {
549
221
  return __awaiter(this, void 0, void 0, function* () {
550
- return new Promise((resolve, reject) => {
551
- const handleGetNFTs = (event) => {
552
- const { type, data } = event.data;
553
- const { origin } = event;
554
- // ignore any broadcast postMessages
555
- if (origin !== this.getOrigin()) {
556
- return;
557
- }
558
- if (type === 'portal:getNFTsError') {
559
- // Remove the event listener
560
- window.removeEventListener('message', handleGetNFTs);
561
- // Reject the promise with the error
562
- return reject(new PortalMpcError(data));
563
- }
564
- else if (type === 'portal:getNFTsResult') {
565
- // Remove the event listener
566
- window.removeEventListener('message', handleGetNFTs);
567
- // Resolve the promise with the result
568
- resolve(data);
569
- }
570
- };
571
- // Bind the function to the message event
572
- window.addEventListener('message', handleGetNFTs);
573
- // Send the request to the iframe
574
- this.postMessage({
575
- type: 'portal:getNFTs',
576
- data: { chainId },
577
- });
222
+ return this.handleRequestToIframeAndPost({
223
+ methodMessage: 'portal:getNFTs',
224
+ errorMessage: 'portal:getNFTsError',
225
+ resultMessage: 'portal:getNFTsResult',
226
+ data: { chainId },
578
227
  });
579
228
  });
580
229
  }
581
230
  getNFTAssets(chainId) {
582
231
  return __awaiter(this, void 0, void 0, function* () {
583
- return new Promise((resolve, reject) => {
584
- const handleGetNFTAssets = (event) => {
585
- const { type, data } = event.data;
586
- const { origin } = event;
587
- // ignore any broadcast postMessages
588
- if (origin !== this.getOrigin()) {
589
- return;
590
- }
591
- if (type === 'portal:getNFTAssetsError') {
592
- // Remove the event listener
593
- window.removeEventListener('message', handleGetNFTAssets);
594
- // Reject the promise with the error
595
- return reject(new PortalMpcError(data));
596
- }
597
- else if (type === 'portal:getNFTAssetsResult') {
598
- // Remove the event listener
599
- window.removeEventListener('message', handleGetNFTAssets);
600
- // Resolve the promise with the result
601
- resolve(data);
602
- }
603
- };
604
- // Bind the function to the message event
605
- window.addEventListener('message', handleGetNFTAssets);
606
- // Send the request to the iframe
607
- this.postMessage({
608
- type: 'portal:getNFTAssets',
609
- data: { chainId },
610
- });
232
+ return this.handleRequestToIframeAndPost({
233
+ methodMessage: 'portal:getNFTAssets',
234
+ errorMessage: 'portal:getNFTAssetsError',
235
+ resultMessage: 'portal:getNFTAssetsResult',
236
+ data: { chainId },
611
237
  });
612
238
  });
613
239
  }
614
240
  getAssets(chainId, includeNfts = false) {
615
241
  return __awaiter(this, void 0, void 0, function* () {
616
- return new Promise((resolve, reject) => {
617
- const handleGetAssets = (event) => {
618
- const { type, data } = event.data;
619
- const { origin } = event;
620
- // ignore any broadcast postMessages
621
- if (origin !== this.getOrigin()) {
622
- return;
623
- }
624
- if (type === 'portal:getAssetsError') {
625
- // Remove the event listener
626
- window.removeEventListener('message', handleGetAssets);
627
- // Reject the promise with the error
628
- return reject(new PortalMpcError(data));
629
- }
630
- else if (type === 'portal:getAssetsResult') {
631
- // Remove the event listener
632
- window.removeEventListener('message', handleGetAssets);
633
- // Resolve the promise with the result
634
- resolve(data);
635
- }
636
- };
637
- // Bind the function to the message event
638
- window.addEventListener('message', handleGetAssets);
639
- // Send the request to the iframe
640
- this.postMessage({
641
- type: 'portal:getAssets',
642
- data: { chainId, includeNfts },
643
- });
242
+ return this.handleRequestToIframeAndPost({
243
+ methodMessage: 'portal:getAssets',
244
+ errorMessage: 'portal:getAssetsError',
245
+ resultMessage: 'portal:getAssetsResult',
246
+ data: { chainId, includeNfts },
644
247
  });
645
248
  });
646
249
  }
647
250
  buildTransaction(chainId, to, token, amount) {
648
251
  return __awaiter(this, void 0, void 0, function* () {
649
- return new Promise((resolve, reject) => {
650
- const handlebuildTransaction = (event) => {
651
- const { type, data } = event.data;
652
- const { origin } = event;
653
- // ignore any broadcast postMessages
654
- if (origin !== this.getOrigin()) {
655
- return;
656
- }
657
- if (type === 'portal:buildTransactionError') {
658
- // Remove the event listener
659
- window.removeEventListener('message', handlebuildTransaction);
660
- // Reject the promise with the error
661
- return reject(new PortalMpcError(data));
662
- }
663
- else if (type === 'portal:buildTransactionResult') {
664
- // Remove the event listener
665
- window.removeEventListener('message', handlebuildTransaction);
666
- // Resolve the promise with the result
667
- resolve(data);
668
- }
669
- };
670
- // Bind the function to the message event
671
- window.addEventListener('message', handlebuildTransaction);
672
- // Send the request to the iframe
673
- this.postMessage({
674
- type: 'portal:buildTransaction',
675
- data: { chainId, to, token, amount },
676
- });
252
+ return this.handleRequestToIframeAndPost({
253
+ methodMessage: 'portal:buildTransaction',
254
+ errorMessage: 'portal:buildTransactionError',
255
+ resultMessage: 'portal:buildTransactionResult',
256
+ data: { chainId, to, token, amount },
677
257
  });
678
258
  });
679
259
  }
680
260
  getQuote(chainId, args, apiKey) {
681
261
  return __awaiter(this, void 0, void 0, function* () {
682
- return new Promise((resolve, reject) => {
683
- const handleGetQuote = (event) => {
684
- const { type, data: result } = event.data;
685
- const { origin } = event;
686
- // ignore any broadcast postMessages
687
- if (origin !== this.getOrigin()) {
688
- return;
689
- }
690
- if (type === 'portal:swaps:getQuoteError') {
691
- // Remove the event listener
692
- window.removeEventListener('message', handleGetQuote);
693
- // Reject the promise with the error
694
- return reject(new PortalMpcError(result));
695
- }
696
- else if (type === 'portal:swaps:getQuoteResult') {
697
- // Remove the event listener
698
- window.removeEventListener('message', handleGetQuote);
699
- // Resolve the promise with the result
700
- resolve(result);
701
- }
702
- };
703
- // Bind the function to the message event
704
- window.addEventListener('message', handleGetQuote);
705
- // Send the request to the iframe
706
- this.postMessage({
707
- type: 'portal:swaps:getQuote',
708
- data: {
709
- apiKey,
710
- args,
711
- chainId,
712
- },
713
- });
262
+ return this.handleRequestToIframeAndPost({
263
+ methodMessage: 'portal:swaps:getQuote',
264
+ errorMessage: 'portal:swaps:getQuoteError',
265
+ resultMessage: 'portal:swaps:getQuoteResult',
266
+ data: {
267
+ apiKey,
268
+ args,
269
+ chainId,
270
+ },
714
271
  });
715
272
  });
716
273
  }
717
274
  getSources(chainId, apiKey) {
718
275
  return __awaiter(this, void 0, void 0, function* () {
719
- return new Promise((resolve, reject) => {
720
- const handleGetSources = (event) => {
721
- const { type, data: result } = event.data;
722
- const { origin } = event;
723
- // ignore any broadcast postMessages
724
- if (origin !== this.getOrigin()) {
725
- return;
726
- }
727
- if (type === 'portal:swaps:getSourcesError') {
728
- // Remove the event listener
729
- window.removeEventListener('message', handleGetSources);
730
- // Reject the promise with the error
731
- return reject(new PortalMpcError(result));
732
- }
733
- else if (type === 'portal:swaps:getSourcesResult') {
734
- // Remove the event listener
735
- window.removeEventListener('message', handleGetSources);
736
- // Resolve the promise with the result
737
- resolve(result);
738
- }
739
- };
740
- // Bind the function to the message event
741
- window.addEventListener('message', handleGetSources);
742
- // Send the request to the iframe
743
- this.postMessage({
744
- type: 'portal:swaps:getSources',
745
- data: {
746
- apiKey,
747
- chainId,
748
- },
749
- });
276
+ return this.handleRequestToIframeAndPost({
277
+ methodMessage: 'portal:swaps:getSources',
278
+ errorMessage: 'portal:swaps:getSourcesError',
279
+ resultMessage: 'portal:swaps:getSourcesResult',
280
+ data: {
281
+ apiKey,
282
+ chainId,
283
+ },
750
284
  });
751
285
  });
752
286
  }
753
287
  getTransactions(chainId, limit, offset, order) {
754
288
  return __awaiter(this, void 0, void 0, function* () {
755
- return new Promise((resolve, reject) => {
756
- const handleGetTransactions = (event) => {
757
- const { type, data } = event.data;
758
- const { origin } = event;
759
- // ignore any broadcast postMessages
760
- if (origin !== this.getOrigin()) {
761
- return;
762
- }
763
- if (type === 'portal:getTransactionsError') {
764
- // Remove the event listener
765
- window.removeEventListener('message', handleGetTransactions);
766
- // Reject the promise with the error
767
- return reject(new PortalMpcError(data));
768
- }
769
- else if (type === 'portal:getTransactionsResult') {
770
- // Remove the event listener
771
- window.removeEventListener('message', handleGetTransactions);
772
- // Resolve the promise with the result
773
- resolve(data);
774
- }
775
- };
776
- // Bind the function to the message event
777
- window.addEventListener('message', handleGetTransactions);
778
- // Send the request to the iframe
779
- this.postMessage({
780
- type: 'portal:getTransactions',
781
- data: {
782
- chainId,
783
- limit,
784
- offset,
785
- order,
786
- },
787
- });
289
+ return this.handleRequestToIframeAndPost({
290
+ methodMessage: 'portal:getTransactions',
291
+ errorMessage: 'portal:getTransactionsError',
292
+ resultMessage: 'portal:getTransactionsResult',
293
+ data: {
294
+ chainId,
295
+ limit,
296
+ offset,
297
+ order,
298
+ },
788
299
  });
789
300
  });
790
301
  }
791
302
  setBackupStatus(status, backupIds) {
792
303
  return __awaiter(this, void 0, void 0, function* () {
793
- return new Promise((resolve, reject) => {
794
- const handleSetBackupStatus = (event) => {
795
- const { type, data } = event.data;
796
- const { origin } = event;
797
- // ignore any broadcast postMessages
798
- if (origin !== this.getOrigin()) {
799
- return;
800
- }
801
- if (type === 'portal:api:setBackupStatusError') {
802
- // Remove the event listener
803
- window.removeEventListener('message', handleSetBackupStatus);
804
- // Reject the promise with the error
805
- return reject(new PortalMpcError(data));
806
- }
807
- else if (type === 'portal:api:setBackupStatusResult') {
808
- // Remove the event listener
809
- window.removeEventListener('message', handleSetBackupStatus);
810
- // Resolve the promise with the result
811
- return resolve(data);
812
- }
813
- };
814
- // Bind the function to the message event
815
- window.addEventListener('message', handleSetBackupStatus);
816
- this.postMessage({
817
- type: 'portal:api:setBackupStatus',
818
- data: {
819
- backupIds,
820
- status,
821
- },
822
- });
304
+ return this.handleRequestToIframeAndPost({
305
+ methodMessage: 'portal:api:setBackupStatus',
306
+ errorMessage: 'portal:api:setBackupStatusError',
307
+ resultMessage: 'portal:api:setBackupStatusResult',
308
+ data: {
309
+ backupIds,
310
+ status,
311
+ },
823
312
  });
824
313
  });
825
314
  }
826
315
  simulateTransaction(transaction, chainId) {
827
316
  return __awaiter(this, void 0, void 0, function* () {
828
- return new Promise((resolve, reject) => {
829
- const handleSimulateTransaction = (event) => {
830
- const { type, data } = event.data;
831
- const { origin } = event;
832
- // ignore any broadcast postMessages
833
- if (origin !== this.getOrigin()) {
834
- return;
835
- }
836
- if (type === 'portal:simulateTransactionError') {
837
- // Remove the event listener
838
- window.removeEventListener('message', handleSimulateTransaction);
839
- // Reject the promise with the error
840
- return reject(new PortalMpcError(data));
841
- }
842
- else if (type === 'portal:simulateTransactionResult') {
843
- // Remove the event listener
844
- window.removeEventListener('message', handleSimulateTransaction);
845
- // Resolve the promise with the result
846
- resolve(data);
847
- }
848
- };
849
- // Bind the function to the message event
850
- window.addEventListener('message', handleSimulateTransaction);
851
- // Send the request to the iframe
852
- this.postMessage({
853
- type: 'portal:simulateTransaction',
854
- data: {
855
- chainId,
856
- transaction,
857
- },
858
- });
317
+ return this.handleRequestToIframeAndPost({
318
+ methodMessage: 'portal:simulateTransaction',
319
+ errorMessage: 'portal:simulateTransactionError',
320
+ resultMessage: 'portal:simulateTransactionResult',
321
+ data: {
322
+ chainId,
323
+ transaction,
324
+ },
859
325
  });
860
326
  });
861
327
  }
862
328
  evaluateTransaction(chainId, transaction, operationType = 'all') {
863
329
  return __awaiter(this, void 0, void 0, function* () {
864
- return new Promise((resolve, reject) => {
865
- const handleEvaluateTransaction = (event) => {
866
- const { type, data } = event.data;
867
- const { origin } = event;
868
- // ignore any broadcast postMessages
869
- if (origin !== this.getOrigin()) {
870
- return;
871
- }
872
- if (type === 'portal:evaluateTransactionError') {
873
- // Remove the event listener
874
- window.removeEventListener('message', handleEvaluateTransaction);
875
- // Reject the promise with the error
876
- return reject(new PortalMpcError(data));
877
- }
878
- else if (type === 'portal:evaluateTransactionResult') {
879
- // Remove the event listener
880
- window.removeEventListener('message', handleEvaluateTransaction);
881
- // Resolve the promise with the result
882
- resolve(data);
883
- }
884
- };
885
- // Bind the function to the message event
886
- window.addEventListener('message', handleEvaluateTransaction);
887
- // Send the request to the iframe
888
- this.postMessage({
889
- type: 'portal:evaluateTransaction',
890
- data: {
891
- chainId,
892
- transaction,
893
- operationType,
894
- },
895
- });
330
+ return this.handleRequestToIframeAndPost({
331
+ methodMessage: 'portal:evaluateTransaction',
332
+ errorMessage: 'portal:evaluateTransactionError',
333
+ resultMessage: 'portal:evaluateTransactionResult',
334
+ data: {
335
+ chainId,
336
+ transaction,
337
+ operationType,
338
+ },
896
339
  });
897
340
  });
898
341
  }
899
342
  storedClientBackupShare(success, backupMethod) {
900
- return new Promise((resolve, reject) => {
901
- const handleStoredClientBackupShare = (event) => {
902
- const { type, data } = event.data;
903
- const { origin } = event;
904
- // ignore any broadcast postMessages
905
- if (origin !== this.getOrigin()) {
906
- return;
907
- }
908
- if (type === 'portal:storedClientBackupShareError') {
909
- // Remove the event listener
910
- window.removeEventListener('message', handleStoredClientBackupShare);
911
- // Reject the promise with the error
912
- return reject(new PortalMpcError(data));
913
- }
914
- else if (type === 'portal:storedClientBackupShareResult') {
915
- // Remove the event listener
916
- window.removeEventListener('message', handleStoredClientBackupShare);
917
- // Resolve the promise with the result
918
- resolve();
919
- }
920
- };
921
- // Bind the function to the message event
922
- window.addEventListener('message', handleStoredClientBackupShare);
923
- // Send the request to the iframe
924
- this.postMessage({
925
- type: 'portal:storedClientBackupShare',
926
- data: {
927
- success,
928
- backupMethod,
929
- },
930
- });
343
+ return this.handleRequestToIframeAndPost({
344
+ methodMessage: 'portal:storedClientBackupShare',
345
+ errorMessage: 'portal:storedClientBackupShareError',
346
+ resultMessage: 'portal:storedClientBackupShareResult',
347
+ data: {
348
+ success,
349
+ backupMethod,
350
+ },
351
+ mapReturnValue: () => undefined,
931
352
  });
932
353
  }
933
354
  formatShares(shares) {
934
355
  return __awaiter(this, void 0, void 0, function* () {
935
- return new Promise((resolve, reject) => {
936
- const handleFormatShares = (event) => {
937
- const { type, data: result } = event.data;
938
- const { origin } = event;
939
- // ignore any broadcast postMessages
940
- if (origin !== this.getOrigin()) {
941
- return;
942
- }
943
- if (type === 'portal:wasm:formatSharesError') {
944
- // Remove the event listener
945
- window.removeEventListener('message', handleFormatShares);
946
- // Reject the promise with the error
947
- return reject(new PortalMpcError(result));
948
- }
949
- else if (type === 'portal:wasm:formatSharesResult') {
950
- // Remove the event listener
951
- window.removeEventListener('message', handleFormatShares);
952
- // Resolve the promise with the result
953
- resolve(result);
954
- }
955
- };
956
- // Bind the function to the message event
957
- window.addEventListener('message', handleFormatShares);
958
- // Send the request to the iframe
959
- this.postMessage({
960
- type: 'portal:wasm:formatShares',
961
- data: shares,
962
- });
356
+ return this.handleRequestToIframeAndPost({
357
+ methodMessage: 'portal:wasm:formatShares',
358
+ errorMessage: 'portal:wasm:formatSharesError',
359
+ resultMessage: 'portal:wasm:formatSharesResult',
360
+ data: shares,
963
361
  });
964
362
  });
965
363
  }
966
364
  getCustodianIdClientIdHashes(data) {
967
365
  return __awaiter(this, void 0, void 0, function* () {
968
- return new Promise((resolve, reject) => {
969
- const handleGetHashes = (event) => {
970
- const { type, data: result } = event.data;
971
- const { origin } = event;
972
- // ignore any broadcast postMessages
973
- if (origin !== this.getOrigin()) {
974
- return;
975
- }
976
- if (type === 'portal:wasm:getCustodianIdClientIdHashesError') {
977
- // Remove the event listener
978
- window.removeEventListener('message', handleGetHashes);
979
- // Reject the promise with the error
980
- return reject(new PortalMpcError(result));
981
- }
982
- else if (type === 'portal:wasm:getCustodianIdClientIdHashesResult') {
983
- // Remove the event listener
984
- window.removeEventListener('message', handleGetHashes);
985
- // Resolve the promise with the result
986
- resolve(result);
987
- }
988
- };
989
- // Bind the function to the message event
990
- window.addEventListener('message', handleGetHashes);
991
- // Send the request to the iframe
992
- this.postMessage({
993
- type: 'portal:wasm:getCustodianIdClientIdHashes',
994
- data,
995
- });
366
+ return this.handleRequestToIframeAndPost({
367
+ methodMessage: 'portal:wasm:getCustodianIdClientIdHashes',
368
+ errorMessage: 'portal:wasm:getCustodianIdClientIdHashesError',
369
+ resultMessage: 'portal:wasm:getCustodianIdClientIdHashesResult',
370
+ data,
996
371
  });
997
372
  });
998
373
  }
@@ -1225,7 +600,7 @@ class Mpc {
1225
600
  /**
1226
601
  * Util to handle requests to the iframe and post the result to the parent
1227
602
  */
1228
- handleRequestToIframeAndPost({ methodMessage, errorMessage, resultMessage, data, }) {
603
+ handleRequestToIframeAndPost({ methodMessage, errorMessage, resultMessage, data, progressMessage, progressCallback, mapReturnValue, }) {
1229
604
  return __awaiter(this, void 0, void 0, function* () {
1230
605
  return new Promise((resolve, reject) => {
1231
606
  const handleRequest = (event) => {
@@ -1245,7 +620,10 @@ class Mpc {
1245
620
  // Remove the event listener
1246
621
  window.removeEventListener('message', handleRequest);
1247
622
  // Resolve the promise with the result
1248
- resolve(result);
623
+ resolve(mapReturnValue ? mapReturnValue(result) : result);
624
+ }
625
+ if (type === progressMessage && progressCallback) {
626
+ void progressCallback(result);
1249
627
  }
1250
628
  };
1251
629
  // Bind the function to the message event