@portal-hq/web 1.0.3 → 2.0.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 = '1.0.3';
12
+ const WEB_SDK_VERSION = '2.0.0';
13
13
  class Mpc {
14
14
  constructor({ portal }) {
15
15
  this.configureIframe = () => {
@@ -73,8 +73,14 @@ class Mpc {
73
73
  // Remove the event listeners
74
74
  window.removeEventListener('message', handleBackup);
75
75
  window.removeEventListener('message', handleProgress);
76
+ const storageCallback = () => __awaiter(this, void 0, void 0, function* () {
77
+ yield this.setBackupStatus('STORED_CLIENT_BACKUP_SHARE', result.backupIds);
78
+ });
76
79
  // Resolve the promise with the result
77
- resolve(result);
80
+ resolve({
81
+ cipherText: result.cipherText,
82
+ storageCallback,
83
+ });
78
84
  }
79
85
  };
80
86
  const handleProgress = (message) => {
@@ -331,7 +337,7 @@ class Mpc {
331
337
  /*******************************
332
338
  * API Methods
333
339
  *******************************/
334
- getBalances() {
340
+ getBalances(chainId) {
335
341
  return __awaiter(this, void 0, void 0, function* () {
336
342
  return new Promise((resolve, reject) => {
337
343
  const handleGetBalances = (event) => {
@@ -359,7 +365,7 @@ class Mpc {
359
365
  // Send the request to the iframe
360
366
  this.postMessage({
361
367
  type: 'portal:getBalances',
362
- data: {},
368
+ data: { chainId },
363
369
  });
364
370
  });
365
371
  });
@@ -397,7 +403,7 @@ class Mpc {
397
403
  });
398
404
  });
399
405
  }
400
- getNFTs() {
406
+ getNFTs(chainId) {
401
407
  return __awaiter(this, void 0, void 0, function* () {
402
408
  return new Promise((resolve, reject) => {
403
409
  const handleGetNFTs = (event) => {
@@ -425,12 +431,12 @@ class Mpc {
425
431
  // Send the request to the iframe
426
432
  this.postMessage({
427
433
  type: 'portal:getNFTs',
428
- data: {},
434
+ data: { chainId },
429
435
  });
430
436
  });
431
437
  });
432
438
  }
433
- getQuote(apiKey, args) {
439
+ getQuote(apiKey, args, chainId) {
434
440
  return __awaiter(this, void 0, void 0, function* () {
435
441
  return new Promise((resolve, reject) => {
436
442
  const handleGetQuote = (event) => {
@@ -461,12 +467,13 @@ class Mpc {
461
467
  data: {
462
468
  apiKey,
463
469
  args,
470
+ chainId,
464
471
  },
465
472
  });
466
473
  });
467
474
  });
468
475
  }
469
- getSources(apiKey) {
476
+ getSources(apiKey, chainId) {
470
477
  return __awaiter(this, void 0, void 0, function* () {
471
478
  return new Promise((resolve, reject) => {
472
479
  const handleGetSources = (event) => {
@@ -496,6 +503,7 @@ class Mpc {
496
503
  type: 'portal:swaps:getSources',
497
504
  data: {
498
505
  apiKey,
506
+ chainId,
499
507
  },
500
508
  });
501
509
  });
@@ -539,7 +547,42 @@ class Mpc {
539
547
  });
540
548
  });
541
549
  }
542
- simulateTransaction(transaction) {
550
+ setBackupStatus(status, backupIds) {
551
+ return __awaiter(this, void 0, void 0, function* () {
552
+ return new Promise((resolve, reject) => {
553
+ const handleSetBackupStatus = (event) => {
554
+ const { type, data } = event.data;
555
+ const { origin } = event;
556
+ // ignore any broadcast postMessages
557
+ if (origin !== this.getOrigin()) {
558
+ return;
559
+ }
560
+ if (type === 'portal:api:setBackupStatusError') {
561
+ // Remove the event listener
562
+ window.removeEventListener('message', handleSetBackupStatus);
563
+ // Reject the promise with the error
564
+ return reject(new PortalMpcError(data));
565
+ }
566
+ else if (type === 'portal:api:setBackupStatusResult') {
567
+ // Remove the event listener
568
+ window.removeEventListener('message', handleSetBackupStatus);
569
+ // Resolve the promise with the result
570
+ return resolve(data);
571
+ }
572
+ };
573
+ // Bind the function to the message event
574
+ window.addEventListener('message', handleSetBackupStatus);
575
+ this.postMessage({
576
+ type: 'portal:api:setBackupStatus',
577
+ data: {
578
+ backupIds,
579
+ status,
580
+ },
581
+ });
582
+ });
583
+ });
584
+ }
585
+ simulateTransaction(transaction, chainId) {
543
586
  return __awaiter(this, void 0, void 0, function* () {
544
587
  return new Promise((resolve, reject) => {
545
588
  const handleSimulateTransaction = (event) => {
@@ -567,7 +610,10 @@ class Mpc {
567
610
  // Send the request to the iframe
568
611
  this.postMessage({
569
612
  type: 'portal:simulateTransaction',
570
- data: transaction,
613
+ data: {
614
+ chainId,
615
+ transaction,
616
+ },
571
617
  });
572
618
  });
573
619
  });
@@ -108,17 +108,21 @@ class Provider {
108
108
  * @param args The arguments of the request being made
109
109
  * @returns Promise<any>
110
110
  */
111
- request({ method, params }) {
111
+ request({ chainId, method, params, }) {
112
112
  return __awaiter(this, void 0, void 0, function* () {
113
113
  if (method === 'eth_chainId') {
114
- return this.portal.chainId;
114
+ return chainId ? parseInt(chainId.split(':')[1]) : this.portal.chainId;
115
115
  }
116
116
  const isSignerMethod = signerMethods.includes(method);
117
- let result;
118
117
  if (!isSignerMethod && !method.startsWith('wallet_')) {
119
118
  // Send to Gateway for RPC calls
120
- const response = yield this.handleGatewayRequest({ method, params });
119
+ const response = yield this.handleGatewayRequest({
120
+ chainId,
121
+ method,
122
+ params,
123
+ });
121
124
  this.emit('portal_signatureReceived', {
125
+ chainId,
122
126
  method,
123
127
  params,
124
128
  signature: response,
@@ -126,21 +130,23 @@ class Provider {
126
130
  if (response.error) {
127
131
  throw new ProviderRpcError(response.error);
128
132
  }
129
- result = response.result;
133
+ return response.result;
130
134
  }
131
135
  else if (isSignerMethod) {
132
136
  // Handle signing
133
137
  const transactionHash = yield this.handleSigningRequest({
138
+ chainId,
134
139
  method,
135
140
  params,
136
141
  });
137
142
  if (transactionHash) {
138
143
  this.emit('portal_signatureReceived', {
144
+ chainId,
139
145
  method,
140
146
  params,
141
147
  signature: transactionHash,
142
148
  });
143
- result = transactionHash;
149
+ return transactionHash;
144
150
  }
145
151
  }
146
152
  else {
@@ -153,7 +159,6 @@ class Provider {
153
159
  },
154
160
  });
155
161
  }
156
- return result;
157
162
  });
158
163
  }
159
164
  /************************
@@ -216,13 +221,13 @@ class Provider {
216
221
  * @param args The arguments of the request being made
217
222
  * @returns Promise<any>
218
223
  */
219
- handleGatewayRequest({ method, params, }) {
224
+ handleGatewayRequest({ chainId, method, params, }) {
220
225
  return __awaiter(this, void 0, void 0, function* () {
221
226
  // Pass request off to the gateway
222
227
  const result = yield fetch(this.portal.getRpcUrl(), {
223
228
  body: JSON.stringify({
224
229
  jsonrpc: '2.0',
225
- id: this.portal.chainId,
230
+ id: chainId ? parseInt(chainId.split(':')[1]) : this.portal.chainId,
226
231
  method,
227
232
  params,
228
233
  }),
@@ -237,7 +242,7 @@ class Provider {
237
242
  * @param args The arguments of the request being made
238
243
  * @returns Promise<any>
239
244
  */
240
- handleSigningRequest({ method, params, }) {
245
+ handleSigningRequest({ chainId, method, params, }) {
241
246
  return __awaiter(this, void 0, void 0, function* () {
242
247
  const isApproved = passiveSignerMethods.includes(method)
243
248
  ? true
@@ -259,7 +264,9 @@ class Provider {
259
264
  case 'eth_signTypedData_v4':
260
265
  case 'personal_sign': {
261
266
  const result = yield this.portal.mpc.sign({
262
- chainId: this.portal.chainId.toString(),
267
+ chainId: chainId
268
+ ? chainId.split(':')[1]
269
+ : this.portal.chainId.toString(),
263
270
  method,
264
271
  params: this.buildParams(method, params),
265
272
  rpcUrl: this.portal.getRpcUrl(),
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "description": "Portal MPC Support for Web",
4
4
  "author": "Portal Labs, Inc.",
5
5
  "homepage": "https://portalhq.io/",
6
- "version": "1.0.3",
6
+ "version": "2.0.0",
7
7
  "license": "MIT",
8
8
  "main": "lib/commonjs/index",
9
9
  "module": "lib/esm/index",
package/src/index.ts CHANGED
@@ -1,8 +1,9 @@
1
1
  import type {
2
2
  BackupConfigs,
3
+ BackupResponse,
3
4
  BackupSharePairMetadata,
4
5
  Balance,
5
- ClientWithCustodianData,
6
+ ClientResponse,
6
7
  EthereumTransaction,
7
8
  FeatureFlags,
8
9
  GDriveConfig,
@@ -168,8 +169,8 @@ class Portal {
168
169
  // Noop
169
170
  },
170
171
  backupConfigs: BackupConfigs = {},
171
- ): Promise<string> {
172
- const cipherText = await this.mpc.backup(
172
+ ): Promise<BackupResponse> {
173
+ const response = await this.mpc.backup(
173
174
  {
174
175
  backupMethod,
175
176
  backupConfigs,
@@ -180,7 +181,7 @@ class Portal {
180
181
  progress,
181
182
  )
182
183
 
183
- return cipherText
184
+ return response
184
185
  }
185
186
 
186
187
  public async recoverWallet(
@@ -250,22 +251,28 @@ class Portal {
250
251
  * Provider Methods
251
252
  ****************************/
252
253
 
253
- public async ethEstimateGas(transaction: EthereumTransaction): Promise<any> {
254
+ public async ethEstimateGas(
255
+ transaction: EthereumTransaction,
256
+ chainId?: string,
257
+ ): Promise<any> {
254
258
  return this.provider.request({
259
+ chainId,
255
260
  method: 'eth_estimateGas',
256
261
  params: transaction,
257
262
  })
258
263
  }
259
264
 
260
- public async ethGasPrice(): Promise<string> {
265
+ public async ethGasPrice(chainId?: string): Promise<string> {
261
266
  return this.provider.request({
267
+ chainId,
262
268
  method: 'eth_gasPrice',
263
269
  params: [],
264
270
  }) as Promise<string>
265
271
  }
266
272
 
267
- public async ethGetBalance(): Promise<string> {
273
+ public async ethGetBalance(chainId?: string): Promise<string> {
268
274
  return this.provider.request({
275
+ chainId,
269
276
  method: 'eth_getBalance',
270
277
  params: [this.address, 'latest'],
271
278
  }) as Promise<string>
@@ -273,15 +280,18 @@ class Portal {
273
280
 
274
281
  public async ethSendTransaction(
275
282
  transaction: EthereumTransaction,
283
+ chainId?: string,
276
284
  ): Promise<string> {
277
285
  return this.provider.request({
286
+ chainId,
278
287
  method: 'eth_sendTransaction',
279
288
  params: transaction,
280
289
  }) as Promise<string>
281
290
  }
282
291
 
283
- public async ethSign(message: string): Promise<string> {
292
+ public async ethSign(message: string, chainId?: string): Promise<string> {
284
293
  return this.provider.request({
294
+ chainId,
285
295
  method: 'eth_sign',
286
296
  params: [this.address, this.stringToHex(message)],
287
297
  }) as Promise<string>
@@ -289,39 +299,57 @@ class Portal {
289
299
 
290
300
  public async ethSignTransaction(
291
301
  transaction: EthereumTransaction,
302
+ chainId?: string,
292
303
  ): Promise<string> {
293
304
  return this.provider.request({
305
+ chainId,
294
306
  method: 'eth_signTransaction',
295
307
  params: transaction,
296
308
  }) as Promise<string>
297
309
  }
298
310
 
299
- public async ethSignTypedData(data: TypedData): Promise<string> {
311
+ public async ethSignTypedData(
312
+ data: TypedData,
313
+ chainId?: string,
314
+ ): Promise<string> {
300
315
  return this.provider.request({
316
+ chainId,
301
317
  method: 'eth_signTypedData',
302
318
  params: [this.address, data],
303
319
  }) as Promise<string>
304
320
  }
305
321
 
306
- public async ethSignTypedDataV3(data: TypedData): Promise<string> {
322
+ public async ethSignTypedDataV3(
323
+ data: TypedData,
324
+ chainId?: string,
325
+ ): Promise<string> {
307
326
  return this.provider.request({
327
+ chainId,
308
328
  method: 'eth_signTypedData_v3',
309
329
  params: [this.address, data],
310
330
  }) as Promise<string>
311
331
  }
312
332
 
313
- public async ethSignTypedDataV4(data: TypedData): Promise<string> {
333
+ public async ethSignTypedDataV4(
334
+ data: TypedData,
335
+ chainId?: string,
336
+ ): Promise<string> {
314
337
  return this.provider.request({
338
+ chainId,
315
339
  method: 'eth_signTypedData_v4',
316
340
  params: [this.address, data],
317
341
  }) as Promise<string>
318
342
  }
319
343
 
320
- public async personalSign(message: string): Promise<string> {
321
- return this.provider.request({
344
+ public async personalSign(
345
+ message: string,
346
+ chainId?: string,
347
+ ): Promise<string> {
348
+ return (await this.provider.request({
349
+ chainId,
322
350
  method: 'personal_sign',
323
351
  params: [this.stringToHex(message), this.address],
324
- }) as Promise<string>
352
+ })) as Promise<string>
325
353
  }
326
354
 
327
355
  /*******************************
@@ -332,7 +360,7 @@ class Portal {
332
360
  return await this.mpc?.getBalances()
333
361
  }
334
362
 
335
- public async getClient(): Promise<ClientWithCustodianData> {
363
+ public async getClient(): Promise<ClientResponse> {
336
364
  return this.mpc?.getClient()
337
365
  }
338
366
 
@@ -352,7 +380,7 @@ class Portal {
352
380
  limit?: number,
353
381
  offset?: number,
354
382
  order?: GetTransactionsOrder,
355
- chainId?: number,
383
+ chainId?: string,
356
384
  ): Promise<Transaction[]> {
357
385
  return this.mpc?.getTransactions(limit, offset, order, chainId)
358
386
  }
@@ -370,12 +398,16 @@ class Portal {
370
398
  public async getQuote(
371
399
  apiKey: string,
372
400
  args: QuoteArgs,
401
+ chainId?: string,
373
402
  ): Promise<QuoteResponse> {
374
- return this.mpc?.getQuote(apiKey, args)
403
+ return this.mpc?.getQuote(apiKey, args, chainId)
375
404
  }
376
405
 
377
- public async getSources(apiKey: string): Promise<Record<string, string>> {
378
- return this.mpc?.getSources(apiKey)
406
+ public async getSources(
407
+ apiKey: string,
408
+ chainId?: string,
409
+ ): Promise<Record<string, string>> {
410
+ return this.mpc?.getSources(apiKey, chainId)
379
411
  }
380
412
 
381
413
  /*******************************
package/src/mpc/index.ts CHANGED
@@ -3,9 +3,10 @@ import { PortalMpcError } from './errors'
3
3
  import Portal, { BackupMethods, GetTransactionsOrder } from '../index'
4
4
  import type {
5
5
  BackupArgs,
6
+ BackupResponse,
6
7
  BackupSharePairMetadata,
7
8
  Balance,
8
- ClientWithCustodianData,
9
+ ClientResponse,
9
10
  EjectArgs,
10
11
  GenerateArgs,
11
12
  IframeConfigurationOptions,
@@ -25,7 +26,7 @@ import type {
25
26
  WorkerResult,
26
27
  } from '../../types'
27
28
 
28
- const WEB_SDK_VERSION = '1.0.3'
29
+ const WEB_SDK_VERSION = '2.0.0'
29
30
 
30
31
  class Mpc {
31
32
  public iframe?: HTMLIFrameElement
@@ -51,7 +52,7 @@ class Mpc {
51
52
  progress: ProgressCallback = () => {
52
53
  // Noop
53
54
  },
54
- ): Promise<string> {
55
+ ): Promise<BackupResponse> {
55
56
  const message = {
56
57
  type: 'portal:wasm:backup',
57
58
  data,
@@ -83,8 +84,18 @@ class Mpc {
83
84
  window.removeEventListener('message', handleBackup)
84
85
  window.removeEventListener('message', handleProgress)
85
86
 
87
+ const storageCallback = async () => {
88
+ await this.setBackupStatus(
89
+ 'STORED_CLIENT_BACKUP_SHARE',
90
+ result.backupIds,
91
+ )
92
+ }
93
+
86
94
  // Resolve the promise with the result
87
- resolve(result as string)
95
+ resolve({
96
+ cipherText: result.cipherText,
97
+ storageCallback,
98
+ })
88
99
  }
89
100
  }
90
101
 
@@ -394,7 +405,7 @@ class Mpc {
394
405
  * API Methods
395
406
  *******************************/
396
407
 
397
- public async getBalances(): Promise<Balance[]> {
408
+ public async getBalances(chainId?: string): Promise<Balance[]> {
398
409
  return new Promise((resolve, reject) => {
399
410
  const handleGetBalances = (event: MessageEvent<WorkerResult>) => {
400
411
  const { type, data } = event.data
@@ -426,12 +437,12 @@ class Mpc {
426
437
  // Send the request to the iframe
427
438
  this.postMessage({
428
439
  type: 'portal:getBalances',
429
- data: {},
440
+ data: { chainId },
430
441
  })
431
442
  })
432
443
  }
433
444
 
434
- public async getClient(): Promise<ClientWithCustodianData> {
445
+ public async getClient(): Promise<ClientResponse> {
435
446
  return new Promise((resolve, reject) => {
436
447
  const handleGetClient = (event: MessageEvent<WorkerResult>) => {
437
448
  const { type, data } = event.data
@@ -453,7 +464,7 @@ class Mpc {
453
464
  window.removeEventListener('message', handleGetClient)
454
465
 
455
466
  // Resolve the promise with the result
456
- resolve(data as ClientWithCustodianData)
467
+ resolve(data as ClientResponse)
457
468
  }
458
469
  }
459
470
 
@@ -468,7 +479,7 @@ class Mpc {
468
479
  })
469
480
  }
470
481
 
471
- public async getNFTs(): Promise<NFT[]> {
482
+ public async getNFTs(chainId?: string): Promise<NFT[]> {
472
483
  return new Promise((resolve, reject) => {
473
484
  const handleGetNFTs = (event: MessageEvent<WorkerResult>) => {
474
485
  const { type, data } = event.data
@@ -500,7 +511,7 @@ class Mpc {
500
511
  // Send the request to the iframe
501
512
  this.postMessage({
502
513
  type: 'portal:getNFTs',
503
- data: {},
514
+ data: { chainId },
504
515
  })
505
516
  })
506
517
  }
@@ -508,6 +519,7 @@ class Mpc {
508
519
  public async getQuote(
509
520
  apiKey: string,
510
521
  args: QuoteArgs,
522
+ chainId?: string,
511
523
  ): Promise<QuoteResponse> {
512
524
  return new Promise((resolve, reject) => {
513
525
  const handleGetQuote = (event: MessageEvent<WorkerResult>) => {
@@ -542,12 +554,16 @@ class Mpc {
542
554
  data: {
543
555
  apiKey,
544
556
  args,
557
+ chainId,
545
558
  },
546
559
  })
547
560
  })
548
561
  }
549
562
 
550
- public async getSources(apiKey: string): Promise<Record<string, string>> {
563
+ public async getSources(
564
+ apiKey: string,
565
+ chainId?: string,
566
+ ): Promise<Record<string, string>> {
551
567
  return new Promise((resolve, reject) => {
552
568
  const handleGetSources = (event: MessageEvent<WorkerResult>) => {
553
569
  const { type, data: result } = event.data
@@ -580,6 +596,7 @@ class Mpc {
580
596
  type: 'portal:swaps:getSources',
581
597
  data: {
582
598
  apiKey,
599
+ chainId,
583
600
  },
584
601
  })
585
602
  })
@@ -589,7 +606,7 @@ class Mpc {
589
606
  limit?: number,
590
607
  offset?: number,
591
608
  order?: GetTransactionsOrder,
592
- chainId?: number,
609
+ chainId?: string,
593
610
  ): Promise<Transaction[]> {
594
611
  return new Promise((resolve, reject) => {
595
612
  const handleGetTransactions = (event: MessageEvent<WorkerResult>) => {
@@ -632,8 +649,51 @@ class Mpc {
632
649
  })
633
650
  }
634
651
 
652
+ public async setBackupStatus(
653
+ status: string,
654
+ backupIds: string[],
655
+ ): Promise<boolean> {
656
+ return new Promise((resolve, reject) => {
657
+ const handleSetBackupStatus = (event: MessageEvent<WorkerResult>) => {
658
+ const { type, data } = event.data
659
+ const { origin } = event
660
+
661
+ // ignore any broadcast postMessages
662
+ if (origin !== this.getOrigin()) {
663
+ return
664
+ }
665
+
666
+ if (type === 'portal:api:setBackupStatusError') {
667
+ // Remove the event listener
668
+ window.removeEventListener('message', handleSetBackupStatus)
669
+
670
+ // Reject the promise with the error
671
+ return reject(new PortalMpcError(data as PortalError))
672
+ } else if (type === 'portal:api:setBackupStatusResult') {
673
+ // Remove the event listener
674
+ window.removeEventListener('message', handleSetBackupStatus)
675
+
676
+ // Resolve the promise with the result
677
+ return resolve(data)
678
+ }
679
+ }
680
+
681
+ // Bind the function to the message event
682
+ window.addEventListener('message', handleSetBackupStatus)
683
+
684
+ this.postMessage({
685
+ type: 'portal:api:setBackupStatus',
686
+ data: {
687
+ backupIds,
688
+ status,
689
+ },
690
+ })
691
+ })
692
+ }
693
+
635
694
  public async simulateTransaction(
636
695
  transaction: SimulateTransactionParam,
696
+ chainId?: string,
637
697
  ): Promise<SimulatedTransaction> {
638
698
  return new Promise((resolve, reject) => {
639
699
  const handleSimulateTransaction = (event: MessageEvent<WorkerResult>) => {
@@ -666,7 +726,10 @@ class Mpc {
666
726
  // Send the request to the iframe
667
727
  this.postMessage({
668
728
  type: 'portal:simulateTransaction',
669
- data: transaction,
729
+ data: {
730
+ chainId,
731
+ transaction,
732
+ },
670
733
  })
671
734
  })
672
735
  }