@portal-hq/web 3.2.3 → 3.3.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.
package/lib/esm/index.js CHANGED
@@ -9,20 +9,22 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
9
9
  };
10
10
  import { Connection, PublicKey, Transaction as SolanaTransaction, SystemProgram, } from '@solana/web3.js';
11
11
  import Mpc from './mpc';
12
- import Provider from './provider';
12
+ import Provider, { RequestMethod } from './provider';
13
13
  class Portal {
14
+ get ready() {
15
+ return this.mpc.ready;
16
+ }
14
17
  constructor({
15
18
  // Required
16
19
  rpcConfig,
17
20
  // Optional
18
21
  apiKey, authToken, authUrl, autoApprove = false, gdrive, passkey, host = 'web.portalhq.io', mpcVersion = 'v6', mpcHost = 'mpc-client.portalhq.io', featureFlags = {}, }) {
19
- this.ready = false;
20
22
  this.errorCallbacks = [];
21
23
  this.readyCallbacks = [];
22
24
  this.sendEth = ({ chainId, to, value, }) => __awaiter(this, void 0, void 0, function* () {
23
25
  return this.provider.request({
24
26
  chainId,
25
- method: 'eth_sendTransaction',
27
+ method: RequestMethod.eth_sendTransaction,
26
28
  params: [
27
29
  {
28
30
  from: this.address,
@@ -219,6 +221,60 @@ class Portal {
219
221
  return sharesOnDevice.ED25519 && sharesOnDevice.SECP256K1;
220
222
  });
221
223
  }
224
+ isWalletBackedUp(chainId) {
225
+ var _a, _b, _c, _d, _e;
226
+ return __awaiter(this, void 0, void 0, function* () {
227
+ const client = yield ((_a = this.mpc) === null || _a === void 0 ? void 0 : _a.getClient());
228
+ if (chainId) {
229
+ const namespace = ((_b = chainId === null || chainId === void 0 ? void 0 : chainId.split(':')) === null || _b === void 0 ? void 0 : _b[0]) || '';
230
+ const curve = (_e = (_d = (_c = client === null || client === void 0 ? void 0 : client.metadata) === null || _c === void 0 ? void 0 : _c.namespaces) === null || _d === void 0 ? void 0 : _d[namespace]) === null || _e === void 0 ? void 0 : _e.curve;
231
+ if (!curve) {
232
+ return false;
233
+ }
234
+ const wallet = client === null || client === void 0 ? void 0 : client.wallets.find((w) => w.curve === curve);
235
+ if (!wallet) {
236
+ return false;
237
+ }
238
+ return wallet.backupSharePairs.some((share) => share.status === 'completed');
239
+ }
240
+ else {
241
+ return client === null || client === void 0 ? void 0 : client.wallets.some((wallet) => wallet.backupSharePairs.some((share) => share.status === 'completed'));
242
+ }
243
+ });
244
+ }
245
+ isWalletRecoverable(chainId) {
246
+ return __awaiter(this, void 0, void 0, function* () {
247
+ const availableRecoveryMethods = yield this.availableRecoveryMethods(chainId);
248
+ return availableRecoveryMethods.length > 0;
249
+ });
250
+ }
251
+ availableRecoveryMethods(chainId) {
252
+ var _a, _b, _c, _d;
253
+ return __awaiter(this, void 0, void 0, function* () {
254
+ const client = yield ((_a = this.mpc) === null || _a === void 0 ? void 0 : _a.getClient());
255
+ // If the client is not found, return an empty array.
256
+ if (!client) {
257
+ return [];
258
+ }
259
+ // If a chainId is provided, return the recovery methods for that chain.
260
+ if (chainId) {
261
+ const namespace = chainId.split(':')[0] || '';
262
+ const curve = (_d = (_c = (_b = client.metadata) === null || _b === void 0 ? void 0 : _b.namespaces) === null || _c === void 0 ? void 0 : _c[namespace]) === null || _d === void 0 ? void 0 : _d.curve;
263
+ if (!curve) {
264
+ return [];
265
+ }
266
+ const wallet = client.wallets.find((w) => w.curve === curve);
267
+ if (!wallet) {
268
+ return [];
269
+ }
270
+ const methods = this.extractBackupMethod(wallet);
271
+ return this.getUniqueBackupMethod(methods);
272
+ }
273
+ // Otherwise, return the recovery methods for all wallets.
274
+ const allMethods = client.wallets.flatMap((wallet) => this.extractBackupMethod(wallet));
275
+ return this.getUniqueBackupMethod(allMethods);
276
+ });
277
+ }
222
278
  /****************************
223
279
  * Provider Methods
224
280
  ****************************/
@@ -241,7 +297,7 @@ class Portal {
241
297
  console.warn('"portal.ethEstimateGas" is deprecated. Use "portal.request" instead.');
242
298
  return this.provider.request({
243
299
  chainId,
244
- method: 'eth_estimateGas',
300
+ method: RequestMethod.eth_estimateGas,
245
301
  params: transaction,
246
302
  });
247
303
  });
@@ -259,7 +315,7 @@ class Portal {
259
315
  console.warn('"portal.ethGasPrice" is deprecated. Use "portal.request" instead.');
260
316
  return this.provider.request({
261
317
  chainId,
262
- method: 'eth_gasPrice',
318
+ method: RequestMethod.eth_gasPrice,
263
319
  params: [],
264
320
  });
265
321
  });
@@ -277,7 +333,7 @@ class Portal {
277
333
  console.warn('"portal.ethGetBalance" is deprecated. Use "portal.request" instead.');
278
334
  return this.provider.request({
279
335
  chainId,
280
- method: 'eth_getBalance',
336
+ method: RequestMethod.eth_getBalance,
281
337
  params: [this.address, 'latest'],
282
338
  });
283
339
  });
@@ -296,7 +352,7 @@ class Portal {
296
352
  console.warn('"portal.ethSendTransaction" is deprecated. Use "portal.request" instead.');
297
353
  return this.provider.request({
298
354
  chainId,
299
- method: 'eth_sendTransaction',
355
+ method: RequestMethod.eth_sendTransaction,
300
356
  params: transaction,
301
357
  });
302
358
  });
@@ -315,7 +371,7 @@ class Portal {
315
371
  console.warn('"portal.ethSignTransaction" is deprecated. Use "portal.request" instead.');
316
372
  return this.provider.request({
317
373
  chainId,
318
- method: 'eth_signTransaction',
374
+ method: RequestMethod.eth_signTransaction,
319
375
  params: transaction,
320
376
  });
321
377
  });
@@ -334,7 +390,7 @@ class Portal {
334
390
  console.warn('"portal.ethSignTypedData" is deprecated. Use "portal.request" instead.');
335
391
  return this.provider.request({
336
392
  chainId,
337
- method: 'eth_signTypedData',
393
+ method: RequestMethod.eth_signTypedData,
338
394
  params: [this.address, data],
339
395
  });
340
396
  });
@@ -353,7 +409,7 @@ class Portal {
353
409
  console.warn('"portal.ethSignTypedDataV3" is deprecated. Use "portal.request" instead.');
354
410
  return this.provider.request({
355
411
  chainId,
356
- method: 'eth_signTypedData_v3',
412
+ method: RequestMethod.eth_signTypedData_v3,
357
413
  params: [this.address, data],
358
414
  });
359
415
  });
@@ -372,7 +428,7 @@ class Portal {
372
428
  console.warn('"portal.ethSignTypedDataV4" is deprecated. Use "portal.request" instead.');
373
429
  return this.provider.request({
374
430
  chainId,
375
- method: 'eth_signTypedData_v4',
431
+ method: RequestMethod.eth_signTypedData_v4,
376
432
  params: [this.address, data],
377
433
  });
378
434
  });
@@ -382,7 +438,7 @@ class Portal {
382
438
  console.warn('"portal.personalSign" is deprecated. Use "portal.request" instead.');
383
439
  return (yield this.provider.request({
384
440
  chainId,
385
- method: 'personal_sign',
441
+ method: RequestMethod.personal_sign,
386
442
  params: [this.stringToHex(message), this.address],
387
443
  }));
388
444
  });
@@ -411,7 +467,7 @@ class Portal {
411
467
  // Get the most recent blockhash.
412
468
  const blockhashResponse = yield this.provider.request({
413
469
  chainId: chainId,
414
- method: 'getLatestBlockhash',
470
+ method: RequestMethod.sol_getLatestBlockhash,
415
471
  params: [],
416
472
  });
417
473
  const blockhash = ((_a = blockhashResponse === null || blockhashResponse === void 0 ? void 0 : blockhashResponse.value) === null || _a === void 0 ? void 0 : _a.blockhash) || '';
@@ -458,7 +514,7 @@ class Portal {
458
514
  // Attempt to sign and send the transaction
459
515
  const transactionResult = yield this.provider.request({
460
516
  chainId: chainId,
461
- method: 'sol_signAndSendTransaction',
517
+ method: RequestMethod.sol_signAndSendTransaction,
462
518
  params: [formattedTransaction],
463
519
  });
464
520
  // If we didn't get a transactionResult, throw an error.
@@ -472,6 +528,9 @@ class Portal {
472
528
  /*******************************
473
529
  * API Methods
474
530
  *******************************/
531
+ /**
532
+ * @deprecated This method is deprecated. Use `portal.getAssets` instead.
533
+ */
475
534
  getBalances(chainId) {
476
535
  var _a;
477
536
  return __awaiter(this, void 0, void 0, function* () {
@@ -484,6 +543,9 @@ class Portal {
484
543
  return (_a = this.mpc) === null || _a === void 0 ? void 0 : _a.getClient();
485
544
  });
486
545
  }
546
+ /**
547
+ * @deprecated This method is deprecated. Use `portal.getNFTAssets` instead.
548
+ */
487
549
  getNFTs(chainId) {
488
550
  var _a;
489
551
  return __awaiter(this, void 0, void 0, function* () {
@@ -496,12 +558,39 @@ class Portal {
496
558
  return (_a = this.mpc) === null || _a === void 0 ? void 0 : _a.getTransactions(chainId, limit, offset, order);
497
559
  });
498
560
  }
561
+ /**
562
+ * @deprecated This method is deprecated. Use `portal.evaluateTransaction` instead.
563
+ */
499
564
  simulateTransaction(chainId, transaction) {
500
565
  var _a;
501
566
  return __awaiter(this, void 0, void 0, function* () {
502
567
  return (_a = this.mpc) === null || _a === void 0 ? void 0 : _a.simulateTransaction(transaction, chainId);
503
568
  });
504
569
  }
570
+ evaluateTransaction(chainId, transaction, operationType = 'all') {
571
+ var _a;
572
+ return __awaiter(this, void 0, void 0, function* () {
573
+ return (_a = this.mpc) === null || _a === void 0 ? void 0 : _a.evaluateTransaction(chainId, transaction, operationType);
574
+ });
575
+ }
576
+ getAssets(chainId, includeNfts = false) {
577
+ var _a;
578
+ return __awaiter(this, void 0, void 0, function* () {
579
+ return (_a = this.mpc) === null || _a === void 0 ? void 0 : _a.getAssets(chainId, includeNfts);
580
+ });
581
+ }
582
+ getNFTAssets(chainId) {
583
+ var _a;
584
+ return __awaiter(this, void 0, void 0, function* () {
585
+ return (_a = this.mpc) === null || _a === void 0 ? void 0 : _a.getNFTAssets(chainId);
586
+ });
587
+ }
588
+ buildTransaction(chainId, to, token, amount) {
589
+ var _a;
590
+ return __awaiter(this, void 0, void 0, function* () {
591
+ return (_a = this.mpc) === null || _a === void 0 ? void 0 : _a.buildTransaction(chainId, to, token, amount);
592
+ });
593
+ }
505
594
  /*******************************
506
595
  * Swaps Methods
507
596
  *******************************/
@@ -548,9 +637,6 @@ class Portal {
548
637
  // Otherwise, something is wrong with the configuration.
549
638
  throw new Error(`[Portal] Could not find a valid rpcConfig entry for chainId: ${chainId}`);
550
639
  }
551
- /**************************
552
- * RPC Encoding Methods
553
- **************************/
554
640
  stringToHex(str) {
555
641
  if (str.startsWith('0x')) {
556
642
  return str;
@@ -563,8 +649,17 @@ class Portal {
563
649
  }
564
650
  return hex;
565
651
  }
652
+ extractBackupMethod(wallet) {
653
+ return wallet.backupSharePairs
654
+ .filter((share) => share.status === 'completed')
655
+ .map((share) => share.backupMethod);
656
+ }
657
+ getUniqueBackupMethod(methods) {
658
+ return Array.from(new Set(methods));
659
+ }
566
660
  }
567
661
  export { MpcError, MpcErrorCodes } from './mpc';
662
+ export { RequestMethod } from './provider';
568
663
  export var MpcStatuses;
569
664
  (function (MpcStatuses) {
570
665
  MpcStatuses["DecryptingShare"] = "Decrypting share";
@@ -594,4 +689,9 @@ export var PortalCurve;
594
689
  PortalCurve["ED25519"] = "ED25519";
595
690
  PortalCurve["SECP256K1"] = "SECP256K1";
596
691
  })(PortalCurve || (PortalCurve = {}));
692
+ export var ChainNamespace;
693
+ (function (ChainNamespace) {
694
+ ChainNamespace["EIP155"] = "eip155";
695
+ ChainNamespace["SOLANA"] = "solana";
696
+ })(ChainNamespace || (ChainNamespace = {}));
597
697
  export default Portal;