@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.
package/README.md ADDED
@@ -0,0 +1,36 @@
1
+ # Portal Web SDK
2
+
3
+ Welcome to the `@portal-hq/web` SDK, the official TypeScript library for integrating Portal's features into your web applications. This package enables you to seamlessly incorporate embedded MPC wallets.
4
+
5
+ ## Features
6
+
7
+ - **Portal Class Constructor**: Initialize the Portal instance in your application.
8
+ - **PortalContext Provider**: Expose the Portal instance to your React component tree.
9
+ - **usePortal Hook**: Consume the Portal instance within your child components for easy access and interaction.
10
+ - **Types and Enums**: Utilize helper types and enums for efficient development with the Portal SDK.
11
+
12
+ ## Documentation
13
+
14
+ For a comprehensive guide and API reference, please visit our [documentation portal](https://docs.portalhq.io/sdk/web).
15
+
16
+ ## Quick Start
17
+
18
+ ### Prerequisites
19
+
20
+ - Node.js and npm or yarn installed.
21
+ - Basic knowledge of TypeScript and React.
22
+ - Access to a Portal Account.
23
+
24
+ ### Installation
25
+
26
+ Install the SDK package using npm or yarn:
27
+
28
+ ```bash
29
+ npm install @portal-hq/web
30
+ # or
31
+ yarn add @portal-hq/web
32
+ ```
33
+
34
+ ### Support
35
+
36
+ For issues or questions please reach out to our support team at support@portalhq.io.
@@ -111,14 +111,14 @@ class Portal {
111
111
  // Noop
112
112
  }, backupConfigs = {}) {
113
113
  return __awaiter(this, void 0, void 0, function* () {
114
- const cipherText = yield this.mpc.backup({
114
+ const response = yield this.mpc.backup({
115
115
  backupMethod,
116
116
  backupConfigs,
117
117
  host: this.host,
118
118
  mpcVersion: this.mpcVersion,
119
119
  featureFlags: this.featureFlags,
120
120
  }, progress);
121
- return cipherText;
121
+ return response;
122
122
  });
123
123
  }
124
124
  recoverWallet(cipherText, backupMethod, backupConfigs = {}, progress = () => {
@@ -167,84 +167,94 @@ class Portal {
167
167
  /****************************
168
168
  * Provider Methods
169
169
  ****************************/
170
- ethEstimateGas(transaction) {
170
+ ethEstimateGas(transaction, chainId) {
171
171
  return __awaiter(this, void 0, void 0, function* () {
172
172
  return this.provider.request({
173
+ chainId,
173
174
  method: 'eth_estimateGas',
174
175
  params: transaction,
175
176
  });
176
177
  });
177
178
  }
178
- ethGasPrice() {
179
+ ethGasPrice(chainId) {
179
180
  return __awaiter(this, void 0, void 0, function* () {
180
181
  return this.provider.request({
182
+ chainId,
181
183
  method: 'eth_gasPrice',
182
184
  params: [],
183
185
  });
184
186
  });
185
187
  }
186
- ethGetBalance() {
188
+ ethGetBalance(chainId) {
187
189
  return __awaiter(this, void 0, void 0, function* () {
188
190
  return this.provider.request({
191
+ chainId,
189
192
  method: 'eth_getBalance',
190
193
  params: [this.address, 'latest'],
191
194
  });
192
195
  });
193
196
  }
194
- ethSendTransaction(transaction) {
197
+ ethSendTransaction(transaction, chainId) {
195
198
  return __awaiter(this, void 0, void 0, function* () {
196
199
  return this.provider.request({
200
+ chainId,
197
201
  method: 'eth_sendTransaction',
198
202
  params: transaction,
199
203
  });
200
204
  });
201
205
  }
202
- ethSign(message) {
206
+ ethSign(message, chainId) {
203
207
  return __awaiter(this, void 0, void 0, function* () {
204
208
  return this.provider.request({
209
+ chainId,
205
210
  method: 'eth_sign',
206
211
  params: [this.address, this.stringToHex(message)],
207
212
  });
208
213
  });
209
214
  }
210
- ethSignTransaction(transaction) {
215
+ ethSignTransaction(transaction, chainId) {
211
216
  return __awaiter(this, void 0, void 0, function* () {
212
217
  return this.provider.request({
218
+ chainId,
213
219
  method: 'eth_signTransaction',
214
220
  params: transaction,
215
221
  });
216
222
  });
217
223
  }
218
- ethSignTypedData(data) {
224
+ ethSignTypedData(data, chainId) {
219
225
  return __awaiter(this, void 0, void 0, function* () {
220
226
  return this.provider.request({
227
+ chainId,
221
228
  method: 'eth_signTypedData',
222
229
  params: [this.address, data],
223
230
  });
224
231
  });
225
232
  }
226
- ethSignTypedDataV3(data) {
233
+ ethSignTypedDataV3(data, chainId) {
227
234
  return __awaiter(this, void 0, void 0, function* () {
228
235
  return this.provider.request({
236
+ chainId,
229
237
  method: 'eth_signTypedData_v3',
230
238
  params: [this.address, data],
231
239
  });
232
240
  });
233
241
  }
234
- ethSignTypedDataV4(data) {
242
+ ethSignTypedDataV4(data, chainId) {
235
243
  return __awaiter(this, void 0, void 0, function* () {
236
244
  return this.provider.request({
245
+ chainId,
237
246
  method: 'eth_signTypedData_v4',
238
247
  params: [this.address, data],
239
248
  });
240
249
  });
241
250
  }
242
- personalSign(message) {
251
+ personalSign(message, chainId) {
243
252
  return __awaiter(this, void 0, void 0, function* () {
244
- return this.provider.request({
253
+ return (yield this.provider.request({
254
+ chainId,
245
255
  method: 'personal_sign',
246
256
  params: [this.stringToHex(message), this.address],
247
- });
257
+ }));
248
258
  });
249
259
  }
250
260
  /*******************************
@@ -295,16 +305,16 @@ class Portal {
295
305
  /*******************************
296
306
  * Swaps Methods
297
307
  *******************************/
298
- getQuote(apiKey, args) {
308
+ getQuote(apiKey, args, chainId) {
299
309
  var _a;
300
310
  return __awaiter(this, void 0, void 0, function* () {
301
- return (_a = this.mpc) === null || _a === void 0 ? void 0 : _a.getQuote(apiKey, args);
311
+ return (_a = this.mpc) === null || _a === void 0 ? void 0 : _a.getQuote(apiKey, args, chainId);
302
312
  });
303
313
  }
304
- getSources(apiKey) {
314
+ getSources(apiKey, chainId) {
305
315
  var _a;
306
316
  return __awaiter(this, void 0, void 0, function* () {
307
- return (_a = this.mpc) === null || _a === void 0 ? void 0 : _a.getSources(apiKey);
317
+ return (_a = this.mpc) === null || _a === void 0 ? void 0 : _a.getSources(apiKey, chainId);
308
318
  });
309
319
  }
310
320
  /*******************************
@@ -12,7 +12,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.MpcErrorCodes = exports.MpcError = void 0;
13
13
  const errors_1 = require("./errors");
14
14
  const index_1 = require("../index");
15
- const WEB_SDK_VERSION = '1.0.3';
15
+ const WEB_SDK_VERSION = '2.0.0';
16
16
  class Mpc {
17
17
  constructor({ portal }) {
18
18
  this.configureIframe = () => {
@@ -76,8 +76,14 @@ class Mpc {
76
76
  // Remove the event listeners
77
77
  window.removeEventListener('message', handleBackup);
78
78
  window.removeEventListener('message', handleProgress);
79
+ const storageCallback = () => __awaiter(this, void 0, void 0, function* () {
80
+ yield this.setBackupStatus('STORED_CLIENT_BACKUP_SHARE', result.backupIds);
81
+ });
79
82
  // Resolve the promise with the result
80
- resolve(result);
83
+ resolve({
84
+ cipherText: result.cipherText,
85
+ storageCallback,
86
+ });
81
87
  }
82
88
  };
83
89
  const handleProgress = (message) => {
@@ -334,7 +340,7 @@ class Mpc {
334
340
  /*******************************
335
341
  * API Methods
336
342
  *******************************/
337
- getBalances() {
343
+ getBalances(chainId) {
338
344
  return __awaiter(this, void 0, void 0, function* () {
339
345
  return new Promise((resolve, reject) => {
340
346
  const handleGetBalances = (event) => {
@@ -362,7 +368,7 @@ class Mpc {
362
368
  // Send the request to the iframe
363
369
  this.postMessage({
364
370
  type: 'portal:getBalances',
365
- data: {},
371
+ data: { chainId },
366
372
  });
367
373
  });
368
374
  });
@@ -400,7 +406,7 @@ class Mpc {
400
406
  });
401
407
  });
402
408
  }
403
- getNFTs() {
409
+ getNFTs(chainId) {
404
410
  return __awaiter(this, void 0, void 0, function* () {
405
411
  return new Promise((resolve, reject) => {
406
412
  const handleGetNFTs = (event) => {
@@ -428,12 +434,12 @@ class Mpc {
428
434
  // Send the request to the iframe
429
435
  this.postMessage({
430
436
  type: 'portal:getNFTs',
431
- data: {},
437
+ data: { chainId },
432
438
  });
433
439
  });
434
440
  });
435
441
  }
436
- getQuote(apiKey, args) {
442
+ getQuote(apiKey, args, chainId) {
437
443
  return __awaiter(this, void 0, void 0, function* () {
438
444
  return new Promise((resolve, reject) => {
439
445
  const handleGetQuote = (event) => {
@@ -464,12 +470,13 @@ class Mpc {
464
470
  data: {
465
471
  apiKey,
466
472
  args,
473
+ chainId,
467
474
  },
468
475
  });
469
476
  });
470
477
  });
471
478
  }
472
- getSources(apiKey) {
479
+ getSources(apiKey, chainId) {
473
480
  return __awaiter(this, void 0, void 0, function* () {
474
481
  return new Promise((resolve, reject) => {
475
482
  const handleGetSources = (event) => {
@@ -499,6 +506,7 @@ class Mpc {
499
506
  type: 'portal:swaps:getSources',
500
507
  data: {
501
508
  apiKey,
509
+ chainId,
502
510
  },
503
511
  });
504
512
  });
@@ -542,7 +550,42 @@ class Mpc {
542
550
  });
543
551
  });
544
552
  }
545
- simulateTransaction(transaction) {
553
+ setBackupStatus(status, backupIds) {
554
+ return __awaiter(this, void 0, void 0, function* () {
555
+ return new Promise((resolve, reject) => {
556
+ const handleSetBackupStatus = (event) => {
557
+ const { type, data } = event.data;
558
+ const { origin } = event;
559
+ // ignore any broadcast postMessages
560
+ if (origin !== this.getOrigin()) {
561
+ return;
562
+ }
563
+ if (type === 'portal:api:setBackupStatusError') {
564
+ // Remove the event listener
565
+ window.removeEventListener('message', handleSetBackupStatus);
566
+ // Reject the promise with the error
567
+ return reject(new errors_1.PortalMpcError(data));
568
+ }
569
+ else if (type === 'portal:api:setBackupStatusResult') {
570
+ // Remove the event listener
571
+ window.removeEventListener('message', handleSetBackupStatus);
572
+ // Resolve the promise with the result
573
+ return resolve(data);
574
+ }
575
+ };
576
+ // Bind the function to the message event
577
+ window.addEventListener('message', handleSetBackupStatus);
578
+ this.postMessage({
579
+ type: 'portal:api:setBackupStatus',
580
+ data: {
581
+ backupIds,
582
+ status,
583
+ },
584
+ });
585
+ });
586
+ });
587
+ }
588
+ simulateTransaction(transaction, chainId) {
546
589
  return __awaiter(this, void 0, void 0, function* () {
547
590
  return new Promise((resolve, reject) => {
548
591
  const handleSimulateTransaction = (event) => {
@@ -570,7 +613,10 @@ class Mpc {
570
613
  // Send the request to the iframe
571
614
  this.postMessage({
572
615
  type: 'portal:simulateTransaction',
573
- data: transaction,
616
+ data: {
617
+ chainId,
618
+ transaction,
619
+ },
574
620
  });
575
621
  });
576
622
  });
@@ -110,17 +110,21 @@ class Provider {
110
110
  * @param args The arguments of the request being made
111
111
  * @returns Promise<any>
112
112
  */
113
- request({ method, params }) {
113
+ request({ chainId, method, params, }) {
114
114
  return __awaiter(this, void 0, void 0, function* () {
115
115
  if (method === 'eth_chainId') {
116
- return this.portal.chainId;
116
+ return chainId ? parseInt(chainId.split(':')[1]) : this.portal.chainId;
117
117
  }
118
118
  const isSignerMethod = signerMethods.includes(method);
119
- let result;
120
119
  if (!isSignerMethod && !method.startsWith('wallet_')) {
121
120
  // Send to Gateway for RPC calls
122
- const response = yield this.handleGatewayRequest({ method, params });
121
+ const response = yield this.handleGatewayRequest({
122
+ chainId,
123
+ method,
124
+ params,
125
+ });
123
126
  this.emit('portal_signatureReceived', {
127
+ chainId,
124
128
  method,
125
129
  params,
126
130
  signature: response,
@@ -128,21 +132,23 @@ class Provider {
128
132
  if (response.error) {
129
133
  throw new errors_1.ProviderRpcError(response.error);
130
134
  }
131
- result = response.result;
135
+ return response.result;
132
136
  }
133
137
  else if (isSignerMethod) {
134
138
  // Handle signing
135
139
  const transactionHash = yield this.handleSigningRequest({
140
+ chainId,
136
141
  method,
137
142
  params,
138
143
  });
139
144
  if (transactionHash) {
140
145
  this.emit('portal_signatureReceived', {
146
+ chainId,
141
147
  method,
142
148
  params,
143
149
  signature: transactionHash,
144
150
  });
145
- result = transactionHash;
151
+ return transactionHash;
146
152
  }
147
153
  }
148
154
  else {
@@ -155,7 +161,6 @@ class Provider {
155
161
  },
156
162
  });
157
163
  }
158
- return result;
159
164
  });
160
165
  }
161
166
  /************************
@@ -218,13 +223,13 @@ class Provider {
218
223
  * @param args The arguments of the request being made
219
224
  * @returns Promise<any>
220
225
  */
221
- handleGatewayRequest({ method, params, }) {
226
+ handleGatewayRequest({ chainId, method, params, }) {
222
227
  return __awaiter(this, void 0, void 0, function* () {
223
228
  // Pass request off to the gateway
224
229
  const result = yield fetch(this.portal.getRpcUrl(), {
225
230
  body: JSON.stringify({
226
231
  jsonrpc: '2.0',
227
- id: this.portal.chainId,
232
+ id: chainId ? parseInt(chainId.split(':')[1]) : this.portal.chainId,
228
233
  method,
229
234
  params,
230
235
  }),
@@ -239,7 +244,7 @@ class Provider {
239
244
  * @param args The arguments of the request being made
240
245
  * @returns Promise<any>
241
246
  */
242
- handleSigningRequest({ method, params, }) {
247
+ handleSigningRequest({ chainId, method, params, }) {
243
248
  return __awaiter(this, void 0, void 0, function* () {
244
249
  const isApproved = passiveSignerMethods.includes(method)
245
250
  ? true
@@ -261,7 +266,9 @@ class Provider {
261
266
  case 'eth_signTypedData_v4':
262
267
  case 'personal_sign': {
263
268
  const result = yield this.portal.mpc.sign({
264
- chainId: this.portal.chainId.toString(),
269
+ chainId: chainId
270
+ ? chainId.split(':')[1]
271
+ : this.portal.chainId.toString(),
265
272
  method,
266
273
  params: this.buildParams(method, params),
267
274
  rpcUrl: this.portal.getRpcUrl(),
package/lib/esm/index.js CHANGED
@@ -105,14 +105,14 @@ class Portal {
105
105
  // Noop
106
106
  }, backupConfigs = {}) {
107
107
  return __awaiter(this, void 0, void 0, function* () {
108
- const cipherText = yield this.mpc.backup({
108
+ const response = yield this.mpc.backup({
109
109
  backupMethod,
110
110
  backupConfigs,
111
111
  host: this.host,
112
112
  mpcVersion: this.mpcVersion,
113
113
  featureFlags: this.featureFlags,
114
114
  }, progress);
115
- return cipherText;
115
+ return response;
116
116
  });
117
117
  }
118
118
  recoverWallet(cipherText, backupMethod, backupConfigs = {}, progress = () => {
@@ -161,84 +161,94 @@ class Portal {
161
161
  /****************************
162
162
  * Provider Methods
163
163
  ****************************/
164
- ethEstimateGas(transaction) {
164
+ ethEstimateGas(transaction, chainId) {
165
165
  return __awaiter(this, void 0, void 0, function* () {
166
166
  return this.provider.request({
167
+ chainId,
167
168
  method: 'eth_estimateGas',
168
169
  params: transaction,
169
170
  });
170
171
  });
171
172
  }
172
- ethGasPrice() {
173
+ ethGasPrice(chainId) {
173
174
  return __awaiter(this, void 0, void 0, function* () {
174
175
  return this.provider.request({
176
+ chainId,
175
177
  method: 'eth_gasPrice',
176
178
  params: [],
177
179
  });
178
180
  });
179
181
  }
180
- ethGetBalance() {
182
+ ethGetBalance(chainId) {
181
183
  return __awaiter(this, void 0, void 0, function* () {
182
184
  return this.provider.request({
185
+ chainId,
183
186
  method: 'eth_getBalance',
184
187
  params: [this.address, 'latest'],
185
188
  });
186
189
  });
187
190
  }
188
- ethSendTransaction(transaction) {
191
+ ethSendTransaction(transaction, chainId) {
189
192
  return __awaiter(this, void 0, void 0, function* () {
190
193
  return this.provider.request({
194
+ chainId,
191
195
  method: 'eth_sendTransaction',
192
196
  params: transaction,
193
197
  });
194
198
  });
195
199
  }
196
- ethSign(message) {
200
+ ethSign(message, chainId) {
197
201
  return __awaiter(this, void 0, void 0, function* () {
198
202
  return this.provider.request({
203
+ chainId,
199
204
  method: 'eth_sign',
200
205
  params: [this.address, this.stringToHex(message)],
201
206
  });
202
207
  });
203
208
  }
204
- ethSignTransaction(transaction) {
209
+ ethSignTransaction(transaction, chainId) {
205
210
  return __awaiter(this, void 0, void 0, function* () {
206
211
  return this.provider.request({
212
+ chainId,
207
213
  method: 'eth_signTransaction',
208
214
  params: transaction,
209
215
  });
210
216
  });
211
217
  }
212
- ethSignTypedData(data) {
218
+ ethSignTypedData(data, chainId) {
213
219
  return __awaiter(this, void 0, void 0, function* () {
214
220
  return this.provider.request({
221
+ chainId,
215
222
  method: 'eth_signTypedData',
216
223
  params: [this.address, data],
217
224
  });
218
225
  });
219
226
  }
220
- ethSignTypedDataV3(data) {
227
+ ethSignTypedDataV3(data, chainId) {
221
228
  return __awaiter(this, void 0, void 0, function* () {
222
229
  return this.provider.request({
230
+ chainId,
223
231
  method: 'eth_signTypedData_v3',
224
232
  params: [this.address, data],
225
233
  });
226
234
  });
227
235
  }
228
- ethSignTypedDataV4(data) {
236
+ ethSignTypedDataV4(data, chainId) {
229
237
  return __awaiter(this, void 0, void 0, function* () {
230
238
  return this.provider.request({
239
+ chainId,
231
240
  method: 'eth_signTypedData_v4',
232
241
  params: [this.address, data],
233
242
  });
234
243
  });
235
244
  }
236
- personalSign(message) {
245
+ personalSign(message, chainId) {
237
246
  return __awaiter(this, void 0, void 0, function* () {
238
- return this.provider.request({
247
+ return (yield this.provider.request({
248
+ chainId,
239
249
  method: 'personal_sign',
240
250
  params: [this.stringToHex(message), this.address],
241
- });
251
+ }));
242
252
  });
243
253
  }
244
254
  /*******************************
@@ -289,16 +299,16 @@ class Portal {
289
299
  /*******************************
290
300
  * Swaps Methods
291
301
  *******************************/
292
- getQuote(apiKey, args) {
302
+ getQuote(apiKey, args, chainId) {
293
303
  var _a;
294
304
  return __awaiter(this, void 0, void 0, function* () {
295
- return (_a = this.mpc) === null || _a === void 0 ? void 0 : _a.getQuote(apiKey, args);
305
+ return (_a = this.mpc) === null || _a === void 0 ? void 0 : _a.getQuote(apiKey, args, chainId);
296
306
  });
297
307
  }
298
- getSources(apiKey) {
308
+ getSources(apiKey, chainId) {
299
309
  var _a;
300
310
  return __awaiter(this, void 0, void 0, function* () {
301
- return (_a = this.mpc) === null || _a === void 0 ? void 0 : _a.getSources(apiKey);
311
+ return (_a = this.mpc) === null || _a === void 0 ? void 0 : _a.getSources(apiKey, chainId);
302
312
  });
303
313
  }
304
314
  /*******************************