@portal-hq/connect 2.0.4 → 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.
@@ -16,6 +16,18 @@ class PortalConnect {
16
16
  // Optional
17
17
  isSimulator = false, autoApprove = false, version = 'v4', apiHost = 'api.portalhq.io', mpcHost = 'mpc.portalhq.io', webSocketServer = 'connect.portalhq.io', }) {
18
18
  this.connectionState = ConnectionStates.DISCONNECTED;
19
+ this.handleSigningRejected = (data) => {
20
+ var _a;
21
+ // Notify the proxy server that the signing request was rejected
22
+ (_a = this.socket) === null || _a === void 0 ? void 0 : _a.send(JSON.stringify({
23
+ event: 'portal_signingRejected',
24
+ data: {
25
+ topic: data.topic,
26
+ transactionHash: '',
27
+ transactionId: data.id,
28
+ },
29
+ }));
30
+ };
19
31
  this.apiKey = apiKey;
20
32
  this.events = {};
21
33
  this.websocketServer = webSocketServer;
@@ -65,22 +77,20 @@ class PortalConnect {
65
77
  * @param connectionString The URI to ask the Connect Proxy to create a websocket connection to
66
78
  */
67
79
  connect(uri) {
68
- return __awaiter(this, void 0, void 0, function* () {
69
- if (this.socket) {
70
- this.socket.close();
71
- }
72
- const protocol = this.websocketServer.startsWith('localhost') ? 'ws' : 'wss';
73
- // @ts-ignore
74
- this.socket = new WebSocket(`${protocol}://${this.websocketServer}/`, null,
75
- // @ts-ignore
76
- {
77
- headers: {
78
- Authorization: `Bearer ${this.apiKey}`,
79
- },
80
- });
81
- this.uri = uri;
82
- this.bindToSocketEvents(this.socket, uri);
80
+ if (this.socket) {
81
+ this.socket.close();
82
+ }
83
+ const protocol = this.websocketServer.startsWith('localhost') ? 'ws' : 'wss';
84
+ this.socket = new WebSocket(`${protocol}://${this.websocketServer}/`, null,
85
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
86
+ // @ts-ignore
87
+ {
88
+ headers: {
89
+ Authorization: `Bearer ${this.apiKey}`,
90
+ },
83
91
  });
92
+ this.uri = uri;
93
+ this.bindToSocketEvents(this.socket, uri);
84
94
  }
85
95
  deinit() {
86
96
  this.events = {};
@@ -95,7 +105,7 @@ class PortalConnect {
95
105
  */
96
106
  setChainId(chainId) {
97
107
  return __awaiter(this, void 0, void 0, function* () {
98
- this.provider.setChainId(chainId, this);
108
+ yield this.provider.setChainId(chainId, this);
99
109
  });
100
110
  }
101
111
  /**
@@ -241,7 +251,7 @@ class PortalConnect {
241
251
  },
242
252
  }));
243
253
  });
244
- socket.onerror = (event) => __awaiter(this, void 0, void 0, function* () {
254
+ socket.onerror = (event) => {
245
255
  let errorMessage = 'An unexpected error occurred.';
246
256
  if (event instanceof ErrorEvent) {
247
257
  // Check if the event is an ErrorEvent
@@ -253,7 +263,7 @@ class PortalConnect {
253
263
  params: new ConnectError(errorMessage, 500),
254
264
  });
255
265
  this.connectionState = ConnectionStates.DISCONNECTED;
256
- });
266
+ };
257
267
  /**
258
268
  * Handles all incoming messages over the websocket connection
259
269
  * (inbound messages proxied via the relay)
@@ -277,12 +287,13 @@ class PortalConnect {
277
287
  this.connectionState = ConnectionStates.DISCONNECTED;
278
288
  this.emit('disconnect', message.data);
279
289
  break;
280
- case 'session_request':
290
+ case 'session_request': {
281
291
  const request = message.data;
282
292
  const params = request.params;
283
293
  // Sign the transaction and get back a transaction hash
284
294
  yield this.handleProviderRequest(params.request.method, params.request.params, params.chainId, request);
285
295
  break;
296
+ }
286
297
  case 'portal_dappSessionRequested':
287
298
  case 'portal_dappSessionRequestedV1':
288
299
  this.handleSessionRequest(message);
@@ -354,45 +365,27 @@ class PortalConnect {
354
365
  });
355
366
  }
356
367
  handleSessionRequest(message) {
357
- return __awaiter(this, void 0, void 0, function* () {
358
- const request = message.data;
359
- // Bind to potential session approval/rejection events
360
- this.on('portal_dappSessionApproved', (data) => this.handleSessionApproved(data, request));
361
- this.on('portal_dappSessionRejected', (data) => this.handleSessionRejected(data, request));
362
- // Emit the session request event
363
- this.emit('portal_dappSessionRequested', request.params);
364
- });
368
+ const request = message.data;
369
+ // Bind to potential session approval/rejection events
370
+ this.on('portal_dappSessionApproved', (data) => this.handleSessionApproved(data, request));
371
+ this.on('portal_dappSessionRejected', (data) => this.handleSessionRejected(data, request));
372
+ // Emit the session request event
373
+ this.emit('portal_dappSessionRequested', request.params);
365
374
  }
366
375
  handleSignatureReceived(data, txHash) {
367
376
  var _a;
368
- return __awaiter(this, void 0, void 0, function* () {
369
- const { id, topic } = data;
370
- // Let the server know the transaction hash
371
- (_a = this.socket) === null || _a === void 0 ? void 0 : _a.send(JSON.stringify({
372
- event: 'signatureReceived',
373
- data: {
374
- topic,
375
- transactionHash: txHash,
376
- transactionId: id,
377
- },
378
- }));
379
- // Let the SDK consumer know the transaction hash
380
- this.emit('portal_signgatureReceived', txHash);
381
- });
382
- }
383
- handleSigningRejected(data) {
384
- var _a;
385
- return __awaiter(this, void 0, void 0, function* () {
386
- // Notify the proxy server that the signing request was rejected
387
- (_a = this.socket) === null || _a === void 0 ? void 0 : _a.send(JSON.stringify({
388
- event: 'portal_signingRejected',
389
- data: {
390
- topic: data.topic,
391
- transactionHash: '',
392
- transactionId: data.id,
393
- },
394
- }));
395
- });
377
+ const { id, topic } = data;
378
+ // Let the server know the transaction hash
379
+ (_a = this.socket) === null || _a === void 0 ? void 0 : _a.send(JSON.stringify({
380
+ event: 'signatureReceived',
381
+ data: {
382
+ topic,
383
+ transactionHash: txHash,
384
+ transactionId: id,
385
+ },
386
+ }));
387
+ // Let the SDK consumer know the transaction hash
388
+ this.emit('portal_signatureReceived', txHash);
396
389
  }
397
390
  sendFinalMessageAndClose() {
398
391
  var _a;
package/lib/esm/index.js CHANGED
@@ -13,6 +13,18 @@ class PortalConnect {
13
13
  // Optional
14
14
  isSimulator = false, autoApprove = false, version = 'v4', apiHost = 'api.portalhq.io', mpcHost = 'mpc.portalhq.io', webSocketServer = 'connect.portalhq.io', }) {
15
15
  this.connectionState = ConnectionStates.DISCONNECTED;
16
+ this.handleSigningRejected = (data) => {
17
+ var _a;
18
+ // Notify the proxy server that the signing request was rejected
19
+ (_a = this.socket) === null || _a === void 0 ? void 0 : _a.send(JSON.stringify({
20
+ event: 'portal_signingRejected',
21
+ data: {
22
+ topic: data.topic,
23
+ transactionHash: '',
24
+ transactionId: data.id,
25
+ },
26
+ }));
27
+ };
16
28
  this.apiKey = apiKey;
17
29
  this.events = {};
18
30
  this.websocketServer = webSocketServer;
@@ -62,22 +74,20 @@ class PortalConnect {
62
74
  * @param connectionString The URI to ask the Connect Proxy to create a websocket connection to
63
75
  */
64
76
  connect(uri) {
65
- return __awaiter(this, void 0, void 0, function* () {
66
- if (this.socket) {
67
- this.socket.close();
68
- }
69
- const protocol = this.websocketServer.startsWith('localhost') ? 'ws' : 'wss';
70
- // @ts-ignore
71
- this.socket = new WebSocket(`${protocol}://${this.websocketServer}/`, null,
72
- // @ts-ignore
73
- {
74
- headers: {
75
- Authorization: `Bearer ${this.apiKey}`,
76
- },
77
- });
78
- this.uri = uri;
79
- this.bindToSocketEvents(this.socket, uri);
77
+ if (this.socket) {
78
+ this.socket.close();
79
+ }
80
+ const protocol = this.websocketServer.startsWith('localhost') ? 'ws' : 'wss';
81
+ this.socket = new WebSocket(`${protocol}://${this.websocketServer}/`, null,
82
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
83
+ // @ts-ignore
84
+ {
85
+ headers: {
86
+ Authorization: `Bearer ${this.apiKey}`,
87
+ },
80
88
  });
89
+ this.uri = uri;
90
+ this.bindToSocketEvents(this.socket, uri);
81
91
  }
82
92
  deinit() {
83
93
  this.events = {};
@@ -92,7 +102,7 @@ class PortalConnect {
92
102
  */
93
103
  setChainId(chainId) {
94
104
  return __awaiter(this, void 0, void 0, function* () {
95
- this.provider.setChainId(chainId, this);
105
+ yield this.provider.setChainId(chainId, this);
96
106
  });
97
107
  }
98
108
  /**
@@ -238,7 +248,7 @@ class PortalConnect {
238
248
  },
239
249
  }));
240
250
  });
241
- socket.onerror = (event) => __awaiter(this, void 0, void 0, function* () {
251
+ socket.onerror = (event) => {
242
252
  let errorMessage = 'An unexpected error occurred.';
243
253
  if (event instanceof ErrorEvent) {
244
254
  // Check if the event is an ErrorEvent
@@ -250,7 +260,7 @@ class PortalConnect {
250
260
  params: new ConnectError(errorMessage, 500),
251
261
  });
252
262
  this.connectionState = ConnectionStates.DISCONNECTED;
253
- });
263
+ };
254
264
  /**
255
265
  * Handles all incoming messages over the websocket connection
256
266
  * (inbound messages proxied via the relay)
@@ -274,12 +284,13 @@ class PortalConnect {
274
284
  this.connectionState = ConnectionStates.DISCONNECTED;
275
285
  this.emit('disconnect', message.data);
276
286
  break;
277
- case 'session_request':
287
+ case 'session_request': {
278
288
  const request = message.data;
279
289
  const params = request.params;
280
290
  // Sign the transaction and get back a transaction hash
281
291
  yield this.handleProviderRequest(params.request.method, params.request.params, params.chainId, request);
282
292
  break;
293
+ }
283
294
  case 'portal_dappSessionRequested':
284
295
  case 'portal_dappSessionRequestedV1':
285
296
  this.handleSessionRequest(message);
@@ -351,45 +362,27 @@ class PortalConnect {
351
362
  });
352
363
  }
353
364
  handleSessionRequest(message) {
354
- return __awaiter(this, void 0, void 0, function* () {
355
- const request = message.data;
356
- // Bind to potential session approval/rejection events
357
- this.on('portal_dappSessionApproved', (data) => this.handleSessionApproved(data, request));
358
- this.on('portal_dappSessionRejected', (data) => this.handleSessionRejected(data, request));
359
- // Emit the session request event
360
- this.emit('portal_dappSessionRequested', request.params);
361
- });
365
+ const request = message.data;
366
+ // Bind to potential session approval/rejection events
367
+ this.on('portal_dappSessionApproved', (data) => this.handleSessionApproved(data, request));
368
+ this.on('portal_dappSessionRejected', (data) => this.handleSessionRejected(data, request));
369
+ // Emit the session request event
370
+ this.emit('portal_dappSessionRequested', request.params);
362
371
  }
363
372
  handleSignatureReceived(data, txHash) {
364
373
  var _a;
365
- return __awaiter(this, void 0, void 0, function* () {
366
- const { id, topic } = data;
367
- // Let the server know the transaction hash
368
- (_a = this.socket) === null || _a === void 0 ? void 0 : _a.send(JSON.stringify({
369
- event: 'signatureReceived',
370
- data: {
371
- topic,
372
- transactionHash: txHash,
373
- transactionId: id,
374
- },
375
- }));
376
- // Let the SDK consumer know the transaction hash
377
- this.emit('portal_signgatureReceived', txHash);
378
- });
379
- }
380
- handleSigningRejected(data) {
381
- var _a;
382
- return __awaiter(this, void 0, void 0, function* () {
383
- // Notify the proxy server that the signing request was rejected
384
- (_a = this.socket) === null || _a === void 0 ? void 0 : _a.send(JSON.stringify({
385
- event: 'portal_signingRejected',
386
- data: {
387
- topic: data.topic,
388
- transactionHash: '',
389
- transactionId: data.id,
390
- },
391
- }));
392
- });
374
+ const { id, topic } = data;
375
+ // Let the server know the transaction hash
376
+ (_a = this.socket) === null || _a === void 0 ? void 0 : _a.send(JSON.stringify({
377
+ event: 'signatureReceived',
378
+ data: {
379
+ topic,
380
+ transactionHash: txHash,
381
+ transactionId: id,
382
+ },
383
+ }));
384
+ // Let the SDK consumer know the transaction hash
385
+ this.emit('portal_signatureReceived', txHash);
393
386
  }
394
387
  sendFinalMessageAndClose() {
395
388
  var _a;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@portal-hq/connect",
3
- "version": "2.0.4",
3
+ "version": "2.0.6",
4
4
  "main": "lib/commonjs/index",
5
5
  "module": "lib/esm/index",
6
6
  "source": "src/index",
@@ -32,5 +32,5 @@
32
32
  "react": "*",
33
33
  "react-native": "*"
34
34
  },
35
- "gitHead": "2d2f1ef628bc8788fa4a9cd32cc44795e547b3a7"
35
+ "gitHead": "d933407992d91fd46d22b003233626dcb06dbf5b"
36
36
  }
package/src/index.ts CHANGED
@@ -99,22 +99,22 @@ class PortalConnect {
99
99
  *
100
100
  * @param connectionString The URI to ask the Connect Proxy to create a websocket connection to
101
101
  */
102
- public async connect(uri: string): Promise<void> {
102
+ public connect(uri: string) {
103
103
  if (this.socket) {
104
104
  this.socket.close()
105
105
  }
106
106
 
107
107
  const protocol = this.websocketServer.startsWith('localhost') ? 'ws' : 'wss'
108
- // @ts-ignore
109
108
  this.socket = new WebSocket(
110
109
  `${protocol}://${this.websocketServer}/`,
111
110
  null,
111
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
112
112
  // @ts-ignore
113
113
  {
114
114
  headers: {
115
115
  Authorization: `Bearer ${this.apiKey}`,
116
116
  },
117
- },
117
+ }
118
118
  )
119
119
  this.uri = uri
120
120
  this.bindToSocketEvents(this.socket, uri)
@@ -134,7 +134,7 @@ class PortalConnect {
134
134
  * @param chainId The number of the chainId to switch to
135
135
  */
136
136
  public async setChainId(chainId: number): Promise<void> {
137
- this.provider.setChainId(chainId, this)
137
+ await this.provider.setChainId(chainId, this)
138
138
  }
139
139
 
140
140
  /**
@@ -210,7 +210,7 @@ class PortalConnect {
210
210
 
211
211
  public removeEventListener(
212
212
  event: string,
213
- listenerToRemove?: EventHandler,
213
+ listenerToRemove?: EventHandler
214
214
  ): void {
215
215
  if (!this.events[event]) {
216
216
  return
@@ -220,7 +220,7 @@ class PortalConnect {
220
220
  this.events[event] = []
221
221
  } else {
222
222
  const filterEventHandlers = (
223
- registeredEventHandler: RegisteredEventHandler,
223
+ registeredEventHandler: RegisteredEventHandler
224
224
  ) => {
225
225
  return registeredEventHandler.handler !== listenerToRemove
226
226
  }
@@ -235,7 +235,7 @@ class PortalConnect {
235
235
  * @returns session proposal with chains from gateway config added
236
236
  */
237
237
  public addChainsToProposal(
238
- proposal: SessionProposalOrMetadata,
238
+ proposal: SessionProposalOrMetadata
239
239
  ): SessionProposalOrMetadata {
240
240
  if (!('params' in proposal) || !('requiredNamespaces' in proposal.params)) {
241
241
  throw new Error('Invalid proposal structure.')
@@ -292,7 +292,7 @@ class PortalConnect {
292
292
  chainId,
293
293
  uri,
294
294
  },
295
- }),
295
+ })
296
296
  )
297
297
  }
298
298
 
@@ -306,11 +306,11 @@ class PortalConnect {
306
306
  uri: this.uri,
307
307
  chainId: (data as SwitchEthereumChainParameter).chainId,
308
308
  },
309
- }),
309
+ })
310
310
  )
311
311
  })
312
312
 
313
- socket.onerror = async (event: Event) => {
313
+ socket.onerror = (event: Event) => {
314
314
  let errorMessage = 'An unexpected error occurred.'
315
315
 
316
316
  if (event instanceof ErrorEvent) {
@@ -333,7 +333,9 @@ class PortalConnect {
333
333
  * @returns
334
334
  */
335
335
  socket.onmessage = async (messageEvent: WebSocketMessageEvent) => {
336
- const message = JSON.parse(messageEvent.data) as WebsocketMessage
336
+ const message = JSON.parse(
337
+ messageEvent.data as string
338
+ ) as WebsocketMessage
337
339
 
338
340
  switch (message.event) {
339
341
  case 'close':
@@ -351,7 +353,7 @@ class PortalConnect {
351
353
  this.emit('disconnect', message.data)
352
354
 
353
355
  break
354
- case 'session_request':
356
+ case 'session_request': {
355
357
  const request = message.data as SessionRequest
356
358
  const params = request.params as ProviderRequestPayload
357
359
  // Sign the transaction and get back a transaction hash
@@ -359,9 +361,10 @@ class PortalConnect {
359
361
  params.request.method,
360
362
  params.request.params,
361
363
  params.chainId,
362
- request,
364
+ request
363
365
  )
364
366
  break
367
+ }
365
368
  case 'portal_dappSessionRequested':
366
369
  case 'portal_dappSessionRequestedV1':
367
370
  this.handleSessionRequest(message)
@@ -371,7 +374,7 @@ class PortalConnect {
371
374
  break
372
375
  default:
373
376
  console.log(
374
- `Received unsupported event "${message.event}". Ignoring.`,
377
+ `Received unsupported event "${message.event}". Ignoring.`
375
378
  )
376
379
  break
377
380
  }
@@ -390,7 +393,7 @@ class PortalConnect {
390
393
  method: string,
391
394
  params: any,
392
395
  chainId: string,
393
- request: SessionRequest,
396
+ request: SessionRequest
394
397
  ): Promise<void> {
395
398
  // Bind to potential signing rejection events
396
399
  this.provider.on('portal_signingRejected', this.handleSigningRejected)
@@ -405,7 +408,7 @@ class PortalConnect {
405
408
  ) {
406
409
  this.handleSignatureReceived(request, signature)
407
410
  }
408
- },
411
+ }
409
412
  )
410
413
 
411
414
  // Pass the request along to the provider
@@ -414,7 +417,7 @@ class PortalConnect {
414
417
 
415
418
  private async handleSessionApproved(
416
419
  data: SessionProposalOrMetadata,
417
- request: SessionRequest,
420
+ request: SessionRequest
418
421
  ): Promise<void> {
419
422
  const address = await this.address
420
423
 
@@ -429,13 +432,13 @@ class PortalConnect {
429
432
  id: request.id,
430
433
  params: data,
431
434
  },
432
- } as WebsocketMessage),
435
+ } as WebsocketMessage)
433
436
  )
434
437
  }
435
438
 
436
439
  private async handleSessionRejected(
437
440
  data: SessionProposalOrMetadata,
438
- request: SessionRequest,
441
+ request: SessionRequest
439
442
  ): Promise<void> {
440
443
  const address = await this.address
441
444
 
@@ -450,31 +453,28 @@ class PortalConnect {
450
453
  id: request.id,
451
454
  params: data,
452
455
  },
453
- } as WebsocketMessage),
456
+ } as WebsocketMessage)
454
457
  )
455
458
  }
456
459
 
457
- private async handleSessionRequest(message: WebsocketMessage): Promise<void> {
460
+ private handleSessionRequest(message: WebsocketMessage) {
458
461
  const request = message.data as SessionRequest
459
462
  // Bind to potential session approval/rejection events
460
463
  this.on('portal_dappSessionApproved', (data) =>
461
- this.handleSessionApproved(data, request),
464
+ this.handleSessionApproved(data as SessionProposalOrMetadata, request)
462
465
  )
463
466
  this.on('portal_dappSessionRejected', (data) =>
464
- this.handleSessionRejected(data, request),
467
+ this.handleSessionRejected(data as SessionProposalOrMetadata, request)
465
468
  )
466
469
 
467
470
  // Emit the session request event
468
471
  this.emit(
469
472
  'portal_dappSessionRequested',
470
- request.params as SessionProposalOrMetadata,
473
+ request.params as SessionProposalOrMetadata
471
474
  )
472
475
  }
473
476
 
474
- private async handleSignatureReceived(
475
- data: SessionRequest,
476
- txHash: any,
477
- ): Promise<void> {
477
+ private handleSignatureReceived(data: SessionRequest, txHash: any) {
478
478
  const { id, topic } = data
479
479
 
480
480
  // Let the server know the transaction hash
@@ -486,14 +486,14 @@ class PortalConnect {
486
486
  transactionHash: txHash,
487
487
  transactionId: id,
488
488
  },
489
- }),
489
+ })
490
490
  )
491
491
 
492
492
  // Let the SDK consumer know the transaction hash
493
- this.emit('portal_signgatureReceived', txHash)
493
+ this.emit('portal_signatureReceived', txHash)
494
494
  }
495
495
 
496
- private async handleSigningRejected(data: SessionRequest): Promise<void> {
496
+ private handleSigningRejected = (data: SessionRequest) => {
497
497
  // Notify the proxy server that the signing request was rejected
498
498
  this.socket?.send(
499
499
  JSON.stringify({
@@ -503,7 +503,7 @@ class PortalConnect {
503
503
  transactionHash: '',
504
504
  transactionId: data.id,
505
505
  },
506
- } as WebsocketMessage),
506
+ } as WebsocketMessage)
507
507
  )
508
508
  }
509
509
 
@@ -517,7 +517,7 @@ class PortalConnect {
517
517
  id: '',
518
518
  topic: this.topic,
519
519
  },
520
- } as WebsocketMessage),
520
+ } as WebsocketMessage)
521
521
  )
522
522
 
523
523
  this.topic = undefined