@portal-hq/provider 2.0.3 → 2.0.6

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.
@@ -32,7 +32,7 @@ class Provider {
32
32
  // Required
33
33
  apiKey, chainId, keychain, gatewayConfig,
34
34
  // Optional
35
- isSimulator = false, autoApprove = false, apiHost = 'api.portalhq.io', mpcHost = 'mpc.portalhq.io', version = 'v4', }) {
35
+ isSimulator = false, autoApprove = false, apiHost = 'api.portalhq.io', mpcHost = 'mpc.portalhq.io', version = 'v4', featureFlags = { optimized: false }, }) {
36
36
  // Handle required fields
37
37
  if (!apiKey || apiKey.length === 0) {
38
38
  throw new utils_1.InvalidApiKeyError();
@@ -65,9 +65,10 @@ class Provider {
65
65
  });
66
66
  // Initialize an MpcSigner
67
67
  this.signer = new signers_1.MpcSigner({
68
- mpcHost: mpcHost,
69
- keychain: keychain,
70
- version: version,
68
+ mpcHost,
69
+ keychain,
70
+ version,
71
+ featureFlags,
71
72
  });
72
73
  }
73
74
  get address() {
@@ -106,6 +107,7 @@ class Provider {
106
107
  return this.gatewayConfig;
107
108
  }
108
109
  else if (typeof this.gatewayConfig === 'object' &&
110
+ // eslint-disable-next-line no-prototype-builtins
109
111
  !this.gatewayConfig.hasOwnProperty(this.chainId)) {
110
112
  // If there's no explicit mapping for the current chainId, error out
111
113
  throw new Error(`[PortalProvider] No RPC endpoint configured for chainId: ${this.chainId}`);
@@ -257,7 +259,7 @@ class Provider {
257
259
  }
258
260
  // Dispatch 'connect' event
259
261
  this.dispatchConnect();
260
- return this;
262
+ return new Promise((resolve) => resolve(this));
261
263
  });
262
264
  }
263
265
  /**
@@ -320,10 +322,8 @@ class Provider {
320
322
  });
321
323
  }
322
324
  dispatchConnect() {
323
- return __awaiter(this, void 0, void 0, function* () {
324
- this.emit('connect', {
325
- chainId: `0x${this.chainId.toString(16)}`,
326
- });
325
+ this.emit('connect', {
326
+ chainId: `0x${this.chainId.toString(16)}`,
327
327
  });
328
328
  }
329
329
  /**
@@ -371,7 +371,7 @@ class Provider {
371
371
  case 'eth_signTransaction':
372
372
  case 'eth_signTypedData_v3':
373
373
  case 'eth_signTypedData_v4':
374
- case 'personal_sign':
374
+ case 'personal_sign': {
375
375
  const result = yield ((_a = this.signer) === null || _a === void 0 ? void 0 : _a.sign({ chainId: this.chainId, method, params }, this));
376
376
  if (result) {
377
377
  try {
@@ -393,6 +393,7 @@ class Provider {
393
393
  }
394
394
  }
395
395
  return result;
396
+ }
396
397
  default:
397
398
  throw new Error('[PortalProvider] Method "' + method + '" not supported');
398
399
  }
@@ -1,19 +1,8 @@
1
1
  "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
2
  Object.defineProperty(exports, "__esModule", { value: true });
12
3
  class Signer {
13
4
  sign(_, __) {
14
- return __awaiter(this, void 0, void 0, function* () {
15
- throw new Error('[Portal] sign() method must be implemented in a subclass of Signer');
16
- });
5
+ throw new Error('[Portal] sign() method must be implemented in a subclass of Signer');
17
6
  }
18
7
  }
19
8
  exports.default = Signer;
@@ -12,7 +12,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
12
12
  const react_native_1 = require("react-native");
13
13
  const utils_1 = require("@portal-hq/utils");
14
14
  class MpcSigner {
15
- constructor({ keychain, isSimulator = false, mpcHost = 'mpc.portalhq.io', version = 'v4', }) {
15
+ constructor({ keychain, isSimulator = false, mpcHost = 'mpc.portalhq.io', version = 'v4', featureFlags = { optimized: false }, }) {
16
16
  this.version = 'v4';
17
17
  this.buildParams = (method, txParams) => {
18
18
  let params = txParams;
@@ -37,6 +37,7 @@ class MpcSigner {
37
37
  this.mpc = react_native_1.NativeModules.PortalMobileMpc;
38
38
  this.mpcHost = mpcHost;
39
39
  this.version = version;
40
+ this.featureFlags = featureFlags;
40
41
  if (!this.mpc) {
41
42
  throw new Error(`[Portal.Provider.MpcSigner] The MPC module could not be found by the signer. This is usually an issue with React Native linking. Please verify that the 'PortalReactNative' module is properly linked to this project.`);
42
43
  }
@@ -64,6 +65,7 @@ class MpcSigner {
64
65
  const metadata = JSON.stringify({
65
66
  clientPlatform: 'REACT_NATIVE',
66
67
  mpcServerVersion: this.version,
68
+ optimized: this.featureFlags.optimized,
67
69
  });
68
70
  const result = yield this.mpc.sign(apiKey, this.mpcHost, signingShare, message.method, JSON.stringify(this.buildParams(method, params)), provider.gatewayUrl, provider.chainId.toString(), metadata);
69
71
  const { data, error } = JSON.parse(String(result));
@@ -30,7 +30,7 @@ class Provider {
30
30
  // Required
31
31
  apiKey, chainId, keychain, gatewayConfig,
32
32
  // Optional
33
- isSimulator = false, autoApprove = false, apiHost = 'api.portalhq.io', mpcHost = 'mpc.portalhq.io', version = 'v4', }) {
33
+ isSimulator = false, autoApprove = false, apiHost = 'api.portalhq.io', mpcHost = 'mpc.portalhq.io', version = 'v4', featureFlags = { optimized: false }, }) {
34
34
  // Handle required fields
35
35
  if (!apiKey || apiKey.length === 0) {
36
36
  throw new InvalidApiKeyError();
@@ -63,9 +63,10 @@ class Provider {
63
63
  });
64
64
  // Initialize an MpcSigner
65
65
  this.signer = new MpcSigner({
66
- mpcHost: mpcHost,
67
- keychain: keychain,
68
- version: version,
66
+ mpcHost,
67
+ keychain,
68
+ version,
69
+ featureFlags,
69
70
  });
70
71
  }
71
72
  get address() {
@@ -104,6 +105,7 @@ class Provider {
104
105
  return this.gatewayConfig;
105
106
  }
106
107
  else if (typeof this.gatewayConfig === 'object' &&
108
+ // eslint-disable-next-line no-prototype-builtins
107
109
  !this.gatewayConfig.hasOwnProperty(this.chainId)) {
108
110
  // If there's no explicit mapping for the current chainId, error out
109
111
  throw new Error(`[PortalProvider] No RPC endpoint configured for chainId: ${this.chainId}`);
@@ -255,7 +257,7 @@ class Provider {
255
257
  }
256
258
  // Dispatch 'connect' event
257
259
  this.dispatchConnect();
258
- return this;
260
+ return new Promise((resolve) => resolve(this));
259
261
  });
260
262
  }
261
263
  /**
@@ -318,10 +320,8 @@ class Provider {
318
320
  });
319
321
  }
320
322
  dispatchConnect() {
321
- return __awaiter(this, void 0, void 0, function* () {
322
- this.emit('connect', {
323
- chainId: `0x${this.chainId.toString(16)}`,
324
- });
323
+ this.emit('connect', {
324
+ chainId: `0x${this.chainId.toString(16)}`,
325
325
  });
326
326
  }
327
327
  /**
@@ -369,7 +369,7 @@ class Provider {
369
369
  case 'eth_signTransaction':
370
370
  case 'eth_signTypedData_v3':
371
371
  case 'eth_signTypedData_v4':
372
- case 'personal_sign':
372
+ case 'personal_sign': {
373
373
  const result = yield ((_a = this.signer) === null || _a === void 0 ? void 0 : _a.sign({ chainId: this.chainId, method, params }, this));
374
374
  if (result) {
375
375
  try {
@@ -391,6 +391,7 @@ class Provider {
391
391
  }
392
392
  }
393
393
  return result;
394
+ }
394
395
  default:
395
396
  throw new Error('[PortalProvider] Method "' + method + '" not supported');
396
397
  }
@@ -1,17 +1,6 @@
1
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
- return new (P || (P = Promise))(function (resolve, reject) {
4
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
- step((generator = generator.apply(thisArg, _arguments || [])).next());
8
- });
9
- };
10
1
  class Signer {
11
2
  sign(_, __) {
12
- return __awaiter(this, void 0, void 0, function* () {
13
- throw new Error('[Portal] sign() method must be implemented in a subclass of Signer');
14
- });
3
+ throw new Error('[Portal] sign() method must be implemented in a subclass of Signer');
15
4
  }
16
5
  }
17
6
  export default Signer;
@@ -10,7 +10,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
10
10
  import { NativeModules } from 'react-native';
11
11
  import { PortalMpcError, } from '@portal-hq/utils';
12
12
  class MpcSigner {
13
- constructor({ keychain, isSimulator = false, mpcHost = 'mpc.portalhq.io', version = 'v4', }) {
13
+ constructor({ keychain, isSimulator = false, mpcHost = 'mpc.portalhq.io', version = 'v4', featureFlags = { optimized: false }, }) {
14
14
  this.version = 'v4';
15
15
  this.buildParams = (method, txParams) => {
16
16
  let params = txParams;
@@ -35,6 +35,7 @@ class MpcSigner {
35
35
  this.mpc = NativeModules.PortalMobileMpc;
36
36
  this.mpcHost = mpcHost;
37
37
  this.version = version;
38
+ this.featureFlags = featureFlags;
38
39
  if (!this.mpc) {
39
40
  throw new Error(`[Portal.Provider.MpcSigner] The MPC module could not be found by the signer. This is usually an issue with React Native linking. Please verify that the 'PortalReactNative' module is properly linked to this project.`);
40
41
  }
@@ -62,6 +63,7 @@ class MpcSigner {
62
63
  const metadata = JSON.stringify({
63
64
  clientPlatform: 'REACT_NATIVE',
64
65
  mpcServerVersion: this.version,
66
+ optimized: this.featureFlags.optimized,
65
67
  });
66
68
  const result = yield this.mpc.sign(apiKey, this.mpcHost, signingShare, message.method, JSON.stringify(this.buildParams(method, params)), provider.gatewayUrl, provider.chainId.toString(), metadata);
67
69
  const { data, error } = JSON.parse(String(result));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@portal-hq/provider",
3
- "version": "2.0.3",
3
+ "version": "2.0.6",
4
4
  "license": "MIT",
5
5
  "main": "lib/commonjs/index",
6
6
  "module": "lib/esm/index",
@@ -19,8 +19,8 @@
19
19
  "test": "jest"
20
20
  },
21
21
  "dependencies": {
22
- "@portal-hq/connect": "^2.0.3",
23
- "@portal-hq/utils": "^2.0.3"
22
+ "@portal-hq/connect": "^2.0.6",
23
+ "@portal-hq/utils": "^2.0.6"
24
24
  },
25
25
  "devDependencies": {
26
26
  "@babel/preset-typescript": "^7.18.6",
@@ -30,5 +30,5 @@
30
30
  "ts-jest": "^29.0.3",
31
31
  "typescript": "^4.8.4"
32
32
  },
33
- "gitHead": "c333fe5280f16a5c5f70b7ae768f112baf7156ba"
33
+ "gitHead": "d933407992d91fd46d22b003233626dcb06dbf5b"
34
34
  }
@@ -20,6 +20,7 @@ import {
20
20
  type SwitchEthereumChainParameter,
21
21
  } from '../../types'
22
22
  import PortalConnect from '@portal-hq/connect'
23
+ import { RpcErrorOptions } from '@portal-hq/utils/types'
23
24
 
24
25
  const passiveSignerMethods = [
25
26
  'eth_accounts',
@@ -74,6 +75,7 @@ class Provider implements IPortalProvider {
74
75
  apiHost = 'api.portalhq.io',
75
76
  mpcHost = 'mpc.portalhq.io',
76
77
  version = 'v4',
78
+ featureFlags = { optimized: false },
77
79
  }: ProviderOptions) {
78
80
  // Handle required fields
79
81
  if (!apiKey || apiKey.length === 0) {
@@ -112,9 +114,10 @@ class Provider implements IPortalProvider {
112
114
 
113
115
  // Initialize an MpcSigner
114
116
  this.signer = new MpcSigner({
115
- mpcHost: mpcHost,
116
- keychain: keychain,
117
- version: version,
117
+ mpcHost,
118
+ keychain,
119
+ version,
120
+ featureFlags,
118
121
  })
119
122
  }
120
123
 
@@ -152,11 +155,12 @@ class Provider implements IPortalProvider {
152
155
  return this.gatewayConfig
153
156
  } else if (
154
157
  typeof this.gatewayConfig === 'object' &&
158
+ // eslint-disable-next-line no-prototype-builtins
155
159
  !this.gatewayConfig.hasOwnProperty(this.chainId)
156
160
  ) {
157
161
  // If there's no explicit mapping for the current chainId, error out
158
162
  throw new Error(
159
- `[PortalProvider] No RPC endpoint configured for chainId: ${this.chainId}`,
163
+ `[PortalProvider] No RPC endpoint configured for chainId: ${this.chainId}`
160
164
  )
161
165
  }
162
166
 
@@ -169,7 +173,7 @@ class Provider implements IPortalProvider {
169
173
 
170
174
  // If we got this far, there's no way to support the chain with the current config
171
175
  throw new Error(
172
- `[PortalProvider] Could not find a valid gatewayConfig entry for chainId: ${this.chainId}`,
176
+ `[PortalProvider] Could not find a valid gatewayConfig entry for chainId: ${this.chainId}`
173
177
  )
174
178
  }
175
179
 
@@ -223,7 +227,7 @@ class Provider implements IPortalProvider {
223
227
 
224
228
  public removeEventListener(
225
229
  event: string,
226
- listenerToRemove?: EventHandler,
230
+ listenerToRemove?: EventHandler
227
231
  ): void {
228
232
  if (!this.events[event]) {
229
233
  return
@@ -233,7 +237,7 @@ class Provider implements IPortalProvider {
233
237
  this.events[event] = []
234
238
  } else {
235
239
  const filterEventHandlers = (
236
- registeredEventHandler: RegisteredEventHandler,
240
+ registeredEventHandler: RegisteredEventHandler
237
241
  ) => {
238
242
  return registeredEventHandler.handler !== listenerToRemove
239
243
  }
@@ -273,7 +277,7 @@ class Provider implements IPortalProvider {
273
277
  })
274
278
 
275
279
  if (response.error) {
276
- throw new ProviderRpcError(response.error)
280
+ throw new ProviderRpcError(response.error as RpcErrorOptions)
277
281
  }
278
282
 
279
283
  result = response.result
@@ -318,7 +322,7 @@ class Provider implements IPortalProvider {
318
322
  */
319
323
  public async setChainId(
320
324
  chainId: number,
321
- connect?: PortalConnect,
325
+ connect?: PortalConnect
322
326
  ): Promise<Provider> {
323
327
  // Update the chainId
324
328
  this.chainId = chainId
@@ -341,7 +345,7 @@ class Provider implements IPortalProvider {
341
345
  // Dispatch 'connect' event
342
346
  this.dispatchConnect()
343
347
 
344
- return this
348
+ return new Promise((resolve) => resolve(this))
345
349
  }
346
350
 
347
351
  /**
@@ -364,7 +368,7 @@ class Provider implements IPortalProvider {
364
368
 
365
369
  if (!connect && (!signingHandlers || signingHandlers.length === 0)) {
366
370
  throw new Error(
367
- `[PortalProvider] Auto-approve is disabled. Cannot perform Provider signing requests without an event handler for the 'portal_signingRequested' event.`,
371
+ `[PortalProvider] Auto-approve is disabled. Cannot perform Provider signing requests without an event handler for the 'portal_signingRequested' event.`
368
372
  )
369
373
  }
370
374
 
@@ -388,7 +392,7 @@ class Provider implements IPortalProvider {
388
392
  ) {
389
393
  resolve(true)
390
394
  }
391
- },
395
+ }
392
396
  )
393
397
 
394
398
  // If the signing request has been rejected, resolve to false
@@ -406,7 +410,7 @@ class Provider implements IPortalProvider {
406
410
  ) {
407
411
  resolve(false)
408
412
  }
409
- },
413
+ }
410
414
  )
411
415
 
412
416
  // Tell any listening clients that signing has been requested
@@ -426,7 +430,7 @@ class Provider implements IPortalProvider {
426
430
  })
427
431
  }
428
432
 
429
- private async dispatchConnect(): Promise<void> {
433
+ private dispatchConnect() {
430
434
  this.emit('connect', {
431
435
  chainId: `0x${this.chainId.toString(16)}`,
432
436
  })
@@ -471,7 +475,7 @@ class Provider implements IPortalProvider {
471
475
 
472
476
  if (!isApproved) {
473
477
  this.log.info(
474
- `[PortalProvider] Request for signing method '${method}' could not be completed because it was not approved by the user.`,
478
+ `[PortalProvider] Request for signing method '${method}' could not be completed because it was not approved by the user.`
475
479
  )
476
480
  return
477
481
  }
@@ -486,10 +490,10 @@ class Provider implements IPortalProvider {
486
490
  case 'eth_signTransaction':
487
491
  case 'eth_signTypedData_v3':
488
492
  case 'eth_signTypedData_v4':
489
- case 'personal_sign':
493
+ case 'personal_sign': {
490
494
  const result = await this.signer?.sign(
491
495
  { chainId: this.chainId, method, params },
492
- this,
496
+ this
493
497
  )
494
498
 
495
499
  if (result) {
@@ -512,9 +516,10 @@ class Provider implements IPortalProvider {
512
516
  }
513
517
 
514
518
  return result
519
+ }
515
520
  default:
516
521
  throw new Error(
517
- '[PortalProvider] Method "' + method + '" not supported',
522
+ '[PortalProvider] Method "' + method + '" not supported'
518
523
  )
519
524
  }
520
525
  }
@@ -2,9 +2,9 @@ import { SigningRequestArguments } from '@portal-hq/utils'
2
2
  import { type SignResult } from '../../types'
3
3
 
4
4
  abstract class Signer {
5
- public async sign(_: SigningRequestArguments, __?: any): Promise<SignResult> {
5
+ public sign(_: SigningRequestArguments, __?: any): Promise<SignResult> {
6
6
  throw new Error(
7
- '[Portal] sign() method must be implemented in a subclass of Signer',
7
+ '[Portal] sign() method must be implemented in a subclass of Signer'
8
8
  )
9
9
  }
10
10
  }
@@ -13,13 +13,15 @@ import {
13
13
  type PortalMobileMpc,
14
14
  type SigningResponse,
15
15
  } from '../../types'
16
+ import { FeatureFlags } from '@portal-hq/core/types'
16
17
 
17
18
  class MpcSigner implements Signer {
18
19
  private isSimulator: boolean
19
20
  private keychain: KeychainAdapter
20
21
  private mpc: PortalMobileMpc
21
22
  private mpcHost: string // should we add a default here mpc.portalhq.io
22
- private version: string = 'v4'
23
+ private version = 'v4'
24
+ private featureFlags: FeatureFlags
23
25
 
24
26
  private get address(): Promise<string | undefined> {
25
27
  return this.keychain.getAddress(this.isSimulator)
@@ -34,23 +36,25 @@ class MpcSigner implements Signer {
34
36
  isSimulator = false,
35
37
  mpcHost = 'mpc.portalhq.io',
36
38
  version = 'v4',
39
+ featureFlags = { optimized: false },
37
40
  }: MpcSignerOptions) {
38
41
  this.isSimulator = isSimulator
39
42
  this.keychain = keychain
40
43
  this.mpc = NativeModules.PortalMobileMpc
41
44
  this.mpcHost = mpcHost
42
45
  this.version = version
46
+ this.featureFlags = featureFlags
43
47
 
44
48
  if (!this.mpc) {
45
49
  throw new Error(
46
- `[Portal.Provider.MpcSigner] The MPC module could not be found by the signer. This is usually an issue with React Native linking. Please verify that the 'PortalReactNative' module is properly linked to this project.`,
50
+ `[Portal.Provider.MpcSigner] The MPC module could not be found by the signer. This is usually an issue with React Native linking. Please verify that the 'PortalReactNative' module is properly linked to this project.`
47
51
  )
48
52
  }
49
53
  }
50
54
 
51
55
  public async sign(
52
56
  message: SigningRequestArguments,
53
- provider: IPortalProvider,
57
+ provider: IPortalProvider
54
58
  ): Promise<any> {
55
59
  const address = await this.address
56
60
  const apiKey = provider.apiKey
@@ -71,6 +75,7 @@ class MpcSigner implements Signer {
71
75
  const metadata = JSON.stringify({
72
76
  clientPlatform: 'REACT_NATIVE',
73
77
  mpcServerVersion: this.version,
78
+ optimized: this.featureFlags.optimized,
74
79
  })
75
80
 
76
81
  const result = await this.mpc.sign(
@@ -81,7 +86,7 @@ class MpcSigner implements Signer {
81
86
  JSON.stringify(this.buildParams(method, params)),
82
87
  provider.gatewayUrl,
83
88
  provider.chainId.toString(),
84
- metadata,
89
+ metadata
85
90
  )
86
91
 
87
92
  const { data, error } = JSON.parse(String(result)) as SigningResponse
package/types.d.ts CHANGED
@@ -22,25 +22,27 @@ export interface MpcSignerOptions extends SignerOptions {
22
22
  isSimulator?: boolean
23
23
  mpcHost?: string
24
24
  version?: string
25
+ featureFlags?: FeatureFlags
25
26
  }
26
27
 
27
28
  export interface PortalMobileMpcMetadata {
28
29
  clientPlatform: string
29
30
  mpcServerVersion: string
31
+ optimized: boolean
30
32
  }
31
33
 
32
34
  export interface PortalMobileMpc {
33
35
  generate: (
34
36
  clientApiKey: string,
35
37
  mpcApiUrl: string,
36
- metadata: string,
38
+ metadata: string
37
39
  ) => Promise<string>
38
40
  rotate: (
39
41
  clientApiKey: string,
40
42
  mpcApiUrl: string,
41
43
  isSim: boolean,
42
44
  dkgResult?: string,
43
- metadata: string,
45
+ metadata: string
44
46
  ) => Promise<string>
45
47
  sign: (
46
48
  clientApiKey: string,
@@ -50,7 +52,7 @@ export interface PortalMobileMpc {
50
52
  requestId: string,
51
53
  mpcUrl: string,
52
54
  chainId: string,
53
- metadata: string,
55
+ metadata: string
54
56
  ) => Promise<string>
55
57
  }
56
58
 
@@ -67,6 +69,7 @@ export interface ProviderOptions {
67
69
  apiHost?: string
68
70
  mpcHost?: string
69
71
  version?: string
72
+ featureFlags?: FeatureFlags
70
73
  }
71
74
 
72
75
  export interface RegisteredEventHandler {