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