@portal-hq/provider 2.0.2 → 2.0.4

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() {
@@ -181,7 +182,7 @@ class Provider {
181
182
  * @param args The arguments of the request being made
182
183
  * @returns Promise<any>
183
184
  */
184
- request({ method, params, connect, }) {
185
+ request({ method, params, chainId, connect, }) {
185
186
  return __awaiter(this, void 0, void 0, function* () {
186
187
  if (method === 'eth_chainId') {
187
188
  return this.chainId;
@@ -206,6 +207,7 @@ class Provider {
206
207
  const transactionHash = yield this.handleSigningRequests({
207
208
  method,
208
209
  params,
210
+ chainId,
209
211
  connect,
210
212
  });
211
213
  if (transactionHash) {
@@ -249,7 +251,7 @@ class Provider {
249
251
  this.emit('chainChanged', {
250
252
  chainId: this.chainId,
251
253
  });
252
- if (connect) {
254
+ if (connect && connect.connected) {
253
255
  connect.emit('portalConnect_chainChanged', {
254
256
  chainId: this.chainId,
255
257
  });
@@ -264,7 +266,7 @@ class Provider {
264
266
  *
265
267
  * @param args The arguments of the request being made
266
268
  */
267
- getApproval({ method, params, connect, }) {
269
+ getApproval({ method, params, chainId, connect, }) {
268
270
  return __awaiter(this, void 0, void 0, function* () {
269
271
  // If autoApprove is enabled, just resolve to true
270
272
  if (this.autoApprove) {
@@ -305,12 +307,14 @@ class Provider {
305
307
  connect.emit('portalConnect_signingRequested', {
306
308
  method,
307
309
  params,
310
+ chainId,
308
311
  });
309
312
  }
310
313
  else {
311
314
  this.emit('portal_signingRequested', {
312
315
  method,
313
316
  params,
317
+ chainId,
314
318
  });
315
319
  }
316
320
  });
@@ -348,12 +352,12 @@ class Provider {
348
352
  * @param args The arguments of the request being made
349
353
  * @returns Promise<any>
350
354
  */
351
- handleSigningRequests({ method, params, connect, }) {
355
+ handleSigningRequests({ method, params, chainId, connect, }) {
352
356
  var _a;
353
357
  return __awaiter(this, void 0, void 0, function* () {
354
358
  const isApproved = passiveSignerMethods.includes(method)
355
359
  ? true
356
- : yield this.getApproval({ method, params, connect });
360
+ : yield this.getApproval({ method, params, chainId, connect });
357
361
  if (!isApproved) {
358
362
  this.log.info(`[PortalProvider] Request for signing method '${method}' could not be completed because it was not approved by the user.`);
359
363
  return;
@@ -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
  }
@@ -61,7 +62,12 @@ class MpcSigner {
61
62
  break;
62
63
  }
63
64
  const signingShare = yield this.signingShare;
64
- const result = yield this.mpc.sign(apiKey, this.mpcHost, signingShare, message.method, JSON.stringify(this.buildParams(method, params)), provider.gatewayUrl, provider.chainId.toString(), this.version);
65
+ const metadata = JSON.stringify({
66
+ clientPlatform: 'REACT_NATIVE',
67
+ mpcServerVersion: this.version,
68
+ optimized: this.featureFlags.optimized,
69
+ });
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);
65
71
  const { data, error } = JSON.parse(String(result));
66
72
  if (error && error.code > 0) {
67
73
  throw new utils_1.PortalMpcError(error);
@@ -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() {
@@ -179,7 +180,7 @@ class Provider {
179
180
  * @param args The arguments of the request being made
180
181
  * @returns Promise<any>
181
182
  */
182
- request({ method, params, connect, }) {
183
+ request({ method, params, chainId, connect, }) {
183
184
  return __awaiter(this, void 0, void 0, function* () {
184
185
  if (method === 'eth_chainId') {
185
186
  return this.chainId;
@@ -204,6 +205,7 @@ class Provider {
204
205
  const transactionHash = yield this.handleSigningRequests({
205
206
  method,
206
207
  params,
208
+ chainId,
207
209
  connect,
208
210
  });
209
211
  if (transactionHash) {
@@ -247,7 +249,7 @@ class Provider {
247
249
  this.emit('chainChanged', {
248
250
  chainId: this.chainId,
249
251
  });
250
- if (connect) {
252
+ if (connect && connect.connected) {
251
253
  connect.emit('portalConnect_chainChanged', {
252
254
  chainId: this.chainId,
253
255
  });
@@ -262,7 +264,7 @@ class Provider {
262
264
  *
263
265
  * @param args The arguments of the request being made
264
266
  */
265
- getApproval({ method, params, connect, }) {
267
+ getApproval({ method, params, chainId, connect, }) {
266
268
  return __awaiter(this, void 0, void 0, function* () {
267
269
  // If autoApprove is enabled, just resolve to true
268
270
  if (this.autoApprove) {
@@ -303,12 +305,14 @@ class Provider {
303
305
  connect.emit('portalConnect_signingRequested', {
304
306
  method,
305
307
  params,
308
+ chainId,
306
309
  });
307
310
  }
308
311
  else {
309
312
  this.emit('portal_signingRequested', {
310
313
  method,
311
314
  params,
315
+ chainId,
312
316
  });
313
317
  }
314
318
  });
@@ -346,12 +350,12 @@ class Provider {
346
350
  * @param args The arguments of the request being made
347
351
  * @returns Promise<any>
348
352
  */
349
- handleSigningRequests({ method, params, connect, }) {
353
+ handleSigningRequests({ method, params, chainId, connect, }) {
350
354
  var _a;
351
355
  return __awaiter(this, void 0, void 0, function* () {
352
356
  const isApproved = passiveSignerMethods.includes(method)
353
357
  ? true
354
- : yield this.getApproval({ method, params, connect });
358
+ : yield this.getApproval({ method, params, chainId, connect });
355
359
  if (!isApproved) {
356
360
  this.log.info(`[PortalProvider] Request for signing method '${method}' could not be completed because it was not approved by the user.`);
357
361
  return;
@@ -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
  }
@@ -59,7 +60,12 @@ class MpcSigner {
59
60
  break;
60
61
  }
61
62
  const signingShare = yield this.signingShare;
62
- const result = yield this.mpc.sign(apiKey, this.mpcHost, signingShare, message.method, JSON.stringify(this.buildParams(method, params)), provider.gatewayUrl, provider.chainId.toString(), this.version);
63
+ const metadata = JSON.stringify({
64
+ clientPlatform: 'REACT_NATIVE',
65
+ mpcServerVersion: this.version,
66
+ optimized: this.featureFlags.optimized,
67
+ });
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);
63
69
  const { data, error } = JSON.parse(String(result));
64
70
  if (error && error.code > 0) {
65
71
  throw new PortalMpcError(error);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@portal-hq/provider",
3
- "version": "2.0.2",
3
+ "version": "2.0.4",
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.2",
23
- "@portal-hq/utils": "^2.0.2"
22
+ "@portal-hq/connect": "^2.0.4",
23
+ "@portal-hq/utils": "^2.0.4"
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": "8ce39a2867dacd6403db80b5fc51aa40c3b3c205"
33
+ "gitHead": "2d2f1ef628bc8788fa4a9cd32cc44795e547b3a7"
34
34
  }
@@ -74,6 +74,7 @@ class Provider implements IPortalProvider {
74
74
  apiHost = 'api.portalhq.io',
75
75
  mpcHost = 'mpc.portalhq.io',
76
76
  version = 'v4',
77
+ featureFlags = { optimized: false },
77
78
  }: ProviderOptions) {
78
79
  // Handle required fields
79
80
  if (!apiKey || apiKey.length === 0) {
@@ -112,9 +113,10 @@ class Provider implements IPortalProvider {
112
113
 
113
114
  // Initialize an MpcSigner
114
115
  this.signer = new MpcSigner({
115
- mpcHost: mpcHost,
116
- keychain: keychain,
117
- version: version,
116
+ mpcHost,
117
+ keychain,
118
+ version,
119
+ featureFlags,
118
120
  })
119
121
  }
120
122
 
@@ -252,6 +254,7 @@ class Provider implements IPortalProvider {
252
254
  public async request({
253
255
  method,
254
256
  params,
257
+ chainId,
255
258
  connect,
256
259
  }: RequestArguments): Promise<any> {
257
260
  if (method === 'eth_chainId') {
@@ -281,6 +284,7 @@ class Provider implements IPortalProvider {
281
284
  const transactionHash = await this.handleSigningRequests({
282
285
  method,
283
286
  params,
287
+ chainId,
284
288
  connect,
285
289
  })
286
290
 
@@ -314,7 +318,10 @@ class Provider implements IPortalProvider {
314
318
  * @param chainId The numerical ID of the chain to switch to
315
319
  * @returns BaseProvider
316
320
  */
317
- public async setChainId(chainId: number, connect?: PortalConnect): Promise<Provider> {
321
+ public async setChainId(
322
+ chainId: number,
323
+ connect?: PortalConnect,
324
+ ): Promise<Provider> {
318
325
  // Update the chainId
319
326
  this.chainId = chainId
320
327
 
@@ -328,8 +335,7 @@ class Provider implements IPortalProvider {
328
335
  chainId: this.chainId,
329
336
  } as SwitchEthereumChainParameter)
330
337
 
331
-
332
- if (connect) {
338
+ if (connect && connect.connected) {
333
339
  connect.emit('portalConnect_chainChanged', {
334
340
  chainId: this.chainId,
335
341
  } as SwitchEthereumChainParameter)
@@ -348,6 +354,7 @@ class Provider implements IPortalProvider {
348
354
  protected async getApproval({
349
355
  method,
350
356
  params,
357
+ chainId,
351
358
  connect,
352
359
  }: RequestArguments): Promise<boolean> {
353
360
  // If autoApprove is enabled, just resolve to true
@@ -409,11 +416,13 @@ class Provider implements IPortalProvider {
409
416
  connect.emit('portalConnect_signingRequested', {
410
417
  method,
411
418
  params,
419
+ chainId,
412
420
  })
413
421
  } else {
414
422
  this.emit('portal_signingRequested', {
415
423
  method,
416
424
  params,
425
+ chainId,
417
426
  })
418
427
  }
419
428
  })
@@ -455,11 +464,12 @@ class Provider implements IPortalProvider {
455
464
  private async handleSigningRequests({
456
465
  method,
457
466
  params,
467
+ chainId,
458
468
  connect,
459
469
  }: RequestArguments): Promise<any> {
460
470
  const isApproved = passiveSignerMethods.includes(method)
461
471
  ? true
462
- : await this.getApproval({ method, params, connect })
472
+ : await this.getApproval({ method, params, chainId, connect })
463
473
 
464
474
  if (!isApproved) {
465
475
  this.log.info(
@@ -13,6 +13,7 @@ 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
@@ -20,6 +21,7 @@ class MpcSigner implements Signer {
20
21
  private mpc: PortalMobileMpc
21
22
  private mpcHost: string // should we add a default here mpc.portalhq.io
22
23
  private version: string = 'v4'
24
+ private featureFlags: FeatureFlags
23
25
 
24
26
  private get address(): Promise<string | undefined> {
25
27
  return this.keychain.getAddress(this.isSimulator)
@@ -34,12 +36,14 @@ 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(
@@ -68,6 +72,12 @@ class MpcSigner implements Signer {
68
72
 
69
73
  const signingShare = await this.signingShare
70
74
 
75
+ const metadata = JSON.stringify({
76
+ clientPlatform: 'REACT_NATIVE',
77
+ mpcServerVersion: this.version,
78
+ optimized: this.featureFlags.optimized,
79
+ })
80
+
71
81
  const result = await this.mpc.sign(
72
82
  apiKey,
73
83
  this.mpcHost,
@@ -76,7 +86,7 @@ class MpcSigner implements Signer {
76
86
  JSON.stringify(this.buildParams(method, params)),
77
87
  provider.gatewayUrl,
78
88
  provider.chainId.toString(),
79
- this.version,
89
+ metadata,
80
90
  )
81
91
 
82
92
  const { data, error } = JSON.parse(String(result)) as SigningResponse
package/types.d.ts CHANGED
@@ -22,20 +22,27 @@ export interface MpcSignerOptions extends SignerOptions {
22
22
  isSimulator?: boolean
23
23
  mpcHost?: string
24
24
  version?: string
25
+ featureFlags?: FeatureFlags
26
+ }
27
+
28
+ export interface PortalMobileMpcMetadata {
29
+ clientPlatform: string
30
+ mpcServerVersion: string
31
+ optimized: boolean
25
32
  }
26
33
 
27
34
  export interface PortalMobileMpc {
28
35
  generate: (
29
36
  clientApiKey: string,
30
37
  mpcApiUrl: string,
31
- mpcVersion: string,
38
+ metadata: string,
32
39
  ) => Promise<string>
33
40
  rotate: (
34
41
  clientApiKey: string,
35
42
  mpcApiUrl: string,
36
43
  isSim: boolean,
37
44
  dkgResult?: string,
38
- mpcVersion: string,
45
+ metadata: string,
39
46
  ) => Promise<string>
40
47
  sign: (
41
48
  clientApiKey: string,
@@ -45,7 +52,7 @@ export interface PortalMobileMpc {
45
52
  requestId: string,
46
53
  mpcUrl: string,
47
54
  chainId: string,
48
- mpcVersion: string,
55
+ metadata: string,
49
56
  ) => Promise<string>
50
57
  }
51
58
 
@@ -62,6 +69,7 @@ export interface ProviderOptions {
62
69
  apiHost?: string
63
70
  mpcHost?: string
64
71
  version?: string
72
+ featureFlags?: FeatureFlags
65
73
  }
66
74
 
67
75
  export interface RegisteredEventHandler {